stats: make the logic kind of make sense
This commit is contained in:
parent
bababe56f3
commit
eca923d7c8
34
src/stats.ts
34
src/stats.ts
|
@ -1,21 +1,24 @@
|
||||||
import { db, type PubkeyStatsRow } from '@/db.ts';
|
import { db, type EventStatsRow, type PubkeyStatsRow } from '@/db.ts';
|
||||||
import { Event } from '@/deps.ts';
|
import { Event } from '@/deps.ts';
|
||||||
|
|
||||||
type PubkeyStat = keyof Omit<PubkeyStatsRow, 'pubkey'>;
|
type PubkeyStat = keyof Omit<PubkeyStatsRow, 'pubkey'>;
|
||||||
|
type EventStat = keyof Omit<EventStatsRow, 'event_id'>;
|
||||||
|
|
||||||
/** Store stats for the event in LMDB. */
|
/** Store stats for the event in LMDB. */
|
||||||
function updateStats(event: Event) {
|
function updateStats(event: Event) {
|
||||||
return updateStatsQuery(event).execute();
|
return updateStatsQuery(event)?.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateStatsQuery(event: Event) {
|
function updateStatsQuery(event: Event) {
|
||||||
|
const firstE = findFirstTag(event, 'e');
|
||||||
|
|
||||||
switch (event.kind) {
|
switch (event.kind) {
|
||||||
case 1:
|
case 1:
|
||||||
return incrementPubkeyStatQuery(event.pubkey, 'notes_count', 1);
|
return incrementPubkeyStatQuery(event.pubkey, 'notes_count', 1);
|
||||||
case 6:
|
case 6:
|
||||||
return await incrementMentionedEvent(event, 'reposts');
|
return firstE ? incrementEventStatQuery(firstE, 'reposts_count', 1) : undefined;
|
||||||
case 7:
|
case 7:
|
||||||
return await incrementMentionedEvent(event, 'reactions');
|
return firstE ? incrementEventStatQuery(firstE, 'reactions_count', 1) : undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,6 +43,27 @@ function incrementPubkeyStatQuery(pubkey: string, stat: PubkeyStat, diff: number
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function incrementEventStatQuery(eventId: string, stat: EventStat, diff: number) {
|
||||||
|
const row: EventStatsRow = {
|
||||||
|
event_id: eventId,
|
||||||
|
replies_count: 0,
|
||||||
|
reposts_count: 0,
|
||||||
|
reactions_count: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
row[stat] = diff;
|
||||||
|
|
||||||
|
return db.insertInto('event_stats')
|
||||||
|
.values(row)
|
||||||
|
.onConflict((oc) =>
|
||||||
|
oc
|
||||||
|
.column('event_id')
|
||||||
|
.doUpdateSet((eb) => ({
|
||||||
|
[stat]: eb(stat, '+', diff),
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function findFirstTag({ tags }: Event, name: string): string | undefined {
|
function findFirstTag({ tags }: Event, name: string): string | undefined {
|
||||||
return tags.find(([n]) => n === name)?.[1];
|
return tags.find(([n]) => n === name)?.[1];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue