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

import (
	"fmt"
	"ior/internal/statsengine"
	"ior/internal/tui"
	"math"
	"strconv"
	"strings"
)

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

	hist := renderHistogram(snap.LatencyHistogram, "Latency Histogram", width, height)
	spark := tui.PanelStyle.Render("Latency sparkline: " + renderSparkline(snap.LatencySeriesNs(), sparklineWidth(width)))
	return strings.Join([]string{hist, spark}, "\n")
}

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

	hist := renderHistogram(snap.GapHistogram, "Gap Histogram", width, height)
	spark := tui.PanelStyle.Render("Gap sparkline: " + renderSparkline(snap.GapSeriesNs(), sparklineWidth(width)))
	return strings.Join([]string{hist, spark}, "\n")
}

func renderHistogram(hist statsengine.HistogramSnapshot, title string, width, height int) string {
	buckets := hist.Buckets()
	if len(buckets) == 0 {
		return tui.PanelStyle.Render(title + ": no data")
	}

	if width <= 0 {
		width = 80
	}

	if height > 0 {
		maxRows := height - 3
		if maxRows < 1 {
			maxRows = 1
		}
		if len(buckets) > maxRows {
			buckets = buckets[:maxRows]
		}
	}

	maxCount := uint64(0)
	labelWidth := 0
	countWidth := len(strconv.FormatUint(hist.Total, 10))
	for _, bucket := range buckets {
		if bucket.Count > maxCount {
			maxCount = bucket.Count
		}
		if len(bucket.Label) > labelWidth {
			labelWidth = len(bucket.Label)
		}
		if digits := len(strconv.FormatUint(bucket.Count, 10)); digits > countWidth {
			countWidth = digits
		}
	}

	barWidth := width - labelWidth - countWidth - 10
	if barWidth < 8 {
		barWidth = 8
	}

	lines := make([]string, 0, len(buckets)+2)
	lines = append(lines, fmt.Sprintf("%s (total=%d)", title, hist.Total))
	for _, bucket := range buckets {
		bar := renderHistogramBar(bucket.Count, maxCount, barWidth)
		lines = append(lines, fmt.Sprintf("%-*s | %-*s %*d", labelWidth, bucket.Label, barWidth, bar, countWidth, bucket.Count))
	}
	lines = append(lines, "Scale: █▓▒░")

	return tui.PanelStyle.Render(strings.Join(lines, "\n"))
}

func renderHistogramBar(count, maxCount uint64, width int) string {
	if count == 0 || maxCount == 0 || width <= 0 {
		return ""
	}

	ratio := float64(count) / float64(maxCount)
	length := int(math.Round(ratio * float64(width)))
	if length < 1 {
		length = 1
	}
	if length > width {
		length = width
	}

	char := '░'
	switch {
	case ratio >= 0.75:
		char = '█'
	case ratio >= 0.5:
		char = '▓'
	case ratio >= 0.25:
		char = '▒'
	}

	return strings.Repeat(string(char), length)
}