Repository navigation
feat: add Command Code quota dashboard - #8
Merged
Merged
Conversation
Fetch each account's Command Code quota from the undocumented /alpha/*
endpoints (whoami, billing/credits, billing/subscriptions, usage/summary)
using the account's existing API key, and surface it in the WebUI.
- Parse defensively: camelCase or snake_case keys, epoch seconds,
milliseconds or ISO timestamps, and flat or `data`-wrapped responses.
Unknown plans fall back to balance-only display; the monthly bar is
derived from the community CLI plan mapping and labelled as estimated.
- Cache per-account snapshots in usage.json behind a dedicated
persistedUsage shape so they never reach the unauthenticated /usage
endpoint. A failed query keeps the last successful snapshot and only
records the error and check time.
- Refresh once at startup and every 5 minutes, at most four accounts in
flight, stopping with the server's shutdown context.
- Expose `quota` on GET /admin/api/accounts, add
POST /admin/api/accounts/{id}/quota/refresh and POST /admin/api/quotas/refresh,
and add a quota summary to GET /admin/api/overview. Adding an account or
replacing its key triggers a refresh for the new credential.
- WebUI: quota sync card on the overview, 5-hour / weekly / estimated
monthly bars with the reference color grading, credit balances, plan and
billing period, per-account and refresh-all buttons. Reset countdowns
include days, and resetAt=0 is shown as "no reset point".
- Accounts tab renders each account and its quota as one block, with the
bars width-bounded so the metadata never overflows.
Tests cover parsing variants, the four upstream requests and their auth,
partial / rejected / non-JSON failures, snapshot retention, persistence,
and the admin endpoints.
countersFor took the counter map by value, so AccountUsage / ClientKeyUsage read the u.accounts / u.clientKeys field at the call site, before accMu was acquired. The quota service now initializes those maps from a background goroutine (SetQuota -> ensureMapsLocked after an account is added), so a concurrent GET /admin/api/accounts raced with that write. Pass the map by pointer and dereference it under accMu. CI reproduced this with `go test -race -count=1 ./...` failing in TestAdminAccountLifecycle.
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.
Summary
Adds a per-account Command Code quota dashboard — 5-hour / weekly / estimated
monthly quota, credit balances, plan and billing period — rendered in the WebUI
and cached in
usage.json.Data is read from the undocumented
/alpha/*endpoints ofapi.commandcode.aiwith each account's existing API key. Endpoint shapes and the plan mapping
follow the community
commandcode-usage project.
What's included
Backend (
internal/app/quota.go)QuotaSnapshot/QuotaWindow/QuotaPlan/QuotaUsageplus the fourupstream queries:
whoami,billing/credits,billing/subscriptions(
?orgId=from whoami),usage/summary.milliseconds / ISO timestamps, flat or
data-wrapped responses. Unknownplans show balances only; the monthly bar is derived from the plan mapping
(
used = cap − remaining) and labelled as estimated.last snapshot and only updates
last_error/last_checked. A 401/403 onwhoamishort-circuits without hitting the remaining endpoints.QuotaServicerefreshes once at startup and every 5 minutes, at most 4accounts in flight, and stops with the server's shutdown context.
Persistence
usage.jsonbehind a dedicatedpersistedUsageshape, so quota data never reaches the unauthenticated/usageendpoint.credential's snapshot and refreshes the new one.
Admin API
GET /admin/api/accountsgains a nestedquotaobject per account.POST /admin/api/accounts/{id}/quota/refresh— refresh one account.POST /admin/api/quotas/refresh— refresh all, or one via{"id": "..."}.GET /admin/api/overviewgains aquotassummary (synced,exceeded,low_balance,last_checked_at).refresh for the new credential.
WebUI
reference color grading (≥50% amber, ≥75% heavier, ≥90% red), credit
balances, plan, billing period, last check time and quota errors.
include days, and
resetAt = 0shows as "no reset point".Testing
go vet ./...andgo test -race -count=1 ./...pass locally — the samechecks CI runs.
internal/app/quota_test.gocover: parsing variants(nesting, key casing, timestamp formats, plan prefix matching, missing
fields), the four upstream requests with their
Bearerheader andorgIdquery, 401/403 / non-JSON / partial failures, snapshot retention on failure,
usage.jsonround-trip, deletion and key-change behavior, and all adminendpoints.
per-account refresh and refresh-all.
Notes
/alpha/*endpoints are unofficial. The parser tolerates field drift,and when a query fails the UI keeps showing the last cached snapshot
alongside the error.
monthly window, so it is labelled as an estimate and hidden for unknown
plans.