Repository navigation
feat(settings): usage statistics page - #1300
Merged
Merged
Conversation
Settings → 用量统计 shows K-brain's token usage across all sessions: metric cards (total tokens, sessions, active days, top model, longest session, current streak, and estimated cost when K-brain prices it), a 52-week Sunday-first heatmap with daily/weekly/cumulative views, a 7/30-day per-model trend and a model share donut that folds more than six models into the top five plus "other models". Charts are plain SVG/CSS on the settings tokens and work in light and dark themes. The page is read-only. It loads when opened and on Refresh, shows loading, error with retry, empty and "update to see usage" states, and is listed only for hosts that can fetch usage. - Desktop: checks /v1/health capabilities.usage_stats, then calls GET /v1/usage/stats?days=371&tz=<browser zone>. - WebUI: new usage.stats request through the Gateway (UsageStatsRequest/UsageStatsResponse, desktop capability USAGE_STATS_V1). The desktop relay and the Gateway's direct K-brain relay answer "unsupported" when K-brain lacks the capability or 404s. - Pure logic (parsing, gap filling, weeks, month labels, model merge, Y max, 万/亿 and K/M/B units) lives in lib/usage/usageStats.ts with node tests; Rust and Go relay tests cover the forwarding.
On the desktop, a K-brain that advertises usage_stats but answers 404 for /v1/usage/stats now gets the unavailable state instead of an error, the same as the desktop and Gateway relays.
K-brain accepts days=1..1100 for /v1/usage/stats. The desktop and Gateway relays capped requests at 731; align them (the page still asks for 371).
A share of the busiest value let one large day push every other cell to the lightest step. Levels now come from the 25th/50th/75th percentiles of the values with usage in the current view (one sample per day for daily, per week for weekly and cumulative). The largest value is always the top step, so ties at the top and a map of equal values show at full strength.
2 tasks done
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.
Closes #1299
Summary
Adds Settings → Usage statistics: token usage across all conversations, by day and by model, fed by the new K-brain endpoint
GET /v1/usage/stats(Stack-Cairn/K-brain#209, released in v0.107.34). The section shows only when the host can reach the endpoint; when the bundled K-brain lackscapabilities.usage_stats(or the route returns 404) it shows an "update LiveAgent" notice instead of an error.costUSDis null).usage.statsgateway request (UsageStatsRequest/UsageStatsResponse, desktop capabilityUSAGE_STATS_V1), relayed by the desktop host (Rust) or the gateway's own K-brain client (Go). Both relays return "unsupported" when K-brain lacks the capability.daysis 1–1100 (the UI sends 371) and the browser's IANA time zone is passed astz.66a0e3f). All 8 asset hashes match the release'sSHA256SUMS.Screenshots
These were captured from the real app UI (the desktop frontend's Vite dev server in headless Chromium driven by Playwright, not the desktop window) against a real K-brain built from
66a0e3fwith a temp HOME and data dir. The session data is synthetic: a seeded generator wrote 330 days of sessions in K-brain's session format, using made-up project names, model names (atlas-large,nova-pro, …) and prices. None of it is real usage. Every number on the page was checked against the/v1/usage/statsJSON fetched at the same time. The unavailable notice comes from the v0.107.32 release binary, which has no usage endpoint.Tests
node --test: usage logic and section tests incrates/agent-gui/test/settings/usage-stats*.test.mjs, 22/22 pass. They cover gap filling, Sunday-first weeks, cumulative series, month label anchoring, quartile levels, >6-slice folding, the Y-max rule, units and capability gating.usage-stats-settings.test.mjsplus a case ingateway-v2-adapters.test.mjs: 22/22 pass. The full WebUIpnpm testalso passes.internal/kbrain/usage_stats_test.goandpbws/usage_stats_test.go;go test ./...passes.cargo test --lib services::gateway, 126 passed.pnpm typecheck:ui,typecheck:guiandtypecheck:webuiare clean, andpnpm lintis clean.node --test scripts/release/verify-kbrain-packaging.test.mjs: 6 passed with the new pin.Known limitations