Skip to content

feat(coderd/database): add AI spend session count query - #29222

Closed
ibetitsmike wants to merge 3 commits into
mike/ai-spend-query-rollupsfrom
mike/ai-spend-query-session-counts
Closed

ibetitsmike wants to merge 3 commits into
mike/ai-spend-query-rollupsfrom
mike/ai-spend-query-session-counts

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Sep 11, 2026 •

Copy link
Copy Markdown
Collaborator

Third of a seven-PR stack that restores the admin Spend page removed in #27330, rebuilt on the cost data AI Gateway already records. The stack lands the three database queries first (per-user spend, rollups, session counts), then the codersdk types, the users endpoint, the summary endpoints, and finally the page itself (#28964). Together the six backend PRs produce exactly the tree previously reviewed as a single PR; they were split so each layer can be read on its own.

Adds ListAIBridgeSpendSessionCounts, which counts distinct (initiator_id, session_id) pairs per client plus one deduplicated total. Summing the per-client counts would overcount because a session can issue requests through several clients.

It is a separate query rather than another grouping set because COUNT(DISTINCT ...) disables hash aggregation on Postgres 13; the dedupe happens in a GROUP BY CTE so the outer count stays in memory. Includes the dbauthz, dbmetrics, and dbmock wiring and querier coverage for the per-client and total counts.

Xum prepared this PR on behalf of @ibetitsmike.

@ibetitsmike
ibetitsmike force-pushed the mike/ai-spend-query-session-counts branch from b7aaecd to fb1a07e Compare September 11, 2026 10:01
@ibetitsmike
ibetitsmike force-pushed the mike/ai-spend-query-rollups branch from d3535c6 to a58258c Compare September 11, 2026 10:01
@ibetitsmike
ibetitsmike marked this pull request as ready for review September 11, 2026 11:31
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 11, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-11T11:34:29.435069Z fb1a07e Draft marked ready
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@ibetitsmike
ibetitsmike force-pushed the mike/ai-spend-query-rollups branch from a58258c to 65a933d Compare September 14, 2026 08:03
@ibetitsmike
ibetitsmike force-pushed the mike/ai-spend-query-session-counts branch from fb1a07e to 11e7250 Compare September 14, 2026 08:03
@ibetitsmike
ibetitsmike marked this pull request as draft September 15, 2026 18:48
@ibetitsmike
ibetitsmike removed this pull request from stack #29225 September 15, 2026 18:49
@ibetitsmike
ibetitsmike force-pushed the mike/ai-spend-query-rollups branch from 65a933d to 4e98501 Compare September 15, 2026 18:51
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

Closing without a counterpart. The stack was reworked onto the released organization spend export (GET /api/v2/organizations/{organization}/ai/spend/export, see #29219 through #28964), and the page no longer shows session counts, so this query has no consumer. The branch is left in place for reference.

Xum closed this PR on behalf of @ibetitsmike.

@linear-code

linear-code Bot commented Sep 30, 2026

Copy link
Copy Markdown

CODAGT-1121

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant