stats: fix queries getting stuck
This commit is contained in:
parent
6a92c5135d
commit
a48c1e51e1
|
@ -171,7 +171,7 @@ function getFilterQuery(filter: DittoFilter): EventQuery {
|
|||
|
||||
return exp
|
||||
.orderBy('created_at', 'desc')
|
||||
.groupBy('pubkey')
|
||||
.groupBy('events.pubkey')
|
||||
.as('authors');
|
||||
},
|
||||
(join) => join.onRef('authors.pubkey', '=', 'events.pubkey'),
|
||||
|
@ -184,9 +184,13 @@ function getFilterQuery(filter: DittoFilter): EventQuery {
|
|||
'authors.tags as author_tags',
|
||||
'authors.created_at as author_created_at',
|
||||
'authors.sig as author_sig',
|
||||
...(filter.relations?.includes('stats')
|
||||
? [
|
||||
'authors.author_stats_followers_count',
|
||||
'authors.author_stats_following_count',
|
||||
'authors.author_stats_notes_count',
|
||||
] as const
|
||||
: []),
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
10
src/stats.ts
10
src/stats.ts
|
@ -9,19 +9,17 @@ type EventStatDiff = ['event_stats', eventId: string, stat: EventStat, diff: num
|
|||
type StatDiff = PubkeyStatDiff | EventStatDiff;
|
||||
|
||||
/** Store stats for the event in LMDB. */
|
||||
function updateStats(event: Event) {
|
||||
async function updateStats(event: Event) {
|
||||
const statDiffs = getStatsDiff(event);
|
||||
if (!statDiffs.length) return;
|
||||
|
||||
const pubkeyDiffs = statDiffs.filter(([table]) => table === 'pubkey_stats') as PubkeyStatDiff[];
|
||||
const eventDiffs = statDiffs.filter(([table]) => table === 'event_stats') as EventStatDiff[];
|
||||
|
||||
return db.transaction().execute(() => {
|
||||
return Promise.all([
|
||||
pubkeyStatsQuery(pubkeyDiffs).execute(),
|
||||
eventStatsQuery(eventDiffs).execute(),
|
||||
await Promise.all([
|
||||
pubkeyDiffs.length ? pubkeyStatsQuery(pubkeyDiffs).execute() : undefined,
|
||||
eventDiffs.length ? eventStatsQuery(eventDiffs).execute() : undefined,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
/** Calculate stats changes ahead of time so we can build an efficient query. */
|
||||
|
|
Loading…
Reference in New Issue