Repository navigation
feat(coderd/database): add paginated organization AI spend user query - #29219
Conversation
2153280 to
aeb88bc
Compare
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: aeb88bce05
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
aeb88bc to
71dfc0c
Compare
|
@codex review |
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
71dfc0c to
7345ff5
Compare
|
This PR was reworked in place on 2026-09-15: the stack now extends the released organization spend export ( @codex review
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7345ff55b7
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Add ListOrganizationAISpendUsers, which aggregates AI Gateway token usage to one row per user for an organization over a [period_start, period_end) window, with optional provider_name, model, and client filters, ordered by cost. Each row carries the organization-wide user count and cost totals so a paginated caller can report them without a second query. The query shares its joins and predicates with ExportOrganizationAISpend so the JSON report and the CSV export account for the same token usage.
7345ff5 to
ba04efe
Compare
Every row of ListOrganizationAISpendUsers carries the count and totals over the whole organization, so filtering rows by the caller's group membership still exposed other users' spend. dbauthz now requires read access to every group member in the organization before running the query. Also cover shifted period windows and token usage recorded outside the window of the interception that produced it.
|
Pushed fixes for the human review round (organization-wide group member authorization in dbauthz, period window tests). @codex review
|
|
Codex Review: Didn't find any major issues. 🚀 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
ListOrganizationAISpendUsers returns the sorted distinct models per user next to providers and clients, so the spend page can show a Models column. The fixture gives one user two models under one provider so the expectations cannot be derived from the providers array.
|
Pushed the models aggregate requested for the spend page (29ed31e: @codex review
|
|
Codex Review: Didn't find any major issues. 🚀 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
First of a four-PR stack that reworks the AI spend page onto the released organization spend export,
GET /api/v2/organizations/{organization}/ai/spend/export, and a sibling per-user endpoint,GET /api/v2/organizations/{organization}/ai/spend/users, instead of the deployment-wide/api/v2/ai-gateway/spend/*endpoints this stack previously proposed. Every PR in the stack was rewritten in place from current main; the head branch name predates the rework. The export's accounting rules are unchanged: token usage bycreated_atin[period_start, period_end), attributed to the organization through the effective budget group, so the page reconciles with the CSV and with daily budget accounting.Adds
ListOrganizationAISpendUsers, the per-user aggregate behind the users endpoint. It shares the export query's joins and predicates, groups to one row per user with priced cost, a count of unpriced usage, and the sorted distinct providers, clients, and models the user spent through (a missing client is reported asUnknown, as in the sessions list), carries the window totals (count,total_cost_micros,total_unpriced_usage_count) as window aggregates ahead ofLIMIT/OFFSET, and orders by cost, then username, then user ID. Because every row carries those totals, dbauthz requires read access to every group member in the organization before running the query, instead of filtering rows by the caller's own membership.Includes the dbauthz wrapper and test, the dbmetrics and dbmock regeneration, and querier tests for grouping, totals across pages, filters, providers, clients, and models, shifted period windows (including token usage recorded outside the window of the interception that produced it), and organization isolation.