π Open source, privacy-first web analytics β a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
calculating online users based on redis heartbeats instead of clickhouse sessions
Blue Mouseea583c236669
diff --git a/src/analytics/analytics.service.ts b/src/analytics/analytics.service.tsindex ac87dc4ad..caa59366b 100644--- a/src/analytics/analytics.service.ts+++ b/src/analytics/analytics.service.ts@@ -810,23 +810,8 @@ export class AnalyticsService {return result}- async getOnlineUserCount(pid: string) {- const keys = await redis.keys(`sd:*:${pid}`)-- if (_isEmpty(keys)) {- return []- }-- const sids = _map(keys, key => key.split(':')[1])-- const query = `SELECT sid, dv, br, os, cc FROM analytics WHERE sid IN (${sids- .map(el => `'${el}'`)- .join(',')})`- const result = await clickhouse.query(query).toPromise()- const processed = _map(_uniqBy(result, 'sid'), el =>- _pick(el, ['dv', 'br', 'os', 'cc']),- )-- return processed+ async getOnlineUserCount(pid: string): Promise<number> {+ // @ts-ignore+ return await redis.countKeysByPattern(`hb:${pid}:*`)}}diff --git a/src/task-manager/task-manager.service.ts b/src/task-manager/task-manager.service.tsindex e64319519..39f4c9266 100644--- a/src/task-manager/task-manager.service.ts+++ b/src/task-manager/task-manager.service.ts@@ -410,9 +410,7 @@ export class TaskManagerService {project.id,)- const onlineCount = online.length-- if (onlineCount >= alert.queryValue) {+ if (online >= alert.queryValue) {// @ts-ignoreawait this.alertService.update(alert.id, {lastTriggered: new Date(),@@ -420,7 +418,7 @@ export class TaskManagerService {this.bot.telegram.sendMessage(project.admin.telegramChatId,- `π Alert *${alert.name}* got triggered!\nYour project *${project.name}* has *${onlineCount}* online users right now!}`,+ `π Alert *${alert.name}* got triggered!\nYour project *${project.name}* has *${online}* online users right now!}`,{parse_mode: 'Markdown',},diff --git a/src/tg-integration/tg.update.ts b/src/tg-integration/tg.update.tsindex aff8bffb7..2da407149 100644--- a/src/tg-integration/tg.update.ts+++ b/src/tg-integration/tg.update.ts@@ -109,7 +109,7 @@ export class SwetrixUpdate {project: project.id,})- const onlineUserCount = await this.analyticsService.getOnlineUserCount(+ const online = await this.analyticsService.getOnlineUserCount(project.id,)@@ -142,7 +142,7 @@ export class SwetrixUpdate {'\n\n' +'*Analytics (last 7 days)*' +'\n' +- `Online users: \`${onlineUserCount.length}\`` ++ `Online users: \`${online}\`` +'\n' +`Page views: \`${stats[project.id].thisWeek}\`` +'\n' +