stats: fix queries getting stuck

This commit is contained in:
Alex Gleason 2023-12-10 15:33:01 -06:00
parent 6a92c5135d
commit a48c1e51e1
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 13 additions and 11 deletions

View File

@ -171,7 +171,7 @@ function getFilterQuery(filter: DittoFilter): EventQuery {
return exp return exp
.orderBy('created_at', 'desc') .orderBy('created_at', 'desc')
.groupBy('pubkey') .groupBy('events.pubkey')
.as('authors'); .as('authors');
}, },
(join) => join.onRef('authors.pubkey', '=', 'events.pubkey'), (join) => join.onRef('authors.pubkey', '=', 'events.pubkey'),
@ -184,9 +184,13 @@ function getFilterQuery(filter: DittoFilter): EventQuery {
'authors.tags as author_tags', 'authors.tags as author_tags',
'authors.created_at as author_created_at', 'authors.created_at as author_created_at',
'authors.sig as author_sig', 'authors.sig as author_sig',
...(filter.relations?.includes('stats')
? [
'authors.author_stats_followers_count', 'authors.author_stats_followers_count',
'authors.author_stats_following_count', 'authors.author_stats_following_count',
'authors.author_stats_notes_count', 'authors.author_stats_notes_count',
] as const
: []),
]); ]);
} }

View File

@ -9,19 +9,17 @@ type EventStatDiff = ['event_stats', eventId: string, stat: EventStat, diff: num
type StatDiff = PubkeyStatDiff | EventStatDiff; type StatDiff = PubkeyStatDiff | EventStatDiff;
/** Store stats for the event in LMDB. */ /** Store stats for the event in LMDB. */
function updateStats(event: Event) { async function updateStats(event: Event) {
const statDiffs = getStatsDiff(event); const statDiffs = getStatsDiff(event);
if (!statDiffs.length) return; if (!statDiffs.length) return;
const pubkeyDiffs = statDiffs.filter(([table]) => table === 'pubkey_stats') as PubkeyStatDiff[]; const pubkeyDiffs = statDiffs.filter(([table]) => table === 'pubkey_stats') as PubkeyStatDiff[];
const eventDiffs = statDiffs.filter(([table]) => table === 'event_stats') as EventStatDiff[]; const eventDiffs = statDiffs.filter(([table]) => table === 'event_stats') as EventStatDiff[];
return db.transaction().execute(() => { await Promise.all([
return Promise.all([ pubkeyDiffs.length ? pubkeyStatsQuery(pubkeyDiffs).execute() : undefined,
pubkeyStatsQuery(pubkeyDiffs).execute(), eventDiffs.length ? eventStatsQuery(eventDiffs).execute() : undefined,
eventStatsQuery(eventDiffs).execute(),
]); ]);
});
} }
/** Calculate stats changes ahead of time so we can build an efficient query. */ /** Calculate stats changes ahead of time so we can build an efficient query. */