Draft: Return per-model token counts from group AI usage events
What does this MR do and why?
Group and project aiUsageData return each GitLab Duo usage event's user, type and time, but not its token counts. Those sit in extras, which only the administrator-only instance query returns. Moving extras to the group type was declined.
This MR adds a typed tokenUsage field to AiUsageEvent, which AiInstanceUsageEvent and CodeSuggestionEvent inherit: one entry for each model with its input and output tokens, read from extras.usage only. extras stays administrator-only.
Why a typed field: #466072 asks for tokens by user and group. The per-model usage map is not one event's: Chat writes it (!259436 (merged)), and the Agent Platform token usage event registered in !262229 (merged) carries the same map once its follow-up records it. The field sits beside the daily table from !260537 (merged) and does not replace it: the table holds Chat sums with no read yet, and the field returns each event's counts.
Events with no namespace are left out of group and project reads (#572546). The AiUsageData description and the docs now say so.
flowchart LR
Q["Group or project<br/>aiUsageData request"] --> F["Resolver and finder"]
F --> H{"Event has a namespace<br/>in the group or project?"}
H -- yes --> E["AiUsageEvent"]
H -. "no: not returned" .-> I["Instance aiUsageData<br/>administrators only"]
E --> T["tokenUsage: extras.usage only,<br/>one entry for each model"]- No new SQL: the group read makes the same 23 queries with and without this MR.
- No migration, instrumentation or frontend change.
References
- Related to #466072
How to set up and validate locally
With ClickHouse running, bundle exec rspec ee/spec/graphql/types/analytics/ai_usage/ai_usage_event_type_spec.rb ee/spec/graphql/types/analytics/ai_usage/ai_usage_event_token_usage_type_spec.rb ee/spec/graphql/types/analytics/ai_usage/ai_instance_usage_event_type_spec.rb ee/spec/graphql/types/analytics/ai_usage/code_suggestion_event_type_spec.rb ee/spec/requests/api/graphql/analytics/ai_analytics/ai_usage_events_spec.rb passes.
| Case, on PostgreSQL and ClickHouse | Master | This MR |
|---|---|---|
all { tokenUsage { model inputTokens outputTokens } } on a group, a project and the instance |
Field 'tokenUsage' doesn't exist |
one entry for each model, otherwise null |
| Group roles | Guest gets no data | Guest gets no data; Reporter and above get entries |
An AI coding assistant wrote this change. The assistant also checked each claim against the source files this MR cites and ran RuboCop and the documentation linters on the changed files on a GitLab development instance.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.