AI Usage Ops Dashboard Implementation Plan
For agentic workers: REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (- [ ]) syntax for tracking.
Goal: Build an owner-only global AI usage dashboard backed by the existing ai_usage table.
Architecture: Add a bot API route that validates x-api-secret, parses range/filter query params, queries global AI usage aggregates, and resolves guild names from the Discord client cache. Add a Next.js global-admin-gated proxy route and a dashboard page that renders summary cards, charts, and comparison tables using existing dashboard primitives.
Tech Stack: Node 22, Express 5, PostgreSQL, Vitest, Next.js 16 App Router, React 19, TypeScript, Tailwind 4, Recharts via StableResponsiveContainer.
Task 1: Backend AI Usage Aggregation
Files:-
Create:
src/api/repositories/aiUsageRepository.js -
Test:
tests/api/repositories/aiUsageRepository.test.js - Step 1: Write failing repository tests
- Step 2: Run repository tests to verify RED
pnpm test -- tests/api/repositories/aiUsageRepository.test.js
Expected: fail because src/api/repositories/aiUsageRepository.js does not exist.
- Step 3: Implement repository
src/api/repositories/aiUsageRepository.js with:
-
parseAiUsageOpsQuery(query)supporting24h,7d,30d,90d, andtypeinclassify/respond/safety. -
parseProviderFromModel(modelString)using the first colon. -
fetchAiUsageOpsSnapshot({ dbPool, client, query })issuing one parameterizedSELECTfromai_usagefor the requested range and optional filters, then aggregating rows in JavaScript. - helper functions for totals, cache rates, weighted TPS, p95 latency, provider/model/type/guild grouping, time buckets, and recent expensive rows.
- Step 4: Run repository tests to verify GREEN
pnpm test -- tests/api/repositories/aiUsageRepository.test.js
Expected: pass.
- Step 5: Commit backend repository
Task 2: Bot API Route
Files:-
Create:
src/api/routes/aiUsage.js -
Modify:
src/api/index.js -
Test:
tests/api/routes/aiUsage.test.js - Step 1: Write failing route tests
- Step 2: Run route tests to verify RED
pnpm test -- tests/api/routes/aiUsage.test.js
Expected: fail with 404 or missing route.
- Step 3: Implement route and mount it
src/api/routes/aiUsage.js using Router(), isValidSecret(req.headers['x-api-secret']), and fetchAiUsageOpsSnapshot({ dbPool: req.app.locals.dbPool, client: req.app.locals.client, query: req.query }).
Mount it in src/api/index.js near /performance:
- Step 4: Run route tests to verify GREEN
pnpm test -- tests/api/routes/aiUsage.test.js
Expected: pass.
- Step 5: Commit bot route
Task 3: Web Proxy, Types, and Page Titles
Files:-
Create:
web/src/app/api/ai-usage/route.ts -
Create:
web/src/types/ai-usage.ts -
Modify:
web/src/lib/page-titles.ts -
Test:
web/tests/api/ai-usage-route.test.ts -
Test:
web/tests/lib/page-titles.test.ts - Step 1: Write failing web API and title tests
authorizeRequestGlobalAdmin and proxyBotApiEndpoint:
web/tests/lib/page-titles.test.ts to expect AI Usage and docs href for /dashboard/ai-usage.
- Step 2: Run web API/title tests to verify RED
pnpm --filter volvox-bot-web test -- tests/api/ai-usage-route.test.ts tests/lib/page-titles.test.ts
Expected: fail because route/title wiring is missing.
- Step 3: Implement web route, types, and title wiring
web/src/app/api/ai-usage/route.ts with dynamic = 'force-dynamic', owner auth, URL search forwarding, and proxyBotApiEndpoint.
Create web/src/types/ai-usage.ts matching the spec response.
Add /dashboard/ai-usage title/docs matcher to web/src/lib/page-titles.ts.
- Step 4: Run web API/title tests to verify GREEN
pnpm --filter volvox-bot-web test -- tests/api/ai-usage-route.test.ts tests/lib/page-titles.test.ts
Expected: pass.
- Step 5: Commit web plumbing
Task 4: Dashboard AI Usage UI
Files:-
Create:
web/src/components/dashboard/ai-usage-dashboard.tsx -
Create:
web/src/app/dashboard/ai-usage/page.tsx -
Modify:
web/src/components/layout/sidebar.tsx -
Test:
web/tests/components/dashboard/ai-usage-dashboard.test.tsx -
Test:
web/tests/components/layout/sidebar.test.tsx - Step 1: Write failing component and sidebar tests
fetch and verify loading, empty, populated, filter changes, and failed response states.
Add sidebar expectations:
- Step 2: Run UI tests to verify RED
pnpm --filter volvox-bot-web test -- tests/components/dashboard/ai-usage-dashboard.test.tsx tests/components/layout/sidebar.test.tsx
Expected: fail because component and nav item do not exist.
- Step 3: Implement dashboard page and component
AiUsageDashboard with:
- range/provider/model/type filter controls.
- summary metric cards.
- time-series chart using
StableResponsiveContainer. - provider/model/type/top-guild/recent-request tables.
- loading, empty, and error states.
web/src/app/dashboard/ai-usage/page.tsx with metadata, isDashboardGlobalAdmin() redirect, ErrorBoundary, and <AiUsageDashboard />.
Add sidebar AI Usage with a lucide icon under System Ops and add /dashboard/ai-usage to GLOBAL_ADMIN_ONLY_HREFS.
- Step 4: Run UI tests to verify GREEN
pnpm --filter volvox-bot-web test -- tests/components/dashboard/ai-usage-dashboard.test.tsx tests/components/layout/sidebar.test.tsx
Expected: pass.
- Step 5: Commit UI
Task 5: Docs, AGENTS Check, and Verification
Files:-
Modify:
docs/features/analytics.mdx -
Modify:
docs/dashboard.mdx -
Read/check:
AGENTS.md - Step 1: Update docs
docs/changelog.mdx except to repair generated output required by docs validation.
- Step 2: Run targeted tests
- Step 3: Run broader checks
- Step 4: Browser verification
agent-browser to open /dashboard/ai-usage, verify owner access, desktop layout, mobile layout, filters, and no visible overflow. If browser tooling cannot run, state that plainly.
- Step 5: Final commit
