Skip to content

Eliminate N+1 query in getOrgChangeDetections - #195

Open
clickmatos wants to merge 1 commit into
mainfrom
fix-org-change-detections-n-plus-1
Open

Eliminate N+1 query in getOrgChangeDetections#195
clickmatos wants to merge 1 commit into
mainfrom
fix-org-change-detections-n-plus-1

Conversation

@clickmatos

Copy link
Copy Markdown
Contributor

Summary

Part of #180 (dashboard slow to load). PR #182 already fixed the bulk of it (per-section Suspense streaming, request-scoped cache() dedup) — this fixes a concrete remaining bottleneck found while verifying that fix held up: getOrgChangeDetections (feeds ChangeAlertPanel) issued one metrics query per repo in a sequential loop, an N+1 that scales linearly with repo count and can stall that panel's stream on large orgs.

Extends the repo_metric_summaries view (already used by getOrgReposSummary for the same "N+1 → 2 bulk queries" fix) with the previous run's values for the metrics detectChanges actually compares (revert_rate, churn_events, ai_detection_coverage_pct, plus a prev_created_at timestamp) — it already had prev_stabilization_ratio for the sparkline delta arrow. getOrgChangeDetections now reads repos + this one pre-aggregated view, 2 queries total regardless of repo count, instead of 1 + N.

Filed #194 for the broader, structural gap this surfaced: there's no caching layer anywhere in the dashboard's data path — repo_metric_summaries is a plain (non-materialized) view recomputed from scratch on every single page load, and no unstable_cache/revalidate wraps any loader. Left out of this PR since it's an architectural decision (materialized view vs. Next.js revalidation) worth its own discussion, not a drop-in fix.

Test plan

  • npx tsc --noEmit — clean
  • npx eslint lib/queries/temporal.ts — clean
  • npx vitest run — 269 passed (detect-changes.test.ts covers detectChanges itself, untouched by this change; getOrgChangeDetections has no unit tests today, consistent with this codebase's convention of integration-testing the query layer against a real DB)
  • Migration not applied anywhere from here. 023_repo_metric_summaries_prev_values.sql needs supabase db push (or applying via the dashboard) against the actual project before getOrgChangeDetections will return real data — until then the new columns don't exist and prev_revert_rate/prev_churn_events/prev_ai_detection_coverage_pct/prev_created_at will read as undefined. Please confirm the migration is applied as part of merging/deploying this.

🤖 Generated with Claude Code

Detecting metric changes across an org's repos issued one `metrics`
query per repo, sequentially — a round-trip count that scales linearly
with repo count and can stall the ChangeAlertPanel's stream on large
orgs.

Extends the repo_metric_summaries view (023) with the previous run's
values for the metrics detectChanges compares, so the DB does the
per-repo "latest vs previous" aggregation once instead of the app
looping N times. getOrgChangeDetections now reads that plus the repo
list — 2 bulk queries total regardless of repo count — mirroring the
same fix getOrgReposSummary already applies against this view (022).

Needs the migration applied to the actual Supabase project (`supabase
db push` or via the dashboard) before this takes effect — not run from
here.

Co-Authored-By: claude-code_2-1-238_agent <claude-code_2-1-238_agent@iris.invalid>
@vercel

vercel Bot commented Aug 26, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
clickbus-iris Ready Ready Preview Aug 26, 2026 7:35pm

Request Review

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