summaryrefslogtreecommitdiff
path: root/internal/tui/dashboard/overview.go
blob: 3f563d4e00714264e43027a67e2e7a34a5a2e96b (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
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
package dashboard

import (
	"fmt"
	"ior/internal/statsengine"
	"ior/internal/tui"
	"strings"
	"time"
)

func renderOverview(snap *statsengine.Snapshot, width, height int) string {
	_ = height
	if snap == nil {
		return tui.PanelStyle.Render("Overview: waiting for stats...")
	}

	boxWidth := summaryBoxWidth(width)
	box1 := renderSyscallBox(snap, boxWidth)
	box2 := renderBytesBox(snap, boxWidth)
	box3 := renderErrorBox(snap, boxWidth)

	row := strings.Join([]string{box1, box2, box3}, "\n")
	trends := fmt.Sprintf(
		"Trends: latency %s  gap %s  throughput %s",
		trendWithArrow(snap.LatencyTrend),
		trendWithArrow(snap.GapTrend),
		trendWithArrow(snap.ThroughputTrend),
	)

	latencySpark := "Latency: " + renderSparkline(snap.LatencySeriesNs(), sparklineWidth(width))
	throughputSpark := "Throughput: " + renderSparkline(snap.ThroughputSeriesB(), sparklineWidth(width))
	topSyscalls := "Top syscalls: " + summarizeTopSyscalls(snap)

	return strings.Join(
		[]string{
			row,
			tui.HighlightStyle.Render(trends),
			tui.PanelStyle.Render(latencySpark),
			tui.PanelStyle.Render(throughputSpark),
			tui.PanelStyle.Render(topSyscalls),
		},
		"\n",
	)
}

func renderSyscallBox(snap *statsengine.Snapshot, width int) string {
	content := fmt.Sprintf(
		"Elapsed: %s\nSyscalls: %d\nRate: %.1f/s",
		formatElapsed(snap.Elapsed),
		snap.TotalSyscalls,
		snap.SyscallRatePerSec,
	)
	return tui.PanelStyle.Width(width).Render(content)
}

func renderBytesBox(snap *statsengine.Snapshot, width int) string {
	content := fmt.Sprintf(
		"Read/s: %s\nWrite/s: %s\nTotal: %s",
		formatBytes(snap.ReadBytesPerSec),
		formatBytes(snap.WriteBytesPerSec),
		formatBytes(float64(snap.TotalBytes)),
	)
	return tui.PanelStyle.Width(width).Render(content)
}

func renderErrorBox(snap *statsengine.Snapshot, width int) string {
	errPercent := 0.0
	if snap.TotalSyscalls > 0 {
		errPercent = float64(snap.TotalErrors) / float64(snap.TotalSyscalls) * 100
	}
	content := fmt.Sprintf(
		"Errors: %d\nError rate: %.2f%%\nLatency mean: %.0fns",
		snap.TotalErrors,
		errPercent,
		snap.LatencyMeanNs,
	)
	return tui.PanelStyle.Width(width).Render(content)
}

func trendWithArrow(trend statsengine.Trend) string {
	switch trend.Direction {
	case statsengine.TrendRising:
		return fmt.Sprintf("↑ %.1f%%", trend.DeltaPercent)
	case statsengine.TrendFalling:
		return fmt.Sprintf("↓ %.1f%%", trend.DeltaPercent)
	default:
		return fmt.Sprintf("→ %.1f%%", trend.DeltaPercent)
	}
}

func summarizeTopSyscalls(snap *statsengine.Snapshot) string {
	syscalls := snap.Syscalls()
	if len(syscalls) == 0 {
		return "none"
	}

	limit := 3
	if len(syscalls) < limit {
		limit = len(syscalls)
	}

	parts := make([]string, 0, limit)
	for _, syscall := range syscalls[:limit] {
		parts = append(parts, fmt.Sprintf("%s(%d)", syscall.Name, syscall.Count))
	}
	return strings.Join(parts, ", ")
}

func formatElapsed(elapsed time.Duration) string {
	if elapsed <= 0 {
		return "0s"
	}
	return elapsed.Round(time.Second).String()
}

func formatBytes(value float64) string {
	units := []string{"B", "KB", "MB", "GB", "TB"}
	unit := 0
	for value >= 1024 && unit < len(units)-1 {
		value /= 1024
		unit++
	}
	if unit == 0 {
		return fmt.Sprintf("%.0f%s", value, units[unit])
	}
	return fmt.Sprintf("%.1f%s", value, units[unit])
}

func summaryBoxWidth(width int) int {
	if width <= 0 {
		return 24
	}
	w := (width - 4) / 3
	if w < 18 {
		return 18
	}
	return w
}

func sparklineWidth(width int) int {
	if width <= 0 {
		return 20
	}
	w := width - 14
	if w < 8 {
		return 8
	}
	if w > 80 {
		return 80
	}
	return w
}