Move refreshAuthorStatsDebounced to stats.ts

This commit is contained in:
Alex Gleason 2024-05-17 18:26:55 -05:00
parent 5c2e3450a9
commit bf479d0162
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 14 additions and 14 deletions

View File

@ -1,6 +1,7 @@
import { NKinds, NostrEvent, NStore } from '@nostrify/nostrify';
import Debug from '@soapbox/stickynotes/debug';
import { InsertQueryBuilder, Kysely } from 'kysely';
import { LRUCache } from 'lru-cache';
import { SetRequired } from 'type-fest';
import { DittoDB } from '@/db/DittoDB.ts';
@ -250,4 +251,13 @@ async function countAuthorStats(
};
}
export { refreshAuthorStats, updateStats };
const lru = new LRUCache<string, true>({ max: 1000 });
/** Calls `refreshAuthorStats` only once per author. */
function refreshAuthorStatsDebounced(pubkey: string): void {
if (lru.get(pubkey)) return;
lru.set(pubkey, true);
refreshAuthorStats(pubkey).catch(() => {});
}
export { refreshAuthorStats, refreshAuthorStatsDebounced, updateStats };

View File

@ -1,12 +1,11 @@
import { NostrEvent, NStore } from '@nostrify/nostrify';
import { LRUCache } from 'lru-cache';
import { matchFilter } from 'nostr-tools';
import { DittoDB } from '@/db/DittoDB.ts';
import { type DittoEvent } from '@/interfaces/DittoEvent.ts';
import { DittoTables } from '@/db/DittoTables.ts';
import { Conf } from '@/config.ts';
import { refreshAuthorStats } from '@/stats.ts';
import { refreshAuthorStatsDebounced } from '@/stats.ts';
interface HydrateOpts {
events: DittoEvent[];
@ -57,7 +56,7 @@ async function hydrateEvents(opts: HydrateOpts): Promise<DittoEvent[]> {
events: await gatherEventStats(cache),
};
requestMissingAuthorStats(events, stats.authors);
refreshMissingAuthorStats(events, stats.authors);
// Dedupe events.
const results = [...new Map(cache.map((event) => [event.id, event])).values()];
@ -270,7 +269,7 @@ async function gatherAuthorStats(events: DittoEvent[]): Promise<DittoTables['aut
}));
}
function requestMissingAuthorStats(events: NostrEvent[], stats: DittoTables['author_stats'][]) {
function refreshMissingAuthorStats(events: NostrEvent[], stats: DittoTables['author_stats'][]) {
const pubkeys = new Set<string>(
events
.filter((event) => event.kind === 0)
@ -286,15 +285,6 @@ function requestMissingAuthorStats(events: NostrEvent[], stats: DittoTables['aut
}
}
const lru = new LRUCache<string, true>({ max: 1000 });
/** Calls `refreshAuthorStats` only once per author. */
function refreshAuthorStatsDebounced(pubkey: string): void {
if (lru.get(pubkey)) return;
lru.set(pubkey, true);
refreshAuthorStats(pubkey).catch(() => {});
}
/** Collect event stats from the events. */
async function gatherEventStats(events: DittoEvent[]): Promise<DittoTables['event_stats'][]> {
const ids = new Set<string>(