diff --git a/Client/src/Pages/Monitors/Details/index.jsx b/Client/src/Pages/Monitors/Details/index.jsx index 6f7d36d3d..aebde997b 100644 --- a/Client/src/Pages/Monitors/Details/index.jsx +++ b/Client/src/Pages/Monitors/Details/index.jsx @@ -119,10 +119,8 @@ const DetailsPage = () => { const fetchDataForTable = useCallback(async () => { try { - const limit = 0; - const res = await axiosInstance.get( - `/monitors/${monitorId}?sortOrder=asc&limit=${limit}`, + `/checks/${monitorId}?sortOrder=desc&filter=${filter}`, { headers: { Authorization: `Bearer ${authToken}`, @@ -135,7 +133,7 @@ const DetailsPage = () => { { id: 2, name: "Date & Time" }, { id: 3, name: "Message" }, ], - rows: res.data.data.checks.map((check, idx) => { + rows: res.data.data.map((check, idx) => { const status = check.status === true ? "up" : "down"; return { @@ -165,7 +163,7 @@ const DetailsPage = () => { } catch (error) { console.log(error); } - }, [authToken, monitorId]); + }, [authToken, monitorId, filter]); const fetchMonitor = useCallback(async () => { try { diff --git a/Server/controllers/checkController.js b/Server/controllers/checkController.js index f3c85352f..5d939cbad 100644 --- a/Server/controllers/checkController.js +++ b/Server/controllers/checkController.js @@ -2,6 +2,7 @@ const { createCheckParamValidation, createCheckBodyValidation, getChecksParamValidation, + getChecksQueryValidation, deleteChecksParamValidation, } = require("../validation/joi"); const { successMessages } = require("../utils/messages"); @@ -35,6 +36,7 @@ const createCheck = async (req, res, next) => { const getChecks = async (req, res, next) => { try { await getChecksParamValidation.validateAsync(req.params); + await getChecksQueryValidation.validateAsync(req.query); } catch (error) { error.status = 422; error.service = SERVICE_NAME; @@ -45,7 +47,7 @@ const getChecks = async (req, res, next) => { } try { - const checks = await req.db.getChecks(req.params.monitorId); + const checks = await req.db.getChecks(req); return res .status(200) .json({ success: true, msg: successMessages.CHECK_GET, data: checks }); diff --git a/Server/db/mongo/modules/checkModule.js b/Server/db/mongo/modules/checkModule.js index 35c313fd9..3cf5de3e5 100644 --- a/Server/db/mongo/modules/checkModule.js +++ b/Server/db/mongo/modules/checkModule.js @@ -30,9 +30,37 @@ const createCheck = async (checkData) => { * @throws {Error} */ -const getChecks = async (monitorId) => { +const getChecks = async (req) => { + const filterLookup = { + day: new Date(new Date().setDate(new Date().getDate() - 1)), + week: new Date(new Date().setDate(new Date().getDate() - 7)), + month: new Date(new Date().setMonth(new Date().getMonth() - 1)), + }; + try { - const checks = await Check.find({ monitorId }); + const { monitorId } = req.params; + let { sortOrder, limit, filter } = req.query; + + // Default limit to 0 if not provided + if (limit === undefined) limit = 0; + + // Default sort order is newest -> oldest + if (sortOrder === "asc") { + sortOrder = 1; + } else if (sortOrder === "desc") { + sortOrder = -1; + } else sortOrder = -1; + + // Build query + const checksQuery = { monitorId: monitorId }; + // Filter checks by "day", "week", or "month" + if (filter !== undefined) { + checksQuery.createdAt = { $gte: filterLookup[filter] }; + } + + const checks = await Check.find(checksQuery) + .limit(limit) + .sort({ createdAt: sortOrder }); return checks; } catch (error) { throw error; diff --git a/Server/utils/dataUtils.js b/Server/utils/dataUtils.js index 3495cc3d6..f1c4b39b2 100644 --- a/Server/utils/dataUtils.js +++ b/Server/utils/dataUtils.js @@ -19,7 +19,6 @@ const NormalizeData = (checks, rangeMin, rangeMax) => { const normalizedChecks = checks.map((check) => { const originalResponseTime = check.responseTime; - console.log(originalResponseTime); // Normalize the response time between 1 and 100 let normalizedResponseTime = rangeMin + diff --git a/Server/validation/joi.js b/Server/validation/joi.js index 8e3dbcfec..d6c198e4a 100644 --- a/Server/validation/joi.js +++ b/Server/validation/joi.js @@ -232,6 +232,12 @@ const getChecksParamValidation = joi.object({ monitorId: joi.string().required(), }); +const getChecksQueryValidation = joi.object({ + sortOrder: joi.string().valid("asc", "desc"), + limit: joi.number(), + filter: joi.string().valid("day", "week", "month"), +}); + const deleteChecksParamValidation = joi.object({ monitorId: joi.string().required(), }); @@ -311,6 +317,7 @@ module.exports = { createCheckParamValidation, createCheckBodyValidation, getChecksParamValidation, + getChecksQueryValidation, deleteChecksParamValidation, deleteUserParamValidation, getPageSpeedCheckParamValidation,