Merge branch 'no-recount' into 'main'

Stop recounting author stats constantly

See merge request soapbox-pub/ditto!312
This commit is contained in:
Alex Gleason 2024-05-25 01:29:30 +00:00
commit fb80c9c2fe
3 changed files with 0 additions and 41 deletions

View File

@ -21,7 +21,6 @@
"@bradenmacdonald/s3-lite-client": "jsr:@bradenmacdonald/s3-lite-client@^0.7.4",
"@db/sqlite": "jsr:@db/sqlite@^0.11.1",
"@isaacs/ttlcache": "npm:@isaacs/ttlcache@^1.4.1",
"@lambdalisue/async": "jsr:@lambdalisue/async@^2.1.1",
"@noble/secp256k1": "npm:@noble/secp256k1@^2.0.0",
"@nostrify/nostrify": "jsr:@nostrify/nostrify@^0.22.4",
"@sentry/deno": "https://deno.land/x/sentry@7.112.2/index.mjs",

View File

@ -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>(

View File

@ -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(() => {}));
}