From dc87d3599dd3d0cc0442872c7061180ac15557ed Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 14 May 2024 15:55:19 -0500 Subject: [PATCH 1/2] Add stats:recompute script --- deno.json | 3 ++- scripts/stats-recompute.ts | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 scripts/stats-recompute.ts diff --git a/deno.json b/deno.json index 0225510..7cd56f0 100644 --- a/deno.json +++ b/deno.json @@ -10,7 +10,8 @@ "check": "deno check src/server.ts", "nsec": "deno run scripts/nsec.ts", "admin:event": "deno run -A scripts/admin-event.ts", - "admin:role": "deno run -A scripts/admin-role.ts" + "admin:role": "deno run -A scripts/admin-role.ts", + "stats:recompute": "deno run -A scripts/stats-recompute.ts" }, "unstable": ["ffi", "kv"], "exclude": ["./public"], diff --git a/scripts/stats-recompute.ts b/scripts/stats-recompute.ts new file mode 100644 index 0000000..9d204c7 --- /dev/null +++ b/scripts/stats-recompute.ts @@ -0,0 +1,36 @@ +import { nip19 } from 'nostr-tools'; + +import { db } from '@/db.ts'; +import { DittoTables } from '@/db/DittoTables.ts'; +import { Storages } from '@/storages.ts'; + +let pubkey: string; +try { + const result = nip19.decode(Deno.args[0]); + if (result.type === 'npub') { + pubkey = result.data; + } else { + throw new Error('Invalid npub'); + } +} catch { + console.error('Invalid npub'); + Deno.exit(1); +} + +const [followList] = await Storages.db.query([{ kinds: [3], authors: [pubkey], limit: 1 }]); + +const authorStats: DittoTables['author_stats'] = { + pubkey, + followers_count: (await Storages.db.count([{ kinds: [3], '#p': [pubkey] }])).count, + following_count: followList?.tags.filter(([name]) => name === 'p')?.length ?? 0, + notes_count: (await Storages.db.count([{ kinds: [1], authors: [pubkey] }])).count, +}; + +await db.insertInto('author_stats') + .values(authorStats) + .onConflict((oc) => + oc + .column('pubkey') + .doUpdateSet(authorStats) + ) + .execute(); From 99a6c668c8844c30500f437986857f7a386ca3c3 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 14 May 2024 18:50:05 -0500 Subject: [PATCH 2/2] Update recompute script --- scripts/stats-recompute.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/scripts/stats-recompute.ts b/scripts/stats-recompute.ts index 9d204c7..dcb0bc0 100644 --- a/scripts/stats-recompute.ts +++ b/scripts/stats-recompute.ts @@ -1,6 +1,6 @@ import { nip19 } from 'nostr-tools'; -import { db } from '@/db.ts'; +import { DittoDB } from '@/db/DittoDB.ts'; import { DittoTables } from '@/db/DittoTables.ts'; import { Storages } from '@/storages.ts'; @@ -17,16 +17,19 @@ try { Deno.exit(1); } -const [followList] = await Storages.db.query([{ kinds: [3], authors: [pubkey], limit: 1 }]); +const store = await Storages.db(); +const kysely = await DittoDB.getInstance(); + +const [followList] = await store.query([{ kinds: [3], authors: [pubkey], limit: 1 }]); const authorStats: DittoTables['author_stats'] = { pubkey, - followers_count: (await Storages.db.count([{ kinds: [3], '#p': [pubkey] }])).count, + followers_count: (await store.count([{ kinds: [3], '#p': [pubkey] }])).count, following_count: followList?.tags.filter(([name]) => name === 'p')?.length ?? 0, - notes_count: (await Storages.db.count([{ kinds: [1], authors: [pubkey] }])).count, + notes_count: (await store.count([{ kinds: [1], authors: [pubkey] }])).count, }; -await db.insertInto('author_stats') +await kysely.insertInto('author_stats') .values(authorStats) .onConflict((oc) => oc