Repository navigation
feat(leaderboard): serve model leaderboards from a Postgres rollup - #13017
Merged
Merged
Conversation
chrarnoldus
force-pushed
the
leaderboard-postgres-rollup
branch
from
November 27, 2026 10:53
31a4cbe to
a69e05a
Compare
Move leaderboard-model-usage and leaderboard-model-provider-usage off Snowflake onto leaderboard_usage_daily, a global per-day rollup built hourly by the leaderboard-usage-rollup cron from microdollar_usage and its metadata. leaderboard-provider-race stays on Snowflake.
chrarnoldus
force-pushed
the
leaderboard-postgres-rollup
branch
from
November 27, 2026 14:16
5220a89 to
59c8a2e
Compare
iscekic
approved these changes
Nov 27, 2026
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
Moves two public leaderboard routes from Snowflake to Postgres:
/api/public/leaderboard-model-usage/api/public/leaderboard-model-provider-usage/api/public/leaderboard-provider-racestays on Snowflake. It depends onmodel_provider_companyandis_open_weights, which come from the dbt model dimension and don't exist in Postgres yet.Rollup table
leaderboard_usage_dailyis a global per-day rollup with no user or organization columns.provider,requested_model,model,inference_provider,mode,is_user_byok.is_user_byokis non-null: a missing metadata row or a NULL flag is stored asfalse.provider, so leaderboard filters can change without rebuilding the table. Filtering after aggregation can differ slightly from per-request filtering. That's acceptable for marketing figures.Building the rollup
/api/cron/leaderboard-usage-rollup, runs hourly at :20. Each run builds the days in the 7-day leaderboard window that have no rows yet, newest first. It builds at most 2 days per run, within a 500 s budget. The budget is checked before every 15-minute slice, so a run stops partway through a day rather than being killed. Unfinished days are reported asremainingand picked up by the next run.created_at, so a day is built 1 hour after it ends, and only once the replica has replayed past that point (pg_last_xact_replay_timestamp()). Until then the day is left out of the leaderboards rather than shown short. Rows written more than an hour after the day ends are not counted. To recount, delete the affected days' rows (or truncate the table) and the next runs rebuild them, 2 days per run.readDbin 15-minute slices. Each slice joinsmicrodollar_usagetomicrodollar_usage_metadataandmode, with both sides bounded bycreated_at(production writes both rows with the samecreated_atin one statement). Each slice runs withstatement_timeout = 60sandwork_mem = 128MB. On a production replica, a warm one-hour query (~660k rows) took 3.3 s and spilled its hash join to disk at the default 32MBwork_mem; the same hour cold exceeded 120 s. Slices keep each query small and well under the replica's 90 smax_standby_streaming_delay.Logging
The cron logs structured JSON to the console only, with no Sentry events or alerts:
leaderboard_usage_rollup_day: one line per built day, withrows,durationMs,slicesandslowestSliceMs.leaderboard_usage_rollup_day_deadline: a day abandoned at the deadline, with how many slices it finished.leaderboard_usage_rollup_day_failed: the day, the stage (readorwrite), the slice start, slices completed, and the Postgres error code (57014statement timeout,40001replica replay conflict). The error is then rethrown.leaderboard_usage_rollup: the run summary, withbuilt,remaining,unsettled,emptyDays,stoppedBy(capordeadline),yesterdayBuilt,replicaLagMsanddurationMs. It's logged as a warning when a built day had no rows, or when yesterday is still missing at 03:00 UTC or later.Routes
public-snowflake-report.tsbecamepublic-report.ts.createPublicReporttakes aloadfunction, and Redis caching is now optional throughcacheKey.createPublicSnowflakeReportstays as a thin wrapper for provider-race, which keeps its Redis cache.s-maxageandstale-while-revalidate) and the in-process cache now expire after 5 minutes instead of 1 hour, for all public reports. The Redis cache that provider race still uses keeps its 1-hour TTL.leaderboard_usage_daily, using the UTC date for the window.[]. Empty means the rollup hasn't been built yet. The 502 keeps the in-process last-known-good value instead of caching an empty leaderboard.Rollout
The first backfill takes about 4 hourly runs; triggering
/api/cron/leaderboard-usage-rollupby hand speeds it up. Until the first run, both routes return 502 for anything not already cached. While the backfill runs they report a partial week, but never a partial day. There is no Snowflake fallback.Verification
NODE_ENV=test npx jest src/app/api/public src/app/api/cron src/lib/leaderboardinapps/web: 48 suites, 310 tests passed. The new rollup tests and route tests run against real Postgres.packages/dbjest: 11 suites passed.pnpm --filter web typecheck,pnpm --filter web lintandpnpm drizzle checkpass.Models used: Claude Opus 5.5 (main session), Claude Haiku 5.5 (exploration subagent).