Add a script to recompute the streak of all authors

This commit is contained in:
Alex Gleason 2025-02-06 15:56:49 -06:00
parent 080c34d13f
commit b480947c4d
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
3 changed files with 50 additions and 0 deletions

View file

@ -23,6 +23,7 @@
"clean:deps": "deno cache --reload src/app.ts", "clean:deps": "deno cache --reload src/app.ts",
"db:populate-search": "deno run -A --env-file --deny-read=.env scripts/db-populate-search.ts", "db:populate-search": "deno run -A --env-file --deny-read=.env scripts/db-populate-search.ts",
"db:populate-extensions": "deno run -A --env-file --deny-read=.env scripts/db-populate-extensions.ts", "db:populate-extensions": "deno run -A --env-file --deny-read=.env scripts/db-populate-extensions.ts",
"db:streak:recompute": "deno run -A --env-file --deny-read=.env scripts/db-streak-recompute.ts",
"vapid": "deno run scripts/vapid.ts" "vapid": "deno run scripts/vapid.ts"
}, },
"unstable": [ "unstable": [

View file

@ -0,0 +1,48 @@
import { Storages } from '@/storages.ts';
const kysely = await Storages.kysely();
const statsQuery = kysely.selectFrom('author_stats').select('pubkey');
for await (const { pubkey } of statsQuery.stream(10)) {
const eventsQuery = kysely
.selectFrom('nostr_events')
.select('created_at')
.where('pubkey', '=', pubkey)
.where('kind', 'in', [1, 20, 1111, 30023])
.orderBy('nostr_events.created_at', 'desc')
.orderBy('nostr_events.id', 'asc');
let end: number | null = null;
let start: number | null = null;
for await (const { created_at } of eventsQuery.stream(20)) {
const createdAt = Number(created_at);
if (!end) {
const now = Math.floor(Date.now() / 1000);
if (now - createdAt > 86400) {
break; // streak broken
}
end = createdAt;
}
if (start && (start - createdAt > 86400)) {
break; // streak broken
}
start = createdAt;
}
await kysely
.updateTable('author_stats')
.set({
streak_end: end,
streak_start: start,
})
.where('pubkey', '=', pubkey)
.execute();
}
Deno.exit();

View file

@ -20,6 +20,7 @@ export async function updateStats({ event, kysely, store, x = 1 }: UpdateStatsOp
case 1: case 1:
case 20: case 20:
case 1111: case 1111:
case 30023:
return handleEvent1(kysely, event, x); return handleEvent1(kysely, event, x);
case 3: case 3:
return handleEvent3(kysely, event, x, store); return handleEvent3(kysely, event, x, store);