feat(scripts): add Stripe revenue trend backfill#2010
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
✅ Files skipped from review due to trivial changes (3)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdds a new backfill CLI and package script to compute daily revenue trend metrics from Stripe subscription events over a UTC date range, reconstructing per-customer subscription states, deriving daily metrics (MRR, churn, new paying orgs, plan counts, revenue totals), and optionally applying updates to Supabase with Changes
Sequence DiagramsequenceDiagram
participant CLI as Backfill CLI
participant Stripe as Stripe API
participant Archive as Event Archive
participant State as Subscription State Engine
participant Calc as Metrics Calculator
participant Supabase as Supabase
CLI->>Stripe: Fetch events (--from/--to, --limit)
Stripe-->>CLI: Subscription events (paginated)
CLI->>Archive: (optional) Load archived events / baseline subscriptions
Archive-->>CLI: Historical events / baselines
CLI->>State: Seed opening state (baseline + pre-range events)
CLI->>State: Feed in-range events day-by-day
State->>State: Replay events, expire subscriptions at end dates
State-->>Calc: Daily subscription snapshots
Calc->>Calc: Compute MRR, churn, new paying orgs, plan counts, revenue totals
Calc-->>CLI: Backfill rows (pending)
CLI->>Supabase: Fetch existing global_stats rows by date_id
Supabase-->>CLI: Existing rows
CLI->>CLI: Diff rows, show pending updates (dry-run)
alt --apply flag set and events not truncated
CLI->>Supabase: Update matching global_stats rows by date_id
Supabase-->>CLI: Update confirmations
end
Estimated Code Review Effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Review rate limit: 0/5 reviews remaining, refill in 54 minutes and 7 seconds. Comment |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 594924745c
ℹ️ 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".
|
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 the current code and only fix it if needed.
Inline comments:
In `@scripts/backfill_revenue_trend_metrics.ts`:
- Around line 974-976: Add a guard that prevents running the destructive --apply
mode when a per-customer scope is specified: after the existing check that
validates customerId (the block that checks customerId.startsWith('cus_')), add
a check that if the --apply flag is set (the variable controlling apply mode,
e.g., apply or isApply) and customerId is provided, throw an Error explaining
that --apply cannot be used with --customer-id because it would overwrite
global_stats; this ensures the script never applies one-customer metrics into
the global_stats aggregate.
🪄 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: 8148281d-f716-4d70-ae9b-0722bf60217b
📒 Files selected for processing (3)
package.jsonscripts/backfill_revenue_trend_metrics.tstests/backfill-revenue-trend-metrics.unit.test.ts
53446a6 to
907666b
Compare
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|



Summary (AI generated)
global_statsrevenue charts.Motivation (AI generated)
The admin revenue and user dashboards depend on historical
global_statscolumns that can be missing or stale for revenue trend charts. The script reconstructs daily subscription state from Stripe subscription events and plan pricing so those charts can be repaired without manual SQL.Business Impact (AI generated)
Accurate revenue trend history improves visibility into subscription growth, churn, plan mix, MRR, ARR, and plan distribution. This helps Capgo monitor revenue movements and customer behavior from the admin dashboard.
Test Plan (AI generated)
bunx eslint --no-ignore scripts/backfill_revenue_trend_metrics.ts tests/backfill-revenue-trend-metrics.unit.test.tsbunx tsc --ignoreConfig --noEmit --allowImportingTsExtensions --moduleResolution bundler --module ESNext --target ES2020 --lib DOM,ESNext --types bun,vitest --strict --esModuleInterop --skipLibCheck scripts/backfill_revenue_trend_metrics.ts tests/backfill-revenue-trend-metrics.unit.test.tsbunx vitest run tests/backfill-retention-metrics.unit.test.ts tests/backfill-revenue-trend-metrics.unit.test.ts tests/logsnag-insights-revenue.unit.test.tsbun lintGenerated with AI
Summary by CodeRabbit
Chores
--applymode to persist updates.Tests