π 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
Added notification of online users
Yevhenii Kulisidi6728feeade23
diff --git a/src/analytics/analytics.service.ts b/src/analytics/analytics.service.tsindex b90ce7450..ac87dc4ad 100644--- a/src/analytics/analytics.service.ts+++ b/src/analytics/analytics.service.ts@@ -10,6 +10,8 @@ import * as _find from 'lodash/find'import * as _now from 'lodash/now'import * as _values from 'lodash/values'import * as _round from 'lodash/round'+import * as _pick from 'lodash/pick'+import * as _uniqBy from 'lodash/uniqBy'import * as dayjs from 'dayjs'import * as utc from 'dayjs/plugin/utc'import * as timezone from 'dayjs/plugin/timezone'@@ -53,7 +55,8 @@ dayjs.extend(timezone)export const getSessionKey = (ip: string, ua: string, pid: string, salt = '') =>`ses_${hash(`${ua}${ip}${pid}${salt}`).toString('hex')}`-export const getSessionDurationKey = (hash: string, pid: string) => `sd:${hash}:${pid}`+export const getSessionDurationKey = (hash: string, pid: string) =>+ `sd:${hash}:${pid}`export const cols = ['cc',@@ -68,12 +71,7 @@ export const cols = ['ca',]-export const perfCols = [- 'cc',- 'pg',- 'dv',- 'br',-]+export const perfCols = ['cc', 'pg', 'dv', 'br']interface chartCHResponse {index: number@@ -167,7 +165,7 @@ export const checkIfTBAllowed = (@Injectable()export class AnalyticsService {- constructor(private readonly projectService: ProjectService) { }+ constructor(private readonly projectService: ProjectService) {}async getRedisProject(pid: string): Promise<Project | null> {const pidKey = getRedisProjectKey(pid)@@ -424,8 +422,9 @@ export class AnalyticsService {...params,[colFilter]: filter,}- query += ` ${isExclusive ? 'AND NOT' : 'AND'- } ${column}={${colFilter}:String}`+ query += ` ${+ isExclusive ? 'AND NOT' : 'AND'+ } ${column}={${colFilter}:String}`}return [query, params]@@ -678,7 +677,10 @@ export class AnalyticsService {const size = _size(res)for (let j = 0; j < size; ++j) {const key = res[j][i]- params[i][key] = _round(millisecondsToSeconds(res[j]['avg(pageLoad)']), 2)+ params[i][key] = _round(+ millisecondsToSeconds(res[j]['avg(pageLoad)']),+ 2,+ )}}@@ -807,4 +809,24 @@ 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+ }}diff --git a/src/project/dto/project.dto.ts b/src/project/dto/project.dto.tsindex 157395e3c..688f1fdc6 100644--- a/src/project/dto/project.dto.ts+++ b/src/project/dto/project.dto.ts@@ -45,4 +45,11 @@ export class ProjectDTO {"When true, anyone on the internet (including Google) would be able to see the project's Dashboard.",})public: boolean++ @ApiProperty({+ required: false,+ description:+ 'Alert if the number of online users exceeds the specified number.',+ })+ alertIfOnlineUsersExceeds: number | null}diff --git a/src/project/entity/project.entity.ts b/src/project/entity/project.entity.tsindex d8c25cf4a..1e288aaa7 100644--- a/src/project/entity/project.entity.ts+++ b/src/project/entity/project.entity.ts@@ -64,4 +64,20 @@ export class Project {)@JoinTable()extensions: ExtensionToProject[]++ @ApiProperty()+ @Column({+ type: 'int',+ nullable: true,+ default: null,+ })+ alertIfOnlineUsersExceeds: number | null++ @ApiProperty()+ @Column({+ type: 'date',+ nullable: true,+ default: null,+ })+ lastSendedAlert: Date | null}diff --git a/src/project/project.controller.ts b/src/project/project.controller.tsindex 9d9b2f521..96c889a8b 100644--- a/src/project/project.controller.ts+++ b/src/project/project.controller.ts@@ -460,6 +460,7 @@ export class ProjectController {project.ipBlacklist = _map(projectDTO.ipBlacklist, _trim)project.name = projectDTO.nameproject.public = projectDTO.public+ project.alertIfOnlineUsersExceeds = projectDTO.alertIfOnlineUsersExceedsawait updateProjectClickhouse(this.projectService.formatToClickhouse(project),@@ -481,6 +482,7 @@ export class ProjectController {project.ipBlacklist = _map(projectDTO.ipBlacklist, _trim)project.name = projectDTO.nameproject.public = projectDTO.public+ project.alertIfOnlineUsersExceeds = projectDTO.alertIfOnlineUsersExceedsawait this.projectService.update(id, _omit(project, ['share', 'admin']))}diff --git a/src/project/project.service.ts b/src/project/project.service.tsindex f4265f995..14fe0c226 100644--- a/src/project/project.service.ts+++ b/src/project/project.service.ts@@ -122,7 +122,10 @@ export class ProjectService {return this.projectsRepository.save(project)}- async update(id: string, projectDTO: ProjectDTO): Promise<any> {+ async update(+ id: string,+ projectDTO: { ProjectDTO?: ProjectDTO; lastSendedAlert?: Date | null },+ ): Promise<any> {return this.projectsRepository.update(id, projectDTO)}@@ -180,8 +183,11 @@ export class ProjectService {return this.projectsRepository.findOne(id, params)}- findWhere(where: Record<string, unknown>): Promise<Project[]> {- return this.projectsRepository.find({ where })+ findWhere(+ where: Record<string, unknown>,+ relations?: string[],+ ): Promise<Project[]> {+ return this.projectsRepository.find({ where, relations })}find(params: object): Promise<Project[]> {diff --git a/src/task-manager/task-manager.service.ts b/src/task-manager/task-manager.service.tsindex 8d1a57333..d88d8d340 100644--- a/src/task-manager/task-manager.service.ts+++ b/src/task-manager/task-manager.service.ts@@ -1,6 +1,6 @@import { Injectable } from '@nestjs/common'import { Cron, CronExpression } from '@nestjs/schedule'-import { IsNull, LessThan } from 'typeorm'+import { IsNull, LessThan, Not } from 'typeorm'import * as bcrypt from 'bcrypt'import * as dayjs from 'dayjs'import * as utc from 'dayjs/plugin/utc'@@ -34,6 +34,9 @@ import {PROJECT_INVITE_EXPIRE,} from '../common/constants'import { getRandomTip } from '../common/utils'+import { InjectBot } from 'nestjs-telegraf'+import { TelegrafContext } from 'src/user/user.controller'+import { Telegraf } from 'telegraf'dayjs.extend(utc)@@ -45,6 +48,7 @@ export class TaskManagerService {private readonly analyticsService: AnalyticsService,private readonly projectService: ProjectService,private readonly actionTokensService: ActionTokensService,+ @InjectBot() private bot: Telegraf<TelegrafContext>,) {}@Cron(CronExpression.EVERY_MINUTE)@@ -334,4 +338,61 @@ export class TaskManagerService {})}}++ // every 5 minutes+ @Cron('*/5 * * * *')+ // // every 5 seconds+ // @Cron('*/5 * * * * *')+ async checkUserOnline(): Promise<void> {+ const projects = await this.projectService.findWhere(+ {+ alertIfOnlineUsersExceeds: Not(IsNull()),+ admin: {+ isTelegramChatIdConfirmed: true,+ },+ },+ ['admin'],+ )++ for (let i = 0; i < _size(projects); ++i) {+ const project = await this.projectService.findOneWithRelations(+ projects[i].id,+ )++ if (project.lastSendedAlert !== null) {+ const lastSendedAlert = new Date(project.lastSendedAlert)+ const now = new Date()++ if (now.getTime() - lastSendedAlert.getTime() < 24 * 60 * 60 * 1000) {+ return+ }+ }++ const online = await this.analyticsService.getOnlineUserCount(+ projects[i].id,+ )++ const onlineCount = online.length++ // console.log(onlineCount, project.admin.telegramChatId)++ if (onlineCount >= projects[i].alertIfOnlineUsersExceeds) {+ if (!project.admin.isTelegramChatIdConfirmed) {+ return+ }++ await this.projectService.update(projects[i].id, {+ lastSendedAlert: new Date(),+ })++ this.bot.telegram.sendMessage(+ project.admin.telegramChatId,+ `π Your project *${projects[i].name}* has *${onlineCount}* online users right now!`,+ {+ parse_mode: 'Markdown',+ },+ )+ }+ }+ }}diff --git a/src/tg-integration/tg.update.ts b/src/tg-integration/tg.update.tsindex e0ef17d54..16f3cf262 100644--- a/src/tg-integration/tg.update.ts+++ b/src/tg-integration/tg.update.ts@@ -50,7 +50,6 @@ export class SwetrixUpdate {const [action, entityId] = ctx.callbackQuery?.['data'].split(':')if (action === 'confirmTelegramChatId') {- // delete the message with the buttonawait ctx.telegram.deleteMessage(ctx.chat.id,ctx.callbackQuery?.['message'].message_id,@@ -100,12 +99,27 @@ export class SwetrixUpdate {return}- const text = `*${project.name}*\nID: \`${project.id}\`\nActive: \`${- project.active ? 'yes' : 'no'- }\`\nPublic: \`${project.public ? 'yes' : 'no'}\n\`Created: \`${dayjs- .utc(project.created)- .tz(user.timezone)- .format('YYYY-MM-DD HH:mm:ss')}\``+ const text =+ `π *${project.name}*` ++ '\n\n' ++ '*Information*' ++ '\n' ++ `ID: \`${project.id}\`` ++ '\n' ++ `Active: \`${project.active ? 'yes' : 'no'}\`` ++ '\n' ++ `Public: \`${project.public ? 'yes' : 'no'}\`` ++ '\n' ++ `Created: \`${dayjs+ .utc(project.created)+ .tz(user.timezone)+ .format('YYYY-MM-DD HH:mm:ss')}\`` ++ '\n\n' ++ '*Alerts*' ++ '\n' ++ `Online users: \`${+ project.alertIfOnlineUsersExceeds ? 'set' : 'not set'+ }\``await ctx.editMessageText(text, {parse_mode: 'Markdown',reply_markup: {