Label chart axes in binary sizes and small percents, and windows in words

A bytes axis read "20.0BB" - compact's billion with a B after it - and
a quiet node's CPU axis read 0% all the way up. Sizes are now binary like
every other on these pages, a percent under ten keeps a decimal, and a
window of an hour is "the last hour" rather than "1h0m0s".

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-09-28 23:30:01 +02:00
1 parent fe0875ee05
commit 9ae526302b
6 files changed
+57 -16

No files matched your search

+1 -1
View File
@@ -75,7 +75,7 @@ func LoadFleetCharts(ctx context.Context, h History, window time.Duration,
// size and a bigger response to parse on every page load.
step := window / 60
out := &FleetCharts{Window: window.String()}
out := &FleetCharts{Window: windowWord(window)}
for _, p := range fleetPanels {
series, err := h.RangeQuery(ctx, p.expr, p.nameBy, window, step)
if err != nil {
+1 -1
View File
@@ -126,7 +126,7 @@ func loadNodeCharts(ctx context.Context, h History, node string, replicas []stri
window time.Duration, log *slog.Logger,
) *nodeCharts {
step := window / 60
out := &nodeCharts{Window: window.String()}
out := &nodeCharts{Window: windowWord(window)}
quoted := fmt.Sprintf("%q", node)
for _, p := range nodePanels {
series, err := h.RangeQuery(ctx, fmt.Sprintf(p.expr, quoted), p.nameBy, window, step)
+1 -1
View File
@@ -9,7 +9,7 @@
<h1>{{.Node.ID}}</h1>
<p class="sub"><span class="state state-{{.Node.State}}">{{.Node.State}}</span>
{{with .Node.OS}} · {{.}}{{end}}{{with .Node.MgmtAddr}} · {{.}}{{end}}
· last seen {{or .Node.LastSeenAgo "never"}} ago · <a href="{{.Logs}}">logs</a> · <a href="{{.Back}}">cluster</a></p>
· last seen {{or .Node.LastSeenAgo "never"}} ago · <a href="{{.Logs}}">logs</a></p>
{{if .OnAdminListener}}<nav><a href="{{.Back}}">cluster</a></nav>{{else}}{{template "nav" .Nav}}{{end}}
</header>
+8 -1
View File
@@ -244,6 +244,11 @@ func niceCeiling(v float64) float64 {
func formatValue(v float64, unit string) string {
switch unit {
case "%":
// A decimal below ten, where whole percents would print a quiet
// machine's axis as 0%, 0%, 0%.
if v != 0 && v*100 < 10 {
return fmt.Sprintf("%.1f%%", v*100)
}
return fmt.Sprintf("%.0f%%", v*100)
case "s":
if v < 1 {
@@ -251,7 +256,9 @@ func formatValue(v float64, unit string) string {
}
return fmt.Sprintf("%.1fs", v)
case "bytes":
return compact(v) + "B"
// Binary, like every other size on these pages. compact's own
// suffixes are for counts, and on bytes read as "20.0BB".
return strings.Replace(humanBytes(int64(v)), " ", "", 1)
default:
return compact(v)
}
+17
View File
@@ -196,3 +196,20 @@ func TestAUsageLegendGivesTotalsNotTheLastSlice(t *testing.T) {
t.Errorf("legend = %v, expected the totals", got)
}
}
func TestChartAxesReadAsSizesAndSmallPercents(t *testing.T) {
// compact's suffixes are for counts: on bytes they read as "20.0BB".
if got := formatValue(20e9, "bytes"); got != "18.6GiB" {
t.Errorf("20e9 bytes = %q", got)
}
// A quiet machine is not 0%, 0%, 0% up its axis.
if got := formatValue(0.005, "%"); got != "0.5%" {
t.Errorf("0.005 = %q", got)
}
if got := formatValue(0.62, "%"); got != "62%" {
t.Errorf("0.62 = %q", got)
}
if got := windowWord(time.Hour); got != "hour" {
t.Errorf("an hour reads %q", got)
}
}
+29 -12
View File
@@ -101,18 +101,7 @@ var funcs = template.FuncMap{
// shows. Binary units because that is what `ls -lh` and every disk-full
// message on the host will say, and a page that disagrees with the shell
// about the size of the same file starts an argument about neither.
"bytes": func(n int64) string {
const unit = 1024
if n < unit {
return fmt.Sprintf("%d B", n)
}
div, exp := int64(unit), 0
for v := n / unit; v >= unit; v /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB", float64(n)/float64(div), "KMGTPE"[exp])
},
"bytes": humanBytes,
// dict lets one chart template be called with named arguments. Go
// templates pass a single value, and four charts differing in three
// strings each is four copies of the same markup otherwise.
@@ -256,3 +245,31 @@ func ago(t time.Time) string {
return unit(int(d.Hours()/24/30+0.5), "month")
}
}
// humanBytes writes a size in binary units, the way the shell does.
func humanBytes(n int64) string {
const unit = 1024
if n < unit {
return fmt.Sprintf("%d B", n)
}
div, exp := int64(unit), 0
for v := n / unit; v >= unit; v /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB", float64(n)/float64(div), "KMGTPE"[exp])
}
// windowWord names a chart window the way a sentence would: "hour", "6
// hours", "30 minutes", rather than Go's 1h0m0s.
func windowWord(d time.Duration) string {
switch {
case d == time.Hour:
return "hour"
case d%time.Hour == 0:
return fmt.Sprintf("%d hours", int(d.Hours()))
case d%time.Minute == 0:
return fmt.Sprintf("%d minutes", int(d.Minutes()))
}
return d.String()
}