|
| 1 | +import { DRY_RUN, runQuery } from './util.js' |
| 2 | +import { writeFile } from 'node:fs/promises' |
| 3 | +import path from 'node:path' |
| 4 | +import type { AppStore } from './types.js' |
| 5 | +import type { CompanionSurfaceDailyCounts, CompanionSurfaceTotalSeen, StatsSamplePeriod } from './prisma/client.js' |
| 6 | + |
| 7 | +export async function runSurfaceCounts(store: AppStore): Promise<void> { |
| 8 | + async function writeData(stats: any[], type: StatsSamplePeriod) { |
| 9 | + const data = stats.map( |
| 10 | + (m) => |
| 11 | + ({ |
| 12 | + type, |
| 13 | + description: m.surface_description?.slice(0, 128), // Trim to fit DB field |
| 14 | + count: Number(m.surfaces) || 0, |
| 15 | + }) satisfies Omit<CompanionSurfaceDailyCounts, 'id' | 'ts'> |
| 16 | + ) |
| 17 | + |
| 18 | + if (DRY_RUN) { |
| 19 | + await writeFile( |
| 20 | + path.join(import.meta.dirname, `../dry-run/surface-daily-counts-${type}.json`), |
| 21 | + JSON.stringify(data, null, 2) |
| 22 | + ) |
| 23 | + } else { |
| 24 | + const res = await store.prismaDest.companionSurfaceDailyCounts.createMany({ data }) |
| 25 | + console.log(`Inserted ${res.count} records for surface daily counts ${type}`) |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + function formatQuery(interval: string) { |
| 30 | + return `SELECT count(distinct id) surfaces, surface_description FROM \`SurfaceUserLastSeen\` where last_seen >= date_sub(CURRENT_DATE, interval ${interval}) group by surface_description;` |
| 31 | + } |
| 32 | + |
| 33 | + await Promise.all([ |
| 34 | + runQuery('Surfaces daily 30day', async () => { |
| 35 | + const rows = await store.srcDb.query(formatQuery('30 day')) |
| 36 | + await writeData(rows, 'day30' as '30day') |
| 37 | + }), |
| 38 | + runQuery('Surfaces daily 7day', async () => { |
| 39 | + const rows = await store.srcDb.query(formatQuery('7 day')) |
| 40 | + await writeData(rows, 'day7' as '7day') |
| 41 | + }), |
| 42 | + runQuery('Surfaces daily 1day', async () => { |
| 43 | + const rows = await store.srcDb.query(formatQuery('24 hour')) |
| 44 | + await writeData(rows, 'day1' as '1day') |
| 45 | + }), |
| 46 | + ]) |
| 47 | +} |
| 48 | + |
| 49 | +export async function runSurfaceTotals(store: AppStore): Promise<void> { |
| 50 | + async function writeData(stats: any[]) { |
| 51 | + const data = stats.map( |
| 52 | + (m) => |
| 53 | + ({ |
| 54 | + description: m.surface_description?.slice(0, 128), // Trim to fit DB field |
| 55 | + count: Number(m.surfaces) || 0, |
| 56 | + }) satisfies Omit<CompanionSurfaceTotalSeen, 'id' | 'ts'> |
| 57 | + ) |
| 58 | + |
| 59 | + if (DRY_RUN) { |
| 60 | + await writeFile( |
| 61 | + path.join(import.meta.dirname, `../dry-run/surface-total-counts.json`), |
| 62 | + JSON.stringify(data, null, 2) |
| 63 | + ) |
| 64 | + } else { |
| 65 | + const res = await store.prismaDest.companionSurfaceTotalSeen.createMany({ data }) |
| 66 | + console.log(`Inserted ${res.count} records for surface totals`) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + const query = `SELECT count(distinct id) surfaces, surface_description FROM \`SurfaceUserLastSeen\` group by surface_description;` |
| 71 | + |
| 72 | + await runQuery('Surfaces total seen', async () => { |
| 73 | + const rows = await store.srcDb.query(query) |
| 74 | + await writeData(rows) |
| 75 | + }) |
| 76 | +} |
0 commit comments