|
| 1 | +import fs from 'node:fs/promises'; |
| 2 | + |
| 3 | +import type { BigQuery, TableField } from '@google-cloud/bigquery'; |
| 4 | +import type { Pool } from 'pg'; |
| 5 | + |
| 6 | +/** |
| 7 | + * The account export, which the event export Job (db/export-events.ts) runs |
| 8 | + * before the event export: copies the Postgres view `account_daily` |
| 9 | + * (migration 0020) into the BigQuery table `account_daily`, replacing the |
| 10 | + * whole table in one load job, so a failed run leaves the previous table. |
| 11 | + * The view counts new accounts per UTC day and organisation, for every day |
| 12 | + * from the first account's through yesterday's, and the rows hold no email |
| 13 | + * address or user id. |
| 14 | + */ |
| 15 | + |
| 16 | +/** One `account_daily` row. */ |
| 17 | +export type AccountRow = { |
| 18 | + // UTC, 2026-10-20 |
| 19 | + day: string; |
| 20 | + // Null on a day without new accounts, its one row |
| 21 | + organisation: string | null; |
| 22 | + accounts: number; |
| 23 | +}; |
| 24 | + |
| 25 | +export const ACCOUNT_SCHEMA: TableField[] = [ |
| 26 | + { |
| 27 | + name: 'day', |
| 28 | + type: 'DATE', |
| 29 | + mode: 'REQUIRED', |
| 30 | + description: 'The UTC day the accounts were created', |
| 31 | + }, |
| 32 | + { |
| 33 | + name: 'organisation', |
| 34 | + type: 'STRING', |
| 35 | + description: |
| 36 | + 'The lower-cased email domain once it holds at least 3 accounts, ' + |
| 37 | + '`Personal email` for a webmail domain, `Other organisations` for any other. ' + |
| 38 | + 'NULL on a day without new accounts, whose one row has 0 accounts', |
| 39 | + }, |
| 40 | + { |
| 41 | + name: 'accounts', |
| 42 | + type: 'INT64', |
| 43 | + mode: 'REQUIRED', |
| 44 | + description: 'The accounts created that day in that organisation, 0 on a day without any', |
| 45 | + }, |
| 46 | +]; |
| 47 | + |
| 48 | +/** Every row of the view, oldest day first. */ |
| 49 | +export const readAccountDaily = async (pool: Pool): Promise<AccountRow[]> => { |
| 50 | + const { rows } = await pool.query<AccountRow>( |
| 51 | + `SELECT to_char(day, 'YYYY-MM-DD') AS day, organisation, accounts |
| 52 | + FROM account_daily |
| 53 | + ORDER BY day, organisation`, |
| 54 | + ); |
| 55 | + |
| 56 | + return rows; |
| 57 | +}; |
| 58 | + |
| 59 | +/** Replaces the BigQuery table `account_daily` with the rows in `file`, one JSON object a line. */ |
| 60 | +export const bigQueryAccountTable = |
| 61 | + ({ datasetId, bigquery }: { datasetId: string; bigquery: BigQuery }) => |
| 62 | + async (file: string): Promise<void> => { |
| 63 | + // Creates the table when it does not exist. Resolves once the load |
| 64 | + // job is done, and throws if it failed. |
| 65 | + await bigquery |
| 66 | + .dataset(datasetId) |
| 67 | + .table('account_daily') |
| 68 | + .load(file, { |
| 69 | + sourceFormat: 'NEWLINE_DELIMITED_JSON', |
| 70 | + writeDisposition: 'WRITE_TRUNCATE', |
| 71 | + schema: { fields: ACCOUNT_SCHEMA }, |
| 72 | + }); |
| 73 | + }; |
| 74 | + |
| 75 | +/** |
| 76 | + * One account export run. Writes every row to `scratchFile`, then replaces |
| 77 | + * the table with it. The view holds a row for every day since the first |
| 78 | + * account's, so a read with no rows is a fault (a wrong database, an |
| 79 | + * emptied table) unless every account is from today, and rejects with the |
| 80 | + * table kept. |
| 81 | + */ |
| 82 | +export const runAccountExport = async ({ |
| 83 | + read, |
| 84 | + replace, |
| 85 | + scratchFile, |
| 86 | + log, |
| 87 | +}: { |
| 88 | + read: () => Promise<AccountRow[]>; |
| 89 | + replace: (file: string) => Promise<void>; |
| 90 | + scratchFile: string; |
| 91 | + log: (line: string) => void; |
| 92 | +}): Promise<void> => { |
| 93 | + const rows = await read(); |
| 94 | + |
| 95 | + if (rows.length === 0) { |
| 96 | + throw new Error('account export: account_daily read no rows, table kept'); |
| 97 | + } |
| 98 | + |
| 99 | + try { |
| 100 | + await fs.writeFile(scratchFile, rows.map((row) => `${JSON.stringify(row)}\n`).join('')); |
| 101 | + await replace(scratchFile); |
| 102 | + } finally { |
| 103 | + await fs.rm(scratchFile, { force: true }); |
| 104 | + } |
| 105 | + |
| 106 | + log(`account_daily: ${rows.length} rows loaded`); |
| 107 | +}; |
0 commit comments