Skip to content

Commit cb39ae2

Browse files
committed
feat: update module stats to new server
1 parent 7d6a705 commit cb39ae2

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

usage-statistics/src/main.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,16 @@ if (!sourceUrl) {
2020
throw new Error('SOURCE_DATABASE_URL environment variable is required')
2121
}
2222

23-
const oldSourceUrl = process.env.OLD_SOURCE_DATABASE_URL
24-
if (!oldSourceUrl) {
25-
throw new Error('OLD_SOURCE_DATABASE_URL environment variable is required')
26-
}
27-
2823
const adapter = new PrismaMariaDb(connectionString)
2924
const prismaDest = new PrismaClient({
3025
adapter,
3126
})
3227

3328
const srcDb = await mariadb.createConnection(sourceUrl)
34-
const oldDb = await mariadb.createConnection(oldSourceUrl)
3529

3630
const store: AppStore = {
3731
prismaDest,
3832
srcDb,
39-
oldDb,
4033
}
4134

4235
try {
@@ -52,7 +45,6 @@ try {
5245

5346
console.log('all done!')
5447
} finally {
55-
await oldDb.end()
5648
await srcDb.end()
5749
await prismaDest.$disconnect()
5850
}

usage-statistics/src/modules-data.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,27 @@ import type { AppStore } from './types.js'
55
import type { CompanionModules, StatsSamplePeriod } from './prisma/client.js'
66

77
function formatQuery(interval: string) {
8-
return `SELECT count(distinct uuid) users, module FROM \`module\` where ts >= date_sub(CURRENT_DATE, interval ${interval}) group by module;`
8+
return `
9+
SELECT
10+
km.module_name as module,
11+
COUNT(DISTINCT muls.user_id) as users
12+
FROM KnownModule km
13+
INNER JOIN ModuleUserLastSeen muls ON km.id = muls.module_id
14+
WHERE km.module_type = 'CONNECTION'
15+
AND km.module_version = ''
16+
AND muls.last_seen >= DATE_SUB(CURRENT_DATE, INTERVAL ${interval})
17+
GROUP BY km.module_name
18+
`
919
}
1020

1121
export async function runModules(store: AppStore): Promise<void> {
12-
const allModules: any[] = await store.oldDb.query('SELECT module FROM `module` group by module;')
22+
const allModules: any[] = await store.srcDb.query(`
23+
SELECT module_name as module
24+
FROM KnownModule
25+
WHERE module_type = 'CONNECTION'
26+
AND module_version = ''
27+
GROUP BY module_name
28+
`)
1329

1430
function convertModules(stats: any[], type: StatsSamplePeriod): Omit<CompanionModules, 'id' | 'ts'>[] {
1531
const statsMap = new Map<string, number>()
@@ -41,16 +57,16 @@ export async function runModules(store: AppStore): Promise<void> {
4157
}
4258

4359
await Promise.all([
44-
runQuery('Platforms 30day', async () => {
45-
const rows = await store.oldDb.query(formatQuery('30 day'))
60+
runQuery('Modules 30day', async () => {
61+
const rows = await store.srcDb.query(formatQuery('30 day'))
4662
await writeData(rows, 'day30' as '30day')
4763
}),
48-
runQuery('Platforms 7day', async () => {
49-
const rows = await store.oldDb.query(formatQuery('7 day'))
64+
runQuery('Modules 7day', async () => {
65+
const rows = await store.srcDb.query(formatQuery('7 day'))
5066
await writeData(rows, 'day7' as '7day')
5167
}),
52-
runQuery('Platforms 1day', async () => {
53-
const rows = await store.oldDb.query(formatQuery('24 hour'))
68+
runQuery('Modules 1day', async () => {
69+
const rows = await store.srcDb.query(formatQuery('24 hour'))
5470
await writeData(rows, 'day1' as '1day')
5571
}),
5672
])

usage-statistics/src/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ import type mariadb from 'mariadb'
44
export interface AppStore {
55
prismaDest: PrismaClient
66
srcDb: mariadb.Connection
7-
oldDb: mariadb.Connection
87
}

0 commit comments

Comments
 (0)