Replace /admin/usage with a per-user usage drill-down on /admin/users - #724
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe PR replaces the admin usage overview with per-user usage loading through ChangesAdmin user usage
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant Admin
participant AdminUsersRoute
participant UsageAPI
participant UsageLoader
participant APP_DB
Admin->>AdminUsersRoute: Select user account
AdminUsersRoute->>UsageAPI: GET /admin/users/usage.json?userId
UsageAPI->>UsageLoader: Load selected user usage
UsageLoader->>APP_DB: Read user, rollups, and entitlements
UsageLoader-->>UsageAPI: Usage payload
UsageAPI-->>AdminUsersRoute: JSON response
AdminUsersRoute-->>Admin: Render charts and quotas
Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
🔎 Preview deployed: https://kody-pr-724.kody-a99.workers.dev Worker: Mocks:
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/worker/client/routes/admin-users.tsx`:
- Around line 170-175: Update invalidateUsage() to also clear usageData (set it
to null or the appropriate empty state) alongside usageLoadedForUserId and
usageFailedForUserId, ensuring selectedUsage cannot display stale entitlements
while the refetch is in progress.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: e9a28b1a-8717-46bc-aa09-b6f4b729689f
📒 Files selected for processing (21)
docs/contributing/architecture/usage-metering.mde2e/admin-rbac.spec.tspackages/worker/client/charts/usage-metric-series.tspackages/worker/client/routes/account-management-components.tsxpackages/worker/client/routes/admin-insights.tsxpackages/worker/client/routes/admin-usage.tsxpackages/worker/client/routes/admin-users.tsxpackages/worker/client/routes/index.tsxpackages/worker/src/app/admin-insights-data.tspackages/worker/src/app/admin-usage-data.tspackages/worker/src/app/admin-user-usage-data.node.test.tspackages/worker/src/app/admin-user-usage-data.tspackages/worker/src/app/handlers/admin-usage.tspackages/worker/src/app/handlers/admin-users.tspackages/worker/src/app/loader-data.tspackages/worker/src/app/router.tspackages/worker/src/app/routes.tspackages/worker/src/mcp/capabilities/admin/admin-capabilities.node.test.tspackages/worker/src/mcp/capabilities/admin/admin-usage-overview.tspackages/worker/src/mcp/capabilities/admin/admin-user-usage.tspackages/worker/src/mcp/capabilities/admin/domain.ts
💤 Files with no reviewable changes (6)
- packages/worker/src/app/admin-usage-data.ts
- packages/worker/src/app/handlers/admin-usage.ts
- packages/worker/src/mcp/capabilities/admin/admin-usage-overview.ts
- packages/worker/client/routes/account-management-components.tsx
- packages/worker/client/routes/admin-usage.tsx
- packages/worker/client/routes/index.tsx
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit dce32f0. Configure here.
| typeof document !== 'undefined' && | ||
| usageLoadedForUserId !== selectedUser.id && | ||
| usageLoadingForUserId !== selectedUser.id && | ||
| usageFailedForUserId !== selectedUser.id |
There was a problem hiding this comment.
Usage load never retries
Medium Severity
After a failed loadUserUsage fetch, usageFailedForUserId stops the render path from queueing another load for that account. Reselecting the same user or switching away and back does not clear that flag, so the Usage & quotas panel can stay on the error state until a full page reload (or a plan save that calls invalidateUsage).
Additional Locations (1)
Reviewed by Cursor Bugbot for commit dce32f0. Configure here.


Why
/admin/usageloaded usage rollups, daily quota counters, and live resource counts for every user on the page (up to 100 accounts × ~7 counter reads each per load). That read model scales linearly with the user base and was already the slowest admin page.What changed
/admin/usageand/admin/usage.jsonare gone — page, handler, client route, nav item, and the paged multi-user read model./admin/users: selecting an account lazily loads a "Usage & quotas" panel from a new admin-only endpointGET /admin/users/usage.json?userId=N. Cost is O(1) per view — one KV-cached rollup query plus a fixed set of entitlement counter reads for exactly one user.usage-metric-series.ts), a metric legend with current-month counts, a quota-watch notice, and an Entitlements table with plan limits and % used (amber above 80%).admin-user-usage-data.tsreplacesadmin-usage-data.ts; keeps the 5-minute KV cache on the per-user rollup read model.admin_usage_overview(paged, whole-user-base) is replaced byadmin_user_usage(byidoremail), matchingadmin_user_getand keeping the same scalability fix on the MCP surface./admin/insightsgraphs are untouched.Demo
Testing
npm run validatefully green locally (format, lint, typecheck, 819 unit tests, 13 Playwright E2E, MCP E2E).admin-user-usage-data.node.test.ts): null for unknown users, zeroed rollups, >80% warnings, plan-less email fallback limits, KV caching, UTC month boundaries.admin-rbac.spec.tsupdated: non-admins get 403 from the usage endpoint; admins see the Usage & quotas panel on/admin/users; the JSON payload leaks no secrets or emails.400withoutuserId,404for unknown users,404on the old/admin/usage, and the full GUI flow in the demo video.System recap — extends existing primitives (medium risk)
Mode: recap · Base:
main@ff425827· Head:dbee844bClassification: extends — the admin usage surface (routes, JSON contract, MCP capability) changes shape; no new primitives, no schema changes.
Primitives touched
app-ui/admin/usageremoved; per-user drill-down +/admin/users/usage.jsonaddedcapability-registryadmin_usage_overviewreplaced by per-useradmin_user_usageusage-meteringentitlementsrbacread:user:anypermissionbundle-artifacts-kvSystem map
The admin users page fetches one selected account's usage through a new JSON endpoint into the existing metering and entitlement reads.
Legend: green = composes (wiring only) · amber = extended by this PR · red = new primitive · gray = context (unchanged, included only when an edge crosses it).
Before / after
Invariants
Per-user isolation unchanged: the endpoint returns aggregate metadata for one account, is admin-gated via
read:user:any, and never returns user content (asserted inadmin-rbac.spec.ts).Summary by CodeRabbit
New Features
Changes
Tests
Documentation