Skip to content

Open source CapCut alternative.

TypeScript

feat(site): billing rollup in the analytics pipeline

David Leae0173aca738
diff --git a/site/src/lib/analytics/pipeline.ts b/site/src/lib/analytics/pipeline.ts
index 28f7ff65..d941ad51 100644
--- a/site/src/lib/analytics/pipeline.ts
+++ b/site/src/lib/analytics/pipeline.ts
@@ -23,6 +23,7 @@ import {
type AnalyticsSnapshotFile,
utcDayOf,
} from "@/lib/analytics/schema";
+import { isActiveProStatus } from "@/lib/billing/pro-subscription";
import { REFERRAL_SOURCES } from "@/lib/onboarding/sequence";
import { prisma } from "@/lib/prisma";
@@ -38,6 +39,10 @@ const WINDOW_DAYS = 60;
const MAX_EXTRACT_DAYS_PER_RUN = 5;
const SNAPSHOT_PAGE_SIZE = 1000;
+// Credit grant sources that correspond to a Stripe charge; the grant amount
+// equals the dollars paid, so these rows double as the revenue record.
+const PAID_GRANT_SOURCES = ["pro_subscription", "stripe_autoreload", "stripe_topup"];
+
export class InvalidDayError extends Error {}
export async function runAnalyticsDaily(payload: {
@@ -161,59 +166,103 @@ async function extractPosthogDay(day: string): Promise<AnalyticsPosthogDayFile>
};
}
-async function writeSnapshot(): Promise<AnalyticsSnapshotFile> {
- const users: AnalyticsSnapshotFile["users"] = [];
- let userCursor: string | undefined;
+// Drain a cursor-paginated query. The page function spreads the cursor args
+// into its findMany, so every table pages the same way.
+async function fetchAllPages<T extends { id: string }>(
+ fetchPage: (cursorArgs: { cursor: { id: string }; skip: 1 } | undefined) => Promise<T[]>,
+): Promise<T[]> {
+ const rows: T[] = [];
+ let cursor: string | undefined;
for (;;) {
- const page = await prisma.user.findMany({
- orderBy: { id: "asc" },
- select: {
- createdAt: true,
- email: true,
- id: true,
- name: true,
- onboarding: { select: { referralAnsweredAt: true, referralSources: true } },
- },
- take: SNAPSHOT_PAGE_SIZE,
- ...(userCursor === undefined ? {} : { cursor: { id: userCursor }, skip: 1 }),
- });
- for (const u of page) {
- users.push({
- createdAt: u.createdAt.toISOString(),
- email: u.email,
- id: u.id,
- name: u.name,
- ...(u.onboarding?.referralAnsweredAt
- ? {
- referralAnsweredAt: u.onboarding.referralAnsweredAt.toISOString(),
- referralSources: u.onboarding.referralSources,
- }
- : {}),
- });
- }
- if (page.length < SNAPSHOT_PAGE_SIZE) break;
- userCursor = page[page.length - 1].id;
+ const page = await fetchPage(
+ cursor === undefined ? undefined : { cursor: { id: cursor }, skip: 1 },
+ );
+ rows.push(...page);
+ if (page.length < SNAPSHOT_PAGE_SIZE) return rows;
+ cursor = page[page.length - 1].id;
}
+}
- const balances: AnalyticsSnapshotFile["balances"] = [];
- let accountCursor: string | undefined;
- for (;;) {
- const page = await prisma.userCreditAccount.findMany({
- orderBy: { id: "asc" },
- select: { balanceMicros: true, id: true, userId: true },
- take: SNAPSHOT_PAGE_SIZE,
- ...(accountCursor === undefined ? {} : { cursor: { id: accountCursor }, skip: 1 }),
- });
- for (const account of page) {
- balances.push({ balanceMicros: account.balanceMicros.toString(), userId: account.userId });
- }
- if (page.length < SNAPSHOT_PAGE_SIZE) break;
- accountCursor = page[page.length - 1].id;
- }
+async function writeSnapshot(): Promise<AnalyticsSnapshotFile> {
+ const users = (
+ await fetchAllPages((cursorArgs) =>
+ prisma.user.findMany({
+ orderBy: { id: "asc" },
+ select: {
+ createdAt: true,
+ email: true,
+ id: true,
+ name: true,
+ onboarding: { select: { referralAnsweredAt: true, referralSources: true } },
+ },
+ take: SNAPSHOT_PAGE_SIZE,
+ ...cursorArgs,
+ }),
+ )
+ ).map((u) => ({
+ createdAt: u.createdAt.toISOString(),
+ email: u.email,
+ id: u.id,
+ name: u.name,
+ ...(u.onboarding?.referralAnsweredAt
+ ? {
+ referralAnsweredAt: u.onboarding.referralAnsweredAt.toISOString(),
+ referralSources: u.onboarding.referralSources,
+ }
+ : {}),
+ }));
+
+ const balances = (
+ await fetchAllPages((cursorArgs) =>
+ prisma.userCreditAccount.findMany({
+ orderBy: { id: "asc" },
+ select: { balanceMicros: true, id: true, userId: true },
+ take: SNAPSHOT_PAGE_SIZE,
+ ...cursorArgs,
+ }),
+ )
+ ).map((account) => ({
+ balanceMicros: account.balanceMicros.toString(),
+ userId: account.userId,
+ }));
+
+ const subscriptions = (
+ await fetchAllPages((cursorArgs) =>
+ prisma.proSubscription.findMany({
+ orderBy: { id: "asc" },
+ select: { cancelAtPeriodEnd: true, id: true, status: true, userId: true },
+ take: SNAPSHOT_PAGE_SIZE,
+ ...cursorArgs,
+ }),
+ )
+ ).map((sub) => ({
+ cancelAtPeriodEnd: sub.cancelAtPeriodEnd,
+ status: sub.status,
+ userId: sub.userId,
+ }));
+
+ const payments = (
+ await fetchAllPages((cursorArgs) =>
+ prisma.userCreditGrant.findMany({
+ orderBy: { id: "asc" },
+ select: { createdAt: true, id: true, originalAmountMicros: true, source: true, userId: true },
+ take: SNAPSHOT_PAGE_SIZE,
+ where: { source: { in: PAID_GRANT_SOURCES } },
+ ...cursorArgs,
+ }),
+ )
+ ).map((grant) => ({
+ amountMicros: grant.originalAmountMicros.toString(),
+ day: utcDayOf(grant.createdAt),
+ source: grant.source,
+ userId: grant.userId,
+ }));
const snapshot: AnalyticsSnapshotFile = {
balances,
generatedAt: new Date().toISOString(),
+ payments,
+ subscriptions,
users,
version: 1,
};
@@ -283,6 +332,7 @@ async function consolidate(
.sort((a, b) => (a.registeredAt < b.registeredAt ? 1 : -1));
const rollup: AnalyticsRollup = {
+ billing: buildBilling(snapshot, days),
days,
generatedAt: new Date().toISOString(),
missing,
@@ -295,6 +345,51 @@ async function consolidate(
return rollup;
}
+// Current subscription state plus the paid-grant revenue record, folded into
+// the counts and per-day sums the dashboard shows. Abandoned checkouts
+// (incomplete statuses) never subscribed, so they count nowhere; past_due and
+// paused sit outside both buckets until Stripe resolves them.
+function buildBilling(
+ snapshot: AnalyticsSnapshotFile,
+ days: string[],
+): NonNullable<AnalyticsRollup["billing"]> {
+ let subscribers = 0;
+ let canceling = 0;
+ let churned = 0;
+ for (const sub of snapshot.subscriptions) {
+ if (isActiveProStatus(sub.status)) {
+ subscribers++;
+ if (sub.cancelAtPeriodEnd) canceling++;
+ } else if (sub.status === "canceled" || sub.status === "unpaid") {
+ churned++;
+ }
+ }
+
+ const dayIndex = new Map(days.map((day, i) => [day, i]));
+ const revenue = days.map(() => ({ proMicros: BigInt(0), topupMicros: BigInt(0) }));
+ let fundedMicros = BigInt(0);
+ for (const payment of snapshot.payments) {
+ const amount = BigInt(payment.amountMicros);
+ fundedMicros += amount;
+ const i = dayIndex.get(payment.day);
+ if (i === undefined) continue;
+ if (payment.source === "pro_subscription") revenue[i].proMicros += amount;
+ else revenue[i].topupMicros += amount;
+ }
+
+ return {
+ canceling,
+ churned,
+ funded: new Set(snapshot.payments.map((payment) => payment.userId)).size,
+ fundedMicros: fundedMicros.toString(),
+ revenue: revenue.map((entry) => ({
+ proMicros: entry.proMicros.toString(),
+ topupMicros: entry.topupMicros.toString(),
+ })),
+ subscribers,
+ };
+}
+
// The snapshot covers every user, so this recomputes the whole referral
// history each run — an answer moved by a replayed onboarding lands on its
// new day with nothing left behind on the old one.
diff --git a/site/src/lib/analytics/schema.ts b/site/src/lib/analytics/schema.ts
index 271b5a0e..32edf1fc 100644
--- a/site/src/lib/analytics/schema.ts
+++ b/site/src/lib/analytics/schema.ts
@@ -76,6 +76,25 @@ export const analyticsSnapshotFileSchema = z.object({
}),
),
balances: z.array(z.object({ userId: z.string(), balanceMicros: z.string() })),
+ // Every Donkey Pro subscription row, whatever its Stripe status.
+ subscriptions: z.array(
+ z.object({
+ userId: z.string(),
+ status: z.string(),
+ cancelAtPeriodEnd: z.boolean(),
+ }),
+ ),
+ // One entry per paid Stripe credit grant — top-ups, auto-reloads, and Pro
+ // monthly allowances. Each grant's amount equals the dollars charged, so
+ // together they are the revenue record.
+ payments: z.array(
+ z.object({
+ userId: z.string(),
+ day: analyticsDaySchema,
+ source: z.string(),
+ amountMicros: z.string(),
+ }),
+ ),
});
export type AnalyticsSnapshotFile = z.infer<typeof analyticsSnapshotFileSchema>;
@@ -108,6 +127,26 @@ export const analyticsRollupSchema = z.object({
),
})
.optional(),
+ // Subscription and revenue rollup from the snapshot's Stripe-backed rows.
+ // Counts are current state at consolidation; revenue aligns with days.
+ // Absent from rollups written before this shipped.
+ billing: z
+ .object({
+ // Pro subscriptions with an active status (active or trialing).
+ subscribers: z.number(),
+ // Active subscriptions set to cancel at period end.
+ canceling: z.number(),
+ // Subscriptions that ended after being live (canceled or unpaid);
+ // abandoned checkouts never count.
+ churned: z.number(),
+ // Users with at least one paid Stripe grant, and their all-time total.
+ funded: z.number(),
+ fundedMicros: z.string(),
+ // Per-day paid charges, aligned with days: Pro allowances vs top-ups
+ // (one-time and auto-reload). BigInt micros as decimal strings.
+ revenue: z.array(z.object({ proMicros: z.string(), topupMicros: z.string() })),
+ })
+ .optional(),
users: z.array(
z.object({
id: z.string(),
@@ -124,3 +163,4 @@ export const analyticsRollupSchema = z.object({
export type AnalyticsRollup = z.infer<typeof analyticsRollupSchema>;
export type AnalyticsRollupUser = AnalyticsRollup["users"][number];
export type AnalyticsReferrals = NonNullable<AnalyticsRollup["referrals"]>;
+export type AnalyticsBilling = NonNullable<AnalyticsRollup["billing"]>;