Skip to content

fix(cron): keep global stats snapshots running#2484

Open
riderx wants to merge 1 commit into
mainfrom
codex/fix-global-stats-logsnag-timeout
Open

fix(cron): keep global stats snapshots running#2484
riderx wants to merge 1 commit into
mainfrom
codex/fix-global-stats-logsnag-timeout

Conversation

@riderx

@riderx riderx commented Jun 11, 2026

Copy link
Copy Markdown
Member

Summary (AI generated)

  • Move logsnag_insights global stats work out of one monolithic request and into independent admin_stats shard messages.
  • Dispatch shards as distinct queue HTTP calls via targets like logsnag_insights_core, logsnag_insights_usage, and logsnag_insights_revenue.
  • Split global stats writes into bounded shards: core, usage, revenue, plugins, builds, retention, paid_products, ltv, and delayed notifications.
  • Add snapshot completion tracking with completed_shards, including notification completion, so retries do not skip notification delivery or resend completed snapshots.
  • Store snapshot orgs count and use snapshot-bounded revenue, active-app, usage, device, plugin, and build windows for replayed dates.
  • Preserve retry behavior by reserving a delayed dispatcher retry before 202, cancelling it only after durable shard queueing, and propagating retry-cancel failures.

Motivation (AI generated)

Cloudflare logs showed /triggers/logsnag_insights switching from 200 ok to 0 canceled around the queue HTTP timeout. The slow work was the full global stats snapshot refresh inside one LogSnag-named trigger. Splitting the queue into separate HTTP shard calls lets Cloudflare retry only the failed metric group and avoids relying on one long Worker invocation.

Business Impact (AI generated)

Restores Capgo admin/global metrics freshness without increasing queue timeouts. Daily snapshots can make partial progress, recover from one slow metric group, and avoid sending LogSnag/tracking events with incomplete, stale, or mixed-date values.

Test Plan (AI generated)

  • bun test tests/cloudflare-datetime.unit.test.ts tests/logsnag-insights-revenue.unit.test.ts
  • bun lint (passes with existing unrelated JSDoc warning in src/services/compatibilityEvents.ts)
  • bun lint:backend
  • bun run cli:typecheck
  • bun run typecheck:backend
  • bun run typecheck:frontend
  • bash scripts/check-supabase-migration-order.sh
  • git diff --check
  • Commit hook: bun run cli:typecheck && bun run typecheck:backend && bun run typecheck:frontend

Generated with AI

@coderabbitai

coderabbitai Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@riderx, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 10 minutes and 15 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more credits in the billing tab to continue.

⌛ How to resolve this issue?

After more reviews become available, 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 include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 66c909ec-ac55-4d8b-bfce-be1e8caa111e

📥 Commits

Reviewing files that changed from the base of the PR and between ab6a886 and cf8400c.

📒 Files selected for processing (6)
  • supabase/functions/_backend/triggers/logsnag_insights.ts
  • supabase/functions/_backend/utils/cloudflare.ts
  • supabase/functions/triggers/index.ts
  • supabase/migrations/20260611160348_add_global_stats_completed_shards.sql
  • tests/cloudflare-datetime.unit.test.ts
  • tests/logsnag-insights-revenue.unit.test.ts
📝 Walkthrough

Walkthrough

Refactors the LogSnag global-stats pipeline from monolithic computation into sharded snapshot updates. Introduces per-shard runners for nine independent metric types, parametrizes Cloudflare analytics time windows via optional referenceDate, tracks shard completion via completed_shards column, extends revenue metrics with credit aggregation, and exposes per-shard HTTP routes through pgmq queue dispatch returning 202 on enqueue.

Changes

Sharded Global Stats Pipeline with Parametrized Analytics Windows

Layer / File(s) Summary
Database schema and shard type infrastructure
supabase/migrations/20260611160348_add_global_stats_completed_shards.sql, supabase/functions/_backend/triggers/logsnag_insights.ts
completed_shards column added to global_stats to track finished shards. Shard type definitions and constants (core/usage/revenue/plugins/builds/retention/paid_products/ltv/notifications) are established.
Snapshot state management and payload normalization
supabase/functions/_backend/triggers/logsnag_insights.ts
Imports restructured. Comprehensive helpers ensure snapshot rows exist, reset/patch completed shards, normalize retry/shard/date payloads, reserve/cancel pgmq retry messages, and compute missing required shards.
Parametrized Cloudflare analytics time windows
supabase/functions/_backend/utils/cloudflare.ts
getLastMonthAnalyticsWindow(referenceDate) helper centralizes rolling 30-day SQL expressions. Analytics functions (readActiveAppsCF, readLastMonthUpdatesCF, readLastMonthDevicesCF, readLastMonthDevicesByPlatformCF, getPluginBreakdownCF) now accept optional referenceDate to parametrize WHERE timestamp filters instead of hardcoded "now()".
Core, usage, and revenue shard implementations
supabase/functions/_backend/triggers/logsnag_insights.ts
Core shard computes onboarded/upgrade-needed orgs and plan v2 totals via RPC. Usage shard aggregates daily register/bundle/storage metrics with backward-compatible fallback. Revenue shard counts canceled/upgraded orgs by stripe_info customer_id and aggregates credits bought/consumed from usage credit tables, with integer rounding for bigint fields. All write incrementally to global_stats snapshot.
Build metrics rolling 30-day window optimization
supabase/functions/_backend/triggers/logsnag_insights.ts
getBuildStats derives rolling 30-day window from snapshot date (prevDayEnd) with explicit ISO start/end timestamps instead of "now". Build_logs queries (total/iOS/Android) use the derived [last30daysStart, last30daysEnd) bounds. Demo-seeded-app counter expanded from single createdAfterIso to explicit createdAfterIso and createdBeforeIso range.
Remaining shards and queue-based dispatcher pipeline
supabase/functions/_backend/triggers/logsnag_insights.ts
Plugins/builds/retention/paid_products/ltv/notifications shard runners adapted to read from snapshot state. Notifications gated on snapshot readiness and required shard completion. Dispatcher enqueues all shard messages into pgmq with per-shard delays via scheduleLogsnagInsightsUpdate. Main app.post endpoint reserves retry messages, schedules background updates via EdgeRuntime.waitUntil, and returns HTTP 202. Exports logsnagInsightsShardApps record of per-shard Hono sub-apps.
Per-shard route registration
supabase/functions/triggers/index.ts
Imports logsnagInsightsShardApps. Registers /logsnag_insights_* routes (core/usage/revenue/plugins/builds/retention/paid_products/ltv/notifications) delegating to corresponding shard sub-apps.
Comprehensive test coverage
tests/cloudflare-datetime.unit.test.ts, tests/logsnag-insights-revenue.unit.test.ts
Tests added for parametrized analytics window determinism, replay metric window derivation, required shard detection with completion normalization, plan bucket/retry count normalization, message-building helpers, and EdgeRuntime background scheduling with reserved retry cancellation across success/failure scenarios.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~75 minutes

Possibly related PRs

  • Cap-go/capgo#2201: Both PRs touch the LogSnag logsnag_insights global-stats pipeline—main PR restructures it into shard-based snapshot updates including the LTV shard, while the retrieved PR adds/implements the LTV computation and persistence (average_ltv/shortest_ltv/longest_ltv) into public.global_stats via logsnag_insights.
  • Cap-go/capgo#2101: The main PR updates logsnag_insights.ts to write new plan_*_conversion_rate fields into global_stats, which the retrieved PR's getAdminGlobalStatsTrend in utils/pg.ts then reads/exposes for the churn/conversion UI charts.

Suggested labels

codex

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 1.85% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive The PR description is AI-generated with summary, motivation, business impact, and test plan provided, but lacks manual author context and detail on implementation specifics. Add author's own summary explaining the refactoring rationale, key architectural changes, and any manual testing steps beyond the automated checks listed.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix(cron): keep global stats snapshots running' clearly summarizes the main change—refactoring LogSnag global stats snapshots to prevent timeout failures by splitting work into sharded queue messages.
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.


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

@codspeed-hq

codspeed-hq Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 43 untouched benchmarks
⏩ 2 skipped benchmarks1


Comparing codex/fix-global-stats-logsnag-timeout (cf8400c) with main (fa78ae2)

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.

@riderx riderx force-pushed the codex/fix-global-stats-logsnag-timeout branch from 5309d18 to 2eab7d2 Compare June 11, 2026 11:30

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2eab7d2668

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread supabase/functions/_backend/triggers/logsnag_insights.ts Outdated
@riderx riderx force-pushed the codex/fix-global-stats-logsnag-timeout branch from 2eab7d2 to 6599525 Compare June 11, 2026 11:47

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6599525296

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread supabase/functions/_backend/triggers/logsnag_insights.ts Outdated
@riderx riderx force-pushed the codex/fix-global-stats-logsnag-timeout branch from 6599525 to ab2c638 Compare June 11, 2026 12:03
@riderx riderx force-pushed the codex/fix-global-stats-logsnag-timeout branch from ab2c638 to aa7b5d9 Compare June 11, 2026 13:30

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: aa7b5d9c4f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread supabase/functions/_backend/triggers/logsnag_insights.ts Outdated
@riderx riderx force-pushed the codex/fix-global-stats-logsnag-timeout branch from aa7b5d9 to 350bfaf Compare June 11, 2026 13:46
@riderx riderx force-pushed the codex/fix-global-stats-logsnag-timeout branch from 350bfaf to 83ff4db Compare June 11, 2026 14:08

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 83ff4db9cd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread supabase/functions/_backend/triggers/logsnag_insights.ts Outdated
@riderx riderx force-pushed the codex/fix-global-stats-logsnag-timeout branch from 83ff4db to 3beacac Compare June 11, 2026 14:24

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3beacac22d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread supabase/functions/_backend/triggers/logsnag_insights.ts
@riderx riderx force-pushed the codex/fix-global-stats-logsnag-timeout branch from 3beacac to a3ec396 Compare June 11, 2026 14:35

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a3ec396759

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread supabase/functions/_backend/triggers/logsnag_insights.ts Outdated
@riderx riderx force-pushed the codex/fix-global-stats-logsnag-timeout branch from a3ec396 to 8d0e6d0 Compare June 11, 2026 14:58

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8d0e6d01b2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread supabase/functions/_backend/triggers/logsnag_insights.ts Outdated
@riderx riderx force-pushed the codex/fix-global-stats-logsnag-timeout branch from 8d0e6d0 to bf7017c Compare June 11, 2026 15:09

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bf7017cf32

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread supabase/functions/_backend/triggers/logsnag_insights.ts Outdated
@riderx riderx force-pushed the codex/fix-global-stats-logsnag-timeout branch from bf7017c to 789914b Compare June 11, 2026 15:22

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 789914b74f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread supabase/functions/_backend/triggers/logsnag_insights.ts
Comment thread supabase/functions/_backend/triggers/logsnag_insights.ts
@riderx riderx force-pushed the codex/fix-global-stats-logsnag-timeout branch from 789914b to 0e82f16 Compare June 11, 2026 15:55

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0e82f16166

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread supabase/functions/_backend/triggers/logsnag_insights.ts Outdated
@riderx riderx force-pushed the codex/fix-global-stats-logsnag-timeout branch from 0e82f16 to 1fd169c Compare June 11, 2026 16:06

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1fd169c0cc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread supabase/functions/_backend/triggers/logsnag_insights.ts Outdated
@riderx riderx force-pushed the codex/fix-global-stats-logsnag-timeout branch from 1fd169c to ab6a886 Compare June 11, 2026 16:15
@coderabbitai coderabbitai Bot added the codex label Jun 11, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ab6a8867a2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread supabase/functions/_backend/triggers/logsnag_insights.ts Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

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 `@tests/logsnag-insights-revenue.unit.test.ts`:
- Around line 199-202: The race timeout of 25ms around the Promise.race that
awaits Promise.resolve(app.request('http://localhost/', { method: 'POST' })) is
too short and causes flaky CI; increase the timeout in the new Promise (the
resolve('timeout') timer) to a more generous value (e.g. 200–500ms) or switch to
deterministic fake timers for that test so the variable result reliably reflects
the request status instead of intermittently returning 'timeout'.
🪄 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: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: da1b8557-0200-4518-9d30-7790918f031a

📥 Commits

Reviewing files that changed from the base of the PR and between e87fbe9 and ab6a886.

📒 Files selected for processing (6)
  • supabase/functions/_backend/triggers/logsnag_insights.ts
  • supabase/functions/_backend/utils/cloudflare.ts
  • supabase/functions/triggers/index.ts
  • supabase/migrations/20260611160348_add_global_stats_completed_shards.sql
  • tests/cloudflare-datetime.unit.test.ts
  • tests/logsnag-insights-revenue.unit.test.ts
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • Cap-go/capacitor-updater (manual)

Comment thread tests/logsnag-insights-revenue.unit.test.ts
@riderx riderx force-pushed the codex/fix-global-stats-logsnag-timeout branch from ab6a886 to a838ccd Compare June 11, 2026 16:37

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a838ccdb39

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread supabase/functions/_backend/triggers/logsnag_insights.ts
@riderx riderx force-pushed the codex/fix-global-stats-logsnag-timeout branch from a838ccd to cf8400c Compare June 11, 2026 17:01
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant