From be9f45aa94118df38d21070bd221d94125b5428d Mon Sep 17 00:00:00 2001 From: Ilker Manap Date: Mon, 28 Sep 2026 18:11:27 +0200 Subject: [PATCH] Write the bar chart's latency the way its table does The ranked bars said "8825 ms" beside a table that said "8.8 s". One formatter, humanLatency, now serves both, with a test. Co-Authored-By: Claude Opus 5.5 (1M context) --- internal/web/charts2_test.go | 8 ++++++++ internal/web/dashboard.go | 2 +- internal/web/web.go | 28 +++++++++++++++------------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/internal/web/charts2_test.go b/internal/web/charts2_test.go index 00d5157..a0e1764 100644 --- a/internal/web/charts2_test.go +++ b/internal/web/charts2_test.go @@ -145,3 +145,11 @@ func TestTheRankedWrapperIsNotGivenAFixedHeight(t *testing.T) { } } } + +func TestLatencyReadsAsAPersonWouldSayIt(t *testing.T) { + for ms, want := range map[float64]string{5: "5 ms", 8825: "8.8 s", 110695: "1m 50s", 225634: "3m 45s"} { + if got := humanLatency(ms); got != want { + t.Errorf("%v ms: got %q, want %q", ms, got, want) + } + } +} diff --git a/internal/web/dashboard.go b/internal/web/dashboard.go index 055899b..b2a5484 100644 --- a/internal/web/dashboard.go +++ b/internal/web/dashboard.go @@ -229,7 +229,7 @@ func (d *DashboardData) addPeopleAndModels(ctx context.Context, usage UsageSourc rows = append(rows, Row{ Label: m.Model, Value: float64(m.Requests), - Note: fmt.Sprintf("%.0f ms", m.AvgLatency), + Note: humanLatency(m.AvgLatency), }) } d.ModelChart = HBarChart(rows, "--viz-3", "req") diff --git a/internal/web/web.go b/internal/web/web.go index fb74023..ea67337 100644 --- a/internal/web/web.go +++ b/internal/web/web.go @@ -44,19 +44,7 @@ var funcs = template.FuncMap{ } return strings.Join(v, ", ") }, - // latency writes a mean latency the way a person reads one: 5 ms, 8.8 s, - // 1m 51s — not 110695 ms. - "latency": func(ms float64) string { - switch { - case ms < 1000: - return fmt.Sprintf("%.0f ms", ms) - case ms < 60_000: - return fmt.Sprintf("%.1f s", ms/1000) - default: - d := time.Duration(ms) * time.Millisecond - return fmt.Sprintf("%dm %02ds", int(d.Minutes()), int(d.Seconds())%60) - } - }, + "latency": humanLatency, "duration": func(sec int64) string { return time.Duration(sec * int64(time.Second)).Round(time.Second).String() }, @@ -208,3 +196,17 @@ func render(w http.ResponseWriter, name string, data any) { http.Error(w, "template error", http.StatusInternalServerError) } } + +// humanLatency writes a mean latency the way a person reads one: 5 ms, 8.8 s, +// 1m 51s — not 110695 ms. +func humanLatency(ms float64) string { + switch { + case ms < 1000: + return fmt.Sprintf("%.0f ms", ms) + case ms < 60_000: + return fmt.Sprintf("%.1f s", ms/1000) + default: + d := time.Duration(ms) * time.Millisecond + return fmt.Sprintf("%dm %02ds", int(d.Minutes()), int(d.Seconds())%60) + } +}