CI / test (push) Successful in 1m50s
The dashboard charted what the cluster had been billed for — requests, tokens, spend, all from llmgw's own store — and nothing about whether it was healthy. Saturation, queue depth, cache occupancy and replica counts were numbers in a table at the instant the page loaded, with no way to see that one of them had been climbing for an hour. Four line charts now, read back from wherever the metrics were scraped to. Read back rather than kept twice. llmgw publishes these already; what a gauge cannot do is remember, and a ring buffer in this process would be a rival copy of the same numbers with its own retention, lost on every restart and disagreeing with the one an operator has on screen elsewhere. monitoring.prometheus is empty by default and the panels are then absent rather than empty. One axis per chart, always. Two measures of different scale belong in two charts: a second y-axis lets any two lines be made to cross wherever the author likes and the reader cannot tell it happened. Four colours, handed out in fixed order, never cycled. A fifth series is summed into "other" and drawn in grey — dropping it would make a chart that does not add up, and inventing a hue for it would put two lines on the page that nobody can tell apart. The fold is by timestamp, not by position: the tail's series do not share sample times, and adding them by index slides points sideways onto each other's clock. A legend for every chart with more than one line, because colour alone is not an encoding a reader can rely on. A flat zero is drawn along the bottom rather than left blank: nothing happening is a real answer, and an empty frame reads as a broken chart. A single sample is not a line and says so instead of putting a dot in the corner. One failed query fails all four panels. Three drawn and one blank invites reading the blank one as "nothing is happening there". NaN is dropped rather than plotted. Prometheus sends it as a string for a sample it has no value for, Go's ParseFloat accepts it happily, and one NaN coordinate is an SVG path that does not draw at all — taking the rest of the line with it. Axis maxima are rounded to a number a person would pick. An axis topping out at 0.8347 makes every gridline unreadable, and the shape is what a chart is for. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
233 lines
6.4 KiB
Go
233 lines
6.4 KiB
Go
package web
|
|
|
|
import (
|
|
"fmt"
|
|
"html/template"
|
|
"math"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"gitea.mynodes.xyz/ilker/llmgw/internal/promq"
|
|
)
|
|
|
|
// vizColours is the categorical palette, in the order it is handed out.
|
|
//
|
|
// Fixed order, never cycled. A fifth line does not get a generated hue — it is
|
|
// folded into "other" by foldSeries below, because a chart whose colours repeat
|
|
// is one where two lines mean the same thing and neither can be read.
|
|
var vizColours = []string{"--viz-1", "--viz-2", "--viz-3", "--viz-4"}
|
|
|
|
// otherColour is the muted grey the tail is drawn in, so it reads as a total
|
|
// rather than as a fifth thing with a name.
|
|
const otherColour = "--muted"
|
|
|
|
const (
|
|
tsW = 720.0
|
|
tsH = 150.0
|
|
tsPadL = 44.0
|
|
tsPadB = 18.0
|
|
tsPadT = 8.0
|
|
tsGridRows = 3
|
|
)
|
|
|
|
// TimeSeries is a chart of several lines over one clock.
|
|
type TimeSeries struct {
|
|
SVG template.HTML
|
|
Legend []LegendEntry
|
|
// Empty is true when there was nothing to draw, so the page can say why
|
|
// rather than showing a frame with no lines in it.
|
|
Empty bool
|
|
Peak string
|
|
}
|
|
|
|
// LegendEntry names one line and the colour it was drawn in.
|
|
//
|
|
// A legend is always present for two lines or more: colour alone is not an
|
|
// encoding anybody can rely on, and a reader who cannot tell two lines apart
|
|
// has a picture rather than a chart.
|
|
type LegendEntry struct {
|
|
Name string
|
|
Colour string
|
|
Last string
|
|
}
|
|
|
|
// LineChart draws several series against one axis.
|
|
//
|
|
// One axis, always. Two measures of different scale belong in two charts: a
|
|
// second y-axis lets any two lines be made to cross wherever the author likes,
|
|
// and the reader has no way to know it happened.
|
|
func LineChart(series []promq.Series, unit string, maxSeries int) TimeSeries {
|
|
if maxSeries <= 0 || maxSeries > len(vizColours) {
|
|
maxSeries = len(vizColours)
|
|
}
|
|
series = foldSeries(series, maxSeries)
|
|
if len(series) == 0 {
|
|
return TimeSeries{Empty: true}
|
|
}
|
|
|
|
minT, maxT, maxV := bounds(series)
|
|
if maxT.Equal(minT) {
|
|
// One instant is not a line. Reported as empty rather than drawn as a
|
|
// dot in the corner, which reads as a broken chart.
|
|
return TimeSeries{Empty: true}
|
|
}
|
|
// A flat zero is a real answer — nothing is happening — and drawing it
|
|
// along the bottom says so, where an autoscaled empty frame would not.
|
|
if maxV <= 0 {
|
|
maxV = 1
|
|
}
|
|
maxV = niceCeiling(maxV)
|
|
|
|
var b strings.Builder
|
|
fmt.Fprintf(&b, `<svg class="chart chart-lines" viewBox="0 0 %.0f %.0f" role="img">`, tsW, tsH)
|
|
writeGrid(&b, maxV, unit)
|
|
|
|
out := TimeSeries{Peak: formatValue(maxV, unit)}
|
|
span := maxT.Sub(minT).Seconds()
|
|
for i, s := range series {
|
|
colour := otherColour
|
|
if s.Name != otherLabel {
|
|
colour = vizColours[i%len(vizColours)]
|
|
}
|
|
writeLine(&b, s, minT, span, maxV, colour)
|
|
out.Legend = append(out.Legend, LegendEntry{
|
|
Name: s.Name, Colour: colour, Last: formatValue(s.Last(), unit),
|
|
})
|
|
}
|
|
b.WriteString(`</svg>`)
|
|
out.SVG = template.HTML(b.String()) //nolint:gosec // numbers and CSS vars only
|
|
return out
|
|
}
|
|
|
|
const otherLabel = "other"
|
|
|
|
// foldSeries keeps the busiest lines and sums the rest into one.
|
|
//
|
|
// Rather than dropping the tail, which would make a chart that does not add up,
|
|
// or colouring it, which needs hues the palette does not have. The fold is
|
|
// labelled so nobody reads it as a single model.
|
|
func foldSeries(in []promq.Series, keep int) []promq.Series {
|
|
clean := make([]promq.Series, 0, len(in))
|
|
for _, s := range in {
|
|
if len(s.Points) > 0 {
|
|
clean = append(clean, s)
|
|
}
|
|
}
|
|
if len(clean) <= keep {
|
|
return clean
|
|
}
|
|
sort.SliceStable(clean, func(i, j int) bool { return clean[i].Max() > clean[j].Max() })
|
|
|
|
rest := clean[keep-1:]
|
|
folded := promq.Series{Name: otherLabel}
|
|
// Summed by timestamp, because the tail's lines do not necessarily share
|
|
// sample times and adding them by index would shift points sideways.
|
|
at := map[int64]float64{}
|
|
for _, s := range rest {
|
|
for _, p := range s.Points {
|
|
at[p.At.Unix()] += p.Value
|
|
}
|
|
}
|
|
keys := make([]int64, 0, len(at))
|
|
for k := range at {
|
|
keys = append(keys, k)
|
|
}
|
|
sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })
|
|
for _, k := range keys {
|
|
folded.Points = append(folded.Points, promq.Point{At: time.Unix(k, 0), Value: at[k]})
|
|
}
|
|
return append(append([]promq.Series{}, clean[:keep-1]...), folded)
|
|
}
|
|
|
|
func bounds(series []promq.Series) (minT, maxT time.Time, maxV float64) {
|
|
for _, s := range series {
|
|
for _, p := range s.Points {
|
|
if minT.IsZero() || p.At.Before(minT) {
|
|
minT = p.At
|
|
}
|
|
if p.At.After(maxT) {
|
|
maxT = p.At
|
|
}
|
|
if p.Value > maxV {
|
|
maxV = p.Value
|
|
}
|
|
}
|
|
}
|
|
return minT, maxT, maxV
|
|
}
|
|
|
|
// writeGrid draws the horizontal rules and their labels.
|
|
//
|
|
// Recessive on purpose: the grid is there to be read off, not looked at, and a
|
|
// grid as dark as the data competes with it.
|
|
func writeGrid(b *strings.Builder, maxV float64, unit string) {
|
|
plotH := tsH - tsPadB - tsPadT
|
|
for i := 0; i <= tsGridRows; i++ {
|
|
y := tsPadT + plotH*float64(i)/tsGridRows
|
|
v := maxV * float64(tsGridRows-i) / tsGridRows
|
|
fmt.Fprintf(b, `<line x1="%.0f" y1="%.1f" x2="%.0f" y2="%.1f" class="axis"/>`,
|
|
tsPadL, y, tsW, y)
|
|
fmt.Fprintf(b, `<text x="%.0f" y="%.1f" class="tick">%s</text>`,
|
|
tsPadL-6, y+3, escapeText(formatValue(v, unit)))
|
|
}
|
|
}
|
|
|
|
func writeLine(b *strings.Builder, s promq.Series, minT time.Time,
|
|
span, maxV float64, colour string,
|
|
) {
|
|
plotW := tsW - tsPadL
|
|
plotH := tsH - tsPadB - tsPadT
|
|
|
|
fmt.Fprintf(b, `<polyline class="line" fill="none" stroke="var(%s)" points="`, cssVar(colour))
|
|
for _, p := range s.Points {
|
|
x := tsPadL + plotW*(p.At.Sub(minT).Seconds()/span)
|
|
y := tsPadT + plotH*(1-clamp01(p.Value/maxV))
|
|
fmt.Fprintf(b, "%.1f,%.1f ", x, y)
|
|
}
|
|
b.WriteString(`"/>`)
|
|
}
|
|
|
|
func clamp01(v float64) float64 {
|
|
if math.IsNaN(v) || v < 0 {
|
|
return 0
|
|
}
|
|
if v > 1 {
|
|
return 1
|
|
}
|
|
return v
|
|
}
|
|
|
|
// niceCeiling rounds an axis maximum up to something a person would choose.
|
|
//
|
|
// An axis topping out at 0.8347 makes every gridline a number nobody can hold
|
|
// in their head, and the shape of the data is what the chart is for.
|
|
func niceCeiling(v float64) float64 {
|
|
if v <= 0 {
|
|
return 1
|
|
}
|
|
mag := math.Pow(10, math.Floor(math.Log10(v)))
|
|
for _, step := range []float64{1, 2, 2.5, 5, 10} {
|
|
if v <= step*mag {
|
|
return step * mag
|
|
}
|
|
}
|
|
return 10 * mag
|
|
}
|
|
|
|
func formatValue(v float64, unit string) string {
|
|
switch unit {
|
|
case "%":
|
|
return fmt.Sprintf("%.0f%%", v*100)
|
|
case "s":
|
|
if v < 1 {
|
|
return fmt.Sprintf("%.0fms", v*1000)
|
|
}
|
|
return fmt.Sprintf("%.1fs", v)
|
|
case "bytes":
|
|
return compact(v) + "B"
|
|
default:
|
|
return compact(v)
|
|
}
|
|
}
|