oneuptime/ProbeAPI/API/Monitor.ts

117 lines
4.1 KiB
TypeScript
Raw Normal View History

2023-05-03 20:13:00 +00:00
import Express, {
ExpressRequest,
ExpressResponse,
ExpressRouter,
NextFunction,
} from 'CommonServer/Utils/Express';
import Response from 'CommonServer/Utils/Response';
2023-05-05 11:55:51 +00:00
import ProbeAuthorization from '../Middleware/ProbeAuthorization';
import MonitorProbe from 'Model/Models/MonitorProbe';
import MonitorProbeService from 'CommonServer/Services/MonitorProbeService';
import QueryHelper from 'CommonServer/Types/Database/QueryHelper';
2023-05-03 20:13:00 +00:00
import OneUptimeDate from 'Common/Types/Date';
2023-05-05 11:55:51 +00:00
import { ProbeExpressRequest } from '../Types/Request';
import BadDataException from 'Common/Types/Exception/BadDataException';
2023-05-05 12:02:23 +00:00
import CronTab from 'CommonServer/Utils/CronTab';
2023-05-05 11:55:51 +00:00
import Monitor from 'Model/Models/Monitor';
import PositiveNumber from 'Common/Types/PositiveNumber';
2023-05-05 12:02:23 +00:00
import { JSONObject } from 'Common/Types/JSON';
2023-05-11 12:25:40 +00:00
import SubscriptionStatus from 'Common/Types/Billing/SubscriptionStatus';
2023-05-03 20:13:00 +00:00
const router: ExpressRouter = Express.getRouter();
router.post(
'/monitor/list',
2023-05-05 11:55:51 +00:00
ProbeAuthorization.isAuthorizedServiceMiddleware,
2023-05-03 20:13:00 +00:00
async (
req: ExpressRequest,
res: ExpressResponse,
next: NextFunction
): Promise<void> => {
try {
2023-05-05 12:02:23 +00:00
const data: JSONObject = req.body;
const limit: number = (data['limit'] as number) || 100;
2023-05-05 11:55:51 +00:00
2023-05-05 12:02:23 +00:00
if (
!(req as ProbeExpressRequest).probe ||
!(req as ProbeExpressRequest).probe?.id
) {
2023-05-03 20:13:00 +00:00
return Response.sendErrorResponse(
req,
res,
2023-05-05 11:55:51 +00:00
new BadDataException('Probe not found')
2023-05-03 20:13:00 +00:00
);
}
2023-05-05 11:55:51 +00:00
//get list of monitors to be monitored
2023-05-05 12:02:23 +00:00
const monitorProbes: Array<MonitorProbe> =
await MonitorProbeService.findBy({
query: {
probeId: (req as ProbeExpressRequest).probe!.id!,
isEnabled: true,
2023-05-10 17:53:36 +00:00
nextPingAt: QueryHelper.lessThanEqualToOrNull(
2023-05-05 12:02:23 +00:00
OneUptimeDate.getCurrentDate()
),
2023-05-11 12:25:40 +00:00
project: {
// get only active projects
paymentProviderSubscriptionStatus: QueryHelper.equalToOrNull([SubscriptionStatus.Active, SubscriptionStatus.Trialing])
}
2023-05-05 12:02:23 +00:00
},
skip: 0,
limit: limit,
select: {
probeId: true,
monitorId: true,
},
populate: {
monitor: {
monitorSteps: true,
monitorType: true,
monitoringInterval: true,
},
},
props: {
isRoot: true,
},
});
2023-05-03 20:13:00 +00:00
2023-05-05 11:55:51 +00:00
// update the lastMonitoredAt field of the monitors
2023-05-03 20:13:00 +00:00
2023-05-05 12:02:23 +00:00
for (const monitorProbe of monitorProbes) {
2023-05-05 11:55:51 +00:00
await MonitorProbeService.updateOneById({
id: monitorProbe.id!,
data: {
lastPingAt: OneUptimeDate.getCurrentDate(),
2023-05-05 12:02:23 +00:00
nextPingAt: CronTab.getNextExecutionTime(
monitorProbe?.monitor?.monitoringInterval as string
),
2023-05-05 11:55:51 +00:00
},
props: {
2023-05-05 12:02:23 +00:00
isRoot: true,
},
2023-05-05 11:55:51 +00:00
});
}
2023-05-03 20:13:00 +00:00
2023-05-05 12:02:23 +00:00
const monitors: Array<Monitor> = monitorProbes.map(
(monitorProbe: MonitorProbe) => {
return monitorProbe.monitor!;
}
);
2023-05-05 11:55:51 +00:00
// return the list of monitors to be monitored
2023-05-05 12:02:23 +00:00
return Response.sendEntityArrayResponse(
req,
res,
monitors,
new PositiveNumber(monitors.length),
Monitor
);
2023-05-03 20:13:00 +00:00
} catch (err) {
return next(err);
}
}
);
export default router;