Name people and projects in usage reports, and total a usage legend
The cluster page's usage table showed session:usr-... and proj-... ids; it now shows the person's email, whether it came through a key or the chat page, and the project's path or "personal". Times read "3 hours ago" rather than "3h7m25s ago", everywhere. The usage page's legend gave each line's latest slice, which was 0 beside a table of hundreds; it gives the window's total, and the table's numbers are right-aligned like everywhere else. The PostgreSQL placeholder rewriting skips line comments. An apostrophe in one opened a string that swallowed every placeholder after it. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
ff86e52126
commit
9b663eb23c
10 files changed
+180
-33
No files matched your search
@@ -48,14 +48,14 @@
|
||||
|
||||
{{if .Rows}}
|
||||
<table>
|
||||
<tr><th>Group</th><th>Requests</th><th>Tokens</th><th>Spend</th><th>Errors</th></tr>
|
||||
<tr><th>Group</th><th class="num">Requests</th><th class="num">Tokens</th><th class="num">Spend</th><th class="num">Errors</th></tr>
|
||||
{{range .Rows}}
|
||||
<tr>
|
||||
<td>{{.Name}}</td>
|
||||
<td>{{compact .Requests}}</td>
|
||||
<td>{{compact .Tokens}}</td>
|
||||
<td>{{.Spend}}</td>
|
||||
<td>{{if .Errors}}{{.Errors}}{{else}}—{{end}}</td>
|
||||
<td class="num">{{compact .Requests}}</td>
|
||||
<td class="num">{{compact .Tokens}}</td>
|
||||
<td class="num">{{.Spend}}</td>
|
||||
<td class="num">{{if .Errors}}{{.Errors}}{{else}}—{{end}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</table>
|
||||
|
||||
@@ -201,14 +201,15 @@ requested whenever a fallback served it.</p>
|
||||
<h2>Usage <span class="note">— last 24 hours</span></h2>
|
||||
{{if .AuthEnabled}}
|
||||
{{if .Usage}}
|
||||
<table><tr><th>Key</th><th>Project</th><th class="num">Requests</th><th class="num">Errors</th><th class="num">Prompt</th><th class="num">Completion</th><th class="num">Spend</th><th>Last</th></tr>
|
||||
<table><tr><th>Person</th><th>Through</th><th>Spends against</th><th class="num">Requests</th><th class="num">Errors</th><th class="num">Prompt</th><th class="num">Completion</th><th class="num">Spend</th><th>Last</th></tr>
|
||||
{{range .Usage}}<tr>
|
||||
<td>{{if .KeyName}}{{.KeyName}} <span class="note">{{.KeyPrefix}}</span>{{else}}<code>{{.KeyID}}</code>{{end}}</td>
|
||||
<td>{{.ProjectID}}</td>
|
||||
<td>{{or .Email "—"}}</td>
|
||||
<td>{{if .Chat}}chat page{{else if .KeyName}}{{.KeyName}} <code class="note">{{.KeyPrefix}}</code>{{else}}<code title="{{.KeyID}}">{{.KeyID}}</code>{{end}}</td>
|
||||
<td>{{if .ProjectPath}}{{.ProjectPath}}{{else if .ProjectID}}<code>{{.ProjectID}}</code>{{else}}personal{{end}}</td>
|
||||
<td class="num">{{.Requests}}</td>
|
||||
<td class="num">{{if .Errors}}<span class="state-failed">{{.Errors}}</span>{{else}}0{{end}}</td>
|
||||
<td class="num">{{.Prompt}}</td>
|
||||
<td class="num">{{.Completion}}</td>
|
||||
<td class="num">{{compact .Prompt}}</td>
|
||||
<td class="num">{{compact .Completion}}</td>
|
||||
<td class="num">{{units .CostMicros}}</td>
|
||||
<td><span class="note">{{ago .LastAt}}</span></td>
|
||||
</tr>{{end}}</table>
|
||||
|
||||
@@ -61,7 +61,7 @@ type LegendEntry struct {
|
||||
// Ranked by peak, which is what a gauge asks: the model that hit the ceiling is
|
||||
// the one worth a colour. For a counter, see LineChartTotals.
|
||||
func LineChart(series []promq.Series, unit string, maxSeries int) TimeSeries {
|
||||
return lineChart(series, unit, maxSeries, promq.Series.Max)
|
||||
return lineChart(series, unit, maxSeries, promq.Series.Max, promq.Series.Last)
|
||||
}
|
||||
|
||||
// LineChartTotals is LineChart ranked by the sum over the window.
|
||||
@@ -70,12 +70,16 @@ func LineChart(series []promq.Series, unit string, maxSeries int) TimeSeries {
|
||||
// the total, and ranking by peak would give a colour to the team that made one
|
||||
// large burst over the team that ran steadily all week — the opposite of what a
|
||||
// usage report is read for.
|
||||
//
|
||||
// Its legend gives each line's total too. The latest slice, which is what a
|
||||
// gauge's legend shows, is usually a quiet one for a counter: a legend reading
|
||||
// "0 · 0 · 0" under a table of hundreds said nothing true.
|
||||
func LineChartTotals(series []promq.Series, unit string, maxSeries int) TimeSeries {
|
||||
return lineChart(series, unit, maxSeries, promq.Series.Sum)
|
||||
return lineChart(series, unit, maxSeries, promq.Series.Sum, promq.Series.Sum)
|
||||
}
|
||||
|
||||
func lineChart(series []promq.Series, unit string, maxSeries int,
|
||||
weight func(promq.Series) float64,
|
||||
weight, legend func(promq.Series) float64,
|
||||
) TimeSeries {
|
||||
if maxSeries <= 0 || maxSeries > len(vizColours) {
|
||||
maxSeries = len(vizColours)
|
||||
@@ -111,7 +115,7 @@ func lineChart(series []promq.Series, unit string, maxSeries int,
|
||||
}
|
||||
writeLine(&b, s, minT, span, maxV, colour)
|
||||
out.Legend = append(out.Legend, LegendEntry{
|
||||
Name: s.Name, Colour: colour, Last: formatValue(s.Last(), unit),
|
||||
Name: s.Name, Colour: colour, Last: formatValue(legend(s), unit),
|
||||
})
|
||||
}
|
||||
b.WriteString(`</svg>`)
|
||||
|
||||
@@ -183,3 +183,16 @@ func TestWithNoMetricsStoreThereAreNoPanels(t *testing.T) {
|
||||
t.Errorf("expected nothing, got %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAUsageLegendGivesTotalsNotTheLastSlice(t *testing.T) {
|
||||
// A counter's latest slice is usually quiet; "0" beside a line that
|
||||
// carried hundreds read as a broken chart.
|
||||
c := LineChartTotals([]promq.Series{ts("ml", 5, 7, 0), ts("vision", 1, 2, 0)}, "", 4)
|
||||
got := map[string]string{}
|
||||
for _, l := range c.Legend {
|
||||
got[l.Name] = l.Last
|
||||
}
|
||||
if got["ml"] != "12" || got["vision"] != "3" {
|
||||
t.Errorf("legend = %v, expected the totals", got)
|
||||
}
|
||||
}
|
||||
+31
-6
@@ -80,12 +80,7 @@ var funcs = template.FuncMap{
|
||||
}
|
||||
return p
|
||||
},
|
||||
"ago": func(t time.Time) string {
|
||||
if t.IsZero() {
|
||||
return "never"
|
||||
}
|
||||
return time.Since(t).Round(time.Second).String() + " ago"
|
||||
},
|
||||
"ago": ago,
|
||||
// Spend is stored in micro-units so accumulation is exact; it is shown in
|
||||
// whole units because that is the number an operator set a budget in.
|
||||
// compact renders a large count the way someone says it aloud: 1.2k rather
|
||||
@@ -226,3 +221,33 @@ func humanLatency(ms float64) string {
|
||||
return fmt.Sprintf("%dm %02ds", int(d.Minutes()), int(d.Seconds())%60)
|
||||
}
|
||||
}
|
||||
|
||||
// ago says how long ago something was the way a person would: to the unit
|
||||
// that matters and no finer. "3h7m25s ago" is precise about seconds nobody
|
||||
// asked about and slower to read than "3 hours ago".
|
||||
func ago(t time.Time) string {
|
||||
if t.IsZero() {
|
||||
return "never"
|
||||
}
|
||||
d := time.Since(t)
|
||||
unit := func(n int, one string) string {
|
||||
if n == 1 {
|
||||
return "1 " + one + " ago"
|
||||
}
|
||||
return fmt.Sprintf("%d %ss ago", n, one)
|
||||
}
|
||||
switch {
|
||||
case d < 45*time.Second:
|
||||
return "just now"
|
||||
case d < time.Hour:
|
||||
return unit(int(d.Minutes()+0.5), "minute")
|
||||
case d < 36*time.Hour:
|
||||
return unit(int(d.Hours()+0.5), "hour")
|
||||
case d < 14*24*time.Hour:
|
||||
return unit(int(d.Hours()/24+0.5), "day")
|
||||
case d < 60*24*time.Hour:
|
||||
return unit(int(d.Hours()/24/7+0.5), "week")
|
||||
default:
|
||||
return unit(int(d.Hours()/24/30+0.5), "month")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user