Add network requests and validation
This commit is contained in:
1 parent
cec8ecd985
commit
9eaab0da1a
6 files changed
+63
-16
No files matched your search
@@ -53,14 +53,10 @@ export const getUptimeMonitorsByTeamId = createAsyncThunk(
|
|||||||
async (token, thunkApi) => {
|
async (token, thunkApi) => {
|
||||||
const user = jwtDecode(token);
|
const user = jwtDecode(token);
|
||||||
try {
|
try {
|
||||||
const res = await networkService.getMonitorsByTeamId(
|
const res = await networkService.getMonitorsAndSummaryByTeamId(
|
||||||
token,
|
token,
|
||||||
user.teamId,
|
user.teamId,
|
||||||
25,
|
["http", "ping"]
|
||||||
["http", "ping"],
|
|
||||||
null,
|
|
||||||
"desc",
|
|
||||||
true
|
|
||||||
);
|
);
|
||||||
return res.data;
|
return res.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -58,9 +58,28 @@ class NetworkService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getMonitorsAndSummaryByTeamId(authToken, teamId, types) {
|
||||||
|
const params = new URLSearchParams();
|
||||||
|
|
||||||
|
if (types) {
|
||||||
|
types.forEach((type) => {
|
||||||
|
params.append("type", type);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return this.axiosInstance.get(
|
||||||
|
`/monitors/team/summary/${teamId}?${params.toString()}`,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${authToken}`,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ************************************
|
* ************************************
|
||||||
* Get all uptime monitors for a user
|
* Get all uptime monitors for a Team
|
||||||
* ************************************
|
* ************************************
|
||||||
*
|
*
|
||||||
* @async
|
* @async
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ const {
|
|||||||
getMonitorsByTeamIdValidation,
|
getMonitorsByTeamIdValidation,
|
||||||
createMonitorBodyValidation,
|
createMonitorBodyValidation,
|
||||||
editMonitorBodyValidation,
|
editMonitorBodyValidation,
|
||||||
|
getMonitorsAndSumamryByTeamIdParamValidation,
|
||||||
|
getMonitorsAndSummaryByTeamIdQueryValidation,
|
||||||
getMonitorsByTeamIdQueryValidation,
|
getMonitorsByTeamIdQueryValidation,
|
||||||
pauseMonitorParamValidation,
|
pauseMonitorParamValidation,
|
||||||
getMonitorStatsByIdParamValidation,
|
getMonitorStatsByIdParamValidation,
|
||||||
@@ -16,10 +18,6 @@ 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
|
||||||
* @async
|
* @async
|
||||||
@@ -197,8 +195,12 @@ const getMonitorById = async (req, res, next) => {
|
|||||||
* @throws {Error}
|
* @throws {Error}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const getMonitorsAndSumamryByTeamId = async (req, res, next) => {
|
const getMonitorsAndSummaryByTeamId = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
|
await getMonitorsAndSumamryByTeamIdParamValidation.validateAsync(
|
||||||
|
req.params
|
||||||
|
);
|
||||||
|
await getMonitorsAndSummaryByTeamIdQueryValidation.validateAsync(req.query);
|
||||||
//validation
|
//validation
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.status = 422;
|
error.status = 422;
|
||||||
@@ -212,8 +214,17 @@ const getMonitorsAndSumamryByTeamId = async (req, res, next) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { teamId, type } = req.params;
|
const { teamId } = req.params;
|
||||||
await req.db.getMonitorsAndSummaryByTeamId(req, res);
|
const { type } = req.query;
|
||||||
|
const monitorsSummary = await req.db.getMonitorsAndSummaryByTeamId(
|
||||||
|
teamId,
|
||||||
|
type
|
||||||
|
);
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: successMessages.MONITOR_GET_BY_USER_ID(teamId),
|
||||||
|
data: monitorsSummary,
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
error.method === undefined &&
|
error.method === undefined &&
|
||||||
|
|||||||
@@ -311,19 +311,21 @@ const getMonitorsAndSummaryByTeamId = async (teamId, type) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (monitor.status === false) {
|
if (monitor.status === false) {
|
||||||
acc.up += 1;
|
acc.down += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (monitor.isActive === false) {
|
if (monitor.isActive === false) {
|
||||||
acc.paused += 1;
|
acc.paused += 1;
|
||||||
}
|
}
|
||||||
|
return acc;
|
||||||
},
|
},
|
||||||
{ up: 0, down: 0, paused: 0 }
|
{ up: 0, down: 0, paused: 0 }
|
||||||
);
|
);
|
||||||
|
console.log(monitorCounts);
|
||||||
monitorCounts.total = monitors.length;
|
monitorCounts.total = monitors.length;
|
||||||
return { monitors, monitorCounts };
|
return { monitors, monitorCounts };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.methodName = "getMonitorsAndSummaryByTeamId";
|
error.method = "getMonitorsAndSummaryByTeamId";
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -7,6 +7,10 @@ router.get("/aggregate/:monitorId", monitorController.getMonitorAggregateStats);
|
|||||||
router.get("/stats/:monitorId", monitorController.getMonitorStatsById);
|
router.get("/stats/:monitorId", monitorController.getMonitorStatsById);
|
||||||
router.get("/certificate/:monitorId", monitorController.getMonitorCertificate);
|
router.get("/certificate/:monitorId", monitorController.getMonitorCertificate);
|
||||||
router.get("/:monitorId", monitorController.getMonitorById);
|
router.get("/:monitorId", monitorController.getMonitorById);
|
||||||
|
router.get(
|
||||||
|
"/team/summary/:teamId",
|
||||||
|
monitorController.getMonitorsAndSummaryByTeamId
|
||||||
|
);
|
||||||
router.get("/team/:teamId", monitorController.getMonitorsByTeamId);
|
router.get("/team/:teamId", monitorController.getMonitorsByTeamId);
|
||||||
|
|
||||||
router.post(
|
router.post(
|
||||||
|
|||||||
@@ -161,6 +161,19 @@ const getMonitorByIdQueryValidation = joi.object({
|
|||||||
normalize: joi.boolean(),
|
normalize: joi.boolean(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const getMonitorsAndSumamryByTeamIdParamValidation = joi.object({
|
||||||
|
teamId: joi.string().required(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const getMonitorsAndSummaryByTeamIdQueryValidation = joi.object({
|
||||||
|
type: joi
|
||||||
|
.alternatives()
|
||||||
|
.try(
|
||||||
|
joi.string().valid("http", "ping", "pagespeed"),
|
||||||
|
joi.array().items(joi.string().valid("http", "ping", "pagespeed"))
|
||||||
|
),
|
||||||
|
});
|
||||||
|
|
||||||
const getMonitorsByTeamIdValidation = joi.object({
|
const getMonitorsByTeamIdValidation = joi.object({
|
||||||
teamId: joi.string().required(),
|
teamId: joi.string().required(),
|
||||||
});
|
});
|
||||||
@@ -381,6 +394,8 @@ module.exports = {
|
|||||||
createMonitorBodyValidation,
|
createMonitorBodyValidation,
|
||||||
getMonitorByIdParamValidation,
|
getMonitorByIdParamValidation,
|
||||||
getMonitorByIdQueryValidation,
|
getMonitorByIdQueryValidation,
|
||||||
|
getMonitorsAndSumamryByTeamIdParamValidation,
|
||||||
|
getMonitorsAndSummaryByTeamIdQueryValidation,
|
||||||
getMonitorsByTeamIdValidation,
|
getMonitorsByTeamIdValidation,
|
||||||
getMonitorsByTeamIdQueryValidation,
|
getMonitorsByTeamIdQueryValidation,
|
||||||
getMonitorStatsByIdParamValidation,
|
getMonitorStatsByIdParamValidation,
|
||||||
|
|||||||
Reference in new issue
Block a user