Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Commit

Permalink
Admin UI: Apply unique constraint to login metric
Browse files Browse the repository at this point in the history
  • Loading branch information
pfrazee committed Apr 21, 2021
1 parent 9b0551b commit c2a7a9b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
9 changes: 6 additions & 3 deletions frontend/static/js/admin/dashboard.js
Expand Up @@ -42,17 +42,20 @@ class Dashboard extends LitElement {
this.requestUpdate()
this.metrics.day = await session.api.server.countMultipleMetricsEvents({
timespan: 'day',
events: ['signed-up', 'logged-in', 'community-created', 'post-created', 'comment-created']
events: ['signed-up', 'logged-in', 'community-created', 'post-created', 'comment-created'],
uniqueBys: {'logged-in': 'user'}
})
this.requestUpdate()
this.metrics.week = await session.api.server.countMultipleMetricsEvents({
timespan: 'week',
events: ['signed-up', 'logged-in', 'community-created', 'post-created', 'comment-created']
events: ['signed-up', 'logged-in', 'community-created', 'post-created', 'comment-created'],
uniqueBys: {'logged-in': 'user'}
})
this.requestUpdate()
this.metrics.month = await session.api.server.countMultipleMetricsEvents({
timespan: 'month',
events: ['signed-up', 'logged-in', 'community-created', 'post-created', 'comment-created']
events: ['signed-up', 'logged-in', 'community-created', 'post-created', 'comment-created'],
uniqueBys: {'logged-in': 'user'}
})
this.requestUpdate()
this.httpHits = Object.entries(await session.api.server.aggregateHttpHits({timespan: 'day'}))
Expand Down
20 changes: 18 additions & 2 deletions lib/metrics.js
Expand Up @@ -34,12 +34,17 @@ export function commentCreated ({user}) {
return metricEventsLog.append({event: 'comment-created', user})
}

export async function listEvents ({event, timespan}) {
export async function listEvents ({event, timespan, uniqueBy}) {
const log = event === 'http-request' ? trafficLog : metricEventsLog
let startTs = timespanToTS(timespan)
let hasSeen = uniqueBy ? new Set() : undefined
return log.query(entry => {
if (entry.ts < startTs) return false
if (event && entry.event !== event) return false
if (uniqueBy) {
if (hasSeen.has(entry[uniqueBy])) return false
hasSeen.add(entry[uniqueBy])
}
return true
})
}
Expand All @@ -48,16 +53,27 @@ export async function countEvents (opts) {
return (await listEvents(opts))?.length || 0
}

export async function countMultipleEvents ({events, timespan}) {
export async function countMultipleEvents ({events, timespan, uniqueBys}) {
if (!events || !Array.isArray(events)) {
throw new Error('Events must be an array of strings')
}
let startTs = timespanToTS(timespan)
const counts = {}
events.forEach(evt => counts[evt] = 0)
let hasSeens = undefined
if (uniqueBys) {
hasSeens = {}
for (let k in uniqueBys) {
hasSeens[k] = new Set()
}
}
await metricEventsLog.query(entry => {
if (entry.ts < startTs) return false
if (!events.includes(entry.event)) return false
if (uniqueBys?.[entry.event]) {
if (hasSeens[entry.event].has(entry[uniqueBys[entry.event]])) return false
hasSeens[entry.event].add(entry[uniqueBys[entry.event]])
}
counts[entry.event]++
return false
})
Expand Down

0 comments on commit c2a7a9b

Please sign in to comment.