oneuptime/ProbeAPI/API/Probe.ts
2022-04-22 21:16:27 +01:00

45 lines
1.4 KiB
TypeScript
Executable File

import Express, {
ExpressRequest,
ExpressResponse,
OneUptimeRequest,
ProbeRequest,
ExpressRouter,
} from 'CommonServer/utils/Express';
import MonitorService from 'CommonServer/Services/MonitorService';
import ProbeAuthorization from 'CommonServer/middleware/ProbeAuthorization';
import {
sendErrorResponse,
sendListResponse,
} from 'CommonServer/utils/Response';
import Exception from 'Common/Types/Exception/Exception';
import PositiveNumber from 'Common/Types/PositiveNumber';
import { Document } from 'CommonServer/Infrastructure/ORM';
const router: ExpressRouter = Express.getRouter();
router.get(
'/monitors',
ProbeAuthorization.isAuthorizedProbe,
async (req: ExpressRequest, res: ExpressResponse) => {
try {
const oneUptimeRequest: OneUptimeRequest = req as OneUptimeRequest;
const limit: PositiveNumber = new PositiveNumber(
parseInt((req.query['limit'] as string) || '10')
);
const monitors: Array<Document> =
await MonitorService.getMonitorsNotPingedByProbeInLastMinute(
(oneUptimeRequest.probe as ProbeRequest).id,
limit
);
return sendListResponse(req, res, monitors, monitors.length);
} catch (error) {
return sendErrorResponse(req, res, error as Exception);
}
}
);
export default router;