📈 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
- Update migrations - Establish BullMQ cleanup/resources - Create CRUD for HTTP monitor - Implement DTOs and Interfaces for validation
Yehor Dremliuhabce4946fda4e
diff --git a/apps/production/src/app.module.ts b/apps/production/src/app.module.tsindex fb940a65a..7453b52fd 100644--- a/apps/production/src/app.module.ts+++ b/apps/production/src/app.module.ts@@ -57,7 +57,7 @@ const modules = [username: process.env.MYSQL_USER,password: process.env.MYSQL_ROOT_PASSWORD,database: process.env.MYSQL_DATABASE,- synchronize: true, // process.env.NODE_ENV !== 'production',+ synchronize: false, // process.env.NODE_ENV !== 'production',entities: [`${__dirname}/**/*.entity{.ts,.js}`],}),BullModule.forRoot({diff --git a/apps/production/src/project/dto/create-monitor.dto.ts b/apps/production/src/project/dto/create-monitor.dto.tsindex 16e89f54d..1ce8de1f2 100644--- a/apps/production/src/project/dto/create-monitor.dto.ts+++ b/apps/production/src/project/dto/create-monitor.dto.ts@@ -12,7 +12,7 @@ import { Type } from 'class-transformer'import { ApiProperty } from '@nestjs/swagger'// Enums for HTTP Methods and Status Codes-enum HttpMethodEnum {+export enum HttpMethodEnum {GET = 'GET',POST = 'POST',PUT = 'PUT',@@ -41,8 +41,13 @@ export enum HttpStatusCodeEnum {GATEWAY_TIMEOUT = 504,}+export enum HttpType {+ HTTP = 'HTTP',+ HTTPS = 'HTTPS',+}+// DTO for HTTP Options-class HttpOptions {+export class HttpOptions {@IsArray()@IsEnum(HttpMethodEnum, { each: true })method: HttpMethodEnum[]@@ -59,8 +64,8 @@ class HttpOptions {// Main DTO for Monitor Requestexport class CreateMonitorHttpRequestDTO {@ApiProperty()- @IsEnum(['HTTP', 'HTTPS'])- monitorType: 'HTTP' | 'HTTPS'+ @IsEnum(HttpType)+ type: HttpType@ApiProperty()@IsString()diff --git a/apps/production/src/project/dto/update-monitor.dto.ts b/apps/production/src/project/dto/update-monitor.dto.tsnew file mode 100644index 000000000..1d1c4d891--- /dev/null+++ b/apps/production/src/project/dto/update-monitor.dto.ts@@ -0,0 +1,56 @@+import {+ IsEnum,+ IsOptional,+ IsString,+ ValidateNested,+ IsUrl,+ IsNumber,+ IsArray,+} from 'class-validator'++import { ApiProperty } from '@nestjs/swagger'+import { HttpStatusCodeEnum, HttpOptions } from './create-monitor.dto'++export class UpdateMonitorHttpRequestDTO {+ @ApiProperty()+ @IsEnum(['HTTP', 'HTTPS'])+ type: 'HTTP' | 'HTTPS'++ @ApiProperty()+ @IsString()+ name: string++ @ApiProperty()+ @IsUrl()+ url: string++ @ApiProperty()+ @IsNumber()+ interval: number++ @ApiProperty()+ @IsNumber()+ retries: number++ @ApiProperty()+ @IsNumber()+ retryInterval: number++ @ApiProperty()+ @IsNumber()+ timeout: number++ @ApiProperty()+ @IsArray()+ @IsEnum(HttpStatusCodeEnum, { each: true })+ acceptedStatusCodes: HttpStatusCodeEnum[]++ @ApiProperty()+ @IsOptional()+ @IsString()+ description?: string++ @ApiProperty()+ @ValidateNested()+ httpOptions: HttpOptions+}diff --git a/apps/production/src/project/entity/monitor.entity.ts b/apps/production/src/project/entity/monitor.entity.tsindex eb4e8986e..2b9c4043d 100644--- a/apps/production/src/project/entity/monitor.entity.ts+++ b/apps/production/src/project/entity/monitor.entity.ts@@ -6,37 +6,48 @@ import {CreateDateColumn,UpdateDateColumn,} from 'typeorm'+import { ApiProperty } from '@nestjs/swagger'import { MonitorGroupEntity } from './monitor-group.entity'@Entity('monitor')export class MonitorEntity {- @PrimaryGeneratedColumn()- id: number+ @ApiProperty()+ @PrimaryGeneratedColumn('uuid')+ id: string+ @ApiProperty()@Column({ type: 'varchar' })type: string+ @ApiProperty()@Column({ type: 'varchar' })name: string+ @ApiProperty()@Column({ type: 'varchar' })url: string+ @ApiProperty()@Column({ type: 'int' })interval: number+ @ApiProperty()@Column({ type: 'int' })retries: number+ @ApiProperty()@Column({ type: 'int' })retryInterval: number+ @ApiProperty()@Column({ type: 'int' })timeout: number+ @ApiProperty()@Column({ type: 'json' })acceptedStatusCodes: number[]+ @ApiProperty()@Column({ type: 'text', nullable: true })description: string | null@@ -45,9 +56,19 @@ export class MonitorEntity {})group: MonitorGroupEntity+ @ApiProperty()@CreateDateColumn()createdAt: Date+ @ApiProperty()@UpdateDateColumn()updatedAt: Date++ @ApiProperty()+ @Column({ type: 'json', nullable: true })+ httpOptions: {+ method: string[]+ body?: Record<string, unknown>+ headers?: Record<string, string>+ }}diff --git a/apps/production/src/project/interfaces/http-request-options.interface.ts b/apps/production/src/project/interfaces/http-request-options.interface.tsnew file mode 100644index 000000000..385c7b6af--- /dev/null+++ b/apps/production/src/project/interfaces/http-request-options.interface.ts@@ -0,0 +1,14 @@+export interface HttpRequestOptions {+ type: string+ url: string+ interval: number+ retries: number+ retryInterval: number+ timeout: number+ acceptedStatusCodes: number[]+ httpOptions: {+ method: string[]+ body?: Record<string, unknown>+ headers?: Record<string, string>+ }+}diff --git a/apps/production/src/project/project.controller.ts b/apps/production/src/project/project.controller.tsindex 3c26b5d26..ebc53311a 100644--- a/apps/production/src/project/project.controller.ts+++ b/apps/production/src/project/project.controller.ts@@ -114,6 +114,7 @@ import { MonitorGroupEntity } from './entity/monitor-group.entity'import { CreateMonitorGroupDto } from './dto/create-monitor-group.dto'import { UpdateMonitorGroupDto } from './dto/update-monitor-group.dto'import { MonitorGroupIdDto } from './dto/monitor-group-id.dto'+import { UpdateMonitorHttpRequestDTO } from './dto/update-monitor.dto'const PROJECTS_MAXIMUM = 50@@ -2375,9 +2376,171 @@ export class ProjectController {throw new NotFoundException('Monitor group not found.')}- // Insert Monitor into DB+ const monitor = await this.projectService.createMonitor(+ body,+ monitorGroup.id,+ )// Create a job in service for sending httpRequest- await this.projectService.sendHttpRequest(body, 'idsome') // TODO+ await this.projectService.sendHttpRequest(monitor.id, monitor)+ }++ @ApiOperation({ summary: 'Get monitor for the project' })+ @ApiBearerAuth()+ @ApiOkResponse({ type: MonitorEntity })+ @Get(':projectId/monitor-groups/:monitorGroupId/monitor/:monitorId')+ @Auth([], true, true)+ public async getMonitor(+ @Param() { projectId }: ProjectIdDto,+ @Param() { monitorGroupId }: MonitorGroupIdDto,+ @Param('monitorId') monitorId: string,+ @CurrentUserId() userId: string,+ ): Promise<MonitorEntity> {+ const project = await this.projectService.findProject(projectId, [+ 'admin',+ 'share',+ ])++ if (!project) {+ throw new NotFoundException('Project not found.')+ }++ const user = await this.userService.findUserV2(userId, ['roles'])++ if (!user) {+ throw new NotFoundException('User not found.')+ }++ this.projectService.allowedToManage(project, userId, user.roles)++ const monitorGroup = await this.projectService.findMonitorGroup(+ monitorGroupId,+ projectId,+ )++ if (!monitorGroup) {+ throw new NotFoundException('Monitor group not found.')+ }++ const monitor = await this.projectService.getMonitor(+ monitorId,+ monitorGroup.id,+ project.id,+ )++ if (!monitor) {+ throw new NotFoundException('Monitor not found.')+ }++ return monitor+ }++ @ApiOperation({ summary: 'Update monitor for the project' })+ @ApiBearerAuth()+ @ApiOkResponse({ type: MonitorEntity })+ @Patch(':projectId/monitor-groups/:monitorGroupId/monitor/:monitorId')+ @Auth([], true, true)+ public async updateMonitor(+ @Param() { projectId }: ProjectIdDto,+ @Param() { monitorGroupId }: MonitorGroupIdDto,+ @Param('monitorId') monitorId: string,+ @Body() body: UpdateMonitorHttpRequestDTO,+ @CurrentUserId() userId: string,+ ): Promise<MonitorEntity> {+ const project = await this.projectService.findProject(projectId, [+ 'admin',+ 'share',+ ])++ if (!project) {+ throw new NotFoundException('Project not found.')+ }++ const user = await this.userService.findUserV2(userId, ['roles'])++ if (!user) {+ throw new NotFoundException('User not found.')+ }++ this.projectService.allowedToManage(project, userId, user.roles)++ const monitorGroup = await this.projectService.findMonitorGroup(+ monitorGroupId,+ projectId,+ )++ if (!monitorGroup) {+ throw new NotFoundException('Monitor group not found.')+ }++ const monitor = await this.projectService.getMonitor(+ monitorId,+ monitorGroup.id,+ project.id,+ )++ if (!monitor) {+ throw new NotFoundException('Monitor not found.')+ }++ const updatedMonitor = await this.projectService.updateMonitor(+ monitorId,+ body,+ )++ await this.projectService.updateHttpRequest(monitor.id, monitor)+ return updatedMonitor+ }++ @ApiOperation({ summary: 'Delete monitor from the project' })+ @ApiBearerAuth()+ @ApiOkResponse({ description: 'Monitor deleted successfully' })+ @Delete(':projectId/monitor-groups/:monitorGroupId/monitor/:monitorId')+ @Auth([], true, true)+ public async deleteMonitor(+ @Param() { projectId }: ProjectIdDto,+ @Param() { monitorGroupId }: MonitorGroupIdDto,+ @Param('monitorId') monitorId: string,+ @CurrentUserId() userId: string,+ ): Promise<void> {+ const project = await this.projectService.findProject(projectId, [+ 'admin',+ 'share',+ ])++ if (!project) {+ throw new NotFoundException('Project not found.')+ }+ const user = await this.userService.findUserV2(userId, ['roles'])++ if (!user) {+ throw new NotFoundException('User not found.')+ }++ this.projectService.allowedToManage(project, userId, user.roles)++ const monitorGroup = await this.projectService.findMonitorGroup(+ monitorGroupId,+ projectId,+ )++ if (!monitorGroup) {+ throw new NotFoundException('Monitor group not found.')+ }++ const monitor = await this.projectService.getMonitor(+ monitorId,+ monitorGroup.id,+ project.id,+ )++ if (!monitor) {+ throw new NotFoundException('Monitor not found.')+ }++ this.projectService.deleteMonitor(monitorId)+ this.projectService.deleteHttpRequest(monitorId)++ this.logger.debug(`Monitor with ID ${monitorId} deleted successfully.`)}}diff --git a/apps/production/src/project/project.service.ts b/apps/production/src/project/project.service.tsindex 1dd193c30..bcba0cf74 100644--- a/apps/production/src/project/project.service.ts+++ b/apps/production/src/project/project.service.ts@@ -81,6 +81,8 @@ import { CreateMonitorGroupDto } from './dto/create-monitor-group.dto'import { MonitorGroupEntity } from './entity/monitor-group.entity'import { UpdateMonitorGroupDto } from './dto/update-monitor-group.dto'import { MonitorEntity } from './entity/monitor.entity'+import { UpdateMonitorHttpRequestDTO } from './dto/update-monitor.dto'+import { HttpRequestOptions } from './interfaces/http-request-options.interface'dayjs.extend(utc)@@ -1478,9 +1480,32 @@ export class ProjectService {return this.monitorRepository.save(monitor)}+ async getMonitor(monitorId: string) {+ return this.monitorRepository.findOne({+ where: {+ id: monitorId,+ },+ })+ }++ // Update a monitor group+ async updateMonitor(+ monitorGroupId: string,+ updateMonitorHttpRequestDto: UpdateMonitorHttpRequestDTO,+ ): Promise<any> {+ await this.monitorRepository.update(+ { id: monitorGroupId },+ updateMonitorHttpRequestDto,+ )+ }++ async deleteMonitor(monitorId: string): Promise<any> {+ return this.monitorRepository.delete(monitorId)+ }+async sendHttpRequest(- monitorHttpRequestDto: CreateMonitorHttpRequestDTO,monitorID: string,+ monitorHttpRequestDto: HttpRequestOptions,) {// TODO crete interface for that stuffawait this.monitorQueue.add('http-request', monitorHttpRequestDto, {@@ -1488,4 +1513,17 @@ export class ProjectService {jobId: monitorID,})}++ async updateHttpRequest(+ monitorID: string,+ updatedHttpRequestDto: HttpRequestOptions,+ ): Promise<void> {+ await this.deleteHttpRequest(monitorID)++ await this.sendHttpRequest(monitorID, updatedHttpRequestDto)+ }++ async deleteHttpRequest(monitorID: string) {+ await this.monitorQueue.remove(monitorID)+ }}diff --git a/docker-compose-dev.yml b/docker-compose-dev.ymlindex 4bd1946d2..f463af4b0 100644--- a/docker-compose-dev.yml+++ b/docker-compose-dev.yml@@ -3,13 +3,13 @@ services:redis:image: redis:7.2-alpineports:- - 6379:6379+ - 6378:6379clickhouse:image: clickhouse/clickhouse-server:23.8-alpineenvironment:CLICKHOUSE_DB: 'analytics'ports:- - 8123:8123+ - 8124:8123mysql:image: mysql:8.0.33environment:@@ -22,7 +22,7 @@ services:image: rediscommander/redis-commander:latestcontainer_name: redis-commanderenvironment:- - REDIS_HOSTS=local:redis:6379+ - REDIS_HOSTS=local:redis:6378ports:- "8081:8081"depends_on:diff --git a/migrations/mysql/2024_09_08.sql b/migrations/mysql/2024_09_08.sqlindex 032b73910..39ccc1b3b 100644--- a/migrations/mysql/2024_09_08.sql+++ b/migrations/mysql/2024_09_08.sql@@ -5,8 +5,8 @@ CREATE TABLE IF NOT EXISTS monitor_groups (CREATE TABLE IF NOT EXISTS monitor (- monitorId INT AUTO_INCREMENT PRIMARY KEY,- monitorType VARCHAR(255) NOT NULL,+ 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,