mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: add GET /organizations/{org}/groups/ai/spend (#27123)
## Description
Adds `GET /api/v2/organizations/{org}/groups/ai/spend?group_ids=...` to return per-group AI spend and configured limits for a set of groups in an organization.
In the UI, this endpoint is used alongside the existing `/api/v2/organizations/{org}/groups` endpoint. AI spend data is kept separate from that endpoint so that:
- Different concepts stay on different endpoints: identity (groups) vs. cost control (spend). Cost control is an additional feature layered on top of groups/orgs.
- Callers that don't need spend information don't pay for its computation.
UI flow:
1. Request `/api/v2/organizations/{org}/groups` → returns the organization's groups.
2. Request `/api/v2/organizations/{org}/groups/ai/spend?group_ids=...` with the IDs from step 1.
The groups endpoint from 1) is currently not paginated, but if pagination is added later, this design keeps the two responses in sync. This spend endpoint intentionally takes `group_ids` rather than paginating on its own, since it depends on the group set from step 1. Pagination could be added in the future, especially for Cost Control-focused pages.
<img width="2880" height="1460" alt="image" src="https://github.com/user-attachments/assets/ea83b74d-6a4f-45a6-af2f-1024e019da07" />
## Changes
- Add `codersdk.OrganizationGroupsAISpend` and `OrganizationGroupAISpend` types, plus a shared `AISpendPeriodWindow` embedded in the spend response.
- Add `GetOrganizationGroupsAISpend` SQL query with a dbauthz per-row filter that mirrors `GET /organizations/{org}/groups`.
- Add handler and route under `/organizations/{organization}/groups/ai/spend` with a required `group_ids` query param (cap 100). Callers with more than 100 groups are expected to batch across multiple requests.
- Add codersdk client method.
- Tests: dbauthz, raw SQL, endpoint, and role-access.
Closes https://linear.app/codercom/issue/AIGOV-466/backend-organization-groups-endpoint-with-groups-spend
> [!NOTE]
> Initially generated by Claude Opus 4.7, modified and reviewed by @ssncferreira
This commit is contained in:
@@ -101,3 +101,23 @@ FROM ai_user_daily_spend
|
||||
WHERE user_id = @user_id
|
||||
AND effective_group_id = @effective_group_id
|
||||
AND day >= ((@period_start::timestamptz) AT TIME ZONE 'UTC')::date;
|
||||
|
||||
-- name: GetOrganizationGroupsAISpend :many
|
||||
-- Returns AI spend limits and aggregate spend for groups in @group_ids that
|
||||
-- belong to @organization_id, on or after period_start until NOW. The spend
|
||||
-- limit is null when the group has no configured budget.
|
||||
-- The period_start parameter is normalized to its UTC calendar day.
|
||||
SELECT
|
||||
groups.id AS group_id,
|
||||
groups.organization_id AS organization_id,
|
||||
budget.spend_limit_micros AS spend_limit_micros,
|
||||
COALESCE(SUM(spend.spend_micros), 0)::BIGINT AS current_spend_micros
|
||||
FROM groups
|
||||
LEFT JOIN group_ai_budgets budget ON budget.group_id = groups.id
|
||||
LEFT JOIN ai_user_daily_spend spend
|
||||
ON spend.effective_group_id = groups.id
|
||||
AND spend.day >= ((@period_start::timestamptz) AT TIME ZONE 'UTC')::date
|
||||
WHERE groups.organization_id = @organization_id
|
||||
AND groups.id = ANY(@group_ids::uuid[])
|
||||
GROUP BY groups.id, budget.spend_limit_micros
|
||||
ORDER BY groups.id;
|
||||
|
||||
Reference in New Issue
Block a user