Skip to content

feat: add organization AI spend overview queries - #31711

Merged
ssncferreira merged 3 commits into
mainfrom
ssncf/ai-spend-overview-queries
Nov 13, 2026
Merged

ssncferreira merged 3 commits into
mainfrom
ssncf/ai-spend-overview-queries

Conversation

@ssncferreira

@ssncferreira ssncferreira commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Adds 3 database queries for the organization AI spend overview. They read the daily spend aggregate (ai_user_daily_spend), scoped to the organization through each spend row's effective group, over a [period_start, period_end) range of UTC days.

  1. GetOrganizationAISpendTotals: the number of users with spend, the total spend, and the median of the users' total spend.
  2. ListOrganizationAISpendPerUser: paginated users, most spend first, with one row for each group their spend was attributed to.
  3. GetOrganizationAISpendPerDay: the spend for every UTC day in the range, including days without spend.

Note: Only users with spend above zero are included, so users with only unpriced usage are not listed.

Changes

  • dbauthz checks matching /ai/spend/users (read on organization group members).
  • Querier and dbauthz tests.

Related to AIGOV-728

Note

Generated by Coder Agents on behalf of @ssncferreira.

@ssncferreira
ssncferreira added this pull request to stack #31712 November 3, 2026 20:04
@ssncferreira ssncferreira changed the title ssncf/ai spend overview queries feat(coderd/database): add organization AI spend overview queries Nov 3, 2026
@ssncferreira
ssncferreira force-pushed the ssncf/ai-spend-overview-queries branch from b32e812 to fa956d9 Compare November 3, 2026 20:10
@linear-code

linear-code Bot commented Nov 3, 2026

Copy link
Copy Markdown

AIGOV-728

@ssncferreira ssncferreira changed the title feat(coderd/database): add organization AI spend overview queries feat: add organization AI spend overview queries Nov 3, 2026
@ssncferreira
ssncferreira force-pushed the ssncf/ai-spend-overview-queries branch from c72d6a2 to 5d31a34 Compare November 5, 2026 13:30
@ssncferreira
ssncferreira marked this pull request as ready for review November 5, 2026 13:34
@ssncferreira
ssncferreira requested a review from mtojek November 6, 2026 09:57

@mtojek mtojek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 Checked the SQL against the schema - existing (effective_group_id, day) index covers all three queries, no new indexes needed.

Comment thread coderd/database/dbauthz/dbauthz.go Resolved
@ssncferreira
ssncferreira force-pushed the ssncf/ai-spend-overview-queries branch from 83cece9 to 1ebe59d Compare November 11, 2026 12:10
@ssncferreira
ssncferreira temporarily deployed to main November 11, 2026 12:10 — with GitHub Actions Inactive
@ssncferreira
ssncferreira force-pushed the ssncf/ai-spend-overview-queries branch from 1ebe59d to 1187570 Compare November 12, 2026 10:20
@ssncferreira
ssncferreira temporarily deployed to main November 12, 2026 10:20 — with GitHub Actions Inactive
@ssncferreira
ssncferreira merged commit 7db53f3 into main Nov 13, 2026
28 of 29 checks passed
@ssncferreira
ssncferreira deleted the ssncf/ai-spend-overview-queries branch November 13, 2026 10:03
@ssncferreira
ssncferreira temporarily deployed to main November 13, 2026 10:03 — with GitHub Actions Inactive
@ssncferreira
ssncferreira temporarily deployed to docs-preview November 13, 2026 10:03 — with GitHub Actions Inactive
ssncferreira added a commit that referenced this pull request Nov 13, 2026
## Description

Adds the experimental `GET
/api/experimental/organizations/{organization}/ai/spend` endpoint that
backs the new Overview tab of the organization AI spend page ([Figma
design](https://www.figma.com/design/6TkixpkUIM5Qv6I7qhM3xZ/AI-Gov---user-spend?node-id=12-5152&t=YlCC7RGSMNrzMxVo-4)).
It returns the organization's spend summary, daily spend, and a page of
users with their per-group spend, using the queries from #31711.

## Changes

- Add the `organizationAISpend` handler and
`ExperimentalClient.OrganizationAISpend`:
- Reuses the period handling of `/ai/spend/users`: `period_start` and
`period_end` must be provided together and span at most 31 days, and the
default is the current budget period.
- Also requires both bounds at 00:00:00 UTC, since the daily spend
aggregate holds whole UTC days.
- Is not limited by the AI Gateway data retention window, since the
daily spend aggregate is not purged. `/ai/spend/users` and the export
keep the retention check, now in `aiSpendRetainedPeriod`.
- Returns `count`, `totals` (`spend_micros`, `median_spend_micros`),
`intervals` (daily spend, one interval per UTC day, zeros included), and
`users` (paginated with `limit`/`offset`, most spend first, with
per-group spend).
- Echoes the applied period, and reads every value in one read-only
repeatable-read transaction so the values agree.
- Extract `parseOrganizationAISpendPage`, shared with `/ai/spend/users`.
- Add handler tests.

Closes AIGOV-728

> [!NOTE]
> Generated by Coder Agents on behalf of @ssncferreira.
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 13, 2026

This branch was previously deployed

2 inactive deployments
main — 1187570d Deployed Nov 13, 2026 by ssncferreira via cla #256173
docs-preview — 1187570d Deployed Nov 13, 2026 by ssncferreira via cleanup #25
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants