From fe8aa54f1c7df67acf6fa891fcf7497a096d8038 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Tue, 20 Aug 2024 18:27:04 +0100 Subject: [PATCH] feat: Add API endpoint to get count of pending incoming request monitors --- Ingestor/API/Monitor.ts | 80 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/Ingestor/API/Monitor.ts b/Ingestor/API/Monitor.ts index 919c6255d21..a7ccc1b8390 100644 --- a/Ingestor/API/Monitor.ts +++ b/Ingestor/API/Monitor.ts @@ -30,6 +30,7 @@ import Monitor from "Common/Models/DatabaseModels/Monitor"; import MonitorProbe from "Common/Models/DatabaseModels/MonitorProbe"; import MonitorService from "Common/Server/Services/MonitorService"; import ProjectService from "Common/Server/Services/ProjectService"; +import MonitorType from "Common/Types/Monitor/MonitorType"; const router: ExpressRouter = Express.getRouter(); @@ -119,6 +120,85 @@ router.get( }, ); +router.get( + "/monitor/pending-count/incoming-request", + ClusterKeyAuthorization.isAuthorizedServiceMiddleware, + async ( + req: ExpressRequest, + res: ExpressResponse, + next: NextFunction, + ): Promise => { + try { + // get count of incoming request monitors which are not checked for heartbeat in last 2 minutes + const incomingMonitorPendingCount: PositiveNumber = + await MonitorService.countBy({ + query: { + ...MonitorService.getEnabledMonitorQuery(), + monitorType: MonitorType.IncomingRequest, + project: { + ...ProjectService.getActiveProjectStatusQuery(), + }, + incomingRequestMonitorHeartbeatCheckedAt: + QueryHelper.lessThanEqualToOrNull( + OneUptimeDate.addRemoveMinutes( + OneUptimeDate.getCurrentDate(), + -2, + ), + ), + }, + props: { + isRoot: true, + }, + }); + + const firstMonitorToBeFetched: Monitor | null = + await MonitorService.findOneBy({ + query: { + ...MonitorService.getEnabledMonitorQuery(), + monitorType: MonitorType.IncomingRequest, + project: { + ...ProjectService.getActiveProjectStatusQuery(), + }, + incomingRequestMonitorHeartbeatCheckedAt: + QueryHelper.lessThanEqualToOrNull( + OneUptimeDate.addRemoveMinutes( + OneUptimeDate.getCurrentDate(), + -2, + ), + ), + }, + select: { + incomingRequestMonitorHeartbeatCheckedAt: true, + monitorSteps: true, + monitorType: true, + monitoringInterval: true, + }, + sort: { + incomingRequestMonitorHeartbeatCheckedAt: SortOrder.Ascending, + }, + props: { + isRoot: true, + }, + }); + + return Response.sendJsonObjectResponse(req, res, { + incomingMonitorPendingCount: incomingMonitorPendingCount.toNumber(), + firstMonitorToBeFetched: firstMonitorToBeFetched, + incomingRequestMonitorHeartbeatCheckedAt: + firstMonitorToBeFetched?.incomingRequestMonitorHeartbeatCheckedAt, + friendlyIncomingRequestMonitorHeartbeatCheckedAt: + firstMonitorToBeFetched?.incomingRequestMonitorHeartbeatCheckedAt + ? OneUptimeDate.getDateAsFormattedStringInMultipleTimezones({ + date: firstMonitorToBeFetched?.incomingRequestMonitorHeartbeatCheckedAt, + }) + : "", + }); + } catch (err) { + return next(err); + } + }, +); + // This API returns the count of the monitor waiting to be monitored. router.get( "/monitor/pending-count/:probeId",