pubkey_stats --> author_stats

This commit is contained in:
Alex Gleason 2023-12-10 16:04:52 -06:00
parent 07dc07ab71
commit 733b8ba9c5
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
4 changed files with 21 additions and 21 deletions

View File

@ -13,11 +13,11 @@ interface DittoDB {
users: UserRow;
relays: RelayRow;
unattached_media: UnattachedMediaRow;
pubkey_stats: PubkeyStatsRow;
author_stats: AuthorStatsRow;
event_stats: EventStatsRow;
}
interface PubkeyStatsRow {
interface AuthorStatsRow {
pubkey: string;
followers_count: number;
following_count: number;
@ -126,4 +126,4 @@ async function migrate() {
await migrate();
export { db, type DittoDB, type EventRow, type EventStatsRow, type PubkeyStatsRow, type TagRow, type UserRow };
export { type AuthorStatsRow, db, type DittoDB, type EventRow, type EventStatsRow, type TagRow, type UserRow };

View File

@ -176,11 +176,11 @@ function getFilterQuery(filter: DittoFilter): EventQuery {
if (filter.relations?.includes('author_stats')) {
query = query
.leftJoin('pubkey_stats', 'pubkey_stats.pubkey', 'events.pubkey')
.leftJoin('author_stats', 'author_stats.pubkey', 'events.pubkey')
.select((eb) => [
eb.fn.coalesce('pubkey_stats.followers_count', eb.val(0)).as('author_stats_followers_count'),
eb.fn.coalesce('pubkey_stats.following_count', eb.val(0)).as('author_stats_following_count'),
eb.fn.coalesce('pubkey_stats.notes_count', eb.val(0)).as('author_stats_notes_count'),
eb.fn.coalesce('author_stats.followers_count', eb.val(0)).as('author_stats_followers_count'),
eb.fn.coalesce('author_stats.following_count', eb.val(0)).as('author_stats_following_count'),
eb.fn.coalesce('author_stats.notes_count', eb.val(0)).as('author_stats_notes_count'),
]);
}
@ -210,7 +210,7 @@ function getFiltersQuery(filters: DittoFilter[]) {
.reduce((result, query) => result.unionAll(query));
}
type AuthorStats = Omit<DittoDB['pubkey_stats'], 'pubkey'>;
type AuthorStats = Omit<DittoDB['author_stats'], 'pubkey'>;
type EventStats = Omit<DittoDB['event_stats'], 'event_id'>;
interface DittoEvent<K extends number = number> extends Event<K> {

View File

@ -2,7 +2,7 @@ import { Kysely } from '@/deps.ts';
export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.createTable('pubkey_stats')
.createTable('author_stats')
.addColumn('pubkey', 'text', (col) => col.primaryKey())
.addColumn('followers_count', 'integer', (col) => col.notNull().defaultTo(0))
.addColumn('following_count', 'integer', (col) => col.notNull().defaultTo(0))
@ -19,6 +19,6 @@ export async function up(db: Kysely<any>): Promise<void> {
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema.dropTable('pubkey_stats').execute();
await db.schema.dropTable('author_stats').execute();
await db.schema.dropTable('event_stats').execute();
}

View File

@ -1,23 +1,23 @@
import { db, type EventStatsRow, type PubkeyStatsRow } from '@/db.ts';
import { type AuthorStatsRow, db, type EventStatsRow } from '@/db.ts';
import { Event, findReplyTag } from '@/deps.ts';
type PubkeyStat = keyof Omit<PubkeyStatsRow, 'pubkey'>;
type AuthorStat = keyof Omit<AuthorStatsRow, 'pubkey'>;
type EventStat = keyof Omit<EventStatsRow, 'event_id'>;
type PubkeyStatDiff = ['pubkey_stats', pubkey: string, stat: PubkeyStat, diff: number];
type AuthorStatDiff = ['author_stats', pubkey: string, stat: AuthorStat, diff: number];
type EventStatDiff = ['event_stats', eventId: string, stat: EventStat, diff: number];
type StatDiff = PubkeyStatDiff | EventStatDiff;
type StatDiff = AuthorStatDiff | EventStatDiff;
/** Store stats for the event in LMDB. */
async function updateStats(event: Event) {
const statDiffs = getStatsDiff(event);
if (!statDiffs.length) return;
const pubkeyDiffs = statDiffs.filter(([table]) => table === 'pubkey_stats') as PubkeyStatDiff[];
const pubkeyDiffs = statDiffs.filter(([table]) => table === 'author_stats') as AuthorStatDiff[];
const eventDiffs = statDiffs.filter(([table]) => table === 'event_stats') as EventStatDiff[];
await Promise.all([
pubkeyDiffs.length ? pubkeyStatsQuery(pubkeyDiffs).execute() : undefined,
pubkeyDiffs.length ? authorStatsQuery(pubkeyDiffs).execute() : undefined,
eventDiffs.length ? eventStatsQuery(eventDiffs).execute() : undefined,
]);
}
@ -31,7 +31,7 @@ function getStatsDiff(event: Event): StatDiff[] {
switch (event.kind) {
case 1:
statDiffs.push(['pubkey_stats', event.pubkey, 'notes_count', 1]);
statDiffs.push(['author_stats', event.pubkey, 'notes_count', 1]);
if (inReplyToId) {
statDiffs.push(['event_stats', inReplyToId, 'replies_count', 1]);
}
@ -50,9 +50,9 @@ function getStatsDiff(event: Event): StatDiff[] {
return statDiffs;
}
function pubkeyStatsQuery(diffs: PubkeyStatDiff[]) {
const values: PubkeyStatsRow[] = diffs.map(([_, pubkey, stat, diff]) => {
const row: PubkeyStatsRow = {
function authorStatsQuery(diffs: AuthorStatDiff[]) {
const values: AuthorStatsRow[] = diffs.map(([_, pubkey, stat, diff]) => {
const row: AuthorStatsRow = {
pubkey,
followers_count: 0,
following_count: 0,
@ -62,7 +62,7 @@ function pubkeyStatsQuery(diffs: PubkeyStatDiff[]) {
return row;
});
return db.insertInto('pubkey_stats')
return db.insertInto('author_stats')
.values(values)
.onConflict((oc) =>
oc