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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
|
package dashboard
import (
"strings"
"time"
"ior/internal/statsengine"
common "ior/internal/tui/common"
"ior/internal/tui/eventstream"
flamegraphtui "ior/internal/tui/flamegraph"
"ior/internal/tui/messages"
"charm.land/bubbles/v2/key"
tea "charm.land/bubbletea/v2"
)
const defaultRefreshMs = 1000
const streamRefreshMs = 200
const flameRefreshMs = 200
const bubbleRefreshMs = 33
const streamChromeRows = 4
const dashboardHelpHintRows = 1
const dashboardExpandedHelpRows = 2
const dashboardTabBarRows = 1
// SnapshotSource is the dashboard data source.
type SnapshotSource interface {
Snapshot() *statsengine.Snapshot
}
type resettableSnapshotSource interface {
Reset()
Snapshot() *statsengine.Snapshot
}
type refreshTickMsg struct{}
type streamTickMsg struct{}
type flameTickMsg struct{}
type bubbleTickMsg struct{}
type streamEditorDoneMsg struct {
err error
}
type tabVizMode uint8
const (
tabVizModeTable tabVizMode = iota
tabVizModeBubbles
tabVizModeTreemap
tabVizModeIcicle
)
// Model is the dashboard tab framework model.
type Model struct {
activeTab Tab
engine SnapshotSource
latest *statsengine.Snapshot
liveTrie flamegraphtui.LiveTrieSource
width int
height int
refreshEvery time.Duration
keys common.KeyMap
pidFilter int
syscallsOffset int
syscallsTreemapSelection int
filesOffset int
filesDirGrouped bool
filesDirOffset int
processesOffset int
syscallsVizMode tabVizMode
filesVizMode tabVizMode
processesVizMode tabVizMode
streamModel eventstream.Model
flamegraphModel flamegraphtui.Model
syscallsChart bubbleChart
filesChart bubbleChart
processesChart bubbleChart
showHelp bool
isDark bool
focused bool
}
// NewModel creates a dashboard model with default refresh cadence.
func NewModel(engine SnapshotSource, streamSource eventstream.Source) Model {
return NewModelWithConfig(engine, streamSource, defaultRefreshMs, common.Keys)
}
// NewModelWithConfig creates a dashboard model with explicit refresh and keys.
func NewModelWithConfig(engine SnapshotSource, streamSource eventstream.Source, refreshMs int, keys common.KeyMap) Model {
if refreshMs <= 0 {
refreshMs = defaultRefreshMs
}
m := Model{
activeTab: TabFlame,
engine: engine,
refreshEvery: time.Duration(refreshMs) * time.Millisecond,
keys: keys,
pidFilter: -1,
syscallsVizMode: tabVizModeTable,
filesVizMode: tabVizModeTable,
processesVizMode: tabVizModeTable,
streamModel: eventstream.NewModel(streamSource),
flamegraphModel: flamegraphtui.NewModel(nil),
syscallsChart: newBubbleChart(),
filesChart: newBubbleChart(),
processesChart: newBubbleChart(),
isDark: true,
focused: true,
}
m.SetDarkMode(true)
return m
}
// Init starts periodic refresh ticks.
func (m Model) Init() tea.Cmd {
cmds := []tea.Cmd{tickCmd(m.refreshEvery)}
switch m.activeTab {
case TabStream:
cmds = append(cmds, streamTickCmd())
case TabFlame:
cmds = append(cmds, flameTickCmd())
default:
if m.bubbleEnabledForTab(m.activeTab) {
cmds = append(cmds, bubbleTickCmdFn())
}
}
if len(cmds) == 1 {
return cmds[0]
}
return tea.Batch(cmds...)
}
// Update handles ticks, snapshots, tab changes, and resize events.
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
streamWidth, streamHeight := streamViewport(msg.Width, msg.Height)
m.streamModel.SetViewport(streamWidth, streamHeight)
flameWidth, flameHeight := flameViewport(msg.Width, msg.Height, m.showHelp)
m.flamegraphModel.SetViewport(flameWidth, flameHeight)
m.setBubbleViewports(flameWidth, flameHeight)
if m.bubbleEnabledForTab(m.activeTab) && m.refreshBubbleData() {
return m, bubbleTickCmdFn()
}
return m, nil
case refreshTickMsg:
if !m.focused {
return m, nil
}
snap := m.snapshot()
return m, tea.Batch(
tickCmd(m.refreshEvery),
func() tea.Msg { return messages.StatsTickMsg{Snap: snap} },
)
case streamTickMsg:
if !m.focused {
return m, nil
}
if m.activeTab != TabStream {
return m, nil
}
m.streamModel.Refresh()
return m, streamTickCmd()
case flameTickMsg:
if !m.focused {
return m, nil
}
if m.activeTab != TabFlame {
return m, nil
}
var animCmd tea.Cmd
if m.liveTrie != nil && m.flamegraphModel.RefreshFromLiveTrie() {
animCmd = m.flamegraphModel.AnimationCmd()
}
if animCmd != nil {
return m, tea.Batch(flameTickCmd(), animCmd)
}
return m, flameTickCmd()
case bubbleTickMsg:
if !m.focused {
return m, nil
}
if !m.bubbleEnabledForTab(m.activeTab) {
return m, nil
}
_ = m.tickActiveBubbleChart()
if m.activeBubbleChartHasNodes() {
return m, bubbleTickCmdFn()
}
return m, nil
case messages.StatsTickMsg:
m.latest = msg.Snap
m.syscallsOffset = clampOffset(m.syscallsOffset, m.maxSyscallsRows())
m.syscallsTreemapSelection = clampOffset(m.syscallsTreemapSelection, m.maxSyscallsRows())
m.filesOffset = clampOffset(m.filesOffset, m.maxFilesRows())
m.filesDirOffset = clampOffset(m.filesDirOffset, m.maxFilesDirRows())
m.processesOffset = clampOffset(m.processesOffset, m.maxProcessesRows())
m.streamModel.Refresh()
if m.refreshBubbleData() {
return m, bubbleTickCmdFn()
}
return m, nil
case tea.KeyPressMsg:
return m.handleKey(msg)
case streamEditorDoneMsg:
if msg.err != nil {
m.streamModel.SetStatusMessage("Open failed: " + msg.err.Error())
}
return m, nil
}
if m.activeTab == TabFlame {
next, cmd := m.flamegraphModel.Update(translateFlamegraphMsg(msg))
m.flamegraphModel = next.(flamegraphtui.Model)
return m, cmd
}
return m, nil
}
func (m Model) handleKey(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
prevActiveTab := m.activeTab
var cmd tea.Cmd
keyStr := msg.String()
if keyStr == "H" {
m.showHelp = !m.showHelp
flameWidth, flameHeight := flameViewport(m.width, m.height, m.showHelp)
m.flamegraphModel.SetViewport(flameWidth, flameHeight)
return m, nil
}
if m.activeTab == TabFlame && m.flamegraphModel.ConsumesKey(msg) {
next, flameCmd := m.flamegraphModel.Update(msg)
m.flamegraphModel = next.(flamegraphtui.Model)
return m, flameCmd
}
handled, scrollCmd := m.handleScrollKey(msg)
if scrollCmd != nil {
cmd = scrollCmd
}
if handled && m.activeTab == TabStream && (keyStr == " " || keyStr == "space") && !m.streamModel.Paused() {
cmd = streamTickCmd()
}
if !handled {
switch {
case key.Matches(msg, m.keys.One):
m.activeTab = TabFlame
handled = true
case key.Matches(msg, m.keys.Tab):
m.activeTab = nextTab(m.activeTab)
handled = true
case key.Matches(msg, m.keys.ShiftTab):
m.activeTab = prevTab(m.activeTab)
handled = true
case key.Matches(msg, m.keys.Two):
m.activeTab = TabOverview
handled = true
case key.Matches(msg, m.keys.Three):
m.activeTab = TabSyscalls
handled = true
case key.Matches(msg, m.keys.Four):
m.activeTab = TabFiles
handled = true
case key.Matches(msg, m.keys.Five):
m.activeTab = TabProcesses
handled = true
case key.Matches(msg, m.keys.Six):
m.activeTab = TabLatency
handled = true
case key.Matches(msg, m.keys.Seven):
m.activeTab = TabStream
handled = true
case key.Matches(msg, m.keys.Visualize):
handled = true
cmd = m.cycleVisualizationMode()
case key.Matches(msg, m.keys.Metric):
handled = true
cmd = m.toggleBubbleMetric()
case key.Matches(msg, m.keys.Refresh):
cmd = m.resetBaselineCmd()
handled = true
case key.Matches(msg, m.keys.DirGroup):
if m.activeTab == TabFiles {
m.filesDirGrouped = !m.filesDirGrouped
if !m.filesDirGrouped && m.filesVizMode != tabVizModeTable {
m.filesVizMode = tabVizModeTable
}
if m.bubbleEnabledForTab(m.activeTab) && m.refreshBubbleData() {
cmd = bubbleTickCmdFn()
}
handled = true
}
}
}
if !handled {
if m.activeTab == TabFlame {
next, flameCmd := m.flamegraphModel.Update(msg)
m.flamegraphModel = next.(flamegraphtui.Model)
return m, flameCmd
}
return m, nil
}
batch := make([]tea.Cmd, 0, 3)
if cmd != nil {
batch = append(batch, cmd)
}
if prevActiveTab != TabStream && m.activeTab == TabStream {
batch = append(batch, streamTickCmd())
}
if prevActiveTab != TabFlame && m.activeTab == TabFlame {
batch = append(batch, flameTickCmd())
}
if prevActiveTab != m.activeTab && m.bubbleEnabledForTab(m.activeTab) {
batch = append(batch, bubbleTickCmdFn())
}
switch len(batch) {
case 0:
return m, nil
case 1:
return m, batch[0]
default:
return m, tea.Batch(batch...)
}
}
func (m *Model) handleScrollKey(msg tea.KeyPressMsg) (bool, tea.Cmd) {
keyStr := msg.String()
if m.bubbleEnabledForTab(m.activeTab) {
switch keyStr {
case "down", "j", "right", "l":
return m.moveBubbleSelection(1), nil
case "up", "k", "left", "h":
return m.moveBubbleSelection(-1), nil
default:
return false, nil
}
}
switch m.activeTab {
case TabSyscalls:
if m.syscallsVizMode == tabVizModeTreemap {
return scrollOffset(keyStr, &m.syscallsTreemapSelection, m.maxSyscallsRows()), nil
}
return scrollOffset(keyStr, &m.syscallsOffset, m.maxSyscallsRows()), nil
case TabFiles:
if m.filesDirGrouped {
return scrollOffset(keyStr, &m.filesDirOffset, m.maxFilesDirRows()), nil
}
return scrollOffset(keyStr, &m.filesOffset, m.maxFilesRows()), nil
case TabProcesses:
return scrollOffset(keyStr, &m.processesOffset, m.maxProcessesRows()), nil
case TabStream:
streamWidth, streamHeight := streamViewport(m.width, m.height)
m.streamModel.SetViewport(streamWidth, streamHeight)
handled := m.streamModel.HandleTeaKey(msg)
if path, ok := m.streamModel.ConsumeOpenEditorRequest(); ok {
editorCmd, err := eventstream.EditorCommandForPath(path)
if err != nil {
m.streamModel.SetStatusMessage("Open failed: " + err.Error())
return true, nil
}
return true, tea.ExecProcess(editorCmd, func(err error) tea.Msg {
return streamEditorDoneMsg{err: err}
})
}
return handled, nil
default:
return false, nil
}
}
func scrollOffset(keyStr string, offset *int, maxRows int) bool {
switch keyStr {
case "down", "j":
if *offset < maxRows-1 {
*offset++
}
return true
case "up", "k":
if *offset > 0 {
*offset--
}
return true
default:
return false
}
}
func (m Model) maxSyscallsRows() int {
if m.latest == nil {
return 0
}
return m.latest.SyscallsCount()
}
func (m Model) maxFilesRows() int {
if m.latest == nil {
return 0
}
return m.latest.FilesCount()
}
func (m Model) maxFilesDirRows() int {
if m.latest == nil {
return 0
}
return len(aggregateFilesByDir(m.latest.Files()))
}
func (m Model) maxProcessesRows() int {
if m.latest == nil {
return 0
}
return m.latest.ProcessesCount()
}
func (m Model) snapshot() *statsengine.Snapshot {
if m.engine == nil {
return nil
}
return m.engine.Snapshot()
}
func (m *Model) resetBaselineCmd() tea.Cmd {
if m.liveTrie != nil {
m.liveTrie.Reset()
}
var snap *statsengine.Snapshot
if resettable, ok := m.engine.(resettableSnapshotSource); ok {
resettable.Reset()
snap = resettable.Snapshot()
} else {
snap = m.snapshot()
}
return func() tea.Msg { return messages.StatsTickMsg{Snap: snap} }
}
// LatestSnapshot returns the most recently received snapshot.
func (m Model) LatestSnapshot() *statsengine.Snapshot {
return m.latest
}
// BlocksGlobalShortcuts reports whether the active tab should suppress a
// top-level shortcut for the given key press.
func (m Model) BlocksGlobalShortcuts(msg tea.KeyPressMsg) bool {
if m.activeTab == TabStream {
return m.streamModel.FilterModalVisible() || m.streamModel.ExportModalVisible() || m.streamModel.SearchModalVisible()
}
if m.activeTab == TabFlame {
return m.flamegraphModel.ConsumesKey(msg)
}
return false
}
// SetStreamSource updates the live stream source used by the stream tab.
func (m *Model) SetStreamSource(source eventstream.Source) {
m.streamModel.SetSource(source)
}
// SetLiveTrie updates the live trie source used by the flamegraph tab.
func (m *Model) SetLiveTrie(liveTrie flamegraphtui.LiveTrieSource) {
m.liveTrie = liveTrie
m.flamegraphModel.SetLiveTrie(liveTrie)
if m.width > 0 && m.height > 0 {
m.flamegraphModel.SetViewport(m.width, m.height)
}
m.flamegraphModel.RefreshFromLiveTrie()
}
// SetDarkMode updates dashboard child models for the active theme.
func (m *Model) SetDarkMode(isDark bool) {
m.isDark = isDark
m.streamModel.SetDarkMode(isDark)
m.flamegraphModel.SetDarkMode(isDark)
m.syscallsChart.SetDarkMode(isDark)
m.filesChart.SetDarkMode(isDark)
m.processesChart.SetDarkMode(isDark)
}
// SetFocused controls whether periodic refresh ticks are processed.
func (m *Model) SetFocused(focused bool) {
m.focused = focused
}
// SnapshotCmd returns a command that fetches and emits a fresh dashboard snapshot.
func (m Model) SnapshotCmd() tea.Cmd {
snap := m.snapshot()
return func() tea.Msg { return messages.StatsTickMsg{Snap: snap} }
}
// SetPidFilter updates the active PID filter used by tab render hints.
func (m *Model) SetPidFilter(pid int) {
m.pidFilter = pid
}
// View renders the tab bar, active tab scaffold, and help bar.
func (m Model) View() tea.View {
width, height := common.EffectiveViewport(m.width, m.height)
_, activeHeight := flameViewport(width, height, m.showHelp)
streamModel := m.streamModel
streamModel.SetFooterVisible(m.showHelp)
if m.activeTab == TabStream {
_, activeHeight = streamViewport(width, height)
}
var b strings.Builder
b.WriteString(renderTabBar(m.activeTab, width))
b.WriteString("\n")
b.WriteString(m.renderActiveContent(width, activeHeight, &streamModel))
b.WriteString("\n")
if m.showHelp {
b.WriteString(renderHelpBar(m.keys, width))
} else {
b.WriteString(renderHelpHint(width))
}
return tea.NewView(common.ScreenStyle.Render(b.String()))
}
func (m Model) renderActiveContent(width, activeHeight int, streamModel *eventstream.Model) string {
if m.activeTab == TabSyscalls && m.syscallsVizMode == tabVizModeTreemap {
return renderSyscallsTreemap(m.latest, width, activeHeight, m.syscallsChart.Metric(), m.syscallsTreemapSelection, m.isDark)
}
if m.activeTab == TabFiles && m.filesVizMode == tabVizModeTreemap && m.filesDirGrouped {
return renderFilesTreemap(m.latest, width, activeHeight, m.filesChart.Metric(), m.filesDirOffset, m.isDark)
}
if m.activeTab == TabProcesses && m.processesVizMode == tabVizModeTreemap {
return renderProcessesTreemap(m.latest, width, activeHeight, m.processesChart.Metric(), m.processesOffset, m.isDark)
}
if m.bubbleEnabledForTab(m.activeTab) {
switch m.activeTab {
case TabSyscalls:
chart := m.syscallsChart
return chart.Render("Syscalls", width, activeHeight)
case TabFiles:
chart := m.filesChart
return chart.Render("Files/Dirs", width, activeHeight)
case TabProcesses:
chart := m.processesChart
return chart.Render("Processes", width, activeHeight)
}
}
return renderActiveTab(
m.activeTab,
m.latest,
streamModel,
&m.flamegraphModel,
width,
activeHeight,
m.pidFilter,
m.syscallsOffset,
m.filesOffset,
m.filesDirGrouped,
m.filesDirOffset,
m.processesOffset,
)
}
func (m *Model) setBubbleViewports(width, height int) {
m.syscallsChart.SetViewport(width, height)
m.filesChart.SetViewport(width, height)
m.processesChart.SetViewport(width, height)
}
func (m *Model) refreshBubbleData() bool {
flameWidth, flameHeight := flameViewport(m.width, m.height, m.showHelp)
m.setBubbleViewports(flameWidth, flameHeight)
syscallsAnimating := m.syscallsChart.SetData(syscallBubbleData(m.latest))
if m.filesDirGrouped {
m.filesChart.SetStatusHint("")
} else {
m.filesChart.SetStatusHint("Files bubble view requires directory mode (press d).")
}
filesAnimating := false
if m.filesDirGrouped {
filesAnimating = m.filesChart.SetData(filesDirBubbleData(m.latest))
} else {
m.filesChart.SetData(nil)
}
processesAnimating := m.processesChart.SetData(processBubbleData(m.latest))
switch m.activeTab {
case TabSyscalls:
return m.syscallsVizMode == tabVizModeBubbles && syscallsAnimating
case TabFiles:
return m.filesVizMode == tabVizModeBubbles && filesAnimating
case TabProcesses:
return m.processesVizMode == tabVizModeBubbles && processesAnimating
default:
return false
}
}
func (m *Model) tickActiveBubbleChart() bool {
switch m.activeTab {
case TabSyscalls:
if m.syscallsVizMode != tabVizModeBubbles {
return false
}
return m.syscallsChart.Tick(0)
case TabFiles:
if m.filesVizMode != tabVizModeBubbles {
return false
}
return m.filesChart.Tick(0)
case TabProcesses:
if m.processesVizMode != tabVizModeBubbles {
return false
}
return m.processesChart.Tick(0)
default:
return false
}
}
func (m *Model) moveBubbleSelection(delta int) bool {
switch m.activeTab {
case TabSyscalls:
return m.syscallsChart.MoveSelection(delta)
case TabFiles:
return m.filesChart.MoveSelection(delta)
case TabProcesses:
return m.processesChart.MoveSelection(delta)
default:
return false
}
}
func (m Model) activeBubbleChartHasNodes() bool {
switch m.activeTab {
case TabSyscalls:
return m.syscallsChart.HasNodes()
case TabFiles:
return m.filesChart.HasNodes()
case TabProcesses:
return m.processesChart.HasNodes()
default:
return false
}
}
func (m Model) bubbleEnabledForTab(tab Tab) bool {
switch tab {
case TabSyscalls:
return m.syscallsVizMode == tabVizModeBubbles
case TabFiles:
return m.filesDirGrouped && m.filesVizMode == tabVizModeBubbles
case TabProcesses:
return m.processesVizMode == tabVizModeBubbles
default:
return false
}
}
func (m *Model) cycleVisualizationMode() tea.Cmd {
allowed := m.allowedVizModes(m.activeTab)
if len(allowed) < 2 {
return nil
}
current := m.tabVizModeFor(m.activeTab)
next := nextVizMode(current, allowed)
m.setTabVizMode(m.activeTab, next)
if next == tabVizModeBubbles {
m.refreshBubbleData()
if m.activeBubbleChartHasNodes() {
return bubbleTickCmdFn()
}
}
return nil
}
func (m *Model) toggleBubbleMetric() tea.Cmd {
switch m.activeTab {
case TabSyscalls:
m.syscallsChart.SetMetric(nextBubbleMetric(m.syscallsChart.Metric()))
m.refreshBubbleData()
if m.syscallsVizMode == tabVizModeBubbles && m.activeBubbleChartHasNodes() {
return bubbleTickCmdFn()
}
case TabFiles:
if !m.filesDirGrouped {
return nil
}
m.filesChart.SetMetric(nextBubbleMetric(m.filesChart.Metric()))
m.refreshBubbleData()
if m.filesVizMode == tabVizModeBubbles && m.activeBubbleChartHasNodes() {
return bubbleTickCmdFn()
}
case TabProcesses:
m.processesChart.SetMetric(nextBubbleMetric(m.processesChart.Metric()))
m.refreshBubbleData()
if m.processesVizMode == tabVizModeBubbles && m.activeBubbleChartHasNodes() {
return bubbleTickCmdFn()
}
}
return nil
}
func (m Model) tabVizModeFor(tab Tab) tabVizMode {
switch tab {
case TabSyscalls:
return m.syscallsVizMode
case TabFiles:
return m.filesVizMode
case TabProcesses:
return m.processesVizMode
default:
return tabVizModeTable
}
}
func (m *Model) setTabVizMode(tab Tab, mode tabVizMode) {
switch tab {
case TabSyscalls:
m.syscallsVizMode = mode
case TabFiles:
m.filesVizMode = mode
case TabProcesses:
m.processesVizMode = mode
}
}
func (m Model) allowedVizModes(tab Tab) []tabVizMode {
switch tab {
case TabSyscalls:
return []tabVizMode{tabVizModeTable, tabVizModeBubbles, tabVizModeTreemap}
case TabProcesses:
return []tabVizMode{tabVizModeTable, tabVizModeBubbles, tabVizModeTreemap}
case TabFiles:
if m.filesDirGrouped {
return []tabVizMode{tabVizModeTable, tabVizModeBubbles, tabVizModeTreemap}
}
return []tabVizMode{tabVizModeTable}
default:
return []tabVizMode{tabVizModeTable}
}
}
func nextVizMode(current tabVizMode, allowed []tabVizMode) tabVizMode {
if len(allowed) == 0 {
return tabVizModeTable
}
for idx, mode := range allowed {
if mode == current {
return allowed[(idx+1)%len(allowed)]
}
}
return allowed[0]
}
func nextBubbleMetric(metric bubbleMetric) bubbleMetric {
if metric == bubbleMetricBytes {
return bubbleMetricCount
}
return bubbleMetricBytes
}
func tickCmd(d time.Duration) tea.Cmd {
return tea.Tick(d, func(time.Time) tea.Msg { return refreshTickMsg{} })
}
func renderActiveTab(tab Tab, snap *statsengine.Snapshot, streamModel *eventstream.Model, flameModel *flamegraphtui.Model, width, height, pidFilter, syscallsOffset, filesOffset int, filesDirGrouped bool, filesDirOffset, processesOffset int) string {
if tab == TabStream {
if streamModel == nil {
return common.PanelStyle.Render("Stream: waiting for source...")
}
return streamModel.View(width, height)
}
if tab == TabFlame {
if flameModel == nil {
return common.PanelStyle.Render("Flame: waiting for model...")
}
flameModel.SetViewport(width, height)
return flameModel.View().Content
}
if snap == nil {
return common.PanelStyle.Render(tab.String() + ": waiting for stats...")
}
switch tab {
case TabOverview:
return renderOverview(snap, width, height)
case TabSyscalls:
return renderSyscallsWithOffset(snap, width, height, syscallsOffset)
case TabFiles:
if filesDirGrouped {
return renderFilesDirGrouped(snap, width, height, filesDirOffset)
}
return renderFilesWithOffset(snap, width, height, filesOffset)
case TabProcesses:
return renderProcessesWithOffset(snap, width, height, processesOffset, pidFilter)
case TabLatency:
return renderLatencyGapsTab(snap, width, height)
default:
return common.PanelStyle.Render("Unknown tab")
}
}
func streamTickCmd() tea.Cmd {
return tea.Tick(streamRefreshMs*time.Millisecond, func(time.Time) tea.Msg { return streamTickMsg{} })
}
func flameTickCmd() tea.Cmd {
return tea.Tick(flameRefreshMs*time.Millisecond, func(time.Time) tea.Msg { return flameTickMsg{} })
}
func bubbleTickCmdFn() tea.Cmd {
return tea.Tick(bubbleRefreshMs*time.Millisecond, func(time.Time) tea.Msg { return bubbleTickMsg{} })
}
func streamViewport(width, height int) (int, int) {
width, height = common.EffectiveViewport(width, height)
height -= streamChromeRows
if height < 1 {
height = 1
}
return width, height
}
func flameViewport(width, height int, showHelp bool) (int, int) {
width, height = common.EffectiveViewport(width, height)
chromeRows := dashboardTabBarRows + dashboardHelpHintRows
if showHelp {
chromeRows = dashboardTabBarRows + dashboardExpandedHelpRows
}
height -= chromeRows
if height < 1 {
height = 1
}
return width, height
}
func translateFlamegraphMsg(msg tea.Msg) tea.Msg {
switch mouse := msg.(type) {
case tea.MouseClickMsg:
m := mouse.Mouse()
m.Y -= dashboardTabBarRows
return tea.MouseClickMsg(m)
case tea.MouseReleaseMsg:
m := mouse.Mouse()
m.Y -= dashboardTabBarRows
return tea.MouseReleaseMsg(m)
case tea.MouseMotionMsg:
m := mouse.Mouse()
m.Y -= dashboardTabBarRows
return tea.MouseMotionMsg(m)
case tea.MouseWheelMsg:
m := mouse.Mouse()
m.Y -= dashboardTabBarRows
return tea.MouseWheelMsg(m)
default:
return msg
}
}
|