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:
Ilker ManapandClaude Opus 5.5 committed 2026-09-28 22:37:21 +02:00
1 parent ff86e52126
commit 9b663eb23c
10 files changed
+180 -33

No files matched your search

+12 -3
View File
@@ -621,21 +621,30 @@ func (s *DB) UsageSince(ctx context.Context, since time.Time) ([]KeyUsage, error
return queryAll(ctx, s.db, `
SELECT u.key_id,
COALESCE(k.name, ''), COALESCE(k.key_prefix, ''), u.project_id,
COALESCE(us.email, ''),
COALESCE(o.name || ' / ' || t.name || ' / ' || p.name, ''),
COUNT(*), SUM(u.prompt_tokens), SUM(u.completion_tokens),
SUM(u.cost_micros),
SUM(CASE WHEN u.status >= 400 THEN 1 ELSE 0 END),
MAX(u.ts)
FROM usage u
LEFT JOIN api_keys k ON k.id = u.key_id
-- Who, as recorded with the spend; the owner of the key for rows
-- written before the person was.
LEFT JOIN users us ON us.id = COALESCE(NULLIF(u.user_id, ''), k.user_id)
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.ts >= ?
-- k.name and k.key_prefix are selected, so PostgreSQL wants them
-- grouped. Both are determined by the key id already in the list.
GROUP BY u.key_id, u.project_id, k.name, k.key_prefix
-- Everything selected beside the sums is grouped, which PostgreSQL
-- wants; each is determined by the key and project already there.
GROUP BY u.key_id, u.project_id, k.name, k.key_prefix, us.email, o.name, t.name, p.name
ORDER BY SUM(u.cost_micros) DESC, COUNT(*) DESC`,
[]any{millis(since)}, func(rows *sql.Rows) (KeyUsage, error) {
var k KeyUsage
var last int64
err := rows.Scan(&k.KeyID, &k.KeyName, &k.KeyPrefix, &k.ProjectID,
&k.Email, &k.ProjectPath,
&k.Requests, &k.Prompt, &k.Completion, &k.CostMicros, &k.Errors, &last)
k.LastAt = fromMillis(last)
return k, err