Skip to content

πŸ“ˆ 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!

More advanced notification of the target being reached has been given

Yevhenii Kulisidi225efe0b66b2
diff --git a/src/project/dto/project.dto.ts b/src/project/dto/project.dto.ts
index 688f1fdc6..642f27ff6 100644
--- a/src/project/dto/project.dto.ts
+++ b/src/project/dto/project.dto.ts
@@ -1,6 +1,25 @@
import { ApiProperty } from '@nestjs/swagger'
import { IsNotEmpty } from 'class-validator'
+export enum QueryMetric {
+ PAGE_VIEWS = 'page_views',
+ UNIQUE_PAGE_VIEWS = 'unique_page_views',
+}
+
+export enum QueryCondition {
+ GREATER_THAN = 'greater_than',
+ LESS_THAN = 'less_than',
+}
+
+export enum QueryTime {
+ LAST_15_MINUTES = 'last_15_minutes',
+ LAST_30_MINUTES = 'last_30_minutes',
+ LAST_1_HOUR = 'last_1_hour',
+ LAST_4_HOURS = 'last_4_hours',
+ LAST_24_HOURS = 'last_24_hours',
+ LAST_48_HOURS = 'last_48_hours',
+}
+
export class ProjectDTO {
@ApiProperty({
example: 'Your awesome project',
@@ -52,4 +71,16 @@ export class ProjectDTO {
'Alert if the number of online users exceeds the specified number.',
})
alertIfOnlineUsersExceeds: number | null
+
+ @ApiProperty()
+ additionalAlertQueryMetric: QueryMetric | null
+
+ @ApiProperty()
+ additionalAlertQueryCondition: QueryCondition | null
+
+ @ApiProperty()
+ additionalAlertQueryValue: number | null
+
+ @ApiProperty()
+ additionalAlertQueryTime: QueryTime | null
}
diff --git a/src/project/entity/project.entity.ts b/src/project/entity/project.entity.ts
index 1e288aaa7..dffba0963 100644
--- a/src/project/entity/project.entity.ts
+++ b/src/project/entity/project.entity.ts
@@ -11,6 +11,11 @@ import { ApiProperty } from '@nestjs/swagger'
import { User } from '../../user/entities/user.entity'
import { ProjectShare } from './project-share.entity'
import { ExtensionToProject } from '../../marketplace/extensions/entities/extension-to-project.entity'
+import {
+ QueryCondition,
+ QueryMetric,
+ QueryTime,
+} from 'src/project/dto/project.dto'
// In case of modifying some properties here, make sure to also edit them in common/constants.ts -> selfhosted -> clickhouse
// and to add them to the GDPR data export email template
@@ -80,4 +85,46 @@ export class Project {
default: null,
})
lastSendedAlert: Date | null
+
+ @ApiProperty()
+ @Column({
+ type: 'boolean',
+ default: false,
+ })
+ isAdditionalAlertEnabled: boolean
+
+ @ApiProperty()
+ @Column({
+ type: 'enum',
+ enum: QueryMetric,
+ nullable: true,
+ default: null,
+ })
+ additionalAlertQueryMetric: QueryMetric | null
+
+ @ApiProperty()
+ @Column({
+ type: 'enum',
+ enum: QueryCondition,
+ nullable: true,
+ default: null,
+ })
+ additionalAlertQueryCondition: QueryCondition | null
+
+ @ApiProperty()
+ @Column({
+ type: 'int',
+ nullable: true,
+ default: null,
+ })
+ additionalAlertQueryValue: number | null
+
+ @ApiProperty()
+ @Column({
+ type: 'enum',
+ enum: QueryTime,
+ nullable: true,
+ default: null,
+ })
+ additionalAlertQueryTime: QueryTime | null
}
diff --git a/src/project/project.controller.ts b/src/project/project.controller.ts
index 96c889a8b..12be65d6c 100644
--- a/src/project/project.controller.ts
+++ b/src/project/project.controller.ts
@@ -461,6 +461,20 @@ export class ProjectController {
project.name = projectDTO.name
project.public = projectDTO.public
project.alertIfOnlineUsersExceeds = projectDTO.alertIfOnlineUsersExceeds
+ project.additionalAlertQueryMetric = projectDTO.additionalAlertQueryMetric
+ project.additionalAlertQueryCondition =
+ projectDTO.additionalAlertQueryCondition
+ project.additionalAlertQueryValue = projectDTO.additionalAlertQueryValue
+ project.additionalAlertQueryTime = projectDTO.additionalAlertQueryTime
+
+ if (
+ projectDTO.additionalAlertQueryMetric &&
+ projectDTO.additionalAlertQueryCondition &&
+ projectDTO.additionalAlertQueryValue &&
+ projectDTO.additionalAlertQueryTime
+ ) {
+ project.isAdditionalAlertEnabled = true
+ }
await updateProjectClickhouse(
this.projectService.formatToClickhouse(project),
@@ -483,6 +497,20 @@ export class ProjectController {
project.name = projectDTO.name
project.public = projectDTO.public
project.alertIfOnlineUsersExceeds = projectDTO.alertIfOnlineUsersExceeds
+ project.additionalAlertQueryMetric = projectDTO.additionalAlertQueryMetric
+ project.additionalAlertQueryCondition =
+ projectDTO.additionalAlertQueryCondition
+ project.additionalAlertQueryValue = projectDTO.additionalAlertQueryValue
+ project.additionalAlertQueryTime = projectDTO.additionalAlertQueryTime
+
+ if (
+ projectDTO.additionalAlertQueryMetric &&
+ projectDTO.additionalAlertQueryCondition &&
+ projectDTO.additionalAlertQueryValue &&
+ projectDTO.additionalAlertQueryTime
+ ) {
+ project.isAdditionalAlertEnabled = true
+ }
await this.projectService.update(id, _omit(project, ['share', 'admin']))
}
diff --git a/src/task-manager/task-manager.service.ts b/src/task-manager/task-manager.service.ts
index d88d8d340..f0f6df9f9 100644
--- a/src/task-manager/task-manager.service.ts
+++ b/src/task-manager/task-manager.service.ts
@@ -37,6 +37,11 @@ import { getRandomTip } from '../common/utils'
import { InjectBot } from 'nestjs-telegraf'
import { TelegrafContext } from 'src/user/user.controller'
import { Telegraf } from 'telegraf'
+import {
+ QueryCondition,
+ QueryMetric,
+ QueryTime,
+} from 'src/project/dto/project.dto'
dayjs.extend(utc)
@@ -395,4 +400,106 @@ export class TaskManagerService {
}
}
}
+
+ // every 5 minutes
+ @Cron('*/5 * * * *')
+ // every 5 seconds
+ @Cron('*/5 * * * * *')
+ async checkAdditionalAlerts(): Promise<void> {
+ const projects = await this.projectService.findWhere(
+ {
+ isAdditionalAlertEnabled: true,
+ 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 isUnique =
+ projects[i].additionalAlertQueryMetric === QueryMetric.UNIQUE_PAGE_VIEWS
+ ? 1
+ : 0
+
+ const time =
+ projects[i].additionalAlertQueryTime === QueryTime.LAST_15_MINUTES
+ ? 15 * 60
+ : projects[i].additionalAlertQueryTime === QueryTime.LAST_30_MINUTES
+ ? 30 * 60
+ : projects[i].additionalAlertQueryTime === QueryTime.LAST_1_HOUR
+ ? 60 * 60
+ : projects[i].additionalAlertQueryTime === QueryTime.LAST_4_HOURS
+ ? 4 * 60 * 60
+ : projects[i].additionalAlertQueryTime === QueryTime.LAST_24_HOURS
+ ? 24 * 60 * 60
+ : projects[i].additionalAlertQueryTime === QueryTime.LAST_48_HOURS
+ ? 48 * 60 * 60
+ : 0
+
+ const isLess =
+ projects[i].additionalAlertQueryCondition === QueryCondition.LESS_THAN
+ ? '<'
+ : '>'
+
+ const query = `SELECT count() FROM analytics WHERE pid = '${
+ projects[i].id
+ }' AND unique = '${isUnique}' AND created ${isLess.replace(
+ '<',
+ '>',
+ )} now() - ${time}`
+ const queryResult = await clickhouse.query(query).toPromise()
+
+ const count = Number(queryResult[0]['count()'])
+
+ console.log(count)
+
+ if (count >= projects[i].additionalAlertQueryValue) {
+ if (!project.admin.isTelegramChatIdConfirmed) {
+ return
+ }
+
+ await this.projectService.update(projects[i].id, {
+ lastSendedAlert: new Date(),
+ })
+
+ const text2 = `πŸ”” Your project *${projects[i].name}* has *${count}* ${
+ projects[i].additionalAlertQueryMetric ===
+ QueryMetric.UNIQUE_PAGE_VIEWS
+ ? 'unique page views'
+ : 'page views'
+ } in the last ${
+ projects[i].additionalAlertQueryTime === QueryTime.LAST_15_MINUTES
+ ? '15 minutes'
+ : projects[i].additionalAlertQueryTime === QueryTime.LAST_30_MINUTES
+ ? '30 minutes'
+ : projects[i].additionalAlertQueryTime === QueryTime.LAST_1_HOUR
+ ? '1 hour'
+ : projects[i].additionalAlertQueryTime === QueryTime.LAST_4_HOURS
+ ? '4 hours'
+ : projects[i].additionalAlertQueryTime === QueryTime.LAST_24_HOURS
+ ? '24 hours'
+ : projects[i].additionalAlertQueryTime === QueryTime.LAST_48_HOURS
+ ? '48 hours'
+ : '0'
+ }!`
+ this.bot.telegram.sendMessage(project.admin.telegramChatId, text2, {
+ parse_mode: 'Markdown',
+ })
+ }
+ }
+ }
}
diff --git a/src/tg-integration/tg.update.ts b/src/tg-integration/tg.update.ts
index 879061ce0..faad01927 100644
--- a/src/tg-integration/tg.update.ts
+++ b/src/tg-integration/tg.update.ts
@@ -136,6 +136,10 @@ export class SwetrixUpdate {
'\n' +
`Online users: \`${
project.alertIfOnlineUsersExceeds ? 'seted' : 'not seted'
+ }\`` +
+ '\n' +
+ `Additional: \`${
+ project.isAdditionalAlertEnabled ? 'seted' : 'not seted'
}\``
await ctx.editMessageText(text, {
parse_mode: 'Markdown',