2023-12-10 17:43:41 +00:00
|
|
|
import { db, type EventStatsRow, type PubkeyStatsRow } from '@/db.ts';
|
2023-12-08 00:43:24 +00:00
|
|
|
import { Event } from '@/deps.ts';
|
|
|
|
|
2023-12-10 17:10:11 +00:00
|
|
|
type PubkeyStat = keyof Omit<PubkeyStatsRow, 'pubkey'>;
|
2023-12-10 17:43:41 +00:00
|
|
|
type EventStat = keyof Omit<EventStatsRow, 'event_id'>;
|
2023-12-08 00:43:24 +00:00
|
|
|
|
|
|
|
/** Store stats for the event in LMDB. */
|
2023-12-10 17:10:11 +00:00
|
|
|
function updateStats(event: Event) {
|
2023-12-10 17:43:41 +00:00
|
|
|
return updateStatsQuery(event)?.execute();
|
2023-12-10 17:10:11 +00:00
|
|
|
}
|
|
|
|
|
2023-12-10 17:43:41 +00:00
|
|
|
function updateStatsQuery(event: Event) {
|
|
|
|
const firstE = findFirstTag(event, 'e');
|
|
|
|
|
2023-12-08 00:43:24 +00:00
|
|
|
switch (event.kind) {
|
2023-12-10 17:10:11 +00:00
|
|
|
case 1:
|
2023-12-10 19:12:35 +00:00
|
|
|
return incrementPubkeyStatsQuery([event.pubkey], 'notes_count', 1);
|
2023-12-08 00:43:24 +00:00
|
|
|
case 6:
|
2023-12-10 19:12:35 +00:00
|
|
|
return firstE ? incrementEventStatsQuery([firstE], 'reposts_count', 1) : undefined;
|
2023-12-08 00:43:24 +00:00
|
|
|
case 7:
|
2023-12-10 19:12:35 +00:00
|
|
|
return firstE ? incrementEventStatsQuery([firstE], 'reactions_count', 1) : undefined;
|
2023-12-08 00:43:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-10 19:12:35 +00:00
|
|
|
function incrementPubkeyStatsQuery(pubkeys: string[], stat: PubkeyStat, diff: number) {
|
|
|
|
const values: PubkeyStatsRow[] = pubkeys.map((pubkey) => {
|
|
|
|
const row: PubkeyStatsRow = {
|
|
|
|
pubkey,
|
|
|
|
followers_count: 0,
|
|
|
|
following_count: 0,
|
|
|
|
notes_count: 0,
|
|
|
|
};
|
|
|
|
row[stat] = diff;
|
|
|
|
return row;
|
|
|
|
});
|
2023-12-10 17:10:11 +00:00
|
|
|
|
|
|
|
return db.insertInto('pubkey_stats')
|
2023-12-10 19:12:35 +00:00
|
|
|
.values(values)
|
2023-12-10 17:10:11 +00:00
|
|
|
.onConflict((oc) =>
|
|
|
|
oc
|
|
|
|
.column('pubkey')
|
|
|
|
.doUpdateSet((eb) => ({
|
|
|
|
[stat]: eb(stat, '+', diff),
|
|
|
|
}))
|
|
|
|
);
|
2023-12-08 00:43:24 +00:00
|
|
|
}
|
|
|
|
|
2023-12-10 19:12:35 +00:00
|
|
|
function incrementEventStatsQuery(eventIds: string[], stat: EventStat, diff: number) {
|
|
|
|
const values: EventStatsRow[] = eventIds.map((event_id) => {
|
|
|
|
const row: EventStatsRow = {
|
|
|
|
event_id,
|
|
|
|
replies_count: 0,
|
|
|
|
reposts_count: 0,
|
|
|
|
reactions_count: 0,
|
|
|
|
};
|
|
|
|
row[stat] = diff;
|
|
|
|
return row;
|
|
|
|
});
|
2023-12-10 17:43:41 +00:00
|
|
|
|
|
|
|
return db.insertInto('event_stats')
|
2023-12-10 19:12:35 +00:00
|
|
|
.values(values)
|
2023-12-10 17:43:41 +00:00
|
|
|
.onConflict((oc) =>
|
|
|
|
oc
|
|
|
|
.column('event_id')
|
|
|
|
.doUpdateSet((eb) => ({
|
|
|
|
[stat]: eb(stat, '+', diff),
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-12-10 17:10:11 +00:00
|
|
|
function findFirstTag({ tags }: Event, name: string): string | undefined {
|
|
|
|
return tags.find(([n]) => n === name)?.[1];
|
2023-12-08 00:43:24 +00:00
|
|
|
}
|
|
|
|
|
2023-12-10 17:10:11 +00:00
|
|
|
export { updateStats };
|