From 733b8ba9c549bb547d529837429043a10569aba1 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 10 Dec 2023 16:04:52 -0600 Subject: [PATCH] pubkey_stats --> author_stats --- src/db.ts | 6 +++--- src/db/events.ts | 10 +++++----- src/db/migrations/009_add_stats.ts | 4 ++-- src/stats.ts | 22 +++++++++++----------- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/db.ts b/src/db.ts index 6f0397f9..4f1a8d91 100644 --- a/src/db.ts +++ b/src/db.ts @@ -13,11 +13,11 @@ interface DittoDB { users: UserRow; relays: RelayRow; unattached_media: UnattachedMediaRow; - pubkey_stats: PubkeyStatsRow; + author_stats: AuthorStatsRow; event_stats: EventStatsRow; } -interface PubkeyStatsRow { +interface AuthorStatsRow { pubkey: string; followers_count: number; following_count: number; @@ -126,4 +126,4 @@ async function migrate() { await migrate(); -export { db, type DittoDB, type EventRow, type EventStatsRow, type PubkeyStatsRow, type TagRow, type UserRow }; +export { type AuthorStatsRow, db, type DittoDB, type EventRow, type EventStatsRow, type TagRow, type UserRow }; diff --git a/src/db/events.ts b/src/db/events.ts index efefbba7..2a5f2fd7 100644 --- a/src/db/events.ts +++ b/src/db/events.ts @@ -176,11 +176,11 @@ function getFilterQuery(filter: DittoFilter): EventQuery { if (filter.relations?.includes('author_stats')) { query = query - .leftJoin('pubkey_stats', 'pubkey_stats.pubkey', 'events.pubkey') + .leftJoin('author_stats', 'author_stats.pubkey', 'events.pubkey') .select((eb) => [ - eb.fn.coalesce('pubkey_stats.followers_count', eb.val(0)).as('author_stats_followers_count'), - eb.fn.coalesce('pubkey_stats.following_count', eb.val(0)).as('author_stats_following_count'), - eb.fn.coalesce('pubkey_stats.notes_count', eb.val(0)).as('author_stats_notes_count'), + eb.fn.coalesce('author_stats.followers_count', eb.val(0)).as('author_stats_followers_count'), + eb.fn.coalesce('author_stats.following_count', eb.val(0)).as('author_stats_following_count'), + eb.fn.coalesce('author_stats.notes_count', eb.val(0)).as('author_stats_notes_count'), ]); } @@ -210,7 +210,7 @@ function getFiltersQuery(filters: DittoFilter[]) { .reduce((result, query) => result.unionAll(query)); } -type AuthorStats = Omit; +type AuthorStats = Omit; type EventStats = Omit; interface DittoEvent extends Event { diff --git a/src/db/migrations/009_add_stats.ts b/src/db/migrations/009_add_stats.ts index 9655c051..60d9447b 100644 --- a/src/db/migrations/009_add_stats.ts +++ b/src/db/migrations/009_add_stats.ts @@ -2,7 +2,7 @@ import { Kysely } from '@/deps.ts'; export async function up(db: Kysely): Promise { await db.schema - .createTable('pubkey_stats') + .createTable('author_stats') .addColumn('pubkey', 'text', (col) => col.primaryKey()) .addColumn('followers_count', 'integer', (col) => col.notNull().defaultTo(0)) .addColumn('following_count', 'integer', (col) => col.notNull().defaultTo(0)) @@ -19,6 +19,6 @@ export async function up(db: Kysely): Promise { } export async function down(db: Kysely): Promise { - await db.schema.dropTable('pubkey_stats').execute(); + await db.schema.dropTable('author_stats').execute(); await db.schema.dropTable('event_stats').execute(); } diff --git a/src/stats.ts b/src/stats.ts index 05e6be35..e076e148 100644 --- a/src/stats.ts +++ b/src/stats.ts @@ -1,23 +1,23 @@ -import { db, type EventStatsRow, type PubkeyStatsRow } from '@/db.ts'; +import { type AuthorStatsRow, db, type EventStatsRow } from '@/db.ts'; import { Event, findReplyTag } from '@/deps.ts'; -type PubkeyStat = keyof Omit; +type AuthorStat = keyof Omit; type EventStat = keyof Omit; -type PubkeyStatDiff = ['pubkey_stats', pubkey: string, stat: PubkeyStat, diff: number]; +type AuthorStatDiff = ['author_stats', pubkey: string, stat: AuthorStat, diff: number]; type EventStatDiff = ['event_stats', eventId: string, stat: EventStat, diff: number]; -type StatDiff = PubkeyStatDiff | EventStatDiff; +type StatDiff = AuthorStatDiff | EventStatDiff; /** Store stats for the event in LMDB. */ async function updateStats(event: Event) { const statDiffs = getStatsDiff(event); if (!statDiffs.length) return; - const pubkeyDiffs = statDiffs.filter(([table]) => table === 'pubkey_stats') as PubkeyStatDiff[]; + const pubkeyDiffs = statDiffs.filter(([table]) => table === 'author_stats') as AuthorStatDiff[]; const eventDiffs = statDiffs.filter(([table]) => table === 'event_stats') as EventStatDiff[]; await Promise.all([ - pubkeyDiffs.length ? pubkeyStatsQuery(pubkeyDiffs).execute() : undefined, + pubkeyDiffs.length ? authorStatsQuery(pubkeyDiffs).execute() : undefined, eventDiffs.length ? eventStatsQuery(eventDiffs).execute() : undefined, ]); } @@ -31,7 +31,7 @@ function getStatsDiff(event: Event): StatDiff[] { switch (event.kind) { case 1: - statDiffs.push(['pubkey_stats', event.pubkey, 'notes_count', 1]); + statDiffs.push(['author_stats', event.pubkey, 'notes_count', 1]); if (inReplyToId) { statDiffs.push(['event_stats', inReplyToId, 'replies_count', 1]); } @@ -50,9 +50,9 @@ function getStatsDiff(event: Event): StatDiff[] { return statDiffs; } -function pubkeyStatsQuery(diffs: PubkeyStatDiff[]) { - const values: PubkeyStatsRow[] = diffs.map(([_, pubkey, stat, diff]) => { - const row: PubkeyStatsRow = { +function authorStatsQuery(diffs: AuthorStatDiff[]) { + const values: AuthorStatsRow[] = diffs.map(([_, pubkey, stat, diff]) => { + const row: AuthorStatsRow = { pubkey, followers_count: 0, following_count: 0, @@ -62,7 +62,7 @@ function pubkeyStatsQuery(diffs: PubkeyStatDiff[]) { return row; }); - return db.insertInto('pubkey_stats') + return db.insertInto('author_stats') .values(values) .onConflict((oc) => oc