Skip to content

[codex] Fix admin global stats date joins#2117

Merged
riderx merged 2 commits into
mainfrom
codex/fix-admin-global-stats-date-filter
May 11, 2026
Merged

[codex] Fix admin global stats date joins#2117
riderx merged 2 commits into
mainfrom
codex/fix-admin-global-stats-date-filter

Conversation

@ToriChanIntegration
Copy link
Copy Markdown
Contributor

@ToriChanIntegration ToriChanIntegration commented May 11, 2026

Summary (AI generated)

  • Guard the admin global stats previous-day join so date_id is only cast when it matches YYYY-MM-DD.
  • Filter malformed global_stats.date_id rows out of the trend query instead of letting one bad row empty the chart response.

Motivation (AI generated)

Admin charts started returning success: true with an empty global_stats_trend dataset after the recent release added a date_id::date self-join. Because date_id is 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.ts
  • bun lint
  • bun lint:backend
  • Commit hook: bun run cli:build && vue-tsc --noEmit

Summary by CodeRabbit

  • Bug Fixes
    • Fixed global statistics trending calculations to ensure only properly formatted dates are processed, improving data accuracy and preventing calculation errors.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 11, 2026

Warning

Rate limit exceeded

@riderx has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 56 minutes and 32 seconds before requesting another review.

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 @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 8d17c631-a62d-490c-a894-d42a1737dabf

📥 Commits

Reviewing files that changed from the base of the PR and between 55a992a and 92dc1be.

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

Walkthrough

The PR adds ISO date format validation to the getAdminGlobalStatsTrend SQL query. The query now filters global_stats rows to only include those with date_id values matching the ISO date pattern (YYYY-MM-DD) and applies the same regex validation to the previous-day join logic, preventing malformed date values from being parsed or used in calculations.

Changes

Date Format Validation in Trend Query

Layer / File(s) Summary
SQL Query Date Filtering
supabase/functions/_backend/utils/pg.ts
getAdminGlobalStatsTrend query now validates that gs.date_id matches the ISO date regex (^\d{4}-\d{2}-\d{2}$) in both the main global_stats selection and the CASE expression for previous-day prev.date_id computation, preventing non-ISO date values from being parsed or used in the join logic.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

  • Cap-go/capgo#2101: Modifies the same previous-day global_stats join logic in getAdminGlobalStatsTrend; this PR adds the ISO-date regex guard to that same join.
  • Cap-go/capgo#2112: Modifies getAdminGlobalStatsTrend to add a new plan_total_conversion_rate field; this PR tightens date filtering in the same query function.
  • Cap-go/capgo#2094: Modifies getAdminGlobalStatsTrend to add paid-activity fields; this PR adds date format validation to the same query function.

Poem

🐰 A date in the wrong format would cause quite a fright,
So regex now guards both the filter and join with care,
ISO dates only shall pass through the night,
No malformed strings will dare venture there! 📅✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: fixing date joins in the admin global stats query logic.
Description check ✅ Passed The PR description includes a summary, motivation, business impact, and test plan. All key sections from the template are addressed with substantive content.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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 unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/fix-admin-global-stats-date-filter

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.

❤️ Share

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 11, 2026

Merging this PR will not alter performance

✅ 43 untouched benchmarks
⏩ 2 skipped benchmarks1


Comparing codex/fix-admin-global-stats-date-filter (92dc1be) with main (38e5856)2

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.

  2. No successful run was found on main (3d184f8) during the generation of this report, so 38e5856 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@riderx riderx marked this pull request as ready for review May 11, 2026 00:34
@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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 38e5856 and 55a992a.

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

Comment thread supabase/functions/_backend/utils/pg.ts
@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown

@jihadMo jihadMo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@riderx riderx merged commit 8f94ad0 into main May 11, 2026
40 checks passed
@riderx riderx deleted the codex/fix-admin-global-stats-date-filter branch May 11, 2026 08:18
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.

3 participants