Stop recounting author stats constantly
This commit is contained in:
parent
6062378b7e
commit
69ff568214
|
@ -21,7 +21,6 @@
|
||||||
"@bradenmacdonald/s3-lite-client": "jsr:@bradenmacdonald/s3-lite-client@^0.7.4",
|
"@bradenmacdonald/s3-lite-client": "jsr:@bradenmacdonald/s3-lite-client@^0.7.4",
|
||||||
"@db/sqlite": "jsr:@db/sqlite@^0.11.1",
|
"@db/sqlite": "jsr:@db/sqlite@^0.11.1",
|
||||||
"@isaacs/ttlcache": "npm:@isaacs/ttlcache@^1.4.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",
|
"@noble/secp256k1": "npm:@noble/secp256k1@^2.0.0",
|
||||||
"@nostrify/nostrify": "jsr:@nostrify/nostrify@^0.22.4",
|
"@nostrify/nostrify": "jsr:@nostrify/nostrify@^0.22.4",
|
||||||
"@sentry/deno": "https://deno.land/x/sentry@7.112.2/index.mjs",
|
"@sentry/deno": "https://deno.land/x/sentry@7.112.2/index.mjs",
|
||||||
|
|
|
@ -5,8 +5,6 @@ import { DittoDB } from '@/db/DittoDB.ts';
|
||||||
import { DittoTables } from '@/db/DittoTables.ts';
|
import { DittoTables } from '@/db/DittoTables.ts';
|
||||||
import { Conf } from '@/config.ts';
|
import { Conf } from '@/config.ts';
|
||||||
import { type DittoEvent } from '@/interfaces/DittoEvent.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';
|
import { findQuoteTag } from '@/utils/tags.ts';
|
||||||
|
|
||||||
interface HydrateOpts {
|
interface HydrateOpts {
|
||||||
|
@ -58,8 +56,6 @@ async function hydrateEvents(opts: HydrateOpts): Promise<DittoEvent[]> {
|
||||||
events: await gatherEventStats(cache),
|
events: await gatherEventStats(cache),
|
||||||
};
|
};
|
||||||
|
|
||||||
refreshMissingAuthorStats(events, stats.authors);
|
|
||||||
|
|
||||||
// Dedupe events.
|
// Dedupe events.
|
||||||
const results = [...new Map(cache.map((event) => [event.id, event])).values()];
|
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. */
|
/** Collect event stats from the events. */
|
||||||
async function gatherEventStats(events: DittoEvent[]): Promise<DittoTables['event_stats'][]> {
|
async function gatherEventStats(events: DittoEvent[]): Promise<DittoTables['event_stats'][]> {
|
||||||
const ids = new Set<string>(
|
const ids = new Set<string>(
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
import { Semaphore } from '@lambdalisue/async';
|
|
||||||
import { NostrEvent, NStore } from '@nostrify/nostrify';
|
import { NostrEvent, NStore } from '@nostrify/nostrify';
|
||||||
import { Kysely, UpdateObject } from 'kysely';
|
import { Kysely, UpdateObject } from 'kysely';
|
||||||
import { LRUCache } from 'lru-cache';
|
|
||||||
import { SetRequired } from 'type-fest';
|
import { SetRequired } from 'type-fest';
|
||||||
|
|
||||||
import { DittoTables } from '@/db/DittoTables.ts';
|
import { DittoTables } from '@/db/DittoTables.ts';
|
||||||
|
@ -266,18 +264,3 @@ export async function refreshAuthorStats(
|
||||||
|
|
||||||
return stats;
|
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(() => {}));
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue