diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-10 23:19:52 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-10 23:19:52 +0200 |
| commit | 802033de1954bd2fe5e912002fb6c8a07743b66d (patch) | |
| tree | 63b7a61c3dfcc31ca6a7b85af73a959c04473445 /internal | |
| parent | f78f9110e9932a559a225104dd8115514d30af59 (diff) | |
dashboard: centralize nil snapshot fallback (task 385)
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/tui/dashboard/model.go | 52 | ||||
| -rw-r--r-- | internal/tui/dashboard/model_test.go | 9 |
2 files changed, 25 insertions, 36 deletions
diff --git a/internal/tui/dashboard/model.go b/internal/tui/dashboard/model.go index 0cc18f8..72290e6 100644 --- a/internal/tui/dashboard/model.go +++ b/internal/tui/dashboard/model.go @@ -362,10 +362,7 @@ func (m Model) selectedSyscallSnapshot() (statsengine.SyscallSnapshot, bool) { } func (m Model) sortedSyscallRows() []statsengine.SyscallSnapshot { - if m.latest == nil { - return nil - } - return sortedSyscallSnapshots(m.latest.Syscalls(), m.syscallsSort) + return sortedSyscallSnapshots(m.snapshotOrZero().Syscalls(), m.syscallsSort) } func (m Model) selectedSyscallName() string { @@ -511,10 +508,7 @@ func (m Model) selectedFileSnapshot() (statsengine.FileSnapshot, bool) { } func (m Model) sortedFileRows() []statsengine.FileSnapshot { - if m.latest == nil { - return nil - } - return sortedFileSnapshots(m.latest.Files(), m.filesSort) + return sortedFileSnapshots(m.snapshotOrZero().Files(), m.filesSort) } func (m Model) selectedFilePath() string { @@ -535,10 +529,7 @@ func (m Model) selectedDirSnapshot() (DirSnapshot, bool) { } func (m Model) sortedDirRows() []DirSnapshot { - if m.latest == nil { - return nil - } - return sortedDirSnapshots(aggregateFilesByDir(m.latest.Files()), m.filesDirSort) + return sortedDirSnapshots(aggregateFilesByDir(m.snapshotOrZero().Files()), m.filesDirSort) } func (m Model) selectedDirPath() string { @@ -651,10 +642,7 @@ func (m Model) selectedProcessFilter() (globalfilter.Filter, string, bool) { } func (m Model) selectedProcessSnapshot() (statsengine.ProcessSnapshot, bool) { - if m.latest == nil { - return statsengine.ProcessSnapshot{}, false - } - rows := m.latest.Processes() + rows := m.snapshotOrZero().Processes() if len(rows) == 0 { return statsengine.ProcessSnapshot{}, false } @@ -670,10 +658,7 @@ func (m Model) selectedProcessSnapshot() (statsengine.ProcessSnapshot, bool) { } func (m Model) sortedProcessTableRows() []statsengine.ProcessSnapshot { - if m.latest == nil { - return nil - } - return sortedProcessTableRows(m.latest.Processes(), m.processesSort) + return sortedProcessTableRows(m.snapshotOrZero().Processes(), m.processesSort) } func (m Model) selectedProcessPID() uint32 { @@ -854,24 +839,15 @@ func (m *Model) clampTableColumns() { } func (m Model) maxSyscallsRows() int { - if m.latest == nil { - return 0 - } - return m.latest.SyscallsCount() + return m.snapshotOrZero().SyscallsCount() } func (m Model) maxFilesRows() int { - if m.latest == nil { - return 0 - } - return m.latest.FilesCount() + return m.snapshotOrZero().FilesCount() } func (m Model) maxFilesDirRows() int { - if m.latest == nil { - return 0 - } - return len(aggregateFilesByDir(m.latest.Files())) + return len(aggregateFilesByDir(m.snapshotOrZero().Files())) } func (m Model) maxFilesDirRowsForMode() int { @@ -883,10 +859,7 @@ func (m Model) maxFilesDirRowsForMode() int { } func (m Model) maxProcessesRows() int { - if m.latest == nil { - return 0 - } - return m.latest.ProcessesCount() + return m.snapshotOrZero().ProcessesCount() } func (m Model) snapshot() *statsengine.Snapshot { @@ -896,6 +869,13 @@ func (m Model) snapshot() *statsengine.Snapshot { return m.engine.Snapshot() } +func (m Model) snapshotOrZero() statsengine.Snapshot { + if m.latest == nil { + return statsengine.Snapshot{} + } + return *m.latest +} + func (m *Model) resetBaselineCmd() tea.Cmd { if m.liveTrie != nil { m.liveTrie.Reset() diff --git a/internal/tui/dashboard/model_test.go b/internal/tui/dashboard/model_test.go index 59af73a..8c73230 100644 --- a/internal/tui/dashboard/model_test.go +++ b/internal/tui/dashboard/model_test.go @@ -75,6 +75,15 @@ func TestFlameViewportClampsHeightWithExpandedHelp(t *testing.T) { } } +func TestSnapshotOrZeroReturnsZeroSnapshotWhenLatestMissing(t *testing.T) { + m := NewModelWithConfig(nil, nil, 250, common.DefaultKeyMap()) + + snap := m.snapshotOrZero() + if snap.SyscallsCount() != 0 || snap.FilesCount() != 0 || snap.ProcessesCount() != 0 { + t.Fatalf("snapshotOrZero() should return an empty snapshot when latest is nil, got counts %d/%d/%d", snap.SyscallsCount(), snap.FilesCount(), snap.ProcessesCount()) + } +} + func TestKeySwitchingChangesActiveTab(t *testing.T) { m := NewModelWithConfig(nil, nil, 250, common.DefaultKeyMap()) |
