diff --git a/internal/store/selfusage.go b/internal/store/selfusage.go new file mode 100644 index 00000000..717d9fb6 --- /dev/null +++ b/internal/store/selfusage.go @@ -0,0 +1,138 @@ +package store + +import ( + "context" + "database/sql" + "fmt" + "strings" + "time" +) + +// PlaceUsage is one person's use in one place: a project, or on their own. +type PlaceUsage struct { + // ProjectID is empty for personal use. + ProjectID string + // Path names the project; empty for personal use. + Path string + Requests int64 + Tokens int64 + CostMicros int64 +} + +// UsageOfUser is one person's own use since a time: where it went, and how it +// ran over time in slices of width. What a person sees of themselves — every +// place they spent, and nothing anybody else did. +func (s *DB) UsageOfUser( + ctx context.Context, userID string, since time.Time, width time.Duration, +) ([]PlaceUsage, []Bucket, error) { + if userID == "" { + return nil, nil, fmt.Errorf("whose usage?") + } + places, err := queryAll(ctx, s.db, ` + SELECT COALESCE(NULLIF(u.project_id, ''), ''), + COALESCE(o.name || ' / ' || t.name || ' / ' || p.name, ''), + COUNT(*), SUM(u.prompt_tokens + u.completion_tokens), SUM(u.cost_micros) + FROM usage u + LEFT JOIN projects p ON p.id = NULLIF(u.project_id, '') + LEFT JOIN teams t ON t.id = p.team_id + LEFT JOIN organizations o ON o.id = t.org_id + WHERE u.user_id = ? AND u.ts >= ? + GROUP BY 1, 2 + ORDER BY 4 DESC`, []any{userID, millis(since)}, + func(rows *sql.Rows) (PlaceUsage, error) { + var pu PlaceUsage + return pu, rows.Scan(&pu.ProjectID, &pu.Path, &pu.Requests, &pu.Tokens, &pu.CostMicros) + }) + if err != nil { + return nil, nil, err + } + + if width <= 0 { + width = 24 * time.Hour + } + ms := width.Milliseconds() + got, err := queryAll(ctx, s.db, ` + SELECT (ts / ?) * ?, COUNT(*), + SUM(CASE WHEN status >= 400 THEN 1 ELSE 0 END), + SUM(prompt_tokens), SUM(completion_tokens), SUM(cost_micros) + FROM usage WHERE user_id = ? AND ts >= ? + GROUP BY 1 ORDER BY 1`, []any{ms, ms, userID, millis(since)}, + func(rows *sql.Rows) (Bucket, error) { + var b Bucket + var start int64 + err := rows.Scan(&start, &b.Requests, &b.Errors, &b.Prompt, &b.Completion, &b.CostMicros) + b.Start = fromMillis(start) + return b, err + }) + if err != nil { + return nil, nil, err + } + // Gap-filled, for the reason UsageBuckets is: a day with nothing in it is + // a day, not a hole. + at := map[int64]Bucket{} + for _, b := range got { + at[millis(b.Start)] = b + } + var buckets []Bucket + for t := (millis(since) / ms) * ms; t <= millis(time.Now()); t += ms { + b, ok := at[t] + if !ok { + b = Bucket{Start: fromMillis(t)} + } + buckets = append(buckets, b) + } + return places, buckets, nil +} + +// SpendOfPool is what has been spent since a time from the pool a limit +// counts against — the figure its budget is compared with. +// +// The pool follows the level: a project, team or organization's budget is +// spent by everyone beneath it; a person's by every key they own and the chat +// page; a per-member limit by one person within one scope, whose ScopeID is +// "scopeID/userID". A key's own limit is not answered here, and gives zero. +func (s *DB) SpendOfPool(ctx context.Context, l Limits, since time.Time) (int64, error) { + under := func(scope Scope) (string, error) { + switch scope { + case ScopeProject: + return `NULLIF(u.project_id, '') = ?`, nil + case ScopeTeam: + return `NULLIF(u.project_id, '') IN (SELECT id FROM projects WHERE team_id = ?)`, nil + case ScopeOrg: + return `NULLIF(u.project_id, '') IN (SELECT p.id FROM projects p + JOIN teams t ON t.id = p.team_id WHERE t.org_id = ?)`, nil + } + return "", fmt.Errorf("no pool for %q", scope) + } + var where string + var args []any + switch l.Scope { + case ScopeUser: + where, args = `u.user_id = ?`, []any{l.ScopeID} + case ScopeOrg, ScopeTeam, ScopeProject: + w, err := under(l.Scope) + if err != nil { + return 0, err + } + where, args = w, []any{l.ScopeID} + case ScopeOrgMember, ScopeTeamMember, ScopeProjectMember: + scopeID, userID, ok := strings.Cut(l.ScopeID, "/") + if !ok { + return 0, fmt.Errorf("a per-member pool is scope/user, not %q", l.ScopeID) + } + level := map[Scope]Scope{ScopeOrgMember: ScopeOrg, ScopeTeamMember: ScopeTeam, + ScopeProjectMember: ScopeProject}[l.Scope] + w, err := under(level) + if err != nil { + return 0, err + } + where, args = `u.user_id = ? AND `+w, []any{userID, scopeID} + default: + return 0, nil + } + var total sql.NullInt64 + err := s.db.QueryRowContext(ctx, + `SELECT SUM(u.cost_micros) FROM usage u WHERE `+where+` AND u.ts >= ?`, + append(args, millis(since))...).Scan(&total) + return total.Int64, err +} diff --git a/internal/store/selfusage_test.go b/internal/store/selfusage_test.go new file mode 100644 index 00000000..9db90706 --- /dev/null +++ b/internal/store/selfusage_test.go @@ -0,0 +1,92 @@ +package store + +import ( + "context" + "path/filepath" + "testing" + "time" +) + +func selfUsageFixture(t *testing.T, s *DB) time.Time { + t.Helper() + ctx := context.Background() + must := func(err error) { + t.Helper() + if err != nil { + t.Fatal(err) + } + } + must(s.PutOrg(ctx, Org{ID: "o", Name: "Acme"})) + must(s.PutTeam(ctx, Team{ID: "t", OrgID: "o", Name: "ml"})) + must(s.PutProject(ctx, Project{ID: "p", TeamID: "t", Name: "training"})) + must(s.PutProject(ctx, Project{ID: "q", TeamID: "t", Name: "eval"})) + now := time.Now() + u := func(user, project string, cost int64) Usage { + return Usage{At: now, KeyID: "session:" + user, UserID: user, ProjectID: project, + Model: "m", ServedModel: "m", PromptTokens: 10, CompletionTokens: 5, CostMicros: cost, Status: 200} + } + must(s.WriteUsage(ctx, []Usage{ + u("ada", "p", 100), u("ada", "p", 100), u("ada", "", 30), u("bob", "p", 1000), u("bob", "q", 7), + })) + return now +} + +func checkSelfUsage(t *testing.T, s *DB) { + now := selfUsageFixture(t, s) + ctx := context.Background() + since := now.Add(-48 * time.Hour) + + places, buckets, err := s.UsageOfUser(ctx, "ada", since, 24*time.Hour) + if err != nil { + t.Fatal(err) + } + got := map[string]PlaceUsage{} + for _, pl := range places { + got[pl.ProjectID] = pl + } + // Ada's own, and none of Bob's. + if len(places) != 2 || got["p"].Requests != 2 || got["p"].Path != "Acme / ml / training" || + got[""].CostMicros != 30 || got["p"].Tokens != 30 { + t.Errorf("places = %+v", places) + } + var total int64 + for _, b := range buckets { + total += b.Requests + } + if total != 3 || len(buckets) < 2 { + t.Errorf("%d buckets carrying %d requests", len(buckets), total) + } + + for _, c := range []struct { + l Limits + want int64 + }{ + {Limits{Scope: ScopeUser, ScopeID: "ada"}, 230}, + {Limits{Scope: ScopeProject, ScopeID: "p"}, 1200}, + {Limits{Scope: ScopeTeam, ScopeID: "t"}, 1207}, + {Limits{Scope: ScopeOrg, ScopeID: "o"}, 1207}, + {Limits{Scope: ScopeProjectMember, ScopeID: "p/ada"}, 200}, + {Limits{Scope: ScopeTeamMember, ScopeID: "t/bob"}, 1007}, + } { + n, err := s.SpendOfPool(ctx, c.l, since) + if err != nil { + t.Fatal(err) + } + if n != c.want { + t.Errorf("%s %s spent %d, want %d", c.l.Scope, c.l.ScopeID, n, c.want) + } + } +} + +func TestAPersonSeesTheirOwnUse(t *testing.T) { + s, err := OpenSQLite(context.Background(), filepath.Join(t.TempDir(), "u.db")) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = s.Close() }) + checkSelfUsage(t, s) +} + +func TestPostgresAPersonSeesTheirOwnUse(t *testing.T) { + checkSelfUsage(t, openPG(t)) +} diff --git a/internal/web/account.go b/internal/web/account.go index c7d0b632..3a720250 100644 --- a/internal/web/account.go +++ b/internal/web/account.go @@ -233,6 +233,13 @@ type limitRow struct{ Name, Value string } type contextLimits struct { Label string Rows []limitRow + // Use is what the person spent here over the window, and Budgets how + // much is left of each budget that applies; both nil without usage. + Use *placeUse + Budgets []budgetBar + + project string + chain []store.Limits } type profileData struct { @@ -244,6 +251,9 @@ type profileData struct { Where map[string]string Memberships []store.Membership LimitsBy []contextLimits + // Own is the person's use over the last thirty days; nil when it cannot + // be read. + Own *ownUse User store.User Keys []store.Key Limits []limitRow @@ -407,8 +417,10 @@ func (p *AccountPages) fillPlaces(ctx context.Context, user store.User, d *profi if err != nil { continue } - d.LimitsBy = append(d.LimitsBy, contextLimits{Label: c.Label, Rows: limitRows(e)}) + d.LimitsBy = append(d.LimitsBy, contextLimits{Label: c.Label, Rows: limitRows(e), + project: project, chain: e.Chain}) } + p.fillOwnUse(ctx, user, d) } func limitRows(e store.Effective) []limitRow { diff --git a/internal/web/selfusage.go b/internal/web/selfusage.go new file mode 100644 index 00000000..fc78fca5 --- /dev/null +++ b/internal/web/selfusage.go @@ -0,0 +1,137 @@ +package web + +import ( + "context" + "fmt" + "html/template" + "time" + + "gitea.mynodes.xyz/ilker/llmgw/internal/accounting" + "gitea.mynodes.xyz/ilker/llmgw/internal/store" +) + +// SelfUsage is what a person's own page reads of their use. Optional: a +// store without it leaves the page showing limits alone. +type SelfUsage interface { + UsageOfUser(ctx context.Context, userID string, since time.Time, width time.Duration) ([]store.PlaceUsage, []store.Bucket, error) + SpendOfPool(ctx context.Context, l store.Limits, since time.Time) (int64, error) +} + +// selfWindow is how far back a person's own page looks. +const selfWindow = 30 * 24 * time.Hour + +// ownUse is the person's last thirty days, for the top of their page. +type ownUse struct { + Requests, Tokens int64 + Spend string + Chart template.HTML +} + +// budgetBar is one budget that applies, and how much of it is gone. +type budgetBar struct { + // Label says whose pool it is: the person's own, or one they share. + Label string + Used, Budget string + Period string + Percent int + // Near is past four-fifths, when it is worth colouring. + Near, Over bool +} + +// placeUse is what a person spent in one place over the window. +type placeUse struct { + Requests, Tokens int64 + Spend string +} + +func (p *AccountPages) fillOwnUse(ctx context.Context, user store.User, d *profileData) { + su, ok := p.keys.(SelfUsage) + if !ok { + return + } + places, buckets, err := su.UsageOfUser(ctx, user.ID, time.Now().Add(-selfWindow), 24*time.Hour) + if err != nil { + p.log.Warn("could not read a person's own usage", "err", err) + return + } + u := &ownUse{} + var cost int64 + byPlace := map[string]placeUse{} + for _, pl := range places { + u.Requests += pl.Requests + u.Tokens += pl.Tokens + cost += pl.CostMicros + byPlace[pl.ProjectID] = placeUse{pl.Requests, pl.Tokens, units(pl.CostMicros)} + } + u.Spend = units(cost) + s := Series{Colour: "--accent"} + for _, b := range buckets { + s.Values = append(s.Values, float64(b.Prompt+b.Completion)) + s.Labels = append(s.Labels, b.Start.UTC().Format("Jan 2")) + } + if u.Requests > 0 { + u.Chart = BarChart(s, "tokens") + } + d.Own = u + + for i := range d.LimitsBy { + c := &d.LimitsBy[i] + if pu, ok := byPlace[c.project]; ok { + c.Use = &pu + } + for _, l := range c.chain { + if l.Budget == nil || l.Scope == store.ScopeKey { + continue + } + start := accounting.PeriodStart(l.BudgetPeriod, time.Now()) + spent, err := su.SpendOfPool(ctx, l, start) + if err != nil { + p.log.Warn("could not read a pool's spend", "scope", l.Scope, "err", err) + continue + } + bar := budgetBar{Label: poolLabel(l.Scope), Used: units(spent), Budget: units(*l.Budget), + Period: periodWord(l.BudgetPeriod)} + if *l.Budget > 0 { + bar.Percent = int(min(spent*100 / *l.Budget, 100)) + bar.Near = spent*5 >= *l.Budget*4 + bar.Over = spent >= *l.Budget + } + c.Budgets = append(c.Budgets, bar) + } + } +} + +// poolLabel says whose budget a limit is, which is the thing a person needs +// to read a bar right: their own is theirs to spend, a project's is shared +// with everyone in it. +func poolLabel(s store.Scope) string { + switch s { + case store.ScopeUser: + return "Your budget, across everything you use" + case store.ScopeProjectMember: + return "Your budget in this project" + case store.ScopeTeamMember: + return "Your budget in this team" + case store.ScopeOrgMember: + return "Your budget in this organization" + case store.ScopeProject: + return "The project's budget, shared with its members" + case store.ScopeTeam: + return "The team's budget, shared across its projects" + case store.ScopeOrg: + return "The organization's budget, shared across it" + } + return string(s) +} + +func periodWord(p store.Period) string { + switch p { + case store.PeriodDay: + return "today" + case store.PeriodMonth: + return "this month" + case store.PeriodNone: + return "in total" + } + return fmt.Sprintf("this %s", p) +} diff --git a/internal/web/selfusage_test.go b/internal/web/selfusage_test.go new file mode 100644 index 00000000..532d9de5 --- /dev/null +++ b/internal/web/selfusage_test.go @@ -0,0 +1,57 @@ +package web + +import ( + "context" + "io" + "log/slog" + "net/http" + "net/http/httptest" + "path/filepath" + "strings" + "testing" + "time" + + "gitea.mynodes.xyz/ilker/llmgw/internal/store" +) + +func TestAPersonSeesTheirUseAndWhatIsLeftOfTheirBudget(t *testing.T) { + ctx := context.Background() + st, err := store.OpenSQLite(ctx, filepath.Join(t.TempDir(), "s.db")) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = st.Close() }) + budget := int64(50e6) + st.SetPersonalDefaults(&store.Limits{Budget: &budget, BudgetPeriod: store.PeriodMonth}) + // 45 of 50 units this month: past four-fifths, so the bar says so. + if err := st.WriteUsage(ctx, []store.Usage{ + {At: time.Now(), KeyID: "session:ada", UserID: "ada", Model: "m", ServedModel: "m", + PromptTokens: 1200, CompletionTokens: 300, CostMicros: 45e6, Status: 200}, + {At: time.Now(), KeyID: "session:bob", UserID: "bob", Model: "m", ServedModel: "m", + PromptTokens: 9, CompletionTokens: 9, CostMicros: 1e6, Status: 200}, + }); err != nil { + t.Fatal(err) + } + + a := fakeAccounts{user: store.User{ID: "ada", Email: "ada@example.com"}} + p := NewAccountPages(a, st, nil, "v0", slog.New(slog.NewTextHandler(io.Discard, nil))) + mux := http.NewServeMux() + p.Register(mux) + req := httptest.NewRequest("GET", "/profile", nil) + req.AddCookie(&http.Cookie{Name: SessionCookie, Value: "good"}) + rec := httptest.NewRecorder() + mux.ServeHTTP(rec, req) + body := rec.Body.String() + + for _, want := range []string{ + "Your use and limits", + // Ada's 1.5k tokens, and none of Bob's. + ">1.5k", "tokens · 30 days", "Tokens per day", + "Your budget, across everything you use", "45.00 of 50.00 this month", + `class="budget near"`, "width: 90%", + } { + if !strings.Contains(body, want) { + t.Errorf("missing %q", want) + } + } +} diff --git a/internal/web/static/base.css b/internal/web/static/base.css index f35ee2a5..795d64c5 100644 --- a/internal/web/static/base.css +++ b/internal/web/static/base.css @@ -522,3 +522,13 @@ table.repo-files td:last-child .opt { display: block; margin-bottom: .2rem; } .bubble.md table { width: auto; max-width: 100%; display: block; overflow-x: auto; font-size: .85rem; } .bubble.md th, .bubble.md td { padding: .25rem .6rem; border-bottom: 1px solid var(--line); } .bubble.md blockquote { border-left: 3px solid var(--line); padding-left: .75rem; color: var(--muted); } +/* How much of a budget is gone, on a person's own page. */ +.budget { max-width: 34rem; margin: .4rem 0 .6rem; } +.budget-head { display: flex; justify-content: space-between; gap: 1rem; font-size: .85rem; color: var(--muted); } +.budget-head b { color: var(--fg); font-variant-numeric: tabular-nums; } +.budget-bar { height: .5rem; border-radius: 999px; background: var(--card); border: 1px solid var(--line); overflow: hidden; margin-top: .25rem; } +.budget-bar > span { display: block; height: 100%; background: var(--accent); } +.budget.near .budget-bar > span { background: var(--warn); } +.budget.over .budget-bar > span { background: var(--bad); } +.budget.over .budget-head b { color: var(--bad); } +.own-use .tile { min-width: 9rem; } diff --git a/internal/web/templates/account.html b/internal/web/templates/account.html index c1a599a3..36eada0b 100644 --- a/internal/web/templates/account.html +++ b/internal/web/templates/account.html @@ -47,10 +47,10 @@ Tell whoever you asked for access that you have signed up.

{{define "profile"}} -Your keys · llmgw +Account · llmgw
-

Your keys

+

Your account

{{.User.Email}}{{if ne .User.Name .User.Email}} ({{.User.Name}}){{end}}

{{template "nav" .Nav}}
@@ -106,6 +106,36 @@ minute. To rotate: create the new key, switch over, wait for "last used" on the old one to stop moving, then revoke it.

{{else}}

You have no keys yet.

{{end}} +

Where you are

+{{if .Memberships}} +

{{range $i, $m := .Memberships}}{{if $i}} · {{end}}{{$m.Path}} {{$m.Scope}}{{end}}

+{{else}}

You are not a member of any organization, team or project.

{{end}} + +

Your use and limits

+{{with .Own}} +
+
{{compact .Requests}}
requests · 30 days
+
{{compact .Tokens}}
tokens · 30 days
+
{{.Spend}}
spend · 30 days
+
+{{with .Chart}}

Tokens per day

{{.}}
{{end}} +{{end}} +{{range .LimitsBy}} +

{{.Label}}{{with .Use}} — {{compact .Requests}} requests, {{compact .Tokens}} tokens in 30 days{{end}}

+{{range .Budgets}} +
+
{{.Label}}{{.Used}} of {{.Budget}} {{.Period}}
+
+
+{{end}} +{{if .Rows}} + +{{range .Rows}}{{end}}
LimitValue
{{.Name}}{{.Value}}
+{{else}}

No limits here.

{{end}} +{{else}}

Nowhere to spend yet.

{{end}} +

Everything that applies, from the organization down to you: the +strictest value wins.

+

Password

{{if .PasswordChanged}}

Your password was changed. Any other browser you were signed in on has been signed out.

{{end}} @@ -121,21 +151,5 @@ signed in on has been signed out.

{{end}}

Changing it signs out every other browser, which is the point if you are changing it because something went wrong.

-

Where you are

-{{if .Memberships}} -

{{range $i, $m := .Memberships}}{{if $i}} · {{end}}{{$m.Path}} {{$m.Scope}}{{end}}

-{{else}}

You are not a member of any organization, team or project.

{{end}} - -

Your limits

-{{range .LimitsBy}} -

{{.Label}}

-{{if .Rows}} - -{{range .Rows}}{{end}}
LimitValue
{{.Name}}{{.Value}}
-{{else}}

No limits here.

{{end}} -{{else}}

Nowhere to spend yet.

{{end}} -

Everything that applies, from the organization down to you: the -strictest value wins.

-
{{end}} diff --git a/internal/web/templates/nav.html b/internal/web/templates/nav.html index 2d41f312..8314ba23 100644 --- a/internal/web/templates/nav.html +++ b/internal/web/templates/nav.html @@ -22,7 +22,7 @@ {{- end}} Chat - Your keys + Account