Repository navigation
feat: add organization AI spend overview endpoint - #31709
ssncferreira merged 3 commits into
Conversation
19849a9 to
793d96e
Compare
793d96e to
af5b8bd
Compare
7a2ccaa to
235b03d
Compare
2cdfea7 to
1f7ebed
Compare
| type OrganizationAISpendOverviewGroup struct { | ||
| GroupID uuid.UUID `json:"group_id" format:"uuid"` | ||
| GroupName string `json:"group_name"` | ||
| GroupDisplayName string `json:"group_display_name"` |
There was a problem hiding this comment.
nit: I don't recall now, is display name mandatory or can it be empty?
There was a problem hiding this comment.
Display name can be empty. Like other endpoints, this returns it as stored, and the UI falls back to group_name which cannot be empty. This seems to be the pattern with other endpoints.
| PeriodEnd: queryEnd, | ||
| // #nosec G115 - The limit is capped above and the offset is parsed as int32. | ||
| LimitOpt: int32(page.Limit), | ||
| // #nosec G115 - The limit is capped above and the offset is parsed as int32. |
There was a problem hiding this comment.
Limit and Offset come from codersdk.OrganizationAISpendPage, which uses int, so they need to be converted to int32 here. Other paginated handlers follow the same pattern, for example https://github.com/coder/coder/blob/main/coderd/users.go#L406-L409
| Count int64 `json:"count"` | ||
| Totals OrganizationAISpendOverviewTotals `json:"totals"` | ||
| // Intervals holds the daily spend, one interval per UTC day in the period | ||
| // (omitting days after the current day). Days without spend are zero. |
There was a problem hiding this comment.
nit: you can add here that the last interval is partial since the day is not over yet
There was a problem hiding this comment.
Good point, added. It's only partial when the period includes the current day, so I worded it that way.
| {name: "period_start", value: period.start}, | ||
| {name: "period_end", value: period.end}, |
There was a problem hiding this comment.
nit: looking at template insights we have start_time and end_time, perhaps we should adjust here?
There was a problem hiding this comment.
The other AI spend endpoints already use period_start and period_end (e.g., the CSV export, /ai/spend/users and /users/{user}/ai/spend). I would prefer to keep it this way for consistency across the AI spend endpoints.
3fe6479 to
b9595cc
Compare
| // retention window. Use it for raw AI Gateway data, which is subject to data | ||
| // retention. It narrows a default period to the window, rejects an explicit | ||
| // period that starts before it, and sets retentionStart. | ||
| func (api *API) aiSpendRetainedPeriod(ctx context.Context, rw http.ResponseWriter, r *http.Request, parser *httpapi.QueryParamParser) (period aiSpendPeriod, ok bool) { |
There was a problem hiding this comment.
These helpers (aiSpendPeriod and aiSpendRetainedPeriod) should have their own tests. Otherwise, the endpoint tests duplicate the period and retention cases. I'll address this in a follow-up to keep this PR focused.
b86a6f6 to
b9ff9cb
Compare
Description
Adds the experimental
GET /api/experimental/organizations/{organization}/ai/spendendpoint that backs the new Overview tab of the organization AI spend page (Figma design). 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
organizationAISpendhandler andExperimentalClient.OrganizationAISpend:/ai/spend/users:period_startandperiod_endmust be provided together and span at most 31 days, and the default is the current budget period./ai/spend/usersand the export keep the retention check, now inaiSpendRetainedPeriod.count,totals(spend_micros,median_spend_micros),intervals(daily spend, one interval per UTC day, zeros included), andusers(paginated withlimit/offset, most spend first, with per-group spend).parseOrganizationAISpendPage, shared with/ai/spend/users.Closes AIGOV-728
Note
Generated by Coder Agents on behalf of @ssncferreira.