mirror of
https://github.com/aljazceru/ditto.git
synced 2026-01-27 01:04:19 +01:00
Stop recounting author stats constantly
This commit is contained in:
@@ -5,8 +5,6 @@ import { DittoDB } from '@/db/DittoDB.ts';
|
||||
import { DittoTables } from '@/db/DittoTables.ts';
|
||||
import { Conf } from '@/config.ts';
|
||||
import { type DittoEvent } from '@/interfaces/DittoEvent.ts';
|
||||
import { Storages } from '@/storages.ts';
|
||||
import { refreshAuthorStatsDebounced } from '@/utils/stats.ts';
|
||||
import { findQuoteTag } from '@/utils/tags.ts';
|
||||
|
||||
interface HydrateOpts {
|
||||
@@ -58,8 +56,6 @@ async function hydrateEvents(opts: HydrateOpts): Promise<DittoEvent[]> {
|
||||
events: await gatherEventStats(cache),
|
||||
};
|
||||
|
||||
refreshMissingAuthorStats(events, stats.authors);
|
||||
|
||||
// Dedupe events.
|
||||
const results = [...new Map(cache.map((event) => [event.id, event])).values()];
|
||||
|
||||
@@ -276,25 +272,6 @@ async function gatherAuthorStats(events: DittoEvent[]): Promise<DittoTables['aut
|
||||
}));
|
||||
}
|
||||
|
||||
async function refreshMissingAuthorStats(events: NostrEvent[], stats: DittoTables['author_stats'][]) {
|
||||
const store = await Storages.db();
|
||||
const kysely = await DittoDB.getInstance();
|
||||
|
||||
const pubkeys = new Set<string>(
|
||||
events
|
||||
.filter((event) => event.kind === 0)
|
||||
.map((event) => event.pubkey),
|
||||
);
|
||||
|
||||
const missing = pubkeys.difference(
|
||||
new Set(stats.map((stat) => stat.pubkey)),
|
||||
);
|
||||
|
||||
for (const pubkey of missing) {
|
||||
refreshAuthorStatsDebounced({ pubkey, store, kysely });
|
||||
}
|
||||
}
|
||||
|
||||
/** Collect event stats from the events. */
|
||||
async function gatherEventStats(events: DittoEvent[]): Promise<DittoTables['event_stats'][]> {
|
||||
const ids = new Set<string>(
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import { Semaphore } from '@lambdalisue/async';
|
||||
import { NostrEvent, NStore } from '@nostrify/nostrify';
|
||||
import { Kysely, UpdateObject } from 'kysely';
|
||||
import { LRUCache } from 'lru-cache';
|
||||
import { SetRequired } from 'type-fest';
|
||||
|
||||
import { DittoTables } from '@/db/DittoTables.ts';
|
||||
@@ -266,18 +264,3 @@ export async function refreshAuthorStats(
|
||||
|
||||
return stats;
|
||||
}
|
||||
|
||||
const authorStatsSemaphore = new Semaphore(10);
|
||||
const refreshedAuthors = new LRUCache<string, true>({ max: 1000 });
|
||||
|
||||
/** Calls `refreshAuthorStats` only once per author. */
|
||||
export function refreshAuthorStatsDebounced(opts: RefreshAuthorStatsOpts): void {
|
||||
if (refreshedAuthors.get(opts.pubkey)) {
|
||||
return;
|
||||
}
|
||||
|
||||
refreshedAuthors.set(opts.pubkey, true);
|
||||
|
||||
authorStatsSemaphore
|
||||
.lock(() => refreshAuthorStats(opts).catch(() => {}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user