Skip to content

[codex] fix admin global stats primary read#2245

Merged
riderx merged 1 commit into
mainfrom
codex/fix-admin-global-stats-primary
May 12, 2026
Merged

[codex] fix admin global stats primary read#2245
riderx merged 1 commit into
mainfrom
codex/fix-admin-global-stats-primary

Conversation

@riderx
Copy link
Copy Markdown
Member

@riderx riderx commented May 12, 2026

Summary (AI generated)

  • Force global_stats_trend admin metrics to use the primary DB connection instead of the read-only replica path.
  • Re-throw DB errors after logging so the admin stats API stops returning success: true with fake empty chart data on query failure.

Motivation (AI generated)

Production primary has global_stats rows for the requested window, but the deployed worker still returned an empty dataset. The remaining failure path was the replica read plus error masking in getAdminGlobalStatsTrend.

Business Impact (AI generated)

Restores admin dashboard trend charts and makes future DB/schema drift visible as an API error instead of silently hiding reporting failures.

Test Plan (AI generated)

  • bun lint:backend
  • bun typecheck
  • bun run supabase:with-env -- bunx vitest run tests/admin-stats.unit.test.ts tests/admin-stats.test.ts
  • bun run supabase:with-env -- bunx vitest run tests/admin-stats.test.ts --config vitest.config.cloudflare.ts
  • bun test:backend
  • bun run supabase:with-env -- bun test:cloudflare:backend
  • Verified production primary global_stats has 28 rows from 2026-04-12 through 2026-05-11 for the current 30-day window.

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling to properly surface database errors instead of silently returning empty results
    • Enhanced global statistics data accuracy by updating the data source used for queries

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 12, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c6ec016a-62f3-469a-856d-9cc392339b90

📥 Commits

Reviewing files that changed from the base of the PR and between 34fe7b8 and 45720ae.

📒 Files selected for processing (1)
  • supabase/functions/_backend/utils/pg.ts

📝 Walkthrough

Walkthrough

The PR updates getAdminGlobalStatsTrend to query the primary database instead of the read-only replica, with comments explaining the replica drift risk, and changes error handling to rethrow exceptions rather than returning empty results on PostgreSQL errors.

Changes

Global stats query and error handling

Layer / File(s) Summary
Query source and error handling
supabase/functions/_backend/utils/pg.ts
getAdminGlobalStatsTrend now queries the primary database instead of the read-only replica to avoid schema/data drift on global_stats, with updated inline comments explaining the reason. The catch block now rethrows the error after logging instead of returning an empty array.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • Cap-go/capgo#2217: Both PRs modify the same function getAdminGlobalStatsTrend in supabase/functions/_backend/utils/pg.ts; this PR switches the query to the primary DB and changes error handling, while the other fixes SQL column qualification.
  • Cap-go/capgo#2117: Both PRs modify getAdminGlobalStatsTrend in supabase/functions/_backend/utils/pg.ts; this PR adjusts the DB client and error handling, while the other tightens SQL date joins.

Suggested reviewers

  • jihadMo

Poem

🐰 A query that swayed 'tween replicas gone,
Now speaks to the primary when dawn breaks anon.
No silent returns when the errors appear—
They holler and crash, crystal loud and clear! 🌟

🚥 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
Title check ✅ Passed The title clearly and concisely identifies the main change: fixing the admin global stats function to read from the primary database instead of the replica.
Description check ✅ Passed The description includes all essential sections: a clear summary explaining the two key changes, motivation for the fix, business impact, and a comprehensive test plan with checked-off verification steps.
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 codex/fix-admin-global-stats-primary

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

@codspeed-hq
Copy link
Copy Markdown
Contributor

codspeed-hq Bot commented May 12, 2026

Merging this PR will not alter performance

✅ 43 untouched benchmarks
⏩ 2 skipped benchmarks1


Comparing codex/fix-admin-global-stats-primary (45720ae) with main (34fe7b8)

Open in CodSpeed

Footnotes

  1. 2 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@sonarqubecloud
Copy link
Copy Markdown

@riderx riderx marked this pull request as ready for review May 12, 2026 13:23
@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@riderx riderx merged commit 6f511f1 into main May 12, 2026
40 checks passed
@riderx riderx deleted the codex/fix-admin-global-stats-primary branch May 12, 2026 13:30
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