diff --git a/internal/router/handlers.go b/internal/router/handlers.go index 453858f1..f0f3381f 100644 --- a/internal/router/handlers.go +++ b/internal/router/handlers.go @@ -156,6 +156,9 @@ func (r *Router) Handler() http.Handler { r.clusterView, r.clusterUsage, version.Version, 5*time.Second, false, web.NavExtras{Mirror: r.mirror != nil}, r.log))) pages.RegisterAdminUsers(mux, r.store) + // Quotas are written on the same tree the accounts page builds, so + // they are mounted together. + pages.RegisterAdminQuotas(mux, r.store) } // Independent of the cluster view: the mirror is a separate machine diff --git a/internal/store/tenancy.go b/internal/store/tenancy.go index 1bfa3598..cbba8673 100644 --- a/internal/store/tenancy.go +++ b/internal/store/tenancy.go @@ -120,3 +120,61 @@ func (s *DB) nameIsFree(ctx context.Context, q string, args ...any) error { } return nil } + +// AllLimits reads every limit that has been set, keyed by scope and id. +// +// All of them in one read rather than one query per group: the quota page shows +// the whole tree, and a row for each of an organization's projects would +// otherwise be a query each. +func (s *DB) AllLimits(ctx context.Context) (map[string]Limits, error) { + rows, err := queryAll(ctx, s.db, ` + SELECT scope_type, scope_id, rpm, tpm, budget_micros, budget_period, allowed_models + FROM limits`, nil, func(rows *sql.Rows) (Limits, error) { + var l Limits + var scope string + var models sql.NullString + var rpm, tpm sql.NullInt64 + var budget sql.NullInt64 + var period sql.NullString + if err := rows.Scan(&scope, &l.ScopeID, &rpm, &tpm, &budget, &period, &models); err != nil { + return l, err + } + l.Scope = Scope(scope) + if rpm.Valid { + v := int(rpm.Int64) + l.RPM = &v + } + if tpm.Valid { + v := int(tpm.Int64) + l.TPM = &v + } + if budget.Valid { + v := budget.Int64 + l.Budget = &v + } + l.BudgetPeriod = Period(period.String) + am, err := decodeModels(models) + l.AllowedModels = am + return l, err + }) + if err != nil { + return nil, err + } + out := make(map[string]Limits, len(rows)) + for _, l := range rows { + out[LimitOwner{Scope: l.Scope, ID: l.ScopeID}.CounterKey()] = l + } + return out, nil +} + +// ClearLimits removes every limit at one scope. +// +// Distinct from setting them all to nothing, which is what an empty form would +// otherwise write: a row of NULLs and no row mean the same to resolution, but +// only one of them says "nobody has set a limit here" when somebody reads the +// table directly. +func (s *DB) ClearLimits(ctx context.Context, scope Scope, id string) error { + _, err := s.db.ExecContext(ctx, + `DELETE FROM limits WHERE scope_type = ? AND scope_id = ?`, string(scope), id) + return err +} diff --git a/internal/web/account.go b/internal/web/account.go index a7edf8cd..b44d61f6 100644 --- a/internal/web/account.go +++ b/internal/web/account.go @@ -55,6 +55,8 @@ type AccountPages struct { // hasMirror is set when /admin/mirror is registered, so the other admin // pages can link to it and only then. hasMirror bool + // hasQuotas is set when /admin/quotas is registered. + hasQuotas bool } // ServedModels supplies the names the profile page's example should use. diff --git a/internal/web/admin_quotas.go b/internal/web/admin_quotas.go new file mode 100644 index 00000000..6f50efd4 --- /dev/null +++ b/internal/web/admin_quotas.go @@ -0,0 +1,378 @@ +package web + +import ( + "context" + "errors" + "fmt" + "net/http" + "strconv" + "strings" + "time" + + "gitea.mynodes.xyz/ilker/llmgw/internal/store" +) + +// QuotaAdmin is the store half of the quota page. +type QuotaAdmin interface { + AllOrgs(ctx context.Context) ([]store.Org, error) + AllTeams(ctx context.Context) ([]store.TeamPath, error) + AllProjects(ctx context.Context) ([]store.ProjectPath, error) + AllLimits(ctx context.Context) (map[string]store.Limits, error) + PutLimits(ctx context.Context, l store.Limits) error + ClearLimits(ctx context.Context, scope store.Scope, id string) error + UsageByScope(ctx context.Context, scope store.Scope, since time.Time) ([]store.ScopeUsage, error) +} + +// quotaRow is one place a quota can be written, and what is written there. +type quotaRow struct { + Scope store.Scope + ID string + // Path is how a person recognises it: "Scania / platform / gateway". + Path string + // Indent is how deep in the tree it sits, so the table reads as one. + Indent int + // Crumb is the full path, for a chooser where indentation cannot carry it. + Crumb string + + RPM string + TPM string + Budget string + Period string + Models string + HasAny bool + + Spend string + Percent int + HasPct bool + + // Shadowed names an ancestor whose limit is stricter, which means the value + // written here does nothing. + // + // Resolution takes the most restrictive along the chain, so a project given + // a budget larger than its team's is not a bigger budget — it is a number + // somebody typed that will never be reached. Without this the page would + // show it as though it applied, and the first sign would be a refusal that + // disagrees with what is on screen. + Shadowed string +} + +type adminQuotasData struct { + Rows []quotaRow + Since string + Message string + Error string + CSRF string + Version string + Now string +} + +// RegisterAdminQuotas mounts quota administration under /admin/quotas. +// +// Reading and writing both, which is a change of position. Setting a quota was +// kept to the shell on the grounds that raising a budget is a financial act and +// a stolen admin session should not manage one. That argument holds for exactly +// one row on this page: resolution takes the most restrictive value along the +// chain, so raising a team or project limit cannot exceed what the organization +// allows. Only the organization's own limit is bounded by nothing. +// +// So the trade is narrower than it looked, and against it stands a quota nobody +// could see or change without a shell on the control plane — which in practice +// means quotas nobody sets. Every change is logged with who made it and what it +// was before. +func (p *AccountPages) RegisterAdminQuotas(mux *http.ServeMux, s QuotaAdmin) { + p.hasQuotas = true + + mux.HandleFunc("GET /admin/quotas", p.adminOnly( + func(w http.ResponseWriter, r *http.Request, _ store.User) { + p.renderQuotas(w, r, s, "", "") + })) + + mux.HandleFunc("POST /admin/quotas", p.adminOnly( + func(w http.ResponseWriter, r *http.Request, admin store.User) { + sess, _, _ := p.session(r) + if !p.accounts.CheckCSRF(sess, r.FormValue("csrf")) { + http.Error(w, "bad request", http.StatusBadRequest) + return + } + msg, err := p.applyQuota(r, s, admin) + if err != nil { + p.log.Warn("a quota change was refused", + "admin", admin.Email, "scope", r.FormValue("scope"), + "id", r.FormValue("id"), "err", err) + p.renderQuotas(w, r, s, "", err.Error()) + return + } + p.renderQuotas(w, r, s, msg, "") + })) +} + +// applyQuota writes one scope's limits, and says what it did. +func (p *AccountPages) applyQuota(r *http.Request, s QuotaAdmin, admin store.User) (string, error) { + // "scope:id", from the chooser, or the two fields the Clear buttons post. + scope, id := store.Scope(r.FormValue("scope")), r.FormValue("id") + if t := r.FormValue("target"); t != "" { + sc, i, ok := strings.Cut(t, ":") + if !ok { + return "", errors.New("choose where the quota goes") + } + scope, id = store.Scope(sc), i + } + switch scope { + case store.ScopeOrg, store.ScopeTeam, store.ScopeProject: + default: + return "", errors.New("a quota belongs to an organization, a team or a project") + } + if id == "" { + return "", errors.New("which one?") + } + + // Before the write, so the audit line can say what it was. A record that a + // budget changed without the number it changed from answers nothing later. + before := "unknown" + if all, err := s.AllLimits(r.Context()); err == nil { + before = describeLimits(all[store.LimitOwner{Scope: scope, ID: id}.CounterKey()]) + } + + if r.FormValue("clear") != "" { + if err := s.ClearLimits(r.Context(), scope, id); err != nil { + return "", err + } + p.log.Info("a quota was cleared", "admin", admin.Email, + "scope", scope, "id", id, "was", before) + return string(scope) + " " + id + ": every limit here removed", nil + } + + l := store.Limits{Scope: scope, ScopeID: id} + var err error + if l.RPM, err = optInt(r.FormValue("rpm"), "requests per minute"); err != nil { + return "", err + } + if l.TPM, err = optInt(r.FormValue("tpm"), "tokens per minute"); err != nil { + return "", err + } + // Whole units in, micro-units stored: the operator set a budget in the unit + // they think in, and the store keeps integers so repeated addition of a + // cost cannot drift. + if l.Budget, err = optUnits(r.FormValue("budget")); err != nil { + return "", err + } + if l.Budget != nil { + l.BudgetPeriod = store.Period(r.FormValue("period")) + if l.BudgetPeriod != store.PeriodDay && l.BudgetPeriod != store.PeriodMonth { + return "", errors.New("a budget needs a period: day or month") + } + } + if m := strings.TrimSpace(r.FormValue("models")); m != "" { + for _, name := range strings.Split(m, ",") { + if name = strings.TrimSpace(name); name != "" { + l.AllowedModels = append(l.AllowedModels, name) + } + } + } + + if err := s.PutLimits(r.Context(), l); err != nil { + return "", err + } + now := describeLimits(l) + p.log.Info("a quota was changed", "admin", admin.Email, + "scope", scope, "id", id, "was", before, "now", now) + return string(scope) + " " + id + ": " + now, nil +} + +func (p *AccountPages) renderQuotas(w http.ResponseWriter, r *http.Request, + s QuotaAdmin, msg, errMsg string, +) { + const window = 30 * 24 * time.Hour + d := adminQuotasData{ + Since: "30 days", Message: msg, Error: errMsg, + CSRF: csrfFor(r), Version: p.version, + Now: time.Now().UTC().Format(time.RFC3339), + } + + limits, err := s.AllLimits(r.Context()) + if err != nil { + p.log.Error("could not read the limits", "err", err) + d.Error = "could not read the limits" + render(w, "admin_quotas", d) + return + } + spend := p.spendByScope(r.Context(), s, time.Now().Add(-window)) + + orgs, _ := s.AllOrgs(r.Context()) + teams, _ := s.AllTeams(r.Context()) + projects, _ := s.AllProjects(r.Context()) + + // Depth first, so a team reads as belonging to the organization above it + // rather than as another row in a flat list. + teamsByOrg := map[string][]store.TeamPath{} + for _, t := range teams { + teamsByOrg[t.OrgID] = append(teamsByOrg[t.OrgID], t) + } + pathOf := map[string]string{} + for _, pr := range projects { + pathOf[pr.ID] = pr.Path + } + + for _, o := range orgs { + d.Rows = append(d.Rows, quotaRow2(store.ScopeOrg, o.ID, o.Name, o.Name, 0, limits, spend)) + for _, t := range teamsByOrg[o.ID] { + d.Rows = append(d.Rows, + quotaRow2(store.ScopeTeam, t.ID, t.Name, o.Name+" / "+t.Name, 1, limits, spend)) + for _, pr := range projects { + if !strings.HasPrefix(pathOf[pr.ID], o.Name+" / "+t.Name+" / ") { + continue + } + name := strings.TrimPrefix(pathOf[pr.ID], o.Name+" / "+t.Name+" / ") + d.Rows = append(d.Rows, + quotaRow2(store.ScopeProject, pr.ID, name, pathOf[pr.ID], 2, limits, spend)) + } + } + } + markShadowed(d.Rows, limits, orgs, teamsByOrg, projects, pathOf) + render(w, "admin_quotas", d) +} + +// spendByScope is what each group has used in the window, keyed like a limit. +func (p *AccountPages) spendByScope(ctx context.Context, s QuotaAdmin, + since time.Time, +) map[string]int64 { + out := map[string]int64{} + for _, sc := range []store.Scope{store.ScopeOrg, store.ScopeTeam, store.ScopeProject} { + rows, err := s.UsageByScope(ctx, sc, since) + if err != nil { + // One level failing must not blank the other two: a page showing + // two thirds of the answer is more use than an error. + p.log.Warn("could not read usage for the quota page", "scope", sc, "err", err) + continue + } + for _, u := range rows { + out[store.LimitOwner{Scope: sc, ID: u.ID}.CounterKey()] = u.CostMicros + } + } + return out +} + +func quotaRow2(scope store.Scope, id, name, crumb string, indent int, + limits map[string]store.Limits, spend map[string]int64, +) quotaRow { + key := store.LimitOwner{Scope: scope, ID: id}.CounterKey() + l := limits[key] + row := quotaRow{ + Scope: scope, ID: id, Path: name, Crumb: crumb, Indent: indent, + RPM: dash(l.RPM), TPM: dash(l.TPM), + Budget: dashUnits(l.Budget), Period: string(l.BudgetPeriod), + Models: strings.Join(l.AllowedModels, ", "), + HasAny: l.RPM != nil || l.TPM != nil || l.Budget != nil || l.AllowedModels != nil, + Spend: units(spend[key]), + } + if l.Budget != nil && *l.Budget > 0 { + row.Percent = int(spend[key] * 100 / *l.Budget) + row.HasPct = true + } + return row +} + +// markShadowed flags rows whose budget an ancestor already caps lower. +func markShadowed(rows []quotaRow, limits map[string]store.Limits, + orgs []store.Org, teamsByOrg map[string][]store.TeamPath, + projects []store.ProjectPath, pathOf map[string]string, +) { + budget := func(scope store.Scope, id string) *int64 { + return limits[store.LimitOwner{Scope: scope, ID: id}.CounterKey()].Budget + } + // Parent of each team and project, so a row can look upward. + parent := map[string]struct { + scope store.Scope + id string + name string + }{} + for _, o := range orgs { + for _, t := range teamsByOrg[o.ID] { + parent[t.ID] = struct { + scope store.Scope + id string + name string + }{store.ScopeOrg, o.ID, o.Name} + for _, pr := range projects { + if strings.HasPrefix(pathOf[pr.ID], o.Name+" / "+t.Name+" / ") { + parent[pr.ID] = struct { + scope store.Scope + id string + name string + }{store.ScopeTeam, t.ID, t.Name} + } + } + } + } + + for i := range rows { + own := budget(rows[i].Scope, rows[i].ID) + if own == nil { + continue + } + for cur := parent[rows[i].ID]; cur.id != ""; cur = parent[cur.id] { + if up := budget(cur.scope, cur.id); up != nil && *up < *own { + rows[i].Shadowed = fmt.Sprintf("%s caps this at %s", cur.name, units(*up)) + break + } + } + } +} + +func optInt(v, what string) (*int, error) { + if strings.TrimSpace(v) == "" { + return nil, nil + } + n, err := strconv.Atoi(strings.TrimSpace(v)) + if err != nil || n < 0 { + return nil, fmt.Errorf("%s must be a whole number, or empty for no limit", what) + } + return &n, nil +} + +// optUnits reads a budget written in whole units and stores micro-units. +func optUnits(v string) (*int64, error) { + if strings.TrimSpace(v) == "" { + return nil, nil + } + f, err := strconv.ParseFloat(strings.TrimSpace(v), 64) + if err != nil || f < 0 { + return nil, errors.New("the budget must be a number, or empty for no budget") + } + m := int64(f * 1e6) + return &m, nil +} + +func describeLimits(l store.Limits) string { + var parts []string + if l.RPM != nil { + parts = append(parts, "rpm "+strconv.Itoa(*l.RPM)) + } + if l.TPM != nil { + parts = append(parts, "tpm "+strconv.Itoa(*l.TPM)) + } + if l.Budget != nil { + parts = append(parts, "budget "+units(*l.Budget)+"/"+string(l.BudgetPeriod)) + } + if l.AllowedModels != nil { + parts = append(parts, "models "+strings.Join(l.AllowedModels, "|")) + } + if len(parts) == 0 { + return "no limits" + } + return strings.Join(parts, ", ") +} + +func dash(v *int) string { + if v == nil { + return "" + } + return strconv.Itoa(*v) +} + +func dashUnits(v *int64) string { + if v == nil { + return "" + } + return units(*v) +} diff --git a/internal/web/admin_quotas_test.go b/internal/web/admin_quotas_test.go new file mode 100644 index 00000000..e582941a --- /dev/null +++ b/internal/web/admin_quotas_test.go @@ -0,0 +1,280 @@ +package web + +import ( + "context" + "io" + "log/slog" + "net/http" + "net/url" + "strings" + "testing" + "time" + + "gitea.mynodes.xyz/ilker/llmgw/internal/store" +) + +type fakeQuotas struct { + orgs []store.Org + teams []store.TeamPath + projects []store.ProjectPath + limits map[string]store.Limits + usage map[store.Scope][]store.ScopeUsage + + put []store.Limits + cleared [][2]string +} + +func (f *fakeQuotas) AllOrgs(context.Context) ([]store.Org, error) { return f.orgs, nil } +func (f *fakeQuotas) AllTeams(context.Context) ([]store.TeamPath, error) { + return f.teams, nil +} + +func (f *fakeQuotas) AllProjects(context.Context) ([]store.ProjectPath, error) { + return f.projects, nil +} + +func (f *fakeQuotas) AllLimits(context.Context) (map[string]store.Limits, error) { + if f.limits == nil { + f.limits = map[string]store.Limits{} + } + return f.limits, nil +} + +func (f *fakeQuotas) PutLimits(_ context.Context, l store.Limits) error { + f.put = append(f.put, l) + return nil +} + +func (f *fakeQuotas) ClearLimits(_ context.Context, s store.Scope, id string) error { + f.cleared = append(f.cleared, [2]string{string(s), id}) + return nil +} + +func (f *fakeQuotas) UsageByScope(_ context.Context, s store.Scope, _ time.Time) ([]store.ScopeUsage, error) { + return f.usage[s], nil +} + +func quotasPage(t *testing.T, s *fakeQuotas, admin bool) *http.ServeMux { + t.Helper() + a := fakeAccounts{user: store.User{ID: "u1", Email: "a@example.com", IsAdmin: admin}} + p := NewAccountPages(a, nil, nil, "v0", slog.New(slog.NewTextHandler(io.Discard, nil))) + mux := http.NewServeMux() + p.RegisterAdminQuotas(mux, s) + return mux +} + +func i64(v int64) *int64 { return &v } + +// oneTeam is an org with a team and a project under it. +func oneTeam() *fakeQuotas { + return &fakeQuotas{ + orgs: []store.Org{{ID: "org-1", Name: "Scania"}}, + teams: []store.TeamPath{{ID: "team-1", Name: "platform", OrgID: "org-1", OrgName: "Scania"}}, + projects: []store.ProjectPath{{ID: "proj-1", Path: "Scania / platform / gateway"}}, + limits: map[string]store.Limits{}, + } +} + +func TestTheWholeTreeIsListedWhetherOrNotItHasAQuota(t *testing.T) { + body := mirrorGet(t, quotasPage(t, oneTeam(), true), "/admin/quotas").Body.String() + + // A group with no quota is the one most likely to need one, so it cannot + // be the one the page leaves out. + for _, want := range []string{"Scania", "platform", "gateway"} { + if !strings.Contains(body, want) { + t.Errorf("%q is missing from the page", want) + } + } +} + +func TestSpendIsShownAgainstTheBudget(t *testing.T) { + s := oneTeam() + s.limits["team:team-1"] = store.Limits{ + Scope: store.ScopeTeam, ScopeID: "team-1", + Budget: i64(2_000_000), BudgetPeriod: store.PeriodMonth, + } + s.usage = map[store.Scope][]store.ScopeUsage{ + store.ScopeTeam: {{ID: "team-1", CostMicros: 1_000_000}}, + } + + body := mirrorGet(t, quotasPage(t, s, true), "/admin/quotas").Body.String() + if !strings.Contains(body, "50%") { + t.Errorf("expected half the budget used to be shown; got %s", body) + } +} + +func TestALimitAnAncestorAlreadyCapsIsMarked(t *testing.T) { + s := oneTeam() + // The team is given more than its organization allows. Resolution takes the + // most restrictive, so this is not a bigger budget — it is a number that + // will never be reached. + s.limits["org:org-1"] = store.Limits{ + Scope: store.ScopeOrg, ScopeID: "org-1", Budget: i64(1_000_000), + BudgetPeriod: store.PeriodMonth, + } + s.limits["team:team-1"] = store.Limits{ + Scope: store.ScopeTeam, ScopeID: "team-1", Budget: i64(5_000_000), + BudgetPeriod: store.PeriodMonth, + } + + body := mirrorGet(t, quotasPage(t, s, true), "/admin/quotas").Body.String() + if !strings.Contains(body, "Scania caps this at") { + t.Errorf("a shadowed limit should say so; got %s", body) + } +} + +func TestALimitNothingCapsIsNotMarked(t *testing.T) { + s := oneTeam() + s.limits["org:org-1"] = store.Limits{ + Scope: store.ScopeOrg, ScopeID: "org-1", Budget: i64(9_000_000), + BudgetPeriod: store.PeriodMonth, + } + s.limits["team:team-1"] = store.Limits{ + Scope: store.ScopeTeam, ScopeID: "team-1", Budget: i64(1_000_000), + BudgetPeriod: store.PeriodMonth, + } + + body := mirrorGet(t, quotasPage(t, s, true), "/admin/quotas").Body.String() + if strings.Contains(body, "caps this at") { + t.Error("a limit stricter than its parent's is in force and must not be flagged") + } +} + +func TestABudgetIsTakenInWholeUnitsAndStoredInMicros(t *testing.T) { + s := oneTeam() + mirrorPost(t, quotasPage(t, s, true), "/admin/quotas", url.Values{ + "csrf": {"tok"}, "scope": {"team"}, "id": {"team-1"}, + "budget": {"1000"}, "period": {"month"}, + }) + + if len(s.put) != 1 || s.put[0].Budget == nil || *s.put[0].Budget != 1_000_000_000 { + t.Fatalf("expected 1000 units stored as micro-units, got %+v", s.put) + } +} + +func TestABudgetWithoutAPeriodIsRefused(t *testing.T) { + s := oneTeam() + body := mirrorPost(t, quotasPage(t, s, true), "/admin/quotas", url.Values{ + "csrf": {"tok"}, "scope": {"team"}, "id": {"team-1"}, "budget": {"1000"}, + }).Body.String() + + // A monthly budget credited to a daily counter is a budget that never + // binds, so the period cannot be left to a default. + if len(s.put) != 0 { + t.Error("nothing should have been written") + } + if !strings.Contains(body, "period") { + t.Errorf("the error should name what is missing; got %s", body) + } +} + +func TestABlankFieldMeansNoLimitRatherThanZero(t *testing.T) { + s := oneTeam() + mirrorPost(t, quotasPage(t, s, true), "/admin/quotas", url.Values{ + "csrf": {"tok"}, "scope": {"team"}, "id": {"team-1"}, "rpm": {"60"}, + }) + + // Zero would be a limit of nothing — every request refused — which is the + // opposite of what an empty box means. + if len(s.put) != 1 { + t.Fatalf("expected one write, got %+v", s.put) + } + if s.put[0].TPM != nil { + t.Errorf("a blank field should leave no limit, got %d", *s.put[0].TPM) + } + if s.put[0].RPM == nil || *s.put[0].RPM != 60 { + t.Errorf("the value given should be written, got %+v", s.put[0].RPM) + } +} + +func TestRubbishInANumberIsRefused(t *testing.T) { + s := oneTeam() + mirrorPost(t, quotasPage(t, s, true), "/admin/quotas", url.Values{ + "csrf": {"tok"}, "scope": {"team"}, "id": {"team-1"}, "rpm": {"lots"}, + }) + if len(s.put) != 0 { + t.Error("nothing should have been written") + } +} + +func TestAQuotaCanBeRemovedEntirely(t *testing.T) { + s := oneTeam() + mirrorPost(t, quotasPage(t, s, true), "/admin/quotas", url.Values{ + "csrf": {"tok"}, "scope": {"team"}, "id": {"team-1"}, "clear": {"1"}, + }) + + // A row of nulls and no row resolve the same, but only one of them tells + // somebody reading the table that nobody has set a limit here. + if len(s.cleared) != 1 || s.cleared[0] != [2]string{"team", "team-1"} { + t.Errorf("expected the limits to be cleared, got %v", s.cleared) + } + if len(s.put) != 0 { + t.Error("clearing must not also write a row") + } +} + +func TestAQuotaOnSomethingThatIsNotAGroupIsRefused(t *testing.T) { + s := oneTeam() + body := mirrorPost(t, quotasPage(t, s, true), "/admin/quotas", url.Values{ + "csrf": {"tok"}, "scope": {"key"}, "id": {"key-1"}, "rpm": {"1"}, + }).Body.String() + + // Per-key limits stay on the CLI: an admin session reaching individual keys + // would put every caller's throughput one click away. + if len(s.put) != 0 { + t.Error("a key limit should not be settable here") + } + if !strings.Contains(body, "organization") { + t.Errorf("the refusal should say what is allowed; got %s", body) + } +} + +func TestChangingAQuotaNeedsTheCSRFToken(t *testing.T) { + s := oneTeam() + w := mirrorPost(t, quotasPage(t, s, true), "/admin/quotas", url.Values{ + "scope": {"team"}, "id": {"team-1"}, "rpm": {"60"}, + }) + if w.Code != http.StatusBadRequest || len(s.put) != 0 { + t.Errorf("expected 400 and no write, got %d %+v", w.Code, s.put) + } +} + +func TestSomebodyWhoIsNotAnAdminSeesNoQuotas(t *testing.T) { + if w := mirrorGet(t, quotasPage(t, oneTeam(), false), "/admin/quotas"); w.Code != http.StatusNotFound { + t.Errorf("expected 404 for a non-admin, got %d", w.Code) + } +} + +func TestTheChooserPicksTheGroupWithoutTypingAnId(t *testing.T) { + s := oneTeam() + mirrorPost(t, quotasPage(t, s, true), "/admin/quotas", url.Values{ + "csrf": {"tok"}, "target": {"team:team-1"}, "rpm": {"60"}, + }) + + // Ids are random so two organizations can both have a "platform" team, + // which makes them impossible to remember and a typo impossible to notice: + // it would write a quota onto nothing and report success. + if len(s.put) != 1 || s.put[0].ScopeID != "team-1" || s.put[0].Scope != store.ScopeTeam { + t.Fatalf("the chooser value did not resolve: %+v", s.put) + } +} + +func TestAChooserValueThatIsNotAPairIsRefused(t *testing.T) { + s := oneTeam() + mirrorPost(t, quotasPage(t, s, true), "/admin/quotas", url.Values{ + "csrf": {"tok"}, "target": {"team-1"}, "rpm": {"60"}, + }) + if len(s.put) != 0 { + t.Error("nothing should have been written") + } +} + +func TestTheChooserOffersEveryGroupByItsFullPath(t *testing.T) { + body := mirrorGet(t, quotasPage(t, oneTeam(), true), "/admin/quotas").Body.String() + + // "platform" alone is ambiguous the moment a second organization has one. + if !strings.Contains(body, `value="team:team-1"`) || + !strings.Contains(body, "Scania / platform") { + t.Errorf("the chooser should name the whole path; got %s", body) + } +} diff --git a/internal/web/admin_users.go b/internal/web/admin_users.go index f1c9d2b3..415395f7 100644 --- a/internal/web/admin_users.go +++ b/internal/web/admin_users.go @@ -39,8 +39,10 @@ type adminUsersData struct { CSRF string Version string Now string - // HasMirror keeps the nav from offering a page that was never mounted. + // HasMirror and HasQuotas keep the nav from offering a page that was never + // mounted. HasMirror bool + HasQuotas bool } // RegisterAdminUsers mounts account administration under /admin/users. @@ -223,6 +225,7 @@ func (p *AccountPages) renderUsers(w http.ResponseWriter, r *http.Request, CSRF: csrfFor(r), Version: p.version, HasMirror: p.hasMirror, + HasQuotas: p.hasQuotas, Now: time.Now().UTC().Format(time.RFC3339), } diff --git a/internal/web/templates/admin_quotas.html b/internal/web/templates/admin_quotas.html new file mode 100644 index 00000000..ec6d01bc --- /dev/null +++ b/internal/web/templates/admin_quotas.html @@ -0,0 +1,100 @@ +{{define "admin_quotas"}} +
+ +What each organization, team and project may use · spend over the last {{.Since}}
+ +{{.Error}}
{{end}} +{{if .Message}}{{.Message}}
{{end}} + +{{if .Rows}} +| Where | RPM | TPM | Budget | Spent | Used | |
|---|---|---|---|---|---|---|
| + {{if eq .Indent 0}}{{.Path}}{{else}}{{.Path}}{{end}} + {{.Scope}} + | +{{if .RPM}}{{.RPM}}{{else}}—{{end}} | +{{if .TPM}}{{.TPM}}{{else}}—{{end}} | +{{if .Budget}}{{.Budget}}{{if .Period}}/{{.Period}}{{end}}{{else}}—{{end}} | +{{.Spend}} | +{{if .HasPct}}{{.Percent}}%{{else}}—{{end}} | ++ + | +
| written here, but {{.Shadowed}} | ||||||
only: {{.Models}} | ||||||
Nothing to give a quota to yet. Make an organization on the +accounts page first.
+{{end}} + +A quota written here is shared by everything beneath it: two +people on one team spend the same budget, which is the number that will refuse +them. Whatever is set is replaced wholesale — a field left blank means no limit +of that kind at this level, not "leave what was there".
+ +Limits compose downward by taking the most restrictive value, so +a team cannot raise what its organization set and a project cannot raise its +team's. Only an organization's own limit is bounded by nothing. Model lists +intersect rather than add: a project cannot allow a model its team withheld.
+ +Still not here, deliberately: per-key limits. A key is issued by
+the person who owns it and bounded by the project it belongs to, and letting an
+admin session reach individual keys would put every caller's throughput one
+click away — llmgw keys limits --scope key for that.
{{len .Pending}} waiting for approval · {{len .Active}} active