From a32b0e7066d0212bf2e3ea15d10be5f50b67813d Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 10 Dec 2023 17:48:21 -0600 Subject: [PATCH] stats: clean up prev usage --- src/stats.ts | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/stats.ts b/src/stats.ts index 578e2a35..32208df8 100644 --- a/src/stats.ts +++ b/src/stats.ts @@ -10,16 +10,19 @@ type EventStatDiff = ['event_stats', eventId: string, stat: EventStat, diff: num type StatDiff = AuthorStatDiff | EventStatDiff; /** Store stats for the event in LMDB. */ -async function updateStats(event: Event & { prev?: Event }) { +async function updateStats(event: Event) { + let prev: Event | undefined; const queries: InsertQueryBuilder[] = []; // Kind 3 is a special case - replace the count with the new list. if (event.kind === 3) { - await maybeSetPrev(event); - queries.push(updateFollowingCountQuery(event as Event<3>)); + prev = await maybeGetPrev(event); + if (!prev || event.created_at >= prev.created_at) { + queries.push(updateFollowingCountQuery(event as Event<3>)); + } } - const statDiffs = getStatsDiff(event); + const statDiffs = getStatsDiff(event, prev); const pubkeyDiffs = statDiffs.filter(([table]) => table === 'author_stats') as AuthorStatDiff[]; const eventDiffs = statDiffs.filter(([table]) => table === 'event_stats') as EventStatDiff[]; @@ -32,7 +35,7 @@ async function updateStats(event: Event & { prev?: Event } /** Calculate stats changes ahead of time so we can build an efficient query. */ -function getStatsDiff(event: Event & { prev?: Event }): StatDiff[] { +function getStatsDiff(event: Event, prev: Event | undefined): StatDiff[] { const statDiffs: StatDiff[] = []; const firstTaggedId = event.tags.find(([name]) => name === 'e')?.[1]; @@ -46,7 +49,7 @@ function getStatsDiff(event: Event & { prev?: Event }): } break; case 3: - statDiffs.push(...getFollowDiff(event as Event<3>, event.prev as Event<3> | undefined)); + statDiffs.push(...getFollowDiff(event as Event<3>, prev as Event<3> | undefined)); break; case 6: if (firstTaggedId) { @@ -114,17 +117,13 @@ function eventStatsQuery(diffs: EventStatDiff[]) { ); } -/** Set the `prev` value on the event to the last version of the event, if any. */ -async function maybeSetPrev(event: Event & { prev?: Event }): Promise { - if (event.prev?.kind === event.kind) return; - +/** Get the last version of the event, if any. */ +async function maybeGetPrev(event: Event): Promise> { const [prev] = await eventsDB.getFilters([ { kinds: [event.kind], authors: [event.pubkey], limit: 1 }, ]); - if (prev.created_at < event.created_at) { - event.prev = prev; - } + return prev; } /** Set the following count to the total number of unique "p" tags in the follow list. */