Skip to content

Commit 7d6a705

Browse files
committed
feat: collect daily surface counts
1 parent d991282 commit 7d6a705

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

usage-statistics/prisma/schema.prisma

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,28 @@ model CompanionUsers {
5858
@@map("companion_users")
5959
}
6060

61+
// Count number of unique user+surface pairs seen in the period
62+
model CompanionSurfaceDailyCounts {
63+
id Int @id @default(autoincrement())
64+
type StatsSamplePeriod
65+
description String @db.VarChar(128)
66+
count Int
67+
ts DateTime @default(now()) @db.DateTime(0)
68+
69+
@@index([ts], map: "ts")
70+
@@index([type], map: "type")
71+
}
72+
73+
// Count total number of seen surfaces
74+
model CompanionSurfaceTotalSeen {
75+
id Int @id @default(autoincrement())
76+
description String @db.VarChar(128)
77+
count Int
78+
ts DateTime @default(now()) @db.DateTime(0)
79+
80+
@@index([ts], map: "ts")
81+
}
82+
6183
enum StatsSamplePeriod {
6284
day1 @map("1day")
6385
day7 @map("7day")

usage-statistics/src/main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { runPlatforms } from './platforms.js'
66
import { runPlatformStats } from './platform-stats.js'
77
import type { AppStore } from './types.js'
88
import mariadb from 'mariadb'
9+
import { runSurfaceCounts, runSurfaceTotals } from './surfaces.js'
910

1011
console.log('hello world')
1112

@@ -45,6 +46,8 @@ try {
4546
runModules(store),
4647
runPlatforms(store),
4748
runPlatformStats(store),
49+
runSurfaceCounts(store),
50+
runSurfaceTotals(store),
4851
])
4952

5053
console.log('all done!')

usage-statistics/src/surfaces.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)