Skip to content

feat: per-user AI usage breakdown in the org summary - #35

Merged
akash4550 merged 1 commit into
mainfrom
feat/ai-usage-per-user
Aug 10, 2026
Merged

feat: per-user AI usage breakdown in the org summary#35
akash4550 merged 1 commit into
mainfrom
feat/ai-usage-per-user

Conversation

@akash4550

Copy link
Copy Markdown
Owner

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 requestsByUser to the existing org-scoped summary:

"requestsByUser": [
  { "userId": "u-1", "requests": 3, "successes": 3, "failures": 0,
    "totalTokens": 45, "totalCostUsd": 0.9 },
  ...
]
  • Per-user requests / successes / failures / totalTokens / totalCostUsd, org-scoped like every other breakdown (two additional groupBy queries on userId, all + success — same pattern as feature/provider).
  • Sorted by spend descending (highest cost first) — mirrors the platform org breakdown's sort intent; the first question is "who costs the most?".
  • Read-only, additive, no schema change, no UI change.

Tests

ai-usage.service.test.ts extended (2 new tests + updated chained mocks):

  • user merging with success counts and cost
  • org-scoping of the user groupBy queries (asserts organizationId in the where clause)
  • spend-descending sort
  • empty-user case (existing sort test)

Validation results

  • API tests: 54 suites, 366 passed (364 pre-existing + 2 new)
  • Web tests: 29 files, 147 passed
  • npm run typecheck: clean
  • npm run build: clean
  • Diff: 3 files, 163 additions / 4 deletions; zero new dependencies; zero migrations; zero UI/deployment/CI changes

Risk

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 requestsByUser field 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.

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.
@netlify

netlify Bot commented Aug 10, 2026

Copy link
Copy Markdown

Deploy Preview for teamsynch-ai canceled.

Name Link
🔨 Latest commit c6c5f83
🔍 Latest deploy log https://app.netlify.com/projects/teamsynch-ai/deploys/6a79cd0e73af670008ffb9b5

@akash4550
akash4550 merged commit 400898f into main Aug 10, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant