summaryrefslogtreecommitdiff
path: root/internal/tui/dashboard/processes.go
blob: b71b22c4b7132a983c57a208c8bf2916ee4a3482 (plain)
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
package dashboard

import (
	"fmt"
	"strconv"
	"strings"

	"ior/internal/statsengine"

	"charm.land/bubbles/v2/table"
)

func renderProcesses(snap *statsengine.Snapshot, width, height int) string {
	return renderProcessesWithOffset(snap, width, height, 0, -1)
}

func renderProcessesWithOffset(snap *statsengine.Snapshot, width, height, offset, pidFilter int) string {
	if snap == nil {
		return "Processes: waiting for stats..."
	}

	rows := processRows(snap.Processes())
	if len(rows) == 0 {
		return "Processes: no data"
	}

	columns := []table.Column{
		{Title: "PID", Width: 8},
		{Title: "Comm", Width: 18},
		{Title: "Syscalls", Width: 10},
		{Title: "Rate/s", Width: 8},
		{Title: "Total Bytes", Width: 12},
		{Title: "Avg Latency", Width: 12},
	}

	tbl := table.New(
		table.WithColumns(columns),
		table.WithRows(rows),
		table.WithFocused(true),
	)
	tbl.SetHeight(syscallTableHeight(height))
	tbl.SetWidth(tableWidth(width))
	cursor := clampOffset(offset, len(rows))
	tbl.SetCursor(cursor)

	out := tbl.View() + fmt.Sprintf("\nRow %d/%d [enter:filter] [v:mode] [b:metric]", cursor+1, len(rows))
	if pidFilter > 0 {
		out += "\n" + "Note: this tab is most useful with All PIDs."
	}
	return out
}

func processRows(processes []statsengine.ProcessSnapshot) []table.Row {
	rows := make([]table.Row, 0, len(processes))
	for _, p := range processes {
		rows = append(rows, table.Row{
			strconv.FormatUint(uint64(p.PID), 10),
			truncateText(p.Comm, 18),
			strconv.FormatUint(p.Syscalls, 10),
			fmt.Sprintf("%.1f", p.RatePerSec),
			formatBytes(float64(p.Bytes)),
			formatDurationNs(p.AvgLatencyNs),
		})
	}
	return rows
}

func truncateText(value string, limit int) string {
	if len(value) <= limit {
		return value
	}
	if limit <= 3 {
		return value[:limit]
	}
	return strings.TrimSpace(value[:limit-3]) + "..."
}