feat: per-user AI usage breakdown in the org summary - #35
Merged
Conversation
The org-scoped AI usage endpoint reports feature/provider/totals, but not WHO drives usage — the operator cannot answer 'which user is burning tokens?' without raw SQL. Adds cost attribution per user: - AIUsageSummary.requestsByUser: per-user requests, successes, failures, totalTokens, totalCostUsd (org-scoped like every other breakdown; two additional groupBy queries on userId, all + success) - sorted by spend descending (highest cost first) — mirrors the platform org breakdown's sort intent - README documents the new breakdown Read-only, additive, no schema change. Tests: user merging with success counts and cost, org-scoping of the user groupBys, spend-desc sort, and empty-user case in the existing sort test.
✅ Deploy Preview for teamsynch-ai canceled.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The org-scoped AI usage endpoint (
GET /api/v1/analytics/ai-usage) reports totals plus feature and provider breakdowns, but not who drives usage. With cost estimation live (PRs #27–#28), the operator still cannot answer "which user is burning tokens?" without raw SQL.Solution
Add
requestsByUserto the existing org-scoped summary:groupByqueries onuserId, all + success — same pattern as feature/provider).Tests
ai-usage.service.test.tsextended (2 new tests + updated chained mocks):organizationIdin the where clause)Validation results
npm run typecheck: cleannpm run build: cleanRisk
LOW. Additive read-only field on an existing org-scoped endpoint; same aggregation patterns and indexes as the existing breakdowns. No tenant-isolation change (still filtered by the authenticated
organizationId).Rollback
Revert the PR. The
requestsByUserfield disappears; nothing else changes. No migration or data rollback.Production behavior
Not changed. No AI calls, providers, billing, auth, or RBAC behavior changed. Only a read-only response field was added.