stats: support multiple values

This commit is contained in:
Alex Gleason 2023-12-10 13:12:35 -06:00
parent eca923d7c8
commit a8944dd7ea
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 27 additions and 23 deletions

View File

@ -14,26 +14,28 @@ function updateStatsQuery(event: Event) {
switch (event.kind) { switch (event.kind) {
case 1: case 1:
return incrementPubkeyStatQuery(event.pubkey, 'notes_count', 1); return incrementPubkeyStatsQuery([event.pubkey], 'notes_count', 1);
case 6: case 6:
return firstE ? incrementEventStatQuery(firstE, 'reposts_count', 1) : undefined; return firstE ? incrementEventStatsQuery([firstE], 'reposts_count', 1) : undefined;
case 7: case 7:
return firstE ? incrementEventStatQuery(firstE, 'reactions_count', 1) : undefined; return firstE ? incrementEventStatsQuery([firstE], 'reactions_count', 1) : undefined;
} }
} }
function incrementPubkeyStatQuery(pubkey: string, stat: PubkeyStat, diff: number) { function incrementPubkeyStatsQuery(pubkeys: string[], stat: PubkeyStat, diff: number) {
const row: PubkeyStatsRow = { const values: PubkeyStatsRow[] = pubkeys.map((pubkey) => {
pubkey, const row: PubkeyStatsRow = {
followers_count: 0, pubkey,
following_count: 0, followers_count: 0,
notes_count: 0, following_count: 0,
}; notes_count: 0,
};
row[stat] = diff; row[stat] = diff;
return row;
});
return db.insertInto('pubkey_stats') return db.insertInto('pubkey_stats')
.values(row) .values(values)
.onConflict((oc) => .onConflict((oc) =>
oc oc
.column('pubkey') .column('pubkey')
@ -43,18 +45,20 @@ function incrementPubkeyStatQuery(pubkey: string, stat: PubkeyStat, diff: number
); );
} }
function incrementEventStatQuery(eventId: string, stat: EventStat, diff: number) { function incrementEventStatsQuery(eventIds: string[], stat: EventStat, diff: number) {
const row: EventStatsRow = { const values: EventStatsRow[] = eventIds.map((event_id) => {
event_id: eventId, const row: EventStatsRow = {
replies_count: 0, event_id,
reposts_count: 0, replies_count: 0,
reactions_count: 0, reposts_count: 0,
}; reactions_count: 0,
};
row[stat] = diff; row[stat] = diff;
return row;
});
return db.insertInto('event_stats') return db.insertInto('event_stats')
.values(row) .values(values)
.onConflict((oc) => .onConflict((oc) =>
oc oc
.column('event_id') .column('event_id')