Add a function to recalculate author stats

This commit is contained in:
Alex Gleason 2024-05-17 13:12:40 -05:00
parent 1a32150f3d
commit a39910fa98
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 37 additions and 26 deletions

View File

@ -1,8 +1,6 @@
import { nip19 } from 'nostr-tools'; import { nip19 } from 'nostr-tools';
import { DittoDB } from '@/db/DittoDB.ts'; import { refreshAuthorStats } from '@/stats.ts';
import { DittoTables } from '@/db/DittoTables.ts';
import { Storages } from '@/storages.ts';
let pubkey: string; let pubkey: string;
try { try {
@ -17,23 +15,4 @@ try {
Deno.exit(1); Deno.exit(1);
} }
const store = await Storages.db(); await refreshAuthorStats(pubkey);
const kysely = await DittoDB.getInstance();
const [followList] = await store.query([{ kinds: [3], authors: [pubkey], limit: 1 }]);
const authorStats: DittoTables['author_stats'] = {
pubkey,
followers_count: (await store.count([{ kinds: [3], '#p': [pubkey] }])).count,
following_count: followList?.tags.filter(([name]) => name === 'p')?.length ?? 0,
notes_count: (await store.count([{ kinds: [1], authors: [pubkey] }])).count,
};
await kysely.insertInto('author_stats')
.values(authorStats)
.onConflict((oc) =>
oc
.column('pubkey')
.doUpdateSet(authorStats)
)
.execute();

View File

@ -1,11 +1,12 @@
import { NKinds, NostrEvent } from '@nostrify/nostrify'; import { NKinds, NostrEvent, NStore } from '@nostrify/nostrify';
import Debug from '@soapbox/stickynotes/debug'; import Debug from '@soapbox/stickynotes/debug';
import { InsertQueryBuilder, Kysely } from 'kysely'; import { InsertQueryBuilder, Kysely } from 'kysely';
import { SetRequired } from 'type-fest';
import { DittoDB } from '@/db/DittoDB.ts'; import { DittoDB } from '@/db/DittoDB.ts';
import { DittoTables } from '@/db/DittoTables.ts'; import { DittoTables } from '@/db/DittoTables.ts';
import { Storages } from '@/storages.ts'; import { Storages } from '@/storages.ts';
import { findReplyTag } from '@/tags.ts'; import { findReplyTag, getTagSet } from '@/tags.ts';
type AuthorStat = keyof Omit<DittoTables['author_stats'], 'pubkey'>; type AuthorStat = keyof Omit<DittoTables['author_stats'], 'pubkey'>;
type EventStat = keyof Omit<DittoTables['event_stats'], 'event_id'>; type EventStat = keyof Omit<DittoTables['event_stats'], 'event_id'>;
@ -216,4 +217,35 @@ function getFollowDiff(event: NostrEvent, prev?: NostrEvent): AuthorStatDiff[] {
]; ];
} }
export { updateStats }; /** Refresh the author's stats in the database. */
async function refreshAuthorStats(pubkey: string): Promise<void> {
const store = await Storages.db();
const stats = await countAuthorStats(store, pubkey);
const kysely = await DittoDB.getInstance();
await kysely.insertInto('author_stats')
.values(stats)
.onConflict((oc) => oc.column('pubkey').doUpdateSet(stats))
.execute();
}
/** Calculate author stats from the database. */
async function countAuthorStats(
store: SetRequired<NStore, 'count'>,
pubkey: string,
): Promise<DittoTables['author_stats']> {
const [{ count: followers_count }, { count: notes_count }, [followList]] = await Promise.all([
store.count([{ kinds: [3], '#p': [pubkey] }]),
store.count([{ kinds: [1], authors: [pubkey] }]),
store.query([{ kinds: [3], authors: [pubkey], limit: 1 }]),
]);
return {
pubkey,
followers_count,
following_count: getTagSet(followList?.tags ?? [], 'p').size,
notes_count,
};
}
export { refreshAuthorStats, updateStats };