Skip to content

perf: index usage rollups#120

Closed
felixvippp-ai wants to merge 5 commits into
Agentpay-Org:mainfrom
felixvippp-ai:codex/usage-rollup-indexes
Closed

perf: index usage rollups#120
felixvippp-ai wants to merge 5 commits into
Agentpay-Org:mainfrom
felixvippp-ai:codex/usage-rollup-indexes

Conversation

@felixvippp-ai

Copy link
Copy Markdown

Closes #59

Summary

  • Replaced full usageStore scans on metrics, stats, agent rollups, service rollups, top agents, and protocol billing totals with maintained usage indexes.
  • Added an indexed usageStore wrapper that keeps byAgent, byService, per-agent totals, per-service totals, and protocol-wide outstanding request totals in sync on set, delete, and clear.
  • Preserved existing response shapes and key convention ${agent}::${serviceId}.
  • Added assertUsageIndexesConsistent() for test-only brute-force parity checks.
  • Added focused regression tests covering add, bulk usage, settle-to-zero, direct overwrite, clear, and indexed agent/service route outputs.

Validation

  • npm.cmd run build
  • $env:NODE_ENV='test'; node --test dist/*.test.js dist/**/*.test.js
  • npm.cmd run lint
  • npx.cmd prettier --check src/store/state.ts src/routes/usage.ts src/routes/services.ts src/routes/metrics.ts src/usage-index.test.ts
  • git diff --check

Full test result from the Windows-compatible equivalent of npm test:

tests 71
suites 6
pass 71
fail 0
cancelled 0
skipped 0
todo 0

Note: the repository npm test script uses POSIX NODE_ENV=test syntax, which Windows cmd.exe cannot execute directly. I ran the same built test set with PowerShell's $env:NODE_ENV='test' before invoking node --test.

Complexity Note

Before this change, hot read paths repeatedly scanned every usage key and split string keys per request. After this change:

  • /api/v1/metrics and /api/v1/stats read maintained totals directly.
  • /api/v1/agents, /api/v1/agents/:agent/total, and /api/v1/agents/:agent/usage use agent indexes.
  • /api/v1/services/:serviceId/usage, /api/v1/services/:serviceId/agents, and /api/v1/services/:serviceId/agents/top use service indexes.
  • /api/v1/billing/total iterates per-service totals instead of every agent/service pair.

@mikewheeleer

Copy link
Copy Markdown
Contributor

thanks for the effort @felixvippp-ai 🙏 this issue isn't assigned to you — please claim an open unassigned issue first, then open your PR. closing for now, hope to see it back 🙌

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.

Optimize usage rollups with secondary indexes to remove full-store scans

2 participants