Skip to content

fix(ui): gateway-wide totals on /apps and /enterprise - #2212

Merged
smakosh merged 2 commits into
mainfrom
fix/public-stats-gateway-wide
May 8, 2026
Merged

smakosh merged 2 commits into
mainfrom
fix/public-stats-gateway-wide

Conversation

@smakosh

@smakosh smakosh commented May 8, 2026 •

Copy link
Copy Markdown
Member

Summary

The /apps headline stats and /enterprise hero stats were showing dramatically smaller numbers than the admin Global Stats widget (e.g. 7.51B vs 127B tokens). Two reasons:

  1. The /public/apps endpoint excludes source = 'unknown' (untagged traffic, the vast majority of requests), and
  2. Headline totals were computed by summing the truncated leaderboard array (apps.reduce(...)), capped at limit=200.

This PR keeps the leaderboard scoped to known apps but makes the headline numbers reflect gateway-wide all-time traffic, matching admin.

Changes

  • apps/api/src/routes/public-apps.ts: split into two queries — leaderboard rows (excludes unknown, capped by limit) + a separate aggregate over all globalSourceStats rows for totalTokens / totalRequests. totalApps is now COUNT(DISTINCT source) FILTER (source <> 'unknown'), no longer capped by limit.
  • apps/ui/src/components/enterprise/hero.tsx: hero now accepts optional totalTokens / totalRequests props and converts them to ticker-friendly value+suffix. "Daily Tokens" and "Customer Savings" stay hardcoded.
  • apps/ui/src/app/enterprise/page.tsx: server-fetches /public/apps (with limit=1, since it only consumes the totals) and passes them into the hero. revalidate = 300.

Test plan

  • Visit /apps — "tokens processed" and "requests routed" should match admin Global Stats; leaderboard list still excludes unknown.
  • Visit /enterprise — hero "Total Tokens Processed" and "Total Requests" stat cards reflect live gateway totals (with hardcoded fallback if fetch fails).
  • pnpm build passes.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Enterprise page now displays live usage statistics including total tokens processed and total requests based on actual application data.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented May 8, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 4a65275e-c351-4583-bbf0-9b3b3f40e783

📥 Commits

Reviewing files that changed from the base of the PR and between 2d9c902 and e1064a4.

📒 Files selected for processing (3)
  • apps/api/src/routes/public-apps.ts
  • apps/ui/src/app/enterprise/page.tsx
  • apps/ui/src/components/enterprise/hero.tsx

Walkthrough

The PR refactors the public apps API to fetch aggregate totals via parallel queries and return precomputed summaries. The Enterprise page is converted to async to server-side fetch these totals with ISR caching, then passes them to the Hero component, which formats and displays them as ticker statistics using a new helper function.

Changes

Enterprise Stats Server-Side Integration

Layer / File(s) Summary
API Aggregate Totals
apps/api/src/routes/public-apps.ts
GET / endpoint now executes two queries in parallel: one fetches per-source usage rows filtered, grouped, and ordered by token count with limit applied; another fetches overall totals (distinct app count excluding unknown, summed tokens, summed requests). Response uses precomputed totalApps, totalTokens, totalRequests instead of deriving totals from the apps array.
Server-Side Fetch & ISR
apps/ui/src/app/enterprise/page.tsx
EnterprisePage is converted to async, exports revalidate = 300 for incremental static regeneration, and defines internal PublicAppsResponse type. Calls fetchServerData("GET", "/public/apps", { limit: "1" }) server-side and passes stats?.totalTokens and stats?.totalRequests as props to HeroEnterprise.
Hero Component Stats Display
apps/ui/src/components/enterprise/hero.tsx
HeroEnterprise now accepts optional totalTokens and totalRequests props via new HeroEnterpriseProps interface. Introduces toTickerStat helper to convert numeric values into formatted { value, suffix } pairs (billions, millions, thousands, or raw). StatCard entries for "Total Tokens Processed" and "Total Requests" are updated to display computed stats instead of hardcoded values.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • theopenco/llmgateway#2170: Adds the /public/apps endpoint with per-source aggregation; this PR refactors the endpoint to compute and return separate aggregate totals and updates UI consumers to use them.

Suggested reviewers

  • steebchen-bot
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically identifies the main change: fixing gateway-wide totals display on the /apps and /enterprise pages, which is the core objective of this PR.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/public-stats-gateway-wide

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@smakosh smakosh self-assigned this May 8, 2026
@smakosh
smakosh added this pull request to the merge queue May 8, 2026
Merged via the queue into main with commit 80bfa92 May 8, 2026
11 checks passed
@smakosh
smakosh deleted the fix/public-stats-gateway-wide branch May 8, 2026 17:35
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.

1 participant