Skip to content

[codex] Fix Stripe admin revenue backfill#2254

Merged
riderx merged 1 commit into
mainfrom
codex/fix-stripe-admin-revenue-backfill
May 12, 2026
Merged

[codex] Fix Stripe admin revenue backfill#2254
riderx merged 1 commit into
mainfrom
codex/fix-stripe-admin-revenue-backfill

Conversation

@riderx
Copy link
Copy Markdown
Member

@riderx riderx commented May 12, 2026

Summary (AI generated)

  • Replaced the old event-based Stripe revenue trend backfill with a single invoice/subscription based rebuild script.
  • Pointed both revenue backfill package commands at the new script and removed the stale legacy implementation and tests.
  • Added coverage for historical inactive Stripe prices, plan classification, revenue snapshots, subscription flow, MRR/ARR, ARR by plan, and churn revenue by plan.

Motivation (AI generated)

The old backfill path only handled recent Stripe Events by default, required apply/date/event-file options for older ranges, skipped missing global_stats dates, and only mapped current plan price IDs. That left admin revenue dashboard charts incomplete back to 2022.

Business Impact (AI generated)

This gives Capgo a reliable repair command for revenue dashboards, improving visibility into plan distribution, subscription flow, churn, MRR, and ARR history. Accurate historical revenue reporting makes business analysis and recovery from bad backfills less fragile.

Test Plan (AI generated)

  • bunx vitest run tests/admin-stripe-backfill-scripts.unit.test.ts
  • bunx eslint --no-ignore scripts/fix_stripe_admin_revenue_dashboard_metrics.ts tests/admin-stripe-backfill-scripts.unit.test.ts
  • bun lint
  • bun typecheck
  • git diff --check

Generated with AI

Summary by CodeRabbit

  • Refactor

    • Consolidated Stripe revenue metrics backfill operations by unifying two separate administrative scripts into a single comprehensive process. This improves efficiency and maintainability when rebuilding admin revenue dashboards and trend metrics from Stripe data.
  • Tests

    • Updated and expanded test coverage to validate the consolidated backfill functionality.

@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.

@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-stripe-admin-revenue-backfill (173c1a6) with main (1daf863)

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 merged commit 7e93659 into main May 12, 2026
43 of 46 checks passed
@riderx riderx deleted the codex/fix-stripe-admin-revenue-backfill branch May 12, 2026 21:53
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 12, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3b8e8f9e-b05a-4692-b5bf-6dfd8b7d29ff

📥 Commits

Reviewing files that changed from the base of the PR and between 1daf863 and 173c1a6.

📒 Files selected for processing (6)
  • package.json
  • scripts/backfill_admin_revenue_dashboard_metrics.ts
  • scripts/backfill_revenue_trend_metrics.ts
  • scripts/fix_stripe_admin_revenue_dashboard_metrics.ts
  • tests/admin-stripe-backfill-scripts.unit.test.ts
  • tests/backfill-revenue-trend-metrics.unit.test.ts

📝 Walkthrough

Walkthrough

Two separate Stripe backfill scripts are consolidated into a single unified script that rebuilds admin revenue dashboard metrics from Stripe subscription and invoice data. Both npm scripts now invoke the consolidated implementation, and test coverage is updated accordingly.

Changes

Stripe Revenue Backfill Consolidation

Layer / File(s) Summary
Configuration, types, and service initialization
scripts/fix_stripe_admin_revenue_dashboard_metrics.ts (lines 1–176)
Imports, environment setup, Stripe and Supabase client initialization, and defines core interfaces (StripePriceLookupEntry, StripeRevenueInterval, StripeRevenueMetricValues, StripeRevenueBackfillRow) plus shared type aliases and constants for plan categories and billing intervals.
Plan classification and Stripe price lookup
scripts/fix_stripe_admin_revenue_dashboard_metrics.ts (lines 177–374)
Numeric normalization and Stripe ID extraction utilities; classifyPlanKeyFromText maps free-text plan names to plan keys; buildStripePriceLookup constructs a Map from DB plans and Stripe prices, inferring billing interval, plan, and computed MRR for each price.
Stripe data retrieval: invoices and subscriptions
scripts/fix_stripe_admin_revenue_dashboard_metrics.ts (lines 376–724)
Invoice-line parsing extracts price/product/subscription IDs, quantity, amounts, and billing periods; subscription filtering and active-state detection; buildStripeRevenueIntervals aggregates Stripe invoices and subscriptions into deduped, sorted revenue intervals keyed by subscription and customer.
Daily metrics computation and backfill row building
scripts/fix_stripe_admin_revenue_dashboard_metrics.ts (lines 726–911)
extractStateForDate derives per-day subscription state and records transitions (new customers, cancellations, upgrades); summarizeMetricsSnapshot computes full revenue metrics including MRR, plan counts, and churn; buildStripeInvoiceRevenueBackfillRows walks the date range, compares computed metrics against existing global_stats rows, and flags changed rows.
Supabase persistence, logging, and main entrypoint
scripts/fix_stripe_admin_revenue_dashboard_metrics.ts (lines 913–1108)
Fetches DB plans and existing global_stats rows; constructs update/insert payloads for changed rows; writes to Supabase with bounded concurrency; logs progress and sample output; main function orchestrates the full flow and auto-runs when the module is the entrypoint.
npm script updates
package.json
Both stripe:backfill-revenue-trends and stripe:backfill-admin-revenue-dashboard now invoke bun scripts/fix_stripe_admin_revenue_dashboard_metrics.ts.
Unit test coverage
tests/admin-stripe-backfill-scripts.unit.test.ts, removed: tests/backfill-revenue-trend-metrics.unit.test.ts
New tests validate plan classification from text, Stripe price lookup construction, and revenue metrics computation across daily snapshots. Old backfill-revenue-trend test suite removed and replaced by focused Stripe invoice revenue tests.

Sequence Diagram

sequenceDiagram
    participant Main as Script Entrypoint
    participant Stripe as Stripe API
    participant Supabase as Supabase (plans, global_stats)
    participant Compute as Metrics Computation

    Main->>Supabase: Fetch plan definitions
    Main->>Supabase: Fetch existing global_stats rows (2022-01-01 → today)
    Main->>Stripe: Fetch active & inactive Stripe prices
    Main->>Compute: Build price lookup (interval, plan, MRR)
    
    rect rgba(100, 150, 200, 0.5)
    note over Stripe,Compute: Retrieve Revenue Intervals
    Main->>Stripe: Fetch paid invoices (subscription line items)
    Stripe-->>Main: Invoice list
    Main->>Stripe: Fetch active/trialing/past_due subscriptions
    Stripe-->>Main: Subscription list
    Main->>Compute: Parse intervals, dedupe by subscription
    Compute-->>Main: Sorted revenue intervals
    end
    
    rect rgba(150, 200, 100, 0.5)
    note over Compute,Supabase: Daily State & Metrics Loop
    Main->>Compute: For each day: extract state as of date
    Compute->>Compute: Record transitions (new, churn, upgrades)
    Compute->>Compute: Summarize into StripeRevenueMetricValues
    Compute->>Compute: Compare vs. existing global_stats row
    Compute-->>Main: Backfill row (changed, current, date_id, exists)
    end
    
    Main->>Supabase: Write only changed rows (update or insert) with bounded concurrency
    Supabase-->>Main: Rows updated/inserted
    Main->>Main: Log progress & sample rows
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • Cap-go/capgo#2010: This PR consolidates the backfill_revenue_trend_metrics script that was introduced in #2010 into the new unified fix_stripe_admin_revenue_dashboard_metrics.ts.
  • Cap-go/capgo#2039: Directly related — #2039 added scripts/backfill_admin_revenue_dashboard_metrics.ts which this PR removes and replaces with the consolidated fix_stripe_admin_revenue_dashboard_metrics.ts, and both update package.json to wire up Stripe backfill commands.
  • Cap-go/capgo#2101: Related — both PRs modify admin revenue backfill logic, update global_stats metrics including per-plan fields, and refactor or extend backfill scripts and their test coverage.

Poem

🐰 Two scripts become one clear path,
Stripe invoices, subscriptions bath—
Daily metrics dance and align,
Revenue trends now unified shine,
Backfill logic consolidated fine! ✨

✨ 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-stripe-admin-revenue-backfill

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

@sonarqubecloud
Copy link
Copy Markdown

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