Skip to content

Commit

Permalink
feat: Add API endpoint to get count of pending incoming request monitors
Browse files Browse the repository at this point in the history
  • Loading branch information
simlarsen committed Aug 20, 2024
1 parent 5237384 commit fe8aa54
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions Ingestor/API/Monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -119,6 +120,85 @@ router.get(
},
);

router.get(
"/monitor/pending-count/incoming-request",
ClusterKeyAuthorization.isAuthorizedServiceMiddleware,
async (
req: ExpressRequest,
res: ExpressResponse,
next: NextFunction,
): Promise<void> => {
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",
Expand Down

0 comments on commit fe8aa54

Please sign in to comment.