Show what a group spent, and fix the reports that never ran on PostgreSQL

Quotas can be written on an organization, a team or a project, and nothing
could show what any of them had used: usage came back per key, per user and per
model only. A quota whose consumption cannot be seen is one nobody reviews
until the day it refuses somebody — and since the previous commit made a group
quota a shared pool, it is now a number several people are spending against
without being able to look at it.

`llmgw ctl usage --by team` sums every key beneath each group, with the budget
that level sets beside it.

Beside it, and only when that level sets one. A team bounded by its
organization's budget has none of its own, and printing the organization's
against each of its teams would read as though each had that much — the exact
misreading this work exists to remove.

Joined through api_keys rather than read from usage.project_id: that column
records where the key was when the request was served, while the quota is
written against the tree. They agree until a key is moved, and a report that
disagrees with the limit shown beside it is the useless one.

Three existing reports were fixed on the way, all failing on PostgreSQL and all
passing in tests, because the tests run on SQLite:

  column "k.name" must appear in the GROUP BY clause    (UsageSince)
  column "us.email" must appear in the GROUP BY clause  (UsageByUser)
  column "usage.ts" must appear in the GROUP BY clause  (UsageBuckets)

SQLite picks an arbitrary row for an ungrouped column and carries on; PostgreSQL
refuses. The first two were selecting names they had not grouped. The third
grouped "ts / ?" while selecting "(ts / ?) * ?", which PostgreSQL matches as
text and so saw a bare ts — it is grouped by ordinal now, the same expression by
construction. The dashboard's usage charts have been empty on the cluster this
whole time, with the reason only in the log.

All four queries were run against the cluster's own PostgreSQL before this was
committed, because passing on SQLite is what they were already doing.

The grouping level picks from a fixed table of levels rather than being
interpolated, so an unknown one is an error and not a fragment of SQL.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5 committed 2026-09-27 12:26:14 +02:00
1 parent ebce41e22d
commit a83493bddb
7 files changed
+524

No files matched your search

+89
View File
@@ -9,6 +9,7 @@ import (
"net/http/httptest"
"strings"
"testing"
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/agent/discovery"
"gitea.mynodes.xyz/ilker/llmgw/internal/control"
@@ -533,3 +534,91 @@ func TestCapacityWithoutAStoreIsAnEmptyListRatherThanAnError(t *testing.T) {
t.Errorf("expected an empty list, got %s", rec.Body)
}
}
type memUsage struct {
rows []store.ScopeUsage
scope store.Scope
since time.Time
err error
}
func (m *memUsage) UsageByScope(_ context.Context, s store.Scope, since time.Time) ([]store.ScopeUsage, error) {
m.scope, m.since = s, since
return m.rows, m.err
}
func usageAPI(t *testing.T, u *memUsage) *http.ServeMux {
t.Helper()
a, _ := setup(t, testModels())
a.SetUsage(u)
mux := http.NewServeMux()
a.Register(mux)
return mux
}
func TestUsageDefaultsToTeamsOverTheLastDay(t *testing.T) {
u := &memUsage{}
rec := httptest.NewRecorder()
usageAPI(t, u).ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/api/v1/usage", nil))
if rec.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rec.Code, rec.Body)
}
// The team is the level a quota is usually written on, so it is the one
// somebody asking for "usage" with no argument means.
if u.scope != store.ScopeTeam {
t.Errorf("expected teams by default, got %q", u.scope)
}
if d := time.Since(u.since); d < 23*time.Hour || d > 25*time.Hour {
t.Errorf("expected roughly a day, got %v", d)
}
}
func TestUsageReportsTheFractionOnlyWhenThereIsABudget(t *testing.T) {
budget := int64(2000)
u := &memUsage{rows: []store.ScopeUsage{
{Scope: store.ScopeTeam, ID: "t1", Name: "platform", CostMicros: 1000,
Budget: &budget, BudgetPeriod: store.PeriodMonth},
{Scope: store.ScopeTeam, ID: "t2", Name: "research", CostMicros: 100},
}}
rec := httptest.NewRecorder()
usageAPI(t, u).ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/api/v1/usage", nil))
var got []ScopeUsageView
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
if got[0].Fraction == nil || *got[0].Fraction != 0.5 {
t.Errorf("expected half the budget used, got %v", got[0].Fraction)
}
// A team bounded only by its organization has no budget of its own; a
// fraction of nothing would have to invent a denominator.
if got[1].Fraction != nil || got[1].Budget != nil {
t.Errorf("a group with no budget should report neither, got %+v", got[1])
}
}
func TestAnUnknownGroupingIsTheCallersMistake(t *testing.T) {
u := &memUsage{err: errors.New(`usage cannot be grouped by "model"`)}
rec := httptest.NewRecorder()
usageAPI(t, u).ServeHTTP(rec,
httptest.NewRequest(http.MethodGet, "/api/v1/usage?by=model", nil))
if rec.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rec.Code)
}
}
func TestABadSinceIsRefusedBeforeTheQuery(t *testing.T) {
u := &memUsage{}
rec := httptest.NewRecorder()
usageAPI(t, u).ServeHTTP(rec,
httptest.NewRequest(http.MethodGet, "/api/v1/usage?since=lastweek", nil))
if rec.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rec.Code)
}
if !u.since.IsZero() {
t.Error("nothing should have reached the store")
}
}