From 56d3e7e1f228447868bbdcf6f6694712fc6803ef Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Mon, 1 Jul 2024 20:00:16 +0100 Subject: [PATCH] chore: Update probeKey type to string in CustomProbeDocumentation and ProbeView components This commit updates the probeKey prop type from ObjectID to string in the CustomProbeDocumentation and ProbeView components. The probeKey is used to identify the probe and was previously defined as ObjectID, but it should be a string. This change ensures that the probeKey is correctly passed and used throughout the components, improving the accuracy and reliability of the application. --- .../Jobs/Probe/UpdateConnectionStatus.ts | 19 ------ CommonServer/Services/ProbeService.ts | 59 ++++++++++++++++++- .../ResetObjectID/ResetObjectID.tsx | 2 +- .../Probe/CustomProbeDocumentation.tsx | 2 +- Dashboard/src/Pages/Settings/ProbeView.tsx | 2 +- Ingestor/API/Register.ts | 5 +- Model/Models/Probe.ts | 8 +-- Probe/Services/Register.ts | 2 +- 8 files changed, 66 insertions(+), 33 deletions(-) diff --git a/App/FeatureSet/Workers/Jobs/Probe/UpdateConnectionStatus.ts b/App/FeatureSet/Workers/Jobs/Probe/UpdateConnectionStatus.ts index 16ae7ada24..35fb2e44d2 100644 --- a/App/FeatureSet/Workers/Jobs/Probe/UpdateConnectionStatus.ts +++ b/App/FeatureSet/Workers/Jobs/Probe/UpdateConnectionStatus.ts @@ -62,16 +62,8 @@ RunCron( connectionStatus = ProbeConnectionStatus.Disconnected; } - let shouldNotifyProbeOwner: boolean = false; let shouldUpdateConnectionStatus: boolean = false; - if ( - probe.connectionStatus && - probe.connectionStatus !== connectionStatus - ) { - shouldNotifyProbeOwner = true; - } - if (probe.connectionStatus !== connectionStatus) { shouldUpdateConnectionStatus = true; } @@ -93,17 +85,6 @@ RunCron( isRoot: true, }, }); - - if (!probe.projectId) { - continue; - } - - if (shouldNotifyProbeOwner) { - await ProbeService.notifyOwnersOnStatusChange({ - probeId: probe.id, - connectionStatus: connectionStatus, - }); - } } } catch (error) { logger.error(error); diff --git a/CommonServer/Services/ProbeService.ts b/CommonServer/Services/ProbeService.ts index 900834a96a..a2f91a69bb 100644 --- a/CommonServer/Services/ProbeService.ts +++ b/CommonServer/Services/ProbeService.ts @@ -1,14 +1,14 @@ import User from "Model/Models/User"; import PostgresDatabase from "../Infrastructure/PostgresDatabase"; import CreateBy from "../Types/Database/CreateBy"; -import { OnCreate } from "../Types/Database/Hooks"; +import { OnCreate, OnUpdate } from "../Types/Database/Hooks"; import DatabaseService from "./DatabaseService"; import ObjectID from "Common/Types/ObjectID"; import Version from "Common/Types/Version"; import Model, { ProbeConnectionStatus } from "Model/Models/Probe"; import ProbeOwnerUser from "Model/Models/ProbeOwnerUser"; import ProbeOwnerUserService from "./ProbeOwnerUserService"; -import { LIMIT_PER_PROJECT } from "Common/Types/Database/LimitMax"; +import LIMIT_MAX, { LIMIT_PER_PROJECT } from "Common/Types/Database/LimitMax"; import ProbeOwnerTeam from "Model/Models/ProbeOwnerTeam"; import ProbeOwnerTeamService from "./ProbeOwnerTeamService"; import TeamMemberService from "./TeamMemberService"; @@ -25,6 +25,7 @@ import { EmailEnvelope } from "Common/Types/Email/EmailMessage"; import EmailTemplateType from "Common/Types/Email/EmailTemplateType"; import DatabaseConfig from "../DatabaseConfig"; import URL from "Common/Types/API/URL"; +import UpdateBy from "../Types/Database/UpdateBy"; export class Service extends DatabaseService { public constructor(postgresDatabase?: PostgresDatabase) { @@ -35,7 +36,7 @@ export class Service extends DatabaseService { createBy: CreateBy, ): Promise> { if (!createBy.data.key) { - createBy.data.key = ObjectID.generate(); + createBy.data.key = ObjectID.generate().toString(); } if (!createBy.data.probeVersion) { @@ -118,6 +119,58 @@ export class Service extends DatabaseService { return users; } + protected override async onBeforeUpdate( + updateBy: UpdateBy, + ): Promise> { + const carryForward: any = { + probesToNotifyOwners: [], + }; + + if (updateBy.data.connectionStatus && updateBy.query.id) { + const probes: Array = await this.findBy({ + query: updateBy.query, + props: { + isRoot: true, + }, + select: { + _id: true, + connectionStatus: true, + }, + skip: 0, + limit: LIMIT_MAX, + }); + + const probesToNotifyOwners: Array = probes.filter( + (probe: Model) => { + return ( + probe.connectionStatus && + probe.connectionStatus !== updateBy.data.connectionStatus + ); + }, + ); + + carryForward.probesToNotifyOwners = probesToNotifyOwners; + } + + return { updateBy: updateBy, carryForward }; + } + + protected override async onUpdateSuccess( + onUpdate: OnUpdate, + _updatedItemIds: Array, + ): Promise> { + if (onUpdate.carryForward.probesToNotifyOwners.length > 0) { + for (const probe of onUpdate.carryForward.probesToNotifyOwners) { + await this.notifyOwnersOnStatusChange({ + probeId: probe.id!, + connectionStatus: probe.connectionStatus!, + }); + } + } + + return Promise.resolve(onUpdate); + } + public async notifyOwnersOnStatusChange(data: { probeId: ObjectID; connectionStatus: ProbeConnectionStatus; diff --git a/CommonUI/src/Components/ResetObjectID/ResetObjectID.tsx b/CommonUI/src/Components/ResetObjectID/ResetObjectID.tsx index fda59b4bce..a63e31d57c 100644 --- a/CommonUI/src/Components/ResetObjectID/ResetObjectID.tsx +++ b/CommonUI/src/Components/ResetObjectID/ResetObjectID.tsx @@ -42,7 +42,7 @@ const ResetObjectID: ( modelType: props.modelType, id: props.modelId, data: { - [props.fieldName]: resetIdTo, + [props.fieldName]: resetIdTo.toString(), }, }); setNewId(resetIdTo); diff --git a/Dashboard/src/Components/Probe/CustomProbeDocumentation.tsx b/Dashboard/src/Components/Probe/CustomProbeDocumentation.tsx index 405a0ca3e6..6eefdfb819 100644 --- a/Dashboard/src/Components/Probe/CustomProbeDocumentation.tsx +++ b/Dashboard/src/Components/Probe/CustomProbeDocumentation.tsx @@ -5,7 +5,7 @@ import { HOST, HTTP_PROTOCOL } from "CommonUI/src/Config"; import React, { FunctionComponent, ReactElement } from "react"; export interface ComponentProps { - probeKey: ObjectID; + probeKey: string; probeId: ObjectID; } diff --git a/Dashboard/src/Pages/Settings/ProbeView.tsx b/Dashboard/src/Pages/Settings/ProbeView.tsx index 3d4e5188e1..521a59f74f 100644 --- a/Dashboard/src/Pages/Settings/ProbeView.tsx +++ b/Dashboard/src/Pages/Settings/ProbeView.tsx @@ -41,7 +41,7 @@ const TeamView: FunctionComponent = ( ): ReactElement => { const [modelId] = useState(Navigation.getLastParamAsObjectID()); - const [probeKey, setProbeKey] = useState(null); + const [probeKey, setProbeKey] = useState(null); return ( diff --git a/Ingestor/API/Register.ts b/Ingestor/API/Register.ts index 26c0e45901..fe9d82671d 100644 --- a/Ingestor/API/Register.ts +++ b/Ingestor/API/Register.ts @@ -1,7 +1,6 @@ import OneUptimeDate from "Common/Types/Date"; import BadDataException from "Common/Types/Exception/BadDataException"; import { JSONObject } from "Common/Types/JSON"; -import ObjectID from "Common/Types/ObjectID"; import ClusterKeyAuthorization from "CommonServer/Middleware/ClusterKeyAuthorization"; import ProbeService from "CommonServer/Services/ProbeService"; import Express, { @@ -39,7 +38,7 @@ router.post( const probe: Probe | null = await ProbeService.findOneBy({ query: { - key: new ObjectID(probeKey), + key: probeKey, isGlobalProbe: true, }, select: { @@ -72,7 +71,7 @@ router.post( let newProbe: Probe = new Probe(); newProbe.isGlobalProbe = true; - newProbe.key = new ObjectID(probeKey); + newProbe.key = probeKey; newProbe.name = data["probeName"] as string; newProbe.description = data["probeDescription"] as string; newProbe.lastAlive = OneUptimeDate.getCurrentDate(); diff --git a/Model/Models/Probe.ts b/Model/Models/Probe.ts index ceca80ff72..3e684a2b24 100755 --- a/Model/Models/Probe.ts +++ b/Model/Models/Probe.ts @@ -98,15 +98,14 @@ export default class Probe extends BaseModel { @TableColumn({ required: true, unique: true, - type: TableColumnType.ObjectID, + type: TableColumnType.ShortText, }) @Column({ - type: ColumnType.ObjectID, + type: ColumnType.ShortText, nullable: false, unique: true, - transformer: ObjectID.getDatabaseTransformer(), }) - public key?: ObjectID = undefined; + public key?: string = undefined; @ColumnAccessControl({ create: [ @@ -510,6 +509,7 @@ export default class Probe extends BaseModel { title: "Connection Status", description: "Connection Status of the Probe", type: TableColumnType.ShortText, + canReadOnRelationQuery: true, }) @Column({ type: ColumnType.ShortText, diff --git a/Probe/Services/Register.ts b/Probe/Services/Register.ts index 0ee8c3ade8..09b7c292fd 100644 --- a/Probe/Services/Register.ts +++ b/Probe/Services/Register.ts @@ -31,7 +31,7 @@ export default class Register { if (!pingMonitoringCheck && websiteMonitoringCheck) { // probe is online but ping monitoring is blocked by the cloud provider. Fallback to port monitoring. logger.warn( - "Ping monitoring is on this machine. Fallback to port monitoring", + "Ping monitoring is disabled on this machine. Fallback to port monitoring", ); LocalCache.setString("PROBE", "PING_MONITORING", "PORT"); }