add DB and controller methods
This commit is contained in:
1 parent
cac89947a4
commit
cec8ecd985
3 files changed
+78
-3
No files matched your search
@@ -16,6 +16,9 @@ const {
|
|||||||
const sslChecker = require("ssl-checker");
|
const sslChecker = require("ssl-checker");
|
||||||
const SERVICE_NAME = "monitorController";
|
const SERVICE_NAME = "monitorController";
|
||||||
const { errorMessages, successMessages } = require("../utils/messages");
|
const { errorMessages, successMessages } = require("../utils/messages");
|
||||||
|
const {
|
||||||
|
getMonitorsAndSummaryByTeamId,
|
||||||
|
} = require("../db/mongo/modules/monitorModule");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns all monitors
|
* Returns all monitors
|
||||||
@@ -186,7 +189,41 @@ const getMonitorById = async (req, res, next) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns all monitors belong to User with UserID
|
* Returns all monitors and a sumamry for a team with TeamID
|
||||||
|
* @async
|
||||||
|
* @param {Express.Request} req
|
||||||
|
* @param {Express.Response} res
|
||||||
|
* @returns {Promise<Express.Response>}
|
||||||
|
* @throws {Error}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const getMonitorsAndSumamryByTeamId = async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
//validation
|
||||||
|
} catch (error) {
|
||||||
|
error.status = 422;
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
error.method === undefined &&
|
||||||
|
error.method === "getMonitorsAndSummaryByTeamId";
|
||||||
|
error.message =
|
||||||
|
error.details?.[0]?.message || error.message || "Validation Error";
|
||||||
|
next(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { teamId, type } = req.params;
|
||||||
|
await req.db.getMonitorsAndSummaryByTeamId(req, res);
|
||||||
|
} catch (error) {
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
error.method === undefined &&
|
||||||
|
error.method === "getMonitorsAndSummaryByTeamId";
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all monitors belong to team with TeamID
|
||||||
* @async
|
* @async
|
||||||
* @param {Express.Request} req
|
* @param {Express.Request} req
|
||||||
* @param {Express.Response} res
|
* @param {Express.Response} res
|
||||||
@@ -424,6 +461,7 @@ module.exports = {
|
|||||||
getMonitorStatsById,
|
getMonitorStatsById,
|
||||||
getMonitorCertificate,
|
getMonitorCertificate,
|
||||||
getMonitorById,
|
getMonitorById,
|
||||||
|
getMonitorsAndSummaryByTeamId,
|
||||||
getMonitorsByTeamId,
|
getMonitorsByTeamId,
|
||||||
createMonitor,
|
createMonitor,
|
||||||
deleteMonitor,
|
deleteMonitor,
|
||||||
|
|||||||
@@ -64,10 +64,9 @@ const {
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
getAllMonitors,
|
getAllMonitors,
|
||||||
getMonitorAggregateStats,
|
|
||||||
testMethod,
|
|
||||||
getMonitorStatsById,
|
getMonitorStatsById,
|
||||||
getMonitorById,
|
getMonitorById,
|
||||||
|
getMonitorsAndSummaryByTeamId,
|
||||||
getMonitorsByTeamId,
|
getMonitorsByTeamId,
|
||||||
createMonitor,
|
createMonitor,
|
||||||
deleteMonitor,
|
deleteMonitor,
|
||||||
@@ -148,6 +147,7 @@ module.exports = {
|
|||||||
getAllMonitors,
|
getAllMonitors,
|
||||||
getMonitorStatsById,
|
getMonitorStatsById,
|
||||||
getMonitorById,
|
getMonitorById,
|
||||||
|
getMonitorsAndSummaryByTeamId,
|
||||||
getMonitorsByTeamId,
|
getMonitorsByTeamId,
|
||||||
createMonitor,
|
createMonitor,
|
||||||
deleteMonitor,
|
deleteMonitor,
|
||||||
|
|||||||
@@ -292,6 +292,42 @@ const getMonitorById = async (monitorId) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get monitors and Summary by TeamID
|
||||||
|
* @async
|
||||||
|
* @param {Express.Request} req
|
||||||
|
* @param {Express.Response} res
|
||||||
|
* @returns {Promise<Array<Monitor>>}
|
||||||
|
* @throws {Error}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const getMonitorsAndSummaryByTeamId = async (teamId, type) => {
|
||||||
|
try {
|
||||||
|
const monitors = await Monitor.find({ teamId, type });
|
||||||
|
const monitorCounts = monitors.reduce(
|
||||||
|
(acc, monitor) => {
|
||||||
|
if (monitor.status === true) {
|
||||||
|
acc.up += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (monitor.status === false) {
|
||||||
|
acc.up += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (monitor.isActive === false) {
|
||||||
|
acc.paused += 1;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ up: 0, down: 0, paused: 0 }
|
||||||
|
);
|
||||||
|
monitorCounts.total = monitors.length;
|
||||||
|
return { monitors, monitorCounts };
|
||||||
|
} catch (error) {
|
||||||
|
error.methodName = "getMonitorsAndSummaryByTeamId";
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get monitors by TeamID
|
* Get monitors by TeamID
|
||||||
* @async
|
* @async
|
||||||
@@ -464,6 +500,7 @@ module.exports = {
|
|||||||
getAllMonitors,
|
getAllMonitors,
|
||||||
getMonitorStatsById,
|
getMonitorStatsById,
|
||||||
getMonitorById,
|
getMonitorById,
|
||||||
|
getMonitorsAndSummaryByTeamId,
|
||||||
getMonitorsByTeamId,
|
getMonitorsByTeamId,
|
||||||
createMonitor,
|
createMonitor,
|
||||||
deleteMonitor,
|
deleteMonitor,
|
||||||
|
|||||||
Reference in new issue
Block a user