[codex] Fix admin global stats date joins#2117
Conversation
|
Warning Rate limit exceeded
You’ve run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThe PR adds ISO date format validation to the ChangesDate Format Validation in Trend Query
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Merging this PR will not alter performance
Comparing Footnotes
|
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@supabase/functions/_backend/utils/pg.ts`:
- Around line 1273-1281: The current regex guard on gs.date_id still allows
non-calendar strings (e.g. "2026-13-40") and can make the cast gs.date_id::date
fail; replace the regex-only check with a safe round-trip validation using
to_date and to_char so only truly valid YYYY-MM-DD dates pass. Concretely,
change conditions that use gs.date_id ~ '^\d{4}-\d{2}-\d{2}$' (and the CASE that
casts gs.date_id::date) to use to_char(to_date(gs.date_id, 'YYYY-MM-DD'),
'YYYY-MM-DD') = gs.date_id and compute the previous date with
(to_date(gs.date_id, 'YYYY-MM-DD') - 1)::text; update the WHERE and LEFT JOIN
prev ON clauses (references: gs.date_id, prev.date_id, endDateOnly) accordingly.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 937aef5d-e3e4-4d9a-9608-327ef7024b3b
📒 Files selected for processing (1)
supabase/functions/_backend/utils/pg.ts
|
jihadMo
left a comment
There was a problem hiding this comment.
Reviewed the current patch after the date validation update. The important part for me is that the previous unsafe gs.date_id::date cast path is gone: the join now computes the previous date only after the YYYY-MM-DD shape check and the to_date/to_char round-trip confirms it is a real calendar date. The WHERE clause applies the same guard before the later trend filtering, so malformed date_id rows should be skipped instead of crashing the admin stats query.
The remaining gs.date_id <= ${endDateOnly} comparison is still a text comparison, but after the round-trip validation every surviving value is normalized YYYY-MM-DD, so lexical order matches chronological order. CI is green on the PR. No blockers from me.



Summary (AI generated)
date_idis only cast when it matchesYYYY-MM-DD.global_stats.date_idrows out of the trend query instead of letting one bad row empty the chart response.Motivation (AI generated)
Admin charts started returning
success: truewith an emptyglobal_stats_trenddataset after the recent release added adate_id::dateself-join. Becausedate_idis stored as text, any non-date historic row can make the query throw, and the helper catches that error by returning an empty array.Business Impact (AI generated)
Restores admin dashboard trend charts used to monitor platform health, revenue, usage, and conversion metrics without requiring a production data cleanup first.
Test Plan (AI generated)
bun test tests/admin-stats.unit.test.tsbun lintbun lint:backendbun run cli:build && vue-tsc --noEmitSummary by CodeRabbit