Merge branch 'stats-fix' into 'main'

stats: fix kysely screaming that we're awaiting a builder instance

See merge request soapbox-pub/ditto!259
This commit is contained in:
Alex Gleason 2024-05-15 15:12:53 +00:00
commit e2cf9457cf
1 changed files with 7 additions and 7 deletions

View File

@ -1,6 +1,6 @@
import { NKinds, NostrEvent } from '@nostrify/nostrify'; import { NKinds, NostrEvent } from '@nostrify/nostrify';
import Debug from '@soapbox/stickynotes/debug'; import Debug from '@soapbox/stickynotes/debug';
import { InsertQueryBuilder } from 'kysely'; import { InsertQueryBuilder, Kysely } from 'kysely';
import { DittoDB } from '@/db/DittoDB.ts'; import { DittoDB } from '@/db/DittoDB.ts';
import { DittoTables } from '@/db/DittoTables.ts'; import { DittoTables } from '@/db/DittoTables.ts';
@ -37,8 +37,10 @@ async function updateStats(event: NostrEvent) {
debug(JSON.stringify({ id: event.id, pubkey: event.pubkey, kind: event.kind, tags: event.tags, statDiffs })); debug(JSON.stringify({ id: event.id, pubkey: event.pubkey, kind: event.kind, tags: event.tags, statDiffs }));
} }
if (pubkeyDiffs.length) queries.push(await authorStatsQuery(pubkeyDiffs)); const kysely = await DittoDB.getInstance();
if (eventDiffs.length) queries.push(await eventStatsQuery(eventDiffs));
if (pubkeyDiffs.length) queries.push(authorStatsQuery(kysely, pubkeyDiffs));
if (eventDiffs.length) queries.push(eventStatsQuery(kysely, eventDiffs));
if (queries.length) { if (queries.length) {
await Promise.all(queries.map((query) => query.execute())); await Promise.all(queries.map((query) => query.execute()));
@ -102,7 +104,7 @@ async function getStatsDiff(event: NostrEvent, prev: NostrEvent | undefined): Pr
} }
/** Create an author stats query from the list of diffs. */ /** Create an author stats query from the list of diffs. */
async function authorStatsQuery(diffs: AuthorStatDiff[]) { function authorStatsQuery(kysely: Kysely<DittoTables>, diffs: AuthorStatDiff[]) {
const values: DittoTables['author_stats'][] = diffs.map(([_, pubkey, stat, diff]) => { const values: DittoTables['author_stats'][] = diffs.map(([_, pubkey, stat, diff]) => {
const row: DittoTables['author_stats'] = { const row: DittoTables['author_stats'] = {
pubkey, pubkey,
@ -114,7 +116,6 @@ async function authorStatsQuery(diffs: AuthorStatDiff[]) {
return row; return row;
}); });
const kysely = await DittoDB.getInstance();
return kysely.insertInto('author_stats') return kysely.insertInto('author_stats')
.values(values) .values(values)
.onConflict((oc) => .onConflict((oc) =>
@ -129,7 +130,7 @@ async function authorStatsQuery(diffs: AuthorStatDiff[]) {
} }
/** Create an event stats query from the list of diffs. */ /** Create an event stats query from the list of diffs. */
async function eventStatsQuery(diffs: EventStatDiff[]) { function eventStatsQuery(kysely: Kysely<DittoTables>, diffs: EventStatDiff[]) {
const values: DittoTables['event_stats'][] = diffs.map(([_, event_id, stat, diff]) => { const values: DittoTables['event_stats'][] = diffs.map(([_, event_id, stat, diff]) => {
const row: DittoTables['event_stats'] = { const row: DittoTables['event_stats'] = {
event_id, event_id,
@ -141,7 +142,6 @@ async function eventStatsQuery(diffs: EventStatDiff[]) {
return row; return row;
}); });
const kysely = await DittoDB.getInstance();
return kysely.insertInto('event_stats') return kysely.insertInto('event_stats')
.values(values) .values(values)
.onConflict((oc) => .onConflict((oc) =>