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
|
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
|
||||||
|
: []),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
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;
|
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. */
|
||||||
|
|
Loading…
Reference in New Issue