Skip to content

fix(usage): load the Usage report from the shared aggregate, fit the models table, name apps - #8728

Closed
andrew05060414 wants to merge 7 commits into
lidge-jun:devfrom
andrew05060414:feat/usage-page-speed-apps
Closed

andrew05060414 wants to merge 7 commits into
lidge-jun:devfrom
andrew05060414:feat/usage-page-speed-apps

Conversation

@andrew05060414

@andrew05060414 andrew05060414 commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Problem. On a busy installation the Usage & Logs page was slow enough to fail. With a synthetic ledger shaped like a real one (270k rows, 745 MB, ~135k requests in 30 days) the dashboard's report request took 23.8 to 64.5 s cold, past the page's 30 s resource timeout, so the page showed "resource request timed out after 30000ms". The models table also needed horizontal scrolling at 1500 px, the Apps table lumped most traffic into "Unclassified" and "Unknown" with no explanation, and the "N requests excluded" captions and the coverage figure gave no reason.

After this PR.

  • The report loads from the shared unfiltered aggregate. Same synthetic ledger: cold request 12.4 s right after start (5.1 s once startup work settles), then 0.01 s; after a restart the page showed the report with the API answering in 0.36 s, because other startup readers had already warmed that aggregate.
  • The models table fits a 1500 px window: seven columns, each secondary figure a second line under the one it qualifies (provider under model, share bar under tokens, measured under requests, input / output in one cell, cache read / write under hit rate). Only the model column stays pinned for narrower windows.
  • Apps get names. Rows written before client tracking are split by the API they arrived on ("Before client tracking · Responses API", "· Chat Completions", "· Anthropic Messages", "· Claude Desktop" …). Requests from clients outside the recognized roster now record a bounded product name (curl, multica-agent-sdk, claude-agent-sdk for the Claude Agent SDK) and show as their own rows. A note under the table says what the two buckets mean.
  • The "N requests excluded" caption explains itself on hover: how many carried no token usage to price and how many used a model with no known price, and that auto-pricing (feat(pricing): fetch model prices from models.dev and OpenRouter, label every price's source #8708) looks such models up on models.dev and OpenRouter while a model in neither needs a provider price override. The coverage panel says what the ratio is and why unreported/unsupported requests are missing.

Root cause of the slowness. The dashboard always sends client=all&device=all. The route treated that as a filtered query, so it bypassed the warmed range:surface cache and went through the filter index, which re-reads every matching row by offset (all of them, for this filter) and builds a 356 MB SQLite projection. It also never shared work with the Home page's unfiltered request. client=all&device=all selects every row, so it is now served from the unfiltered aggregate and echoed back with the sourceFilter and legacyUnclassifiedRequests fields the page validates.

Implementation notes.

  • src/server/management/usage-source-presentation.ts: selectsEverySource and the response shaping; the legacy (no-source) response keeps its old shape.
  • src/usage/client.ts: usageClientName keeps only the first product token of originator/User-Agent, lowercased, versionless, matching ^[a-z0-9][a-z0-9._-]{0,39}$, or nothing. It is recorded only when the closed classifier says unknown; header values are never stored.
  • src/usage/app-breakdown.ts: apps rows gain optional name / legacyWire; at most 24 names per report (and a per-partition key budget), the rest fold into plain Unknown. The hub schema accepts the new fields.
  • Behaviour of usageApp, source filters and the SQL filter index is unchanged.

Stacked on #8708 (auto model pricing) because the hover copy and the base branch's price catalog belong together; it contains that PR's commits until it lands.

Verification

Measured on a local instance (OPENCODEX_HOME under .tmp/) seeded with generated rows; no real usage data was used.

before after
GET /api/usage?range=30d&client=all&device=all cold 23.8 s (64.5 s with a stale index) 12.4 s at startup, 5.1 s after startup settles
same, warm 0.01 s 0.01 s
page, fresh load 40 s after restart timed out at 30 s API 0.36 s, report rendered
models table at 1500 px scrollWidth 1376 in 1094 1094 in 1094
  • bun run typecheck, bun run lint:gui, bun run privacy:scan, bun run structure:check, bun run skill:surface:check: pass.
  • bun test tests/ci-workflows/file-size-ratchet.test.ts: 9 pass.
  • bun test tests/usage/usage-client-attribution.test.ts: 23 pass (new: client naming, legacy-wire split, name folding).
  • bun test tests/server/hub-usage.test.ts: 7 pass.
  • GUI: usage-layout, usage-custom-range, usage-app-breakdown and the other gui/tests/*usage* files: 241 pass after updating the two table-layout assertions; new case for named rows, legacy wires and the exclusion tooltip.
  • tests/server/api-usage.test.ts and tests/server/api-key-attribution.test.ts could not be run to completion on the Windows dev machine: every server-starting case fails in afterEach with EBUSY ... rm <tmp> while the config-dir ACL hardening (icacls) is still running, including cases this PR does not touch. The new route case (the all-apps, all-devices dashboard query reuses the unfiltered aggregate) is left to CI; please run with ci:full.

Screenshots (synthetic data):

English, 1500 px wide: models table without horizontal scroll, named apps, coverage note

usage page, English

Chinese

usage page, Chinese

Readiness (2026-11-13)

  • Branch is 6 commit(s) behind dev (873ec9b), inside the gate's window; no rebase for review.
  • No Codex or CodeRabbit findings are open on this PR (CodeRabbit skipped the draft; there are no inline review comments).
  • Also merged into a local integration build with the other open PRs of this series and deployed on a real install. The combined build passed bun run typecheck, structure:check, skill:surface:check, privacy:scan, the layout and file-size guards and this PR's focused tests; after the deploy one real request each through Claude, GPT, Gemini, DeepSeek and Grok returned 200 and the error rate did not rise.
  • Still needs the maintainer-sponsored label: the hygiene gate flags src/server/auth-cors.ts (through feat(pricing): fetch model prices from models.dev and OpenRouter, label every price's source #8708, which this PR is stacked on). Everything else is ready for review.

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Docs or release notes were updated when needed.
  • Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults.

Review readiness checklist

This PR stays in draft until every box below is ticked. Tick all four boxes once the requirements are met:

  • Required local validation passed; commands, results, and any full-suite exception are documented.

  • I pushed my PR to a recent dev commit (at most 10 behind; a maintainer may still ask for the exact tip before merge).

  • I resolved all correct Codex and CodeRabbit findings.

  • My PR is ready for review.

…xis's source, and probe what a relay accepts

GET /api/models routed rows gain capabilitySources (manual > probed > reported >
inferred > unknown) and display-only inferredCapabilities from a same-name lookup
over the vendor registry, bundled metadata and other providers' declarations.
POST /api/providers/probe-capabilities sends a baseline, a 1x1 image and one
request per effort through the completion diagnostic, asks before spending on a
metered provider, and with apply writes the conclusive axes plus a sparse
modelCapabilityProbes label.
…el every price's source

Automatic prices fill the two lowest tiers of price resolution (manual > official > bundled >
models.dev > OpenRouter). Catalogs are cached in price-catalog-cache.json, refreshed daily and on
a pricing miss (merging only the missing keys, 6h per-key cooldown, never on the request path).
Catalog prices are verified-derived estimates carrying their feed URL and fetch time.

Adds an opt-in providers.<name>.priceMultiplier for relays (never applied to manual prices),
apiEquivalent labelling for subscription providers, a price field on /api/models rows,
POST /api/pricing/refresh and PUT /api/providers/:name/price-multiplier. The dashboard shows a
source chip per model and adds adopt-as-manual, refresh and multiplier controls.

Also registers POST /api/providers/probe-capabilities, which the base commit left out of the
route registry.
…e route registry

The capability probe handler was routed in provider-routes but never declared, so the registry reconciliation test failed.
…name its apps

The dashboard always asks for client=all&device=all. The route treated that
as a filtered query, so it skipped the warmed range:surface cache and rebuilt
the report through the filter index, which re-reads every row by offset. On a
270k-row ledger the cold request took 24-65 s and the page hit its 30 s
timeout. Every-source queries now read the unfiltered aggregate and echo the
source fields the page validates.

The models table fits 1500 px in seven columns, with secondary figures as a
second line. Apps rows written before client tracking are split by the wire
they used, unknown clients record a bounded product name from their
originator or User-Agent, and the excluded-request caption and the coverage
panel explain themselves on hover.
@coderabbitai

coderabbitai Bot commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

@github-actions github-actions Bot added the intake: hygiene-blocked Deterministic PR hygiene checks failed label Nov 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Deterministic hygiene checks failed.

  • unsponsored_surface — This changes an authentication, workflow, release-automation, or dependency surface. MAINTAINERS.md requires security review for these; ask a maintainer to apply maintainer-sponsored once they have reviewed it. Paths: src/server/auth-cors.ts.

@github-actions github-actions Bot added the bug Something isn't working label Nov 14, 2026
@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

⏳ DRAFT

  • hygiene: unsponsored_surface.

What to do

  • Fix unsponsored_surface — This changes an authentication, workflow, release-automation, or dependency surface. MAINTAINERS.md requires security review for these; ask a maintainer to apply maintainer-sponsored once they have reviewed it. Paths: src/server/auth-cors.ts.

Review readiness checklist

  • ✅ Required local validation passed; commands, results, and any full-suite exception are documented.
  • ✅ I pushed my PR to a recent dev commit (at most 10 behind; a maintainer may still ask for the exact tip before merge).
  • ✅ I resolved all correct Codex and CodeRabbit findings.
  • ✅ My PR is ready for review.

✅ 4/4 boxes ticked.

This pull request was already a draft. Its draft status will be preserved after every issue above is resolved.

Hygiene

⚠️ Deterministic hygiene checks failed.

  • unsponsored_surface — This changes an authentication, workflow, release-automation, or dependency surface. MAINTAINERS.md requires security review for these; ask a maintainer to apply maintainer-sponsored once they have reviewed it. Paths: src/server/auth-cors.ts.

@andrew05060414

Copy link
Copy Markdown
Contributor Author

Superseded by #8738, which carries this change together with the related model metadata and usage PRs as one commit each on current dev, with no stacking. The design notes in this description still apply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working intake: hygiene-blocked Deterministic PR hygiene checks failed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant