/
dashboard.go
293 lines (257 loc) · 7.75 KB
/
dashboard.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package cui
import (
"fmt"
"github.com/daveshanley/vacuum/model"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
"github.com/pb33f/libopenapi/datamodel"
"github.com/pb33f/libopenapi/index"
)
// Dashboard represents the dashboard controlling container
type Dashboard struct {
C chan bool
run bool
grid *ui.Grid
helpGrid *ui.Grid
title *widgets.Paragraph
tabs TabbedView
healthGaugeItems []ui.GridItem
categoryHealthGauge []CategoryGauge
resultSet *model.RuleResultSet
index *index.SpecIndex
info *datamodel.SpecInfo
selectedTabIndex int
ruleCategories []*model.RuleCategory
selectedCategory *model.RuleCategory
selectedRule *model.Rule
selectedRuleIndex int
selectedViolationIndex int
selectedViolation *model.RuleFunctionResult
violationViewActive bool
helpViewActive bool
uiEvents <-chan ui.Event
Version string
}
func CreateDashboard(resultSet *model.RuleResultSet, index *index.SpecIndex, info *datamodel.SpecInfo) *Dashboard {
db := new(Dashboard)
db.resultSet = resultSet
db.index = index
db.info = info
return db
}
// GenerateTabbedView generates tabs
func (dash *Dashboard) GenerateTabbedView() {
var labels []string
for _, cat := range dash.ruleCategories {
labels = append(labels, cat.Name)
}
tv := widgets.NewTabPane(labels...)
tv.BorderTop = false
tv.BorderLeft = false
tv.BorderRight = false
tv.BorderBottom = true
dash.tabs = TabbedView{tv: tv, dashboard: dash}
dash.selectedTabIndex = 0
dash.selectedCategory = dash.ruleCategories[0]
dash.tabs.generateDescriptionGridItem()
dash.tabs.generateRulesInCategory()
if len(dash.tabs.currentRuleResults.RuleResults) > 0 {
dash.selectedRule = dash.tabs.currentRuleResults.RuleResults[0].Rule
}
dash.tabs.generateRuleViolations()
//dash.tabs.setActiveViolation()
dash.tabs.generateRuleViolationView()
}
// ComposeGauges prepares health gauges for rendering into the main grid.
func (dash *Dashboard) ComposeGauges() {
var gridItems []ui.GridItem
for _, gauge := range dash.categoryHealthGauge {
numCat := float64(len(dash.categoryHealthGauge))
ratio := 0.8 / (numCat)
gridItems = append(gridItems, ui.NewRow(ratio, gauge.g))
}
dash.healthGaugeItems = gridItems
}
// Render will render the dashboard.
func (dash *Dashboard) Render() error {
if err := ui.Init(); err != nil {
return err
}
dash.run = true
defer ui.Close()
// extract categories and calculate coverage.
var gauges []CategoryGauge
cats := model.RuleCategoriesOrdered
var catsFiltered []*model.RuleCategory
if dash.resultSet != nil {
for i := range cats {
res := dash.resultSet.GetResultsByRuleCategory(cats[i].Id)
if len(res) >= 1 {
catsFiltered = append(catsFiltered, cats[i])
}
}
}
for _, cat := range catsFiltered {
score := dash.resultSet.CalculateCategoryHealth(cat.Id)
gauges = append(gauges, NewCategoryGauge(cat.Name, score, cat))
}
// todo: create a formula for this.
gauges = append(gauges, NewCategoryGauge("Overall Health", 12, model.RuleCategoriesOrdered[0]))
dash.categoryHealthGauge = gauges
dash.ruleCategories = catsFiltered
dash.grid = ui.NewGrid()
termWidth, termHeight := ui.TerminalDimensions()
dash.grid.SetRect(0, 0, termWidth, termHeight)
dash.helpGrid = ui.NewGrid()
dash.helpGrid.SetRect(0, 0, termWidth, termHeight)
dash.GenerateTabbedView()
dash.ComposeGauges()
dash.setGrid()
//dash.tabs.setActiveCategoryIndex(dash.tabs.tv.ActiveTabIndex)
ui.Render(dash.grid, dash.title)
dash.eventLoop(cats)
return nil
}
func (dash *Dashboard) eventLoop(cats []*model.RuleCategory) {
var uiEvents <-chan ui.Event
if dash.uiEvents == nil {
uiEvents = ui.PollEvents()
dash.uiEvents = uiEvents
} else {
uiEvents = dash.uiEvents
}
// TODO: clean this damn mess up.
for {
e := <-uiEvents
switch e.ID {
case "q", "<C-c>":
return
case "h":
dash.helpViewActive = true
case "<Tab>":
dash.violationViewActive = false
if dash.tabs.tv.ActiveTabIndex == len(cats)-1 { // loop around and around.
dash.tabs.tv.ActiveTabIndex = 0
} else {
dash.tabs.tv.FocusRight()
}
dash.tabs.setActiveCategoryIndex(dash.tabs.tv.ActiveTabIndex)
dash.generateViewsAfterEvent()
case "<Enter>":
dash.violationViewActive = true
dash.generateViewsAfterEvent()
case "<Escape>":
dash.violationViewActive = false
dash.helpViewActive = false
dash.generateViewsAfterEvent()
case "x", "<Right>":
dash.violationViewActive = false
dash.tabs.tv.FocusRight()
dash.tabs.setActiveCategoryIndex(dash.tabs.tv.ActiveTabIndex)
dash.generateViewsAfterEvent()
case "s", "<Left>":
dash.violationViewActive = false
dash.tabs.tv.FocusLeft()
dash.tabs.setActiveCategoryIndex(dash.tabs.tv.ActiveTabIndex)
dash.generateViewsAfterEvent()
case "z", "<Down>":
if dash.violationViewActive {
dash.tabs.scrollViolationsDown()
} else {
dash.tabs.scrollRulesDown()
}
case "a", "<Up>":
if dash.violationViewActive {
dash.tabs.scrollViolationsUp()
} else {
dash.tabs.scrollRulesUp()
}
}
ui.Clear()
if dash.helpViewActive {
ui.Render(dash.helpGrid)
} else {
ui.Render(dash.grid, dash.title)
}
}
}
func (dash *Dashboard) generateViewsAfterEvent() {
dash.tabs.generateRuleViolations()
dash.tabs.setActiveViolation()
dash.tabs.generateRuleViolationView()
}
func (dash *Dashboard) setGrid() {
p := widgets.NewParagraph()
// todo: bring in correct versioning.
p.Text = fmt.Sprintf("vacuum %v: ", dash.Version) +
"[Select Category](fg:white,bg:clear,md:bold) = <Tab>,\u2B05\uFE0F\u27A1\uFE0F/S,X | " +
"[Change Rule](fg:white,bg:clear,md:bold) = \u2B06\u2B07/A,Z | " +
"[Select / Leave Rule](fg:white,bg:clear,md:bold) = <Enter> / <Esc>"
p.TextStyle = ui.NewStyle(ui.ColorCyan, ui.ColorClear)
p.Border = true
p.BorderStyle = ui.NewStyle(ui.ColorCyan)
p.PaddingLeft = 0
p.PaddingTop = 0
p.PaddingBottom = 0
p.PaddingRight = 0
//p.SetRect(0, 0, 800, 3)
dash.title = p
if !dash.helpViewActive {
if dash.tabs.descriptionGridItem != nil {
dash.grid.Set(
ui.NewRow(0.07, p),
ui.NewRow(0.93,
// TODO: bring statistics back via a shortcut key combo, they take up too much space and don't add
// enough value out of the box.
//ui.NewCol(0.2,
// dash.healthGaugeItems[0], dash.healthGaugeItems[1], dash.healthGaugeItems[2], dash.healthGaugeItems[3],
// dash.healthGaugeItems[4], dash.healthGaugeItems[5], dash.healthGaugeItems[6], dash.healthGaugeItems[7],
// //dash.healthGaugeItems[8],
// ui.NewRow(0.3, NewStatsChart(dash.index, dash.info).bc),
//),
//ui.NewCol(0.01, nil),
ui.NewCol(1.0,
ui.NewRow(0.1, dash.tabs.tv),
ui.NewRow(0.9,
ui.NewCol(0.6,
*dash.tabs.descriptionGridItem,
*dash.tabs.rulesListGridItem,
*dash.tabs.violationListGridItem,
),
ui.NewCol(0.4,
*dash.tabs.violationViewGridItem,
*dash.tabs.violationSnippetGridItem,
*dash.tabs.violationFixGridItem),
),
),
),
)
}
}
h := widgets.NewParagraph()
h.Text = "This is the help screen, but it's not ready yet!"
h.TextStyle = ui.NewStyle(ui.ColorGreen, ui.ColorYellow)
h.BorderStyle = ui.NewStyle(ui.ColorBlack, ui.ColorBlack)
// only render, if we can render.
if dash.tabs.descriptionGridItem != nil {
dash.helpGrid.Set(
ui.NewRow(1,
ui.NewCol(1.0, h),
))
}
}
func getColorForPercentage(percent int) ui.Color {
if percent <= 30 {
return ui.ColorRed
}
if percent > 30 && percent <= 70 {
return ui.ColorYellow
}
if percent > 70 && percent <= 90 {
return ui.ColorBlue
}
if percent > 80 {
return ui.ColorGreen
}
return ui.ColorClear
}