perf(stats): bound the review-volume trend's own-ledger query by an index, not a full scan - #4727
Conversation
…ndex, not a full scan (#4723) loadOwnLedgerDayRows previously computed each PR's true first-publish date via MIN(created_at) over the ENTIRE github_app.pr_public_surface_published history before discarding anything outside the trailing 8-week window -- an unbounded scan that grows with the whole audit_events table. Splits it into two index-backed steps instead: which PRs had any publish event in the trailing window (existing event_type+created_at index), then each candidate's true first-publish across its FULL history (new target_key+created_at index, migrations/0142). Confirmed via EXPLAIN QUERY PLAN that both steps now SEARCH an index rather than SCAN the table. A naive single-pass prefilter (raw created_at >= sinceIso before the MIN()) would have been a correctness regression, not just a perf fix: a PR whose true first-publish is outside the window but got re-published inside it (a fresh push triggering re-review) would resolve to the wrong, too-recent date. The new regression test proves such a PR is still correctly excluded -- verified failing against the naive version first.
|
Superagent didn't find any vulnerabilities or security issues in this PR. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4727 +/- ##
=======================================
Coverage 94.21% 94.21%
=======================================
Files 439 439
Lines 38704 38704
Branches 14101 14101
=======================================
Hits 36466 36466
Misses 1576 1576
Partials 662 662
🚀 New features to boost your workflow:
|
|
Warning 🟨🟨🟨🟨🟨🟨🟨🟨🟨🟨🟨🟨 ⏸️ Gittensory review result - manual review recommendedReview updated: 2026-07-10 21:20:40 UTC
⏸️ Suggested Action - Manual Review
Review summary Nits — 6 non-blocking
Linked issue satisfactionAddressed Review context
Contributor next steps
Signal definitions
🟩 Safe / merged · 🟦 Advisory · 🟨 Held for review · 🟥 Blocked / closed 💰 Earn for open-source contributions like this. Gittensor lets GitHub contributors earn for the work they already do — register to start earning →. Checked by Gittensory, a quiet PR intelligence layer for OSS maintainers.
|
Summary
loadOwnLedgerDayRows(src/services/public-review-volume-trend.ts, shipped in feat(stats): add a public review-volume/filtered-rate trend and 2 more hero sparklines #4722) computed each PR's true first-publish date viaMIN(created_at)over the entiregithub_app.pr_public_surface_publishedhistory before discarding anything outside the trailing 8-week trend window — an effectively-unbounded scan that grows with the wholeaudit_eventstable on every/v1/public/statsrequest.recent_keys— which PRs had any publish event in the trailing window. Uses the existingaudit_events_type_created_idx (event_type, created_at).true_first_seen— for just those candidates, the TRUE first-publish date across the PR's full history. Uses a newaudit_events_target_key_created_idx (target_key, created_at)(migrations/0142).EXPLAIN QUERY PLANagainst the real migrated schema that both steps nowSEARCH ... USING INDEXrather thanSCANthe table.Closes #4723.
Why not a simpler prefilter
A naive single-pass fix (filter raw
created_at >= sinceIsobefore theMIN()) would have been a correctness regression, not just a perf tweak: a PR whose true first-publish is outside the window, but which got a legitimate re-publish (e.g. a fresh push triggering re-review) inside the window, would resolve to the wrong, too-recent date and get misattributed to that week instead of correctly excluded. This is exactly why #4723 flagged the naive fix as unsafe rather than just landing it under review-cycle time pressure.The new regression test proves the two-step version handles this correctly — verified failing against the naive single-pass version first (temporarily reverted, confirmed the test genuinely catches the bug, restored the fix).
Scope
type(scope): short summaryConventional Commit format.CONTRIBUTING.mdand does not reintroduce GitHub Pages, VitePress,site/, orCNAME.Validation
git diff --checknpm run actionlint— skipped, no.github/workflows/**changes.npm run typechecknpm run db:migrations:check— 145 migrations OK, contiguous through 0142.npm run db:schema-drift:check—src/db/schema.tsmatchesmigrations/.npm run test:coverage(targeted, not the full unsharded suite — see Notes) locally; the rewrittenloadOwnLedgerDayRowsis 100% stmts/branch/funcs/lines.npm run test:workers— skipped, no Worker-specific code beyond what typecheck/coverage already cover.npm run build:mcp/npm run test:mcp-pack— skipped, no MCP package changes.npm run ui:openapi:check/ui:lint/ui:typecheck/ui:test/ui:build— skipped, noapps/gittensory-ui/**changes; no API response shape changed, only the query computing it.npm audit --audit-level=moderateSafety
reviewVolumeTrendfield's values are now computed by a query with the same output, verified by the full existing test suite passing unchanged plus the new regression test.Notes
New migration:
migrations/0142_audit_events_target_key_created_idx.sqladdsaudit_events_target_key_created_idx (target_key, created_at)— a lookup index (not unique;target_keyrepeats once per lifecycle event on a PR).src/db/schema.ts'sauditEventstable definition updated to match.Test coverage: extended
test/unit/public-review-volume-trend.test.tswith a new end-to-end regression test seeding a PR whose true first-publish is 20 weeks old (well outside the 8-week window) with a re-publish event in the current week, asserting it's excluded from every bucket — the exact scenario a naive prefilter gets wrong. All 9 pre-existing tests in the file (and the 2 integration tests exercising the same code path viaGET /v1/public/stats) pass unchanged against the rewritten query, confirming it's output-equivalent for every previously-tested scenario.Verified via
EXPLAIN QUERY PLAN(not committed, a local one-off check against the real migrated schema): bothrecent_keysandtrue_first_seenshowSEARCH ... USING INDEX, confirming the fix achieves its stated goal.