From ba1e5487c24731743ad98ae51b18e2a4794ccac8 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Wed, 3 May 2023 13:15:03 +0100 Subject: [PATCH] add monitor type --- Common/Types/Monitor/MonitorType.ts | 4 +- CommonServer/Services/MonitorService.ts | 65 +++++++++++++++++++++++++ Model/Models/Probe.ts | 34 +++++++++++++ ProbeAPI/API/Register.ts | 1 + 4 files changed, 102 insertions(+), 2 deletions(-) diff --git a/Common/Types/Monitor/MonitorType.ts b/Common/Types/Monitor/MonitorType.ts index 7ae24d9a60..f240cd59c0 100644 --- a/Common/Types/Monitor/MonitorType.ts +++ b/Common/Types/Monitor/MonitorType.ts @@ -5,9 +5,9 @@ enum MonitorType { Website = 'Website', API = 'API', Ping = 'Ping', - // Kubernetes = 'Kubernetes', + Kubernetes = 'Kubernetes', IP = 'IP', - // IncomingRequest = 'IncomingRequest', + IncomingRequest = 'IncomingRequest', } export default MonitorType; diff --git a/CommonServer/Services/MonitorService.ts b/CommonServer/Services/MonitorService.ts index e0eb5d8348..5a1450bf17 100644 --- a/CommonServer/Services/MonitorService.ts +++ b/CommonServer/Services/MonitorService.ts @@ -9,6 +9,12 @@ import MonitorStatusTimelineService from './MonitorStatusTimelineService'; import ObjectID from 'Common/Types/ObjectID'; import DatabaseCommonInteractionProps from 'Common/Types/Database/DatabaseCommonInteractionProps'; import MonitorStatusTimeline from 'Model/Models/MonitorStatusTimeline'; +import ProbeService from './ProbeService'; +import { LIMIT_PER_PROJECT } from 'Common/Types/Database/LimitMax'; +import MonitorProbe from 'Model/Models/MonitorProbe'; +import OneUptimeDate from 'Common/Types/Date'; +import MonitorProbeService from './MonitorProbeService'; +import MonitorType from 'Common/Types/Monitor/MonitorType'; export class Service extends DatabaseService { public constructor(postgresDatabase?: PostgresDatabase) { @@ -69,9 +75,68 @@ export class Service extends DatabaseService { createdItem.currentMonitorStatusId, onCreate.createBy.props ); + + if(createdItem.monitorType && (createdItem.monitorType === MonitorType.API || createdItem.monitorType === MonitorType.IncomingRequest || createdItem.monitorType === MonitorType.Website || createdItem.monitorType === MonitorType.Ping || createdItem.monitorType === MonitorType.IP)) { + await this.addDefaultProbesToMonitor(createdItem.projectId, createdItem.id); + } + return createdItem; } + + public async addDefaultProbesToMonitor(projectId: ObjectID, monitorId: ObjectID) { + const globalProbes = await ProbeService.findBy({ + query: { + isGlobalProbe: true, + shouldAutoEnableProbeOnNewMonitors: true + }, + select: { + _id: true + }, + skip: 0, + limit: LIMIT_PER_PROJECT, + props: { + isRoot: true + } + }); + + const projectProbes = await ProbeService.findBy({ + query: { + isGlobalProbe: false, + shouldAutoEnableProbeOnNewMonitors: true, + projectId: projectId + }, + select: { + _id: true + }, + skip: 0, + limit: LIMIT_PER_PROJECT, + props: { + isRoot: true + } + }) + + const totalProbes = [...globalProbes, ...projectProbes]; + + for (const probe of totalProbes) { + const monitorProbe: MonitorProbe = new MonitorProbe(); + + monitorProbe.monitorId = monitorId; + monitorProbe.probeId = probe.id!; + monitorProbe.projectId = projectId; + monitorProbe.isEnabled = true; + monitorProbe.lastPingAt = OneUptimeDate.getCurrentDate(); + monitorProbe.nextPingAt = OneUptimeDate.getCurrentDate(); + + await MonitorProbeService.create({ + data: monitorProbe, + props: { + isRoot: true + } + }); + } + } + public async changeMonitorStatus( projectId: ObjectID, monitorIds: Array, diff --git a/Model/Models/Probe.ts b/Model/Models/Probe.ts index ca13b4f414..2bb934ef6d 100755 --- a/Model/Models/Probe.ts +++ b/Model/Models/Probe.ts @@ -427,4 +427,38 @@ export default class Probe extends BaseModel { default: false, }) public isGlobalProbe?: boolean = undefined; + + + @ColumnAccessControl({ + create: [ + Permission.ProjectOwner, + Permission.ProjectAdmin, + Permission.ProjectMember, + Permission.CanCreateProjectStatusPage, + ], + read: [ + Permission.ProjectOwner, + Permission.ProjectAdmin, + Permission.ProjectMember, + Permission.CanReadProjectStatusPage, + ], + update: [ + Permission.ProjectOwner, + Permission.ProjectAdmin, + Permission.ProjectMember, + Permission.CanEditProjectStatusPage, + ], + }) + @TableColumn({ + isDefaultValueColumn: true, + required: true, + type: TableColumnType.Boolean, + }) + @Column({ + type: ColumnType.Boolean, + nullable: false, + unique: false, + default: false, + }) + public shouldAutoEnableProbeOnNewMonitors?: boolean = undefined; } diff --git a/ProbeAPI/API/Register.ts b/ProbeAPI/API/Register.ts index 3c11b35f95..a4d2831da1 100644 --- a/ProbeAPI/API/Register.ts +++ b/ProbeAPI/API/Register.ts @@ -75,6 +75,7 @@ router.post( newProbe.name = data['probeName'] as string; newProbe.description = data['probeDescription'] as string; newProbe.lastAlive = OneUptimeDate.getCurrentDate(); + newProbe.shouldAutoEnableProbeOnNewMonitors = true; newProbe = await ProbeService.create({ data: newProbe,