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) {
case 1:
return incrementPubkeyStatQuery(event.pubkey, 'notes_count', 1);
return incrementPubkeyStatsQuery([event.pubkey], 'notes_count', 1);
case 6:
return firstE ? incrementEventStatQuery(firstE, 'reposts_count', 1) : undefined;
return firstE ? incrementEventStatsQuery([firstE], 'reposts_count', 1) : undefined;
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 values: PubkeyStatsRow[] = pubkeys.map((pubkey) => {
const row: PubkeyStatsRow = {
pubkey,
followers_count: 0,
following_count: 0,
notes_count: 0,
};
row[stat] = diff;
return row;
});
return db.insertInto('pubkey_stats')
.values(row)
.values(values)
.onConflict((oc) =>
oc
.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 values: EventStatsRow[] = eventIds.map((event_id) => {
const row: EventStatsRow = {
event_id: eventId,
event_id,
replies_count: 0,
reposts_count: 0,
reactions_count: 0,
};
row[stat] = diff;
return row;
});
return db.insertInto('event_stats')
.values(row)
.values(values)
.onConflict((oc) =>
oc
.column('event_id')