From 7e1a5b6ec86d959f38c0c12715f05dc0d34297c3 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 18 Sep 2024 10:32:41 +0800 Subject: [PATCH 1/4] add filter param, implement filter --- Client/src/Utils/NetworkService.js | 4 +-- Server/db/mongo/modules/monitorModule.js | 36 ++++++++++++++++-------- Server/validation/joi.js | 5 +++- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/Client/src/Utils/NetworkService.js b/Client/src/Utils/NetworkService.js index 683ce1aba..2e257d366 100644 --- a/Client/src/Utils/NetworkService.js +++ b/Client/src/Utils/NetworkService.js @@ -98,7 +98,7 @@ class NetworkService { limit, types, status, - sortOrder, + checkOrder, normalize, page, rowsPerPage @@ -112,7 +112,7 @@ class NetworkService { }); } if (status) params.append("status", status); - if (sortOrder) params.append("sortOrder", sortOrder); + if (checkOrder) params.append("checkOrder", checkOrder); if (normalize) params.append("normalize", normalize); if (page) params.append("page", page); if (rowsPerPage) params.append("rowsPerPage", rowsPerPage); diff --git a/Server/db/mongo/modules/monitorModule.js b/Server/db/mongo/modules/monitorModule.js index a7c00d726..ccfe61f3e 100644 --- a/Server/db/mongo/modules/monitorModule.js +++ b/Server/db/mongo/modules/monitorModule.js @@ -347,13 +347,25 @@ const getMonitorsAndSummaryByTeamId = async (teamId, type) => { */ const getMonitorsByTeamId = async (req, res) => { try { - let { limit, type, status, sortOrder, normalize, page, rowsPerPage } = - req.query || {}; - const monitorQuery = { teamId: req.params.teamId }; - const monitorsCount = await Monitor.countDocuments({ - teamId: req.params.teamId, + let { + limit, type, - }); + status, + checkOrder, + normalize, + page, + rowsPerPage, + filter, + } = req.query || {}; + const monitorQuery = { teamId: req.params.teamId, type }; + // Add filter if provided + if (filter !== undefined) { + monitorQuery.$or = [ + { name: { $regex: filter, $options: "i" } }, + { url: { $regex: filter, $options: "i" } }, + ]; + } + const monitorsCount = await Monitor.countDocuments(monitorQuery); // Pagination let skip = 0; @@ -367,11 +379,11 @@ const getMonitorsByTeamId = async (req, res) => { } // Default sort order is newest -> oldest - if (sortOrder === "asc") { - sortOrder = 1; - } else if (sortOrder === "desc") { - sortOrder = -1; - } else sortOrder = -1; + if (checkOrder === "asc") { + checkOrder = 1; + } else if (checkOrder === "desc") { + checkOrder = -1; + } else checkOrder = -1; const monitors = await Monitor.find(monitorQuery) .skip(skip) @@ -402,7 +414,7 @@ const getMonitorsByTeamId = async (req, res) => { let checks = await model .find(checksQuery) .sort({ - createdAt: sortOrder, + createdAt: checkOrder, }) .limit(limit); diff --git a/Server/validation/joi.js b/Server/validation/joi.js index dfb0e6fe7..dc82dc73e 100644 --- a/Server/validation/joi.js +++ b/Server/validation/joi.js @@ -180,7 +180,7 @@ const getMonitorsByTeamIdValidation = joi.object({ const getMonitorsByTeamIdQueryValidation = joi.object({ status: joi.boolean(), - sortOrder: joi.string().valid("asc", "desc"), + checkOrder: joi.string().valid("asc", "desc"), limit: joi.number(), normalize: joi.boolean(), type: joi @@ -191,6 +191,9 @@ const getMonitorsByTeamIdQueryValidation = joi.object({ ), page: joi.number(), rowsPerPage: joi.number(), + filter: joi.string(), + field: joi.string(), + order: joi.string().valid("asc", "desc"), }); const getMonitorStatsByIdParamValidation = joi.object({ From 2d2b101eb7c52c9ec8ac3157ecb4a157fa515ee4 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 18 Sep 2024 10:39:47 +0800 Subject: [PATCH 2/4] implement sorting --- .../src/Pages/Monitors/Home/MonitorTable/index.jsx | 5 ++++- Client/src/Utils/NetworkService.js | 8 +++++++- Server/db/mongo/modules/monitorModule.js | 13 ++++++++++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Client/src/Pages/Monitors/Home/MonitorTable/index.jsx b/Client/src/Pages/Monitors/Home/MonitorTable/index.jsx index 5c43ad880..f09b98215 100644 --- a/Client/src/Pages/Monitors/Home/MonitorTable/index.jsx +++ b/Client/src/Pages/Monitors/Home/MonitorTable/index.jsx @@ -151,7 +151,10 @@ const MonitorTable = ({ isAdmin }) => { "desc", true, page, - rowsPerPage + rowsPerPage, + null, + "createdAt", + "desc" ); setMonitors(res?.data?.data?.monitors ?? []); setMonitorCount(res?.data?.data?.monitorCount ?? 0); diff --git a/Client/src/Utils/NetworkService.js b/Client/src/Utils/NetworkService.js index 2e257d366..02f009a36 100644 --- a/Client/src/Utils/NetworkService.js +++ b/Client/src/Utils/NetworkService.js @@ -101,7 +101,10 @@ class NetworkService { checkOrder, normalize, page, - rowsPerPage + rowsPerPage, + filter, + field, + order ) { const params = new URLSearchParams(); @@ -116,6 +119,9 @@ class NetworkService { if (normalize) params.append("normalize", normalize); if (page) params.append("page", page); if (rowsPerPage) params.append("rowsPerPage", rowsPerPage); + if (filter) params.append("filter", filter); + if (field) params.append("field", field); + if (order) params.append("order", order); return this.axiosInstance.get( `/monitors/team/${teamId}?${params.toString()}`, diff --git a/Server/db/mongo/modules/monitorModule.js b/Server/db/mongo/modules/monitorModule.js index ccfe61f3e..85fe8ab03 100644 --- a/Server/db/mongo/modules/monitorModule.js +++ b/Server/db/mongo/modules/monitorModule.js @@ -356,9 +356,13 @@ const getMonitorsByTeamId = async (req, res) => { page, rowsPerPage, filter, + field, + order, } = req.query || {}; + const monitorQuery = { teamId: req.params.teamId, type }; // Add filter if provided + // $options: "i" makes the search case-insensitive if (filter !== undefined) { monitorQuery.$or = [ { name: { $regex: filter, $options: "i" } }, @@ -385,9 +389,16 @@ const getMonitorsByTeamId = async (req, res) => { checkOrder = -1; } else checkOrder = -1; + // Sort order for monitors + let sort = {}; + if (field !== undefined && order !== undefined) { + sort[field] = order === "asc" ? 1 : -1; + } + const monitors = await Monitor.find(monitorQuery) .skip(skip) - .limit(rowsPerPage); + .limit(rowsPerPage) + .sort(sort); // Early return if limit is set to -1, indicating we don't want any checks if (limit === "-1") { From cab023709b4f2fb68189de09b6b57ce5a88a1afb Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 18 Sep 2024 10:40:20 +0800 Subject: [PATCH 3/4] default no filtering or ordering --- Client/src/Pages/Monitors/Home/MonitorTable/index.jsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Client/src/Pages/Monitors/Home/MonitorTable/index.jsx b/Client/src/Pages/Monitors/Home/MonitorTable/index.jsx index f09b98215..e93858d3e 100644 --- a/Client/src/Pages/Monitors/Home/MonitorTable/index.jsx +++ b/Client/src/Pages/Monitors/Home/MonitorTable/index.jsx @@ -153,8 +153,8 @@ const MonitorTable = ({ isAdmin }) => { page, rowsPerPage, null, - "createdAt", - "desc" + null, + null ); setMonitors(res?.data?.data?.monitors ?? []); setMonitorCount(res?.data?.data?.monitorCount ?? 0); From 4748afb93a29d65174a940f5ba846fff5a80e9dc Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 18 Sep 2024 10:57:44 +0800 Subject: [PATCH 4/4] refactor networkSerivce.getMonitorsByTeamId to take a config objet --- Client/src/Pages/Incidents/index.jsx | 25 ++++++---- .../Monitors/Home/MonitorTable/index.jsx | 26 +++++----- Client/src/Pages/PageSpeed/index.jsx | 25 ++++++---- Client/src/Utils/NetworkService.js | 50 +++++++++++-------- Server/db/mongo/modules/monitorModule.js | 6 ++- 5 files changed, 74 insertions(+), 58 deletions(-) diff --git a/Client/src/Pages/Incidents/index.jsx b/Client/src/Pages/Incidents/index.jsx index 4e7a37c6d..10ab30ad5 100644 --- a/Client/src/Pages/Incidents/index.jsx +++ b/Client/src/Pages/Incidents/index.jsx @@ -25,17 +25,20 @@ const Incidents = () => { useEffect(() => { const fetchMonitors = async () => { setLoading(true); - const res = await networkService.getMonitorsByTeamId( - authState.authToken, - authState.user.teamId, - -1, - null, - null, - null, - null, - null, - null - ); + const res = await networkService.getMonitorsByTeamId({ + authToken: authState.authToken, + teamId: authState.user.teamId, + limit: -1, + types: null, + status: null, + checkOrder: null, + normalize: null, + page: null, + rowsPerPage: null, + filter: null, + field: null, + order: null, + }); // Reduce to a lookup object for 0(1) lookup if (res?.data?.data?.monitors?.length > 0) { const monitorLookup = res.data.data.monitors.reduce((acc, monitor) => { diff --git a/Client/src/Pages/Monitors/Home/MonitorTable/index.jsx b/Client/src/Pages/Monitors/Home/MonitorTable/index.jsx index e93858d3e..7203edda7 100644 --- a/Client/src/Pages/Monitors/Home/MonitorTable/index.jsx +++ b/Client/src/Pages/Monitors/Home/MonitorTable/index.jsx @@ -142,20 +142,20 @@ const MonitorTable = ({ isAdmin }) => { try { const { authToken } = authState; const user = jwtDecode(authToken); - const res = await networkService.getMonitorsByTeamId( + const res = await networkService.getMonitorsByTeamId({ authToken, - user.teamId, - 25, - ["http", "ping"], - null, - "desc", - true, - page, - rowsPerPage, - null, - null, - null - ); + teamId: user.teamId, + limit: 25, + types: ["http", "ping"], + status: null, + checkOrder: "desc", + normalize: true, + page: page, + rowsPerPage: rowsPerPage, + filter: null, + field: null, + order: null, + }); setMonitors(res?.data?.data?.monitors ?? []); setMonitorCount(res?.data?.data?.monitorCount ?? 0); } catch (error) { diff --git a/Client/src/Pages/PageSpeed/index.jsx b/Client/src/Pages/PageSpeed/index.jsx index a76e4cca1..3e78b1d6b 100644 --- a/Client/src/Pages/PageSpeed/index.jsx +++ b/Client/src/Pages/PageSpeed/index.jsx @@ -29,17 +29,20 @@ const PageSpeed = ({ isAdmin }) => { const fetchMonitors = async () => { try { setIsLoading(true); - const res = await networkService.getMonitorsByTeamId( - authToken, - user.teamId, - 10, - ["pagespeed"], - null, - "desc", - true, - null, - null - ); + const res = await networkService.getMonitorsByTeamId({ + authToken: authToken, + teamId: user.teamId, + limit: 10, + types: ["pagespeed"], + status: null, + checkOrder: "desc", + normalize: true, + page: null, + rowsPerPage: null, + filter: null, + field: null, + order: null, + }); if (res?.data?.data?.monitors) { setMonitors(res.data.data.monitors); } diff --git a/Client/src/Utils/NetworkService.js b/Client/src/Utils/NetworkService.js index 02f009a36..63323d5a8 100644 --- a/Client/src/Utils/NetworkService.js +++ b/Client/src/Utils/NetworkService.js @@ -83,29 +83,37 @@ class NetworkService { * ************************************ * * @async - * @param {string} authToken - The authorization token to be used in the request header. - * @param {string} userId - The ID of the user whose monitors are to be retrieved. - * @param {number} [limit] - The maximum number of monitors to retrieve. - * @param {Array} [types] - The types of monitors to retrieve. - * @param {string} [status] - The status of the monitors to retrieve. - * @param {string} [sortOrder] - The order in which to sort the retrieved monitors. - * @param {boolean} [normalize] - Whether to normalize the retrieved monitors. + * @param {Object} config - The configuration object. + * @param {string} config.authToken - The authorization token to be used in the request header. + * @param {string} config.teamId - The ID of the team whose monitors are to be retrieved. + * @param {number} [config.limit] - The maximum number of monitors to retrieve. + * @param {Array} [config.types] - The types of monitors to retrieve. + * @param {string} [config.status] - The status of the monitors to retrieve. + * @param {string} [config.checkOrder] - The order in which to sort the retrieved monitors. + * @param {boolean} [config.normalize] - Whether to normalize the retrieved monitors. + * @param {number} [config.page] - The page number for pagination. + * @param {number} [config.rowsPerPage] - The number of rows per page for pagination. + * @param {string} [config.filter] - The filter to apply to the monitors. + * @param {string} [config.field] - The field to sort by. + * @param {string} [config.order] - The order in which to sort the field. * @returns {Promise} The response from the axios GET request. */ - async getMonitorsByTeamId( - authToken, - teamId, - limit, - types, - status, - checkOrder, - normalize, - page, - rowsPerPage, - filter, - field, - order - ) { + async getMonitorsByTeamId(config) { + const { + authToken, + teamId, + limit, + types, + status, + checkOrder, + normalize, + page, + rowsPerPage, + filter, + field, + order, + } = config; + const params = new URLSearchParams(); if (limit) params.append("limit", limit); diff --git a/Server/db/mongo/modules/monitorModule.js b/Server/db/mongo/modules/monitorModule.js index 85fe8ab03..002bf9c41 100644 --- a/Server/db/mongo/modules/monitorModule.js +++ b/Server/db/mongo/modules/monitorModule.js @@ -359,8 +359,10 @@ const getMonitorsByTeamId = async (req, res) => { field, order, } = req.query || {}; - - const monitorQuery = { teamId: req.params.teamId, type }; + const monitorQuery = { teamId: req.params.teamId }; + if (type !== undefined) { + monitorQuery.type = type; + } // Add filter if provided // $options: "i" makes the search case-insensitive if (filter !== undefined) {