📈 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
Change to endpoints to integer instead of UUID
Yehor Dremliuha1caf15bf5d5b
diff --git a/apps/production/src/analytics/analytics.controller.ts b/apps/production/src/analytics/analytics.controller.tsindex 42dfd5f1a..40284f6ea 100644--- a/apps/production/src/analytics/analytics.controller.ts+++ b/apps/production/src/analytics/analytics.controller.ts@@ -81,7 +81,7 @@ import {PerfMeasure,} from './interfaces'import { GetSessionsDto } from './dto/get-sessions.dto'-import {GetMonitorDataDto} from './dto/get-monitor-data.dto'+import { GetMonitorDataDto } from './dto/get-monitor-data.dto'import { GetSessionDto } from './dto/get-session.dto'import { ErrorDTO } from './dto/error.dto'import { GetErrorsDto } from './dto/get-errors.dto'@@ -1573,13 +1573,10 @@ export class AnalyticsController {}}-@ApiOperation({ summary: 'Get monitor data' })@ApiBearerAuth()@ApiOkResponse({ description: 'Monitor data retrieved successfully' })- @Get(- 'monitor-data',- )+ @Get('monitor-data')@Auth([], true, true)public async getMonitorData(@Query() parameters: GetMonitorDataDto,@@ -1597,7 +1594,6 @@ export class AnalyticsController {timezone,} = parameters-// TODO: CHECK THAT MONITOR GROUP BELONGS TO THE PROVIDED PIDthis.analyticsService.validatePID(pid)@@ -1646,7 +1642,8 @@ export class AnalyticsController {diff,)// TODO change to Uint64; filters.- let subQuery = 'FROM monitor_responses WHERE monitorID = {monitorId:FixedString(36)} AND created BETWEEN {groupFrom:String} AND {groupTo:String}'+ const subQuery =+ 'FROM monitor_responses WHERE monitorId = {monitorId:UInt64} AND created BETWEEN {groupFrom:String} AND {groupTo:String}'const paramsData = {params: {monitorId,@@ -1657,14 +1654,15 @@ export class AnalyticsController {// RETURN avg(responseTime) as avgResponseTime AS WELL// BY GROUPS LIKE 0-100, 100-200, 200-500, 500-1000, etc.- const result = await this.analyticsService.groupMonitorResponsesByTimeBucket(- newTimebucket,- groupFrom,- groupTo,- subQuery,- paramsData,- safeTimezone,- )+ const result =+ await this.analyticsService.groupMonitorResponsesByTimeBucket(+ newTimebucket,+ groupFrom,+ groupTo,+ subQuery,+ paramsData,+ safeTimezone,+ )return {...result,diff --git a/apps/production/src/analytics/analytics.service.ts b/apps/production/src/analytics/analytics.service.tsindex 1a4643798..1ef2d1b8c 100644--- a/apps/production/src/analytics/analytics.service.ts+++ b/apps/production/src/analytics/analytics.service.ts@@ -2379,7 +2379,7 @@ export class AnalyticsService {${timeBucketFunc}(created) as tz_createdFROM monitor_responsesWHERE- monitorID = {monitorId:FixedString(36)}+ monitorId = {monitorId:UInt64}AND created BETWEEN {groupFrom:String} AND {groupTo:String}) as subqueryGROUP BY ${groupBy}diff --git a/apps/production/src/analytics/dto/get-monitor-data.dto.ts b/apps/production/src/analytics/dto/get-monitor-data.dto.tsindex b560f1db5..8190006eb 100644--- a/apps/production/src/analytics/dto/get-monitor-data.dto.ts+++ b/apps/production/src/analytics/dto/get-monitor-data.dto.ts@@ -22,7 +22,7 @@ export class GetMonitorDataDto {@ApiProperty()@IsNotEmpty()- monitorId: string+ monitorId: number@ApiProperty()@IsNotEmpty()diff --git a/apps/production/src/project/consumers/monitor.consumer.ts b/apps/production/src/project/consumers/monitor.consumer.tsindex ddc06519c..73375bc50 100644--- a/apps/production/src/project/consumers/monitor.consumer.ts+++ b/apps/production/src/project/consumers/monitor.consumer.ts@@ -21,7 +21,7 @@ export class MonitorConsumer {@Process('http-request')async httpRequestProcess(- job: Job<CreateMonitorHttpRequestDTO & { monitorID: string }>,+ job: Job<CreateMonitorHttpRequestDTO & { monitorId: number }>,): Promise<any> {this.logger.debug(`Consumer works ${JSON.stringify(job.data)}`)const res: {@@ -32,7 +32,7 @@ export class MonitorConsumer {} = await firstValueFrom(this.monitorService.send('http-request', job.data))this.logger.debug(`Consumer works ${JSON.stringify(res)}`)- const monitor = await this.projectService.getMonitor(job.data.monitorID)+ const monitor = await this.projectService.getMonitor(job.data.monitorId)if (!monitor.acceptedStatusCodes.includes(res.statusCode)) {// await this.mailerService.sed(monitor.group.project.admin.email, 'fuck')@@ -52,7 +52,7 @@ export class MonitorConsumer {format: 'JSONEachRow',values: [{- monitorID: job.data.monitorID,+ monitorId: job.data.monitorId,region: res.region,responseTime: res.responseTime,timestamp: res.timestamp,diff --git a/apps/production/src/project/entity/monitor.entity.ts b/apps/production/src/project/entity/monitor.entity.tsindex 2b9c4043d..1168132cb 100644--- a/apps/production/src/project/entity/monitor.entity.ts+++ b/apps/production/src/project/entity/monitor.entity.ts@@ -12,8 +12,8 @@ import { MonitorGroupEntity } from './monitor-group.entity'@Entity('monitor')export class MonitorEntity {@ApiProperty()- @PrimaryGeneratedColumn('uuid')- id: string+ @PrimaryGeneratedColumn('increment', { type: 'bigint' })+ id: number@ApiProperty()@Column({ type: 'varchar' })diff --git a/apps/production/src/project/project.controller.ts b/apps/production/src/project/project.controller.tsindex bc27a0162..de1abce41 100644--- a/apps/production/src/project/project.controller.ts+++ b/apps/production/src/project/project.controller.ts@@ -2396,7 +2396,7 @@ export class ProjectController {public async getMonitor(@Param() { projectId }: ProjectIdDto,@Param() { monitorGroupId }: MonitorGroupIdDto,- @Param('monitorId') monitorId: string,+ @Param('monitorId') monitorId: number,@CurrentUserId() userId: string,): Promise<MonitorEntity> {const project = await this.projectService.findProject(projectId, [@@ -2442,7 +2442,7 @@ export class ProjectController {public async updateMonitor(@Param() { projectId }: ProjectIdDto,@Param() { monitorGroupId }: MonitorGroupIdDto,- @Param('monitorId') monitorId: string,+ @Param('monitorId') monitorId: number,@Body() body: UpdateMonitorHttpRequestDTO,@CurrentUserId() userId: string,): Promise<MonitorEntity> {@@ -2495,7 +2495,7 @@ export class ProjectController {public async deleteMonitor(@Param() { projectId }: ProjectIdDto,@Param() { monitorGroupId }: MonitorGroupIdDto,- @Param('monitorId') monitorId: string,+ @Param('monitorId') monitorId: number,@CurrentUserId() userId: string,): Promise<void> {const project = await this.projectService.findProject(projectId, [diff --git a/apps/production/src/project/project.service.ts b/apps/production/src/project/project.service.tsindex f6935eff8..f40d9bd24 100644--- a/apps/production/src/project/project.service.ts+++ b/apps/production/src/project/project.service.ts@@ -1480,7 +1480,7 @@ export class ProjectService {return this.monitorRepository.save(monitor)}- async getMonitor(monitorId: string) {+ async getMonitor(monitorId: number) {return this.monitorRepository.findOne({where: {id: monitorId,@@ -1491,48 +1491,50 @@ export class ProjectService {// Update a monitor groupasync updateMonitor(- monitorGroupId: string,+ monitorId: number,updateMonitorHttpRequestDto: UpdateMonitorHttpRequestDTO,): Promise<any> {await this.monitorRepository.update(- { id: monitorGroupId },+ { id: monitorId },updateMonitorHttpRequestDto,)}- async deleteMonitor(monitorId: string): Promise<any> {+ async deleteMonitor(monitorId: number): Promise<any> {return this.monitorRepository.delete(monitorId)}async sendHttpRequest(- monitorID: string,+ monitorId: number,monitorHttpRequestDto: HttpRequestOptions,) {// TODO crete interface for that stuffawait this.monitorQueue.add('http-request',- { ...monitorHttpRequestDto, monitorID },+ { ...monitorHttpRequestDto, monitorId },{repeat: { every: monitorHttpRequestDto.interval * 1000 },- jobId: monitorID,+ jobId: monitorId.toString(),},)}async updateHttpRequest(- monitorID: string,+ monitorId: number,updatedHttpRequestDto: HttpRequestOptions,): Promise<void> {- await this.deleteHttpRequest(monitorID)+ await this.deleteHttpRequest(monitorId)- await this.sendHttpRequest(monitorID, {+ await this.sendHttpRequest(monitorId, {...updatedHttpRequestDto,timeout: updatedHttpRequestDto.timeout * 1000,})}- async deleteHttpRequest(monitorID: string) {- const job = await this.monitorQueue.getJob(monitorID)+ async deleteHttpRequest(monitorId: number) {+ const job = await this.monitorQueue.getJob(monitorId.toString())+ this.logger.debug(`Job ${JSON.stringify(job)}`)+ this.logger.debug(`Monitor Id: ${monitorId.toString()}`)if (job) {await job.remove()}diff --git a/migrations/clickhouse/2024_09_13.js b/migrations/clickhouse/2024_09_13.jsindex 1e8112852..ebfdfd5ad 100644--- a/migrations/clickhouse/2024_09_13.js+++ b/migrations/clickhouse/2024_09_13.js@@ -4,7 +4,7 @@ const queries = [// Monitor responses table`CREATE TABLE IF NOT EXISTS ${dbName}.monitor_responses(- monitorID FixedString(36),+ monitorId UInt64,region LowCardinality(Nullable(String)),responseTime UInt32,timestamp UInt32,@@ -13,7 +13,7 @@ const queries = [)ENGINE = MergeTree()PARTITION BY toYYYYMM(created)- ORDER BY (monitorID, created);`,+ ORDER BY (monitorId, created);`,];queriesRunner(queries);diff --git a/migrations/clickhouse/initialise_database.js b/migrations/clickhouse/initialise_database.jsindex 66496e3a8..62544cba5 100644--- a/migrations/clickhouse/initialise_database.js+++ b/migrations/clickhouse/initialise_database.js@@ -140,7 +140,7 @@ const CLICKHOUSE_INIT_QUERIES = [`CREATE TABLE IF NOT EXISTS ${dbName}.monitor_responses(- monitorID FixedString(36),+ monitorId UInt64,region LowCardinality(Nullable(String)),responseTime UInt32,timestamp UInt32,@@ -149,7 +149,7 @@ const CLICKHOUSE_INIT_QUERIES = [)ENGINE = MergeTree()PARTITION BY toYYYYMM(created)- ORDER BY (monitorID, created);`,+ ORDER BY (monitorId, created);`,]const initialiseDatabase = async () => {diff --git a/migrations/mysql/2024_09_08.sql b/migrations/mysql/2024_09_08.sqlindex 39ccc1b3b..277ff4c7d 100644--- a/migrations/mysql/2024_09_08.sql+++ b/migrations/mysql/2024_09_08.sql@@ -1,24 +1,25 @@CREATE TABLE IF NOT EXISTS monitor_groups (groupId INT AUTO_INCREMENT PRIMARY KEY,- groupName VARCHAR(255) NOT NULL+ name VARCHAR(255) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;-CREATE TABLE IF NOT EXISTS monitor (- id INT AUTO_INCREMENT PRIMARY KEY,- type VARCHAR(255) NOT NULL,- name VARCHAR(255) NOT NULL,- url VARCHAR(255) NOT NULL,- `interval` INT NOT NULL,- retries INT NOT NULL,- retryInterval INT NOT NULL,- timeout INT NOT NULL,- acceptedStatusCodes JSON NOT NULL,- description TEXT,- groupName VARCHAR(255) NOT NULL,- userID INT NOT NULL,- createdAt DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),- updatedAt DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),- FOREIGN KEY (groupName) REFERENCES monitor_groups(groupName),- FOREIGN KEY (userID) REFERENCES users(userId)-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;+CREATE TABLE `monitor` (+ `id` bigint NOT NULL AUTO_INCREMENT,+ `type` varchar(255) NOT NULL,+ `name` varchar(255) NOT NULL,+ `url` varchar(255) NOT NULL,+ `interval` int NOT NULL,+ `retries` int NOT NULL,+ `retryInterval` int NOT NULL,+ `timeout` int NOT NULL,+ `acceptedStatusCodes` json NOT NULL,+ `description` text,+ `createdAt` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),+ `updatedAt` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),+ `httpOptions` json DEFAULT NULL,+ `groupId` varchar(36) DEFAULT NULL,+ PRIMARY KEY (`id`),+ FOREIGN KEY (`groupId`) REFERENCES `monitor_groups` (`id`) ON DELETE CASCADE+) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4+