-
Notifications
You must be signed in to change notification settings - Fork 0
/
resultModel.go
170 lines (154 loc) · 4.37 KB
/
resultModel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package tui
import (
"fmt"
"net/url"
"sort"
"strconv"
"strings"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/buonotti/apisense/errors"
"github.com/buonotti/apisense/util"
"github.com/buonotti/apisense/validation/pipeline"
)
var (
selectedResult pipeline.TestCaseResult
validatorOutputRows []table.Row
updateValidatorOutputRows = false
)
type resultModel struct {
keymap keymap
table table.Model
validatorOutputModel tea.Model
}
func ResultModel() tea.Model {
t := table.New(
table.WithColumns(getResultColumns()),
table.WithRows(nil),
table.WithFocused(true),
table.WithHeight(7),
)
s := table.DefaultStyles()
s.Selected = s.Selected.
Foreground(lipgloss.Color("#F38BA8")).
Background(lipgloss.Color("#1e1e2e")).
Bold(false)
t.SetStyles(s)
return resultModel{
keymap: DefaultKeyMap,
table: t,
validatorOutputModel: ValidatorOutputModel(),
}
}
func (r resultModel) Init() tea.Cmd {
return nil
}
func (r resultModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmdModel tea.Cmd
if choiceReportModel != "validatedEndpointModel" {
if updateResultRows {
t := table.New(
table.WithColumns(getResultColumns()),
table.WithRows(resultRows),
table.WithFocused(true),
table.WithHeight(7),
)
s := table.DefaultStyles()
s.Selected = s.Selected.
Foreground(lipgloss.Color("#F38BA8")).
Background(lipgloss.Color("#1e1e2e")).
Bold(false)
t.SetStyles(s)
r.table = t
updateResultRows = false
}
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, r.keymap.back):
if choiceReportModel == "resultModel" {
choiceReportModel = "validatedEndpointModel"
}
case key.Matches(msg, r.keymap.quit):
return r, tea.Quit
case key.Matches(msg, r.keymap.choose):
i, err := strconv.Atoi(r.table.SelectedRow()[0])
errors.CheckErr(err)
res, err := getSelectedResult(selectedValidatedEndpoint, i)
errors.CheckErr(err)
selectedResult = res
validatorOutputRows = getValidatorOutputRows(selectedResult.ValidatorResults)
if choiceReportModel != "resultModel" {
r.validatorOutputModel, cmdModel = r.validatorOutputModel.Update(msg)
r.table, cmd = r.table.Update(msg)
return r, tea.Batch(cmd, cmdModel)
}
choiceReportModel = "validatorOutputModel"
updateValidatorOutputRows = true
r.table, cmd = r.table.Update(msg)
return r, tea.Batch(cmd, cmdModel)
}
}
r.validatorOutputModel, cmdModel = r.validatorOutputModel.Update(msg)
r.table, cmd = r.table.Update(msg)
return r, tea.Batch(cmd, cmdModel)
}
return r, nil
}
func (r resultModel) View() string {
if choiceReportModel != "resultModel" {
return lipgloss.NewStyle().Render(r.validatorOutputModel.View())
}
return lipgloss.NewStyle().Render(r.table.View())
}
func getResultRows(results []pipeline.TestCaseResult) []table.Row {
rows := make([]table.Row, 0)
queries := make([][]string, 0)
queriesToRender := make([]string, 0)
for _, result := range results {
u, err := url.Parse(result.Url)
errors.CheckErr(err)
query := make([]string, 0)
for value := range u.Query() {
query = append(query, value+"="+u.Query().Get(value))
sort.Strings(query)
}
queries = append(queries, query)
}
rQueries := util.Transpose(queries)
for _, query := range rQueries {
for i, result := range query {
if i > 0 {
if strings.Split(query[i-1], "=")[1] != strings.Split(result, "=")[1] {
queriesToRender = append(queriesToRender, strings.Split(result, "=")[0])
break
}
}
}
}
for i, result := range queries {
s := ""
for _, query := range result {
if util.Contains(queriesToRender, strings.Split(query, "=")[0]) {
s += query + ", "
}
}
rows = append(rows, table.Row{fmt.Sprintf("%v", i), s})
}
return rows
}
func getResultColumns() []table.Column {
return []table.Column{
{Title: "", Width: 3},
{Title: "Url", Width: 73},
}
}
func getSelectedResult(validatedEndpoint pipeline.ValidatedEndpoint, index int) (pipeline.TestCaseResult, error) {
if index > len(validatedEndpoint.TestCaseResults) || index < 0 {
return pipeline.TestCaseResult{}, errors.ModelError.New("Index out of range")
}
return validatedEndpoint.TestCaseResults[index], nil
}