-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathui.go
More file actions
435 lines (390 loc) · 11.5 KB
/
Copy pathui.go
File metadata and controls
435 lines (390 loc) · 11.5 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package ui
import (
"fmt"
"log"
"sort"
"strings"
"github.com/becheran/roumon/internal/model"
"github.com/gizak/termui/v3/widgets"
termui "github.com/gizak/termui/v3"
)
const (
padding = 1
keepRoutineHist = 100
)
// statusAbbreviations maps common goroutine status names to consistent abbreviations
var statusAbbreviations = map[string]string{
"running": "run",
"runnable": "rbl",
"waiting": "wai",
"IO wait": "IO",
"chan receive": "cha",
"chan send": "ch1",
"select": "sel",
"sync.Mutex.Lock": "syn",
"sync.Cond.Wait": "syw",
"syscall": "sys",
"sleep": "slp",
"idle": "idl",
"dead": "ded",
"copystack": "cps",
"preempted": "pre",
"GC assist wait": "gca",
"GC sweep wait": "gcs",
"GC scavenge wait": "gcv",
}
// UI contains all user interface elements
type UI struct {
list *widgets.List
filter *widgets.Paragraph
details *widgets.Paragraph
routineHist *widgets.Plot
barchart *widgets.BarChart
barchartLegend *widgets.Paragraph
paused *widgets.Paragraph
legend *widgets.Paragraph
help *widgets.Paragraph
grid *termui.Grid
filtered bool
origData []model.Goroutine
filteredData []model.Goroutine
minGoRoutines int
maxGoRoutines int
avgGoRoutines float64
}
// NewUI creates a new console user interface
func NewUI() *UI {
if err := termui.Init(); err != nil {
log.Fatalf("Failed to initialize termui: %v", err)
}
filter := widgets.NewParagraph()
filter.Text = "TYPE TO FILTER"
filter.TextStyle.Fg = termui.ColorWhite
filter.BorderStyle.Fg = termui.ColorGreen
filter.Title = "Filter"
filter.PaddingTop = padding
filter.PaddingRight = padding
filter.PaddingLeft = padding
filter.PaddingBottom = padding
plot := widgets.NewPlot()
plot.Data = make([][]float64, 1)
plot.Data[0] = make([]float64, 2, keepRoutineHist)
plot.AxesColor = termui.ColorWhite
plot.LineColors[0] = termui.ColorGreen
plot.HorizontalScale = 2
plot.PaddingTop = padding
plot.PaddingRight = padding
plot.PaddingLeft = padding
plot.PaddingBottom = padding
routineList := widgets.NewList()
routineList.PaddingTop = padding
routineList.PaddingRight = padding
routineList.PaddingLeft = padding
routineList.PaddingBottom = padding
routineList.Rows = []string{}
routineList.TextStyle.Fg = termui.ColorGreen
routineList.SelectedRowStyle.Fg = termui.ColorWhite
routineList.SelectedRowStyle.Bg = termui.ColorGreen
details := widgets.NewParagraph()
details.PaddingTop = padding
details.PaddingRight = padding
details.PaddingLeft = padding
details.PaddingBottom = padding
details.Title = "Details"
details.TextStyle = termui.NewStyle(termui.ColorWhite)
details.SetRect(0, 0, 60, 10)
barchart := widgets.NewBarChart()
barchart.Title = "Status"
barchart.BarWidth = 3
barchart.BarGap = 1
barchart.BarColors = []termui.Color{termui.ColorGreen}
barchart.LabelStyles = []termui.Style{termui.NewStyle(termui.ColorWhite)}
barchart.PaddingTop = padding
barchart.PaddingRight = padding
barchart.PaddingLeft = padding
barchart.PaddingBottom = padding
barchart.BorderRight = false
barchartLabel := widgets.NewParagraph()
barchartLabel.BorderLeft = false
barchartLabel.PaddingTop = padding
barchartLabel.PaddingRight = padding
barchartLabel.PaddingLeft = padding
barchartLabel.PaddingBottom = padding
barchartLabel.Text = ""
help := widgets.NewParagraph()
help.TextStyle.Fg = termui.ColorGreen
help.Text = "Help\n\nArrows up/down: Select from list\nText input: Filter results\nF10: Quit\nF2: Pause\n\nPress any key to continue"
help.PaddingBottom = 2
help.PaddingLeft = 2
help.PaddingRight = 2
help.PaddingTop = 2
paused := widgets.NewParagraph()
paused.TextStyle.Fg = termui.ColorGreen
paused.Text = "Paused. Press any key to continue"
paused.PaddingBottom = 2
paused.PaddingLeft = 2
paused.PaddingRight = 2
paused.PaddingTop = 2
legend := widgets.NewParagraph()
legend.Text = "F1 Help | F2 Pause | F10 Quit"
legend.TextStyle.Fg = termui.ColorGreen
legend.Border = false
grid := termui.NewGrid()
ui := UI{
filter: filter,
list: routineList,
details: details,
routineHist: plot,
barchart: barchart,
barchartLegend: barchartLabel,
help: help,
paused: paused,
legend: legend,
grid: grid,
}
grid.Set(
termui.NewRow(3.0/10,
termui.NewCol(3.0/10,
termui.NewCol(5.0/8, ui.barchart),
termui.NewCol(3.0/8, ui.barchartLegend)),
termui.NewCol(7.0/10, ui.routineHist),
),
termui.NewRow(7.0/10,
termui.NewCol(1.0/6,
termui.NewRow(1.5/10, ui.filter),
termui.NewRow(8.5/10, ui.list)),
termui.NewCol(5.0/6, ui.details),
),
)
ui.updatePlotTitle()
return &ui
}
func (ui *UI) updatePlotTitle() {
ui.routineHist.Title = fmt.Sprintf("History # goroutines (Min: %d Avg: %0.2f Max: %d)",
ui.minGoRoutines, ui.avgGoRoutines, ui.maxGoRoutines)
}
func (ui *UI) updateStatus() {
typeCount := make(map[string]float64)
for i := 0; i < len(ui.origData); i++ {
num := typeCount[ui.origData[i].Status]
typeCount[ui.origData[i].Status] = num + 1
}
types := make([]string, 0, len(typeCount))
for key := range typeCount {
types = append(types, key)
}
sort.Strings(types)
data := make([]float64, len(types))
labels := make([]string, len(types))
legend := ""
usedAbbrevs := make(map[string]bool)
for idx, t := range types {
data[idx] = typeCount[t]
// Use predefined abbreviation or fallback to custom logic
abbrev, exists := statusAbbreviations[t]
if !exists {
// Fallback: use first 3 characters
abbrev = t[:min(3, len(t))]
}
// Handle collisions by adding a number suffix
originalAbbrev := abbrev
counter := 2
for usedAbbrevs[abbrev] {
abbrev = fmt.Sprintf("%s%d", originalAbbrev[:min(2, len(originalAbbrev))], counter)
counter++
}
usedAbbrevs[abbrev] = true
labels[idx] = abbrev
// Add to legend with count
legend = fmt.Sprintf("%s%s: %s (%.0f)\n", legend, abbrev, t, typeCount[t])
}
ui.barchart.Data = data
ui.barchart.Labels = labels
ui.barchartLegend.Text = legend
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func (ui *UI) updateList() {
if ui.filter.Text == "" || !ui.filtered {
ui.filteredData = ui.origData
} else {
ui.filteredData = make([]model.Goroutine, 0)
for _, d := range ui.origData {
filterText := strings.ToLower(ui.filter.Text)
matchID := strings.Contains(strings.ToLower(fmt.Sprintf("%d", d.ID)), filterText)
matchStatus := strings.Contains(strings.ToLower(d.Status), filterText)
matchCreatedBy := d.CratedBy != nil && strings.Contains(strings.ToLower(d.CratedBy.String()), filterText)
matchStackTrace := model.StackContains(d.StackTrace, filterText)
matchLockedToThread := d.LockedToThread && strings.Contains("locked to thread", filterText)
if matchStatus || matchID || matchCreatedBy || matchStackTrace || matchLockedToThread {
ui.filteredData = append(ui.filteredData, d)
}
}
}
// Update list
ui.list.Rows = make([]string, len(ui.filteredData))
for i := 0; i < len(ui.filteredData); i++ {
ui.list.Rows[i] = fmt.Sprintf("%05d %s ", ui.filteredData[i].ID, ui.filteredData[i].Status)
}
if len(ui.filteredData) == 0 {
ui.list.SelectedRow = 0
ui.details.Text = ""
ui.list.Title = "Routines (0/0)"
return
}
if ui.list.SelectedRow >= len(ui.filteredData) {
ui.list.SelectedRow = len(ui.filteredData) - 1
} else if ui.list.SelectedRow < 0 {
ui.list.SelectedRow = 0
}
selectedData := ui.filteredData[ui.list.SelectedRow]
trace := ""
for _, t := range selectedData.StackTrace {
trace += fmt.Sprintf(" %s\n", t.String())
}
createdBy := ""
if selectedData.CratedBy != nil {
createdBy = fmt.Sprintf("Created by:\n %s\n\n", selectedData.CratedBy.String())
}
lockedToThread := ""
if selectedData.LockedToThread {
lockedToThread = " [locked to thread](mod:bold)"
}
ui.details.Text = fmt.Sprintf("ID: [%d](mod:bold)\n\nStatus: [%s](mod:bold)\n\nWait Since: [%d min](mod:bold)%s\n\n%sTrace:\n%s",
selectedData.ID,
selectedData.Status,
selectedData.WaitSinceMin,
lockedToThread,
createdBy,
trace)
ui.list.Title = fmt.Sprintf("Routines (%d/%d)", ui.list.SelectedRow+1, len(ui.list.Rows))
}
// Stop UI and close all event listeners
func (ui *UI) Stop() {
termui.Close()
}
func (ui *UI) resize(width, height int) {
log.Printf("Resize to: (%d,%d)", width, height)
ui.paused.SetRect(width/2.0-25, height/4.0-4, width/2.0+25, height/4.0+4)
ui.help.SetRect(width/2.0-20, height/4.0-10, width/2.0+20, height/4.0+10)
ui.legend.SetRect(width-35, height-4, width-1, height-1)
ui.grid.SetRect(0, 0, width, height)
}
// Run UI in fullscreen mode
func (ui *UI) Run(terminate chan<- error, routinesUpdate <-chan []model.Goroutine) {
ui.updateList()
termWidth, termHeight := termui.TerminalDimensions()
ui.resize(termWidth, termHeight)
termui.Render(ui.grid, ui.legend)
pollEvents := termui.PollEvents()
for {
select {
case evt := <-pollEvents:
switch evt.Type {
case termui.MouseEvent:
continue
case termui.ResizeEvent:
resized, ok := evt.Payload.(termui.Resize)
if !ok {
log.Printf("Failed to parse payload for resize. %v", evt)
} else {
ui.resize(resized.Width, resized.Height)
}
case termui.KeyboardEvent:
terminateEvent := ui.handleKeyEvent(evt.ID, pollEvents)
if terminateEvent {
terminate <- nil
return
}
}
case routines := <-routinesUpdate:
// History data size cannot be limited in termui. This is a workaround
var keepRoutineHist = (ui.routineHist.Dx() - 10) >> 1
ui.origData = routines
if len(ui.routineHist.Data[0]) >= keepRoutineHist {
ui.routineHist.Data[0] = ui.routineHist.Data[0][1:]
}
ui.routineHist.Data[0] = append(ui.routineHist.Data[0], float64(len(routines)))
if ui.minGoRoutines == 0 || len(routines) < ui.minGoRoutines {
ui.minGoRoutines = len(routines)
}
if len(routines) > ui.maxGoRoutines {
ui.maxGoRoutines = len(routines)
}
if ui.avgGoRoutines > 0 {
ui.avgGoRoutines = (ui.avgGoRoutines + float64(len(routines))) / 2.0
} else {
ui.avgGoRoutines = float64(len(routines))
}
ui.updatePlotTitle()
ui.updateList()
ui.updateStatus()
}
termui.Render(ui.grid, ui.legend)
}
}
func (ui *UI) handleKeyEvent(keyID string, pollEvents <-chan termui.Event) (terminate bool) {
switch keyID {
case "<C-c>", "<F10>":
return true
case "<F1>":
termui.Render(ui.grid, ui.legend, ui.help)
e := <-pollEvents
if e.ID == "<C-c>" || e.ID == "<F10>" {
return true
}
termui.Render(ui.grid, ui.legend)
case "<F2>":
// Pause
termui.Render(ui.grid, ui.legend, ui.paused)
e := <-pollEvents
if e.ID == "<C-c>" || e.ID == "<F10>" {
return true
}
termui.Render(ui.grid, ui.legend)
case "<Down>":
ui.list.ScrollDown()
ui.updateList()
case "<Up>":
ui.list.ScrollUp()
ui.updateList()
case "<PageDown>":
ui.list.ScrollPageDown()
ui.updateList()
case "<PageUp>":
ui.list.ScrollPageUp()
ui.updateList()
case "<Home>":
ui.list.ScrollTop()
ui.updateList()
case "<End>":
ui.list.ScrollBottom()
ui.updateList()
case "<Backspace>", "<C-<Backspace>>":
if len(ui.filter.Text) > 0 {
ui.filter.Text = ui.filter.Text[:len(ui.filter.Text)-1]
}
case "<Space>":
if !ui.filtered {
ui.filter.Text = ""
}
ui.filtered = true
ui.filter.Text += " "
ui.updateList()
default:
// < sign
if keyID[0] != 0x3C {
if !ui.filtered {
ui.filter.Text = ""
}
ui.filtered = true
ui.filter.Text += keyID
}
ui.updateList()
}
return false
}