Skip to content

Open source CapCut alternative.

TypeScript

feat(site): the scan snapshots cloud storage onto the outreach row

David Le36a220e2d2ac
diff --git a/site/prisma/Marketing.prisma b/site/prisma/Marketing.prisma
index 8a7ade41..1f5c7861 100644
--- a/site/prisma/Marketing.prisma
+++ b/site/prisma/Marketing.prisma
@@ -23,6 +23,8 @@ model UserOutreach {
// The scan's snapshot, so the list renders from this table alone.
spentMicros BigInt @default(0)
balanceMicros BigInt @default(0)
+ // Cloud media the account was holding when the scan ran.
+ storageBytes BigInt @default(0)
lastActiveAt DateTime?
// When the balance last reached zero; null for an account still in credit.
ranOutAt DateTime?
diff --git a/site/src/app/api/marketing/outreach/route.ts b/site/src/app/api/marketing/outreach/route.ts
index 19584041..fe98066f 100644
--- a/site/src/app/api/marketing/outreach/route.ts
+++ b/site/src/app/api/marketing/outreach/route.ts
@@ -51,6 +51,7 @@ const rowSelect = {
sentCount: true,
spentMicros: true,
status: true,
+ storageBytes: true,
user: { select: { createdAt: true, email: true, id: true, name: true } },
} as const;
@@ -65,24 +66,13 @@ type OutreachRow = {
sentCount: number;
spentMicros: bigint;
status: string;
+ storageBytes: bigint;
user: { createdAt: Date; email: string; id: string; name: string };
};
-// Cloud media held by each of these accounts. Storage is read live: an upload
-// is the thing being checked, and it moves between scans. Absent row means
-// nothing stored.
-async function storageByUser(userIds: string[]): Promise<Map<string, bigint>> {
- if (userIds.length === 0) return new Map();
- const rows = await prisma.cutStorageUsage.findMany({
- select: { bytes: true, userId: true },
- where: { userId: { in: userIds } },
- });
- return new Map(rows.map((row) => [row.userId, row.bytes]));
-}
-
// Micros and byte counts are BigInt and dates are Date; the client wants
// neither.
-function serialize(row: OutreachRow, storage: Map<string, bigint>) {
+function serialize(row: OutreachRow) {
return {
balance: creditMicrosToString(row.balanceMicros),
email: row.user.email,
@@ -97,18 +87,13 @@ function serialize(row: OutreachRow, storage: Map<string, bigint>) {
signedUpAt: row.user.createdAt.toISOString(),
spent: creditMicrosToString(row.spentMicros),
status: row.status,
- storageBytes: (storage.get(row.user.id) ?? BigInt(0)).toString(),
+ storageBytes: row.storageBytes.toString(),
userId: row.user.id,
};
}
-// One row and the storage that goes with it.
-async function serializeOne(row: OutreachRow) {
- return serialize(row, await storageByUser([row.user.id]));
-}
-
-// The list is whatever the nightly outreach-scan job wrote, plus each
-// account's cloud bytes; no credit data is touched here.
+// The list is whatever the nightly outreach-scan job wrote, so this reads one
+// table and touches no credit data.
export const GET = withSuperUser(async (request) => {
const params = new URL(request.url).searchParams;
const parsed = listQuerySchema.safeParse({
@@ -156,11 +141,7 @@ export const GET = withSuperUser(async (request) => {
counts[group.status as keyof typeof counts] = group._count;
}
}
- const storage = await storageByUser(rows.map((row) => row.user.id));
- return NextResponse.json({
- counts,
- rows: rows.map((row) => serialize(row, storage)),
- });
+ return NextResponse.json({ counts, rows: rows.map(serialize) });
}
// Each list is about a different moment: when they last used the product,
@@ -180,8 +161,7 @@ export const GET = withSuperUser(async (request) => {
where: { campaign: CREDIT_SPENDERS_CAMPAIGN, status },
});
- const storage = await storageByUser(rows.map((row) => row.user.id));
- return NextResponse.json({ rows: rows.map((row) => serialize(row, storage)) });
+ return NextResponse.json({ rows: rows.map(serialize) });
});
export const POST = withSuperUser(async (request) => {
@@ -211,7 +191,6 @@ export const POST = withSuperUser(async (request) => {
const actorUserId = request.donkey.userId;
if (parsed.data.action === "send") {
- const storage = await storageByUser([outreach.user.id]);
try {
await sendOutreachEmail({
attempt: outreach.sentCount + 1,
@@ -227,7 +206,7 @@ export const POST = withSuperUser(async (request) => {
firstName: outreach.user.name.trim().split(/\s+/)[0] || outreach.user.name,
name: outreach.user.name,
spent: creditMicrosToString(outreach.spentMicros),
- storage: formatBytes(Number(storage.get(outreach.user.id) ?? BigInt(0))),
+ storage: formatBytes(Number(outreach.storageBytes)),
},
});
} catch (error) {
@@ -250,7 +229,7 @@ export const POST = withSuperUser(async (request) => {
select: rowSelect,
where: { id: outreach.id },
});
- return NextResponse.json({ row: serialize(row, storage) });
+ return NextResponse.json({ row: serialize(row) });
}
const data =
@@ -265,5 +244,5 @@ export const POST = withSuperUser(async (request) => {
select: rowSelect,
where: { id: outreach.id },
});
- return NextResponse.json({ row: await serializeOne(row) });
+ return NextResponse.json({ row: serialize(row) });
});
diff --git a/site/src/app/su/outreach/page.tsx b/site/src/app/su/outreach/page.tsx
index 5d4ceaa6..481aa993 100644
--- a/site/src/app/su/outreach/page.tsx
+++ b/site/src/app/su/outreach/page.tsx
@@ -73,9 +73,9 @@ function ago(iso: string | null): string {
return `${Math.floor(mins / (60 * 24))}d ago`;
}
-// The numbers the scan wrote plus the cloud bytes read live; the badges say
-// what they mean without a second read. Storage shows only when there is any,
-// so a row with the badge is someone with media parked in the cloud.
+// The list carries the numbers the scan wrote; the badges say what they mean
+// without a second read. Storage shows only when there is any, so a row with
+// the badge is someone with media parked in the cloud.
function RowBadges({ row, group }: { row: OutreachRow; group?: string }) {
const broke = Number(row.balance) <= 0;
const stored = Number(row.storageBytes);
diff --git a/site/src/lib/jobs/outreach-scan.ts b/site/src/lib/jobs/outreach-scan.ts
index 67216438..eb379de5 100644
--- a/site/src/lib/jobs/outreach-scan.ts
+++ b/site/src/lib/jobs/outreach-scan.ts
@@ -108,24 +108,19 @@ export const outreachScanJob = defineJob(z.object({}).strict(), async () => {
where: { ...marketable, id: { in: cloudIds } },
})
: [];
- // Working in the cloud is not the same as keeping anything there; the list
- // wants the people holding media.
- const holding =
- cloudCandidates.length > 0
- ? await prisma.cutStorageUsage.findMany({
- select: { userId: true },
- where: {
- bytes: { gt: BigInt(0) },
- userId: { in: cloudCandidates.map((user) => user.id) },
- },
- })
- : [];
- const holdingIds = new Set(holding.map((row) => row.userId));
+ // What everyone in view is holding. It does two jobs: the number the row
+ // carries, and the line between working in the cloud and keeping something
+ // there — the list wants the people holding media.
+ const stored = await prisma.cutStorageUsage.findMany({
+ select: { bytes: true, userId: true },
+ where: { userId: { in: [...spenderIds, ...cloudCandidates.map((user) => user.id)] } },
+ });
+ const storedBy = new Map(stored.map((row) => [row.userId, row.bytes]));
const accounts = [
...spenders,
...cloudCandidates
- .filter((user) => holdingIds.has(user.id))
+ .filter((user) => (storedBy.get(user.id) ?? BigInt(0)) > BigInt(0))
.map((user) => ({
balanceMicros: user.creditAccount?.balanceMicros ?? zeroCreditMicros,
lifetimeChargedMicros: user.creditAccount?.lifetimeChargedMicros ?? zeroCreditMicros,
@@ -175,6 +170,7 @@ export const outreachScanJob = defineJob(z.object({}).strict(), async () => {
ranOutAt: ranOutBy.get(account.userId) ?? null,
scannedAt: startedAt,
spentMicros: account.lifetimeChargedMicros,
+ storageBytes: storedBy.get(account.userId) ?? BigInt(0),
};
const prior = existingBy.get(account.userId);
// A contacted row rests for OUTREACH_RECONTACT_DAYS, then comes back
diff --git a/site/src/queries/outreach.ts b/site/src/queries/outreach.ts
index 821e66f3..277d1018 100644
--- a/site/src/queries/outreach.ts
+++ b/site/src/queries/outreach.ts
@@ -21,8 +21,8 @@ export type OutreachRow = {
// USD strings, as the credits API returns them everywhere else.
spent: string;
balance: string;
- // Cloud media the account is holding, in bytes. A BigInt on the server, so
- // it travels as a decimal string.
+ // Cloud media the account held when the scan ran, in bytes. A BigInt on the
+ // server, so it travels as a decimal string.
storageBytes: string;
lastActiveAt: string | null;
ranOutAt: string | null;