From e22e374eef80b1ec4c58088c6917c394d2da8cf8 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Tue, 3 Sep 2024 20:28:12 -0700 Subject: [PATCH 01/11] Added avgResponseTime to monitor stats, changed date grouping for aggregate stats --- Server/db/mongo/modules/monitorModule.js | 36 +++++++++++++++--------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/Server/db/mongo/modules/monitorModule.js b/Server/db/mongo/modules/monitorModule.js index b913519b6..0f0189694 100644 --- a/Server/db/mongo/modules/monitorModule.js +++ b/Server/db/mongo/modules/monitorModule.js @@ -77,11 +77,11 @@ const getLatestResponseTime = (checks) => { }; /** - * Helper function to get average 24h response time + * Helper function to get average response time * @param {Array} checks Array of check objects. * @returns {number} Timestamp of the most recent check. */ -const getAverageResponseTime24Hours = (checks) => { +const getAverageResponseTime = (checks) => { if (!checks || checks.length === 0) { return 0; } @@ -178,11 +178,12 @@ const getMonitorAggregateStats = async (monitorId, dateRange) => { // Group checks by hour if range is day if (dateRange === "day") { groupedChecks = checks.reduce((acc, check) => { - const hour = new Date(check.createdAt).getHours(); - if (!acc[hour]) { - acc[hour] = { hour, checks: [] }; + const time = new Date(check.createdAt); + time.setMinutes(0, 0, 0); + if (!acc[time]) { + acc[time] = { time, checks: [] }; } - acc[hour].checks.push(check); + acc[time].checks.push(check); return acc; }, {}); } @@ -190,11 +191,11 @@ const getMonitorAggregateStats = async (monitorId, dateRange) => { // Group checks by day if range is week or month else { groupedChecks = checks.reduce((acc, check) => { - const day = new Date(check.createdAt).toISOString().split("T")[0]; // Extract the date part - if (!acc[day]) { - acc[day] = { day, checks: [] }; + const time = new Date(check.createdAt).toISOString().split("T")[0]; // Extract the date part + if (!acc[time]) { + acc[time] = { time, checks: [] }; } - acc[day].checks.push(check); + acc[time].checks.push(check); return acc; }, {}); } @@ -210,8 +211,7 @@ const getMonitorAggregateStats = async (monitorId, dateRange) => { totalChecks; return { - hour: group.hour, - day: group.day, + time: group.time, totalChecks, totalIncidents, avgResponseTime, @@ -303,11 +303,19 @@ const getMonitorStatsById = async (req) => { ]); // HTTP/PING Specific stats + + //24 hour data monitorStats.avgResponseTime24hours = - getAverageResponseTime24Hours(checks24Hours); - monitorStats.uptime7Days = getUptimePercentage(checks7Days); + getAverageResponseTime(checks24Hours); monitorStats.uptime24Hours = getUptimePercentage(checks24Hours); + + //7 day data + monitorStats.uptime7Days = getUptimePercentage(checks7Days); + monitorStats.avgResponseTime7days = getAverageResponseTime(checks7Days); + // 30 day data monitorStats.uptime30Days = getUptimePercentage(checks30Days); + monitorStats.avgResponseTime30days = getAverageResponseTime(checks30Days); + monitorStats.statusBar = getStatusBarValues(monitor, checks60Mins); } From 9edf5ba9f9e6674d06bef8a3944800270a797c25 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 4 Sep 2024 11:02:18 -0700 Subject: [PATCH 02/11] Refactor --- Client/src/Components/Label/index.jsx | 1 - Server/db/mongo/MongoDB.js | 2 +- Server/db/mongo/modules/monitorModule.js | 246 +++++++---------------- 3 files changed, 79 insertions(+), 170 deletions(-) diff --git a/Client/src/Components/Label/index.jsx b/Client/src/Components/Label/index.jsx index 330c6de0a..886f66fb9 100644 --- a/Client/src/Components/Label/index.jsx +++ b/Client/src/Components/Label/index.jsx @@ -126,7 +126,6 @@ ColoredLabel.propTypes = { */ const StatusLabel = ({ status, text, customStyles }) => { - console.log(status); const theme = useTheme(); const colors = { up: { diff --git a/Server/db/mongo/MongoDB.js b/Server/db/mongo/MongoDB.js index 6f959f758..29233b73a 100644 --- a/Server/db/mongo/MongoDB.js +++ b/Server/db/mongo/MongoDB.js @@ -65,6 +65,7 @@ const { const { getAllMonitors, getMonitorAggregateStats, + testMethod, getMonitorStatsById, getMonitorById, getMonitorsByTeamId, @@ -145,7 +146,6 @@ module.exports = { resetPassword, checkSuperadmin, getAllMonitors, - getMonitorAggregateStats, getMonitorStatsById, getMonitorById, getMonitorsByTeamId, diff --git a/Server/db/mongo/modules/monitorModule.js b/Server/db/mongo/modules/monitorModule.js index 0f0189694..bf056782b 100644 --- a/Server/db/mongo/modules/monitorModule.js +++ b/Server/db/mongo/modules/monitorModule.js @@ -145,39 +145,103 @@ const getStatusBarValues = (monitor, checks) => { }; /** - * Get aggregate monitor stats for charts + * Get stats by monitor ID * @async * @param {Express.Request} req * @param {Express.Response} res * @returns {Promise} * @throws {Error} */ -const getMonitorAggregateStats = async (monitorId, dateRange) => { +const getMonitorStatsById = async (req) => { const startDates = { 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)), }; - + const endDate = new Date(); try { - const startDate = startDates[dateRange]; - if (!startDate) { - throw new Error("Invalid date range specified"); + // Get monitor + const { monitorId } = req.params; + let { limit, sortOrder, dateRange, numToDisplay, normalize } = req.query; + const monitor = await Monitor.findById(monitorId); + if (monitor === null || monitor === undefined) { + throw new Error(errorMessages.DB_FIND_MONTIOR_BY_ID(monitorId)); } - const endDate = new Date(); - const checks = await Check.find({ - monitorId: monitorId, + // This effectively removes limit, returning all checks + if (limit === undefined) limit = 0; + + // Default sort order is newest -> oldest + sortOrder = sortOrder === "asc" ? 1 : -1; + + let model = + monitor.type === "http" || monitor.type === "ping" + ? Check + : PageSpeedCheck; + + const monitorStats = { + ...monitor.toObject(), + }; + + // Build checks query + const checksQuery = { monitorId: monitor._id }; + + // Get all checks + const checksAll = await model.find(checksQuery).sort({ + createdAt: sortOrder, + }); + + const checksQueryForDateRange = { + ...checksQuery, createdAt: { - $gte: startDate, + $gte: startDates[dateRange], $lte: endDate, }, - }); + }; + + const checksForDateRange = await model + .find(checksQueryForDateRange) + .sort({ createdAt: sortOrder }); + + if (monitor.type === "http" || monitor.type === "ping") { + // HTTP/PING Specific stats + monitorStats.avgResponseTime = getAverageResponseTime(checksForDateRange); + monitorStats.uptime = getUptimePercentage(checksForDateRange); + } + + monitorStats.incidents = getIncidents(checksForDateRange); + + // If more than numToDisplay checks, pick every nth check + + let nthChecks = checksForDateRange; + + if ( + numToDisplay !== undefined && + checksForDateRange && + checksForDateRange.length > numToDisplay + ) { + const n = Math.ceil(checksForDateRange.length / numToDisplay); + nthChecks = checksForDateRange.filter((_, index) => index % n === 0); + } + + // Normalize checks if requested + if (normalize !== undefined) { + const normailzedChecks = NormalizeData(checksForDateRange, 1, 100); + monitorStats.checks = normailzedChecks; + } else { + monitorStats.checks = nthChecks; + } + + monitorStats.uptimeDuration = calculateUptimeDuration(checksAll); + monitorStats.lastChecked = getLastChecked(checksAll); + monitorStats.latestResponseTime = getLatestResponseTime(checksAll); + + // Aggregate data let groupedChecks; // Group checks by hour if range is day if (dateRange === "day") { - groupedChecks = checks.reduce((acc, check) => { + groupedChecks = checksForDateRange.reduce((acc, check) => { const time = new Date(check.createdAt); time.setMinutes(0, 0, 0); if (!acc[time]) { @@ -188,20 +252,8 @@ const getMonitorAggregateStats = async (monitorId, dateRange) => { }, {}); } - // Group checks by day if range is week or month - else { - groupedChecks = checks.reduce((acc, check) => { - const time = new Date(check.createdAt).toISOString().split("T")[0]; // Extract the date part - if (!acc[time]) { - acc[time] = { time, checks: [] }; - } - acc[time].checks.push(check); - return acc; - }, {}); - } - // Map grouped checks to stats - stats = Object.values(groupedChecks).map((group) => { + const aggregateData = Object.values(groupedChecks).map((group) => { const totalChecks = group.checks.length; const totalIncidents = group.checks.filter( (check) => check.status === false @@ -217,148 +269,7 @@ const getMonitorAggregateStats = async (monitorId, dateRange) => { avgResponseTime, }; }); - - return stats; - } catch (error) { - throw error; - } -}; - -/** - * Get stats by monitor ID - * @async - * @param {Express.Request} req - * @param {Express.Response} res - * @returns {Promise} - * @throws {Error} - */ -const getMonitorStatsById = async (req) => { - const filterLookup = { - hour: new Date(new Date().getTime() - 60 * 60 * 1000), - 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 { monitorId } = req.params; - let { status, limit, sortOrder, dateRange, numToDisplay, normalize } = - req.query; - - // This effectively removes limit, returning all checks - if (limit === undefined) limit = 0; - - // Default sort order is newest -> oldest - sortOrder = sortOrder === "asc" ? 1 : -1; - - // Get monitor - const monitor = await Monitor.findById(monitorId); - if (monitor === null || monitor === undefined) { - throw new Error(errorMessages.DB_FIND_MONTIOR_BY_ID(monitorId)); - } - - // Determine if this is a pagespeed monitor or an http/ping monitor - let model = - monitor.type === "http" || monitor.type === "ping" - ? Check - : PageSpeedCheck; - - // Build monitor stats object - const monitorStats = { - ...monitor.toObject(), - }; - - // Start building query - const checksQuery = { monitorId: monitor._id }; - - // Get all checks - const checksAll = await model.find(checksQuery).sort({ - createdAt: sortOrder, - }); - - if (monitor.type === "http" || monitor.type === "ping") { - const checksQuery24Hours = { - ...checksQuery, - createdAt: { $gte: filterLookup.day }, - }; - const checksQuery7Days = { - ...checksQuery, - createdAt: { $gte: filterLookup.week }, - }; - const checksQuery30Days = { - ...checksQuery, - createdAt: { $gte: filterLookup.month }, - }; - const checksQuery60Mins = { - ...checksQuery, - createdAt: { $gte: filterLookup.hour }, - }; - - const [checks24Hours, checks7Days, checks30Days, checks60Mins] = - await Promise.all([ - model.find(checksQuery24Hours).sort({ createdAt: sortOrder }), - model.find(checksQuery7Days).sort({ createdAt: sortOrder }), - model.find(checksQuery30Days).sort({ createdAt: sortOrder }), - model.find(checksQuery60Mins).sort({ createdAt: sortOrder }), - ]); - - // HTTP/PING Specific stats - - //24 hour data - monitorStats.avgResponseTime24hours = - getAverageResponseTime(checks24Hours); - monitorStats.uptime24Hours = getUptimePercentage(checks24Hours); - - //7 day data - monitorStats.uptime7Days = getUptimePercentage(checks7Days); - monitorStats.avgResponseTime7days = getAverageResponseTime(checks7Days); - // 30 day data - monitorStats.uptime30Days = getUptimePercentage(checks30Days); - monitorStats.avgResponseTime30days = getAverageResponseTime(checks30Days); - - monitorStats.statusBar = getStatusBarValues(monitor, checks60Mins); - } - - //Get checks for dateRange - if (status !== undefined) { - checksQuery.status = status; - } - - // Filter checks by "day", "week", or "month" - if (dateRange !== undefined) { - checksQuery.createdAt = { $gte: filterLookup[dateRange] }; - } - - let dateRangeChecks = await model - .find(checksQuery) - .sort({ - createdAt: sortOrder, - }) - .limit(limit); - - const incidents = getIncidents(dateRangeChecks); - - // If more than numToDisplay checks, pick every nth check - if ( - numToDisplay !== undefined && - dateRangeChecks && - dateRangeChecks.length > numToDisplay - ) { - const n = Math.ceil(dateRangeChecks.length / numToDisplay); - dateRangeChecks = dateRangeChecks.filter((_, index) => index % n === 0); - } - - // Normalize checks if requested - if (normalize !== undefined) { - dateRangeChecks = NormalizeData(dateRangeChecks, 1, 100); - } - - // Add common stats and stats that depend on the dateRange - monitorStats.uptimeDuration = calculateUptimeDuration(checksAll); - monitorStats.lastChecked = getLastChecked(checksAll); - monitorStats.latestResponseTime = getLatestResponseTime(checksAll); - monitorStats.incidents = incidents; - monitorStats.checks = dateRangeChecks; + monitorStats.aggregateData = aggregateData; return monitorStats; } catch (error) { throw error; @@ -548,7 +459,6 @@ const editMonitor = async (candidateId, candidateMonitor) => { module.exports = { getAllMonitors, - getMonitorAggregateStats, getMonitorStatsById, getMonitorById, getMonitorsByTeamId, From 5ccb85e75519b9faee4cc1ae078476f339477b21 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 4 Sep 2024 11:09:08 -0700 Subject: [PATCH 03/11] remove unused methods --- Server/db/mongo/modules/monitorModule.js | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/Server/db/mongo/modules/monitorModule.js b/Server/db/mongo/modules/monitorModule.js index bf056782b..9f801a9df 100644 --- a/Server/db/mongo/modules/monitorModule.js +++ b/Server/db/mongo/modules/monitorModule.js @@ -35,7 +35,7 @@ const calculateUptimeDuration = (checks) => { const latestCheck = new Date(checks[0].createdAt); let latestDownCheck = 0; - for (let i = 0; i < checks.length; i++) { + for (let i = checks.length; i <= 0; i--) { if (checks[i].status === false) { latestDownCheck = new Date(checks[i].createdAt); break; @@ -121,28 +121,6 @@ const getIncidents = (checks) => { return check.status === false ? (acc += 1) : acc; }, 0); }; -/** - * Helper function to get all incidents - * @param {Array} checks Array of check objects. - * @returns {Array} Array of booleans representing up/down. - */ -const getStatusBarValues = (monitor, checks) => { - const checksIn60Mins = Math.floor((60 * 60 * 1000) / monitor.interval); - const noBlankChecks = checksIn60Mins - checks.length; - - const statusBarValues = checks.map((check) => { - return { - status: check.status, - responseTime: check.responseTime, - value: 75, - }; - }); - - for (let i = 0; i < noBlankChecks; i++) { - statusBarValues.push({ status: undefined, responseTime: 0, value: 75 }); - } - return statusBarValues.reverse(); -}; /** * Get stats by monitor ID From df16bf3db55285053f242c9d31b31bb8f32b800a Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 4 Sep 2024 11:25:08 -0700 Subject: [PATCH 04/11] Moved http/ping specific data --- Server/db/mongo/modules/monitorModule.js | 70 ++++++++++++------------ Server/middleware/handleErrors.js | 5 +- 2 files changed, 39 insertions(+), 36 deletions(-) diff --git a/Server/db/mongo/modules/monitorModule.js b/Server/db/mongo/modules/monitorModule.js index 9f801a9df..ab867ff47 100644 --- a/Server/db/mongo/modules/monitorModule.js +++ b/Server/db/mongo/modules/monitorModule.js @@ -145,7 +145,6 @@ const getMonitorStatsById = async (req) => { if (monitor === null || monitor === undefined) { throw new Error(errorMessages.DB_FIND_MONTIOR_BY_ID(monitorId)); } - // This effectively removes limit, returning all checks if (limit === undefined) limit = 0; @@ -185,6 +184,40 @@ const getMonitorStatsById = async (req) => { // HTTP/PING Specific stats monitorStats.avgResponseTime = getAverageResponseTime(checksForDateRange); monitorStats.uptime = getUptimePercentage(checksForDateRange); + + // Aggregate data + let groupedChecks; + // Group checks by hour if range is day + if (dateRange === "day") { + groupedChecks = checksForDateRange.reduce((acc, check) => { + const time = new Date(check.createdAt); + time.setMinutes(0, 0, 0); + if (!acc[time]) { + acc[time] = { time, checks: [] }; + } + acc[time].checks.push(check); + return acc; + }, {}); + } + + // Map grouped checks to stats + const aggregateData = Object.values(groupedChecks).map((group) => { + const totalChecks = group.checks.length; + const totalIncidents = group.checks.filter( + (check) => check.status === false + ).length; + const avgResponseTime = + group.checks.reduce((sum, check) => sum + check.responseTime, 0) / + totalChecks; + + return { + time: group.time, + totalChecks, + totalIncidents, + avgResponseTime, + }; + }); + monitorStats.aggregateData = aggregateData; } monitorStats.incidents = getIncidents(checksForDateRange); @@ -214,42 +247,9 @@ const getMonitorStatsById = async (req) => { monitorStats.lastChecked = getLastChecked(checksAll); monitorStats.latestResponseTime = getLatestResponseTime(checksAll); - // Aggregate data - - let groupedChecks; - // Group checks by hour if range is day - if (dateRange === "day") { - groupedChecks = checksForDateRange.reduce((acc, check) => { - const time = new Date(check.createdAt); - time.setMinutes(0, 0, 0); - if (!acc[time]) { - acc[time] = { time, checks: [] }; - } - acc[time].checks.push(check); - return acc; - }, {}); - } - - // Map grouped checks to stats - const aggregateData = Object.values(groupedChecks).map((group) => { - const totalChecks = group.checks.length; - const totalIncidents = group.checks.filter( - (check) => check.status === false - ).length; - const avgResponseTime = - group.checks.reduce((sum, check) => sum + check.responseTime, 0) / - totalChecks; - - return { - time: group.time, - totalChecks, - totalIncidents, - avgResponseTime, - }; - }); - monitorStats.aggregateData = aggregateData; return monitorStats; } catch (error) { + error.methodName = "getMonitorStatsById"; throw error; } }; diff --git a/Server/middleware/handleErrors.js b/Server/middleware/handleErrors.js index 1e1e955c5..db51c1af6 100644 --- a/Server/middleware/handleErrors.js +++ b/Server/middleware/handleErrors.js @@ -5,7 +5,10 @@ const handleErrors = (error, req, res, next) => { const status = error.status || 500; const message = error.message || errorMessages.FRIENDLY_ERROR; const service = error.service || errorMessages.UNKNOWN_SERVICE; - logger.error(error.message, { service: service }); + logger.error(error.message, { + service: service, + methodName: error.methodName, + }); res.status(status).json({ success: false, msg: message }); }; From ffc132e19a670f900e922b5edacecf886d084bb5 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 4 Sep 2024 11:45:54 -0700 Subject: [PATCH 05/11] Add missing else block --- Client/src/Pages/PageSpeed/Details/index.jsx | 3 ++- Server/db/mongo/modules/monitorModule.js | 17 +++++++++++++---- Server/service/networkService.js | 2 +- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Client/src/Pages/PageSpeed/Details/index.jsx b/Client/src/Pages/PageSpeed/Details/index.jsx index 29c0b72e0..f4dd1f64d 100644 --- a/Client/src/Pages/PageSpeed/Details/index.jsx +++ b/Client/src/Pages/PageSpeed/Details/index.jsx @@ -213,10 +213,11 @@ const PageSpeedDetails = () => { monitorId, "desc", 50, - null, + "day", null, null ); + console.log(res.data.data); setMonitor(res?.data?.data ?? {}); setAudits(res?.data?.data?.checks?.[0]?.audits ?? []); } catch (error) { diff --git a/Server/db/mongo/modules/monitorModule.js b/Server/db/mongo/modules/monitorModule.js index ab867ff47..858976285 100644 --- a/Server/db/mongo/modules/monitorModule.js +++ b/Server/db/mongo/modules/monitorModule.js @@ -147,7 +147,6 @@ const getMonitorStatsById = async (req) => { } // This effectively removes limit, returning all checks if (limit === undefined) limit = 0; - // Default sort order is newest -> oldest sortOrder = sortOrder === "asc" ? 1 : -1; @@ -182,8 +181,9 @@ const getMonitorStatsById = async (req) => { if (monitor.type === "http" || monitor.type === "ping") { // HTTP/PING Specific stats - monitorStats.avgResponseTime = getAverageResponseTime(checksForDateRange); - monitorStats.uptime = getUptimePercentage(checksForDateRange); + monitorStats.periodAvgResponseTime = + getAverageResponseTime(checksForDateRange); + monitorStats.periodUptime = getUptimePercentage(checksForDateRange); // Aggregate data let groupedChecks; @@ -198,6 +198,15 @@ const getMonitorStatsById = async (req) => { acc[time].checks.push(check); return acc; }, {}); + } else { + groupedChecks = checksForDateRange.reduce((acc, check) => { + const day = new Date(check.createdAt).toISOString().split("T")[0]; // Extract the date part + if (!acc[day]) { + acc[day] = { day, checks: [] }; + } + acc[day].checks.push(check); + return acc; + }, {}); } // Map grouped checks to stats @@ -220,7 +229,7 @@ const getMonitorStatsById = async (req) => { monitorStats.aggregateData = aggregateData; } - monitorStats.incidents = getIncidents(checksForDateRange); + monitorStats.periodIncidents = getIncidents(checksForDateRange); // If more than numToDisplay checks, pick every nth check diff --git a/Server/service/networkService.js b/Server/service/networkService.js index 863349dbf..cfa2a2c35 100644 --- a/Server/service/networkService.js +++ b/Server/service/networkService.js @@ -197,7 +197,6 @@ class NetworkService { */ async handlePagespeed(job) { let isAlive; - try { const url = job.data.url; const response = await axios.get( @@ -297,6 +296,7 @@ class NetworkService { async logAndStoreCheck(data, writeToDB) { try { const insertedCheck = await writeToDB(data); + console.log(insertedCheck); return insertedCheck.status; } catch (error) { console.log(error); From be582edeeb649f7760d91189d06d1a47f90017a8 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 4 Sep 2024 11:54:20 -0700 Subject: [PATCH 06/11] add time --- Client/src/Pages/Monitors/Details/index.jsx | 1 + Server/db/mongo/modules/monitorModule.js | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Client/src/Pages/Monitors/Details/index.jsx b/Client/src/Pages/Monitors/Details/index.jsx index 833e11ef9..4bea7d836 100644 --- a/Client/src/Pages/Monitors/Details/index.jsx +++ b/Client/src/Pages/Monitors/Details/index.jsx @@ -89,6 +89,7 @@ const DetailsPage = ({ isAdmin }) => { 50, true ); + console.log(res.data.data); setMonitor(res?.data?.data ?? {}); } catch (error) { logger.error(error); diff --git a/Server/db/mongo/modules/monitorModule.js b/Server/db/mongo/modules/monitorModule.js index 858976285..44ec99a6d 100644 --- a/Server/db/mongo/modules/monitorModule.js +++ b/Server/db/mongo/modules/monitorModule.js @@ -200,11 +200,11 @@ const getMonitorStatsById = async (req) => { }, {}); } else { groupedChecks = checksForDateRange.reduce((acc, check) => { - const day = new Date(check.createdAt).toISOString().split("T")[0]; // Extract the date part - if (!acc[day]) { - acc[day] = { day, checks: [] }; + const time = new Date(check.createdAt).toISOString().split("T")[0]; // Extract the date part + if (!acc[time]) { + acc[time] = { time, checks: [] }; } - acc[day].checks.push(check); + acc[time].checks.push(check); return acc; }, {}); } From a5697d419cbdc85f42c8281c483a11cf968c5fb1 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 4 Sep 2024 11:54:59 -0700 Subject: [PATCH 07/11] Remove console log --- Client/src/Pages/Monitors/Details/index.jsx | 1 - 1 file changed, 1 deletion(-) diff --git a/Client/src/Pages/Monitors/Details/index.jsx b/Client/src/Pages/Monitors/Details/index.jsx index 4bea7d836..833e11ef9 100644 --- a/Client/src/Pages/Monitors/Details/index.jsx +++ b/Client/src/Pages/Monitors/Details/index.jsx @@ -89,7 +89,6 @@ const DetailsPage = ({ isAdmin }) => { 50, true ); - console.log(res.data.data); setMonitor(res?.data?.data ?? {}); } catch (error) { logger.error(error); From 3270d61624edcb04e7b948a39465690ca13278a7 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 4 Sep 2024 12:00:01 -0700 Subject: [PATCH 08/11] Add period total checks --- Server/db/mongo/modules/monitorModule.js | 1 + Server/service/networkService.js | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/Server/db/mongo/modules/monitorModule.js b/Server/db/mongo/modules/monitorModule.js index 44ec99a6d..393aa93cc 100644 --- a/Server/db/mongo/modules/monitorModule.js +++ b/Server/db/mongo/modules/monitorModule.js @@ -230,6 +230,7 @@ const getMonitorStatsById = async (req) => { } monitorStats.periodIncidents = getIncidents(checksForDateRange); + monitor.periodTotalChecks = checksForDateRange.length; // If more than numToDisplay checks, pick every nth check diff --git a/Server/service/networkService.js b/Server/service/networkService.js index cfa2a2c35..e25a63ce5 100644 --- a/Server/service/networkService.js +++ b/Server/service/networkService.js @@ -296,7 +296,6 @@ class NetworkService { async logAndStoreCheck(data, writeToDB) { try { const insertedCheck = await writeToDB(data); - console.log(insertedCheck); return insertedCheck.status; } catch (error) { console.log(error); From 6424a0436f9bbd68f5777df330f0330fcbd3b3d9 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 4 Sep 2024 12:15:53 -0700 Subject: [PATCH 09/11] Fixed incorrect assignment --- Server/db/mongo/modules/monitorModule.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Server/db/mongo/modules/monitorModule.js b/Server/db/mongo/modules/monitorModule.js index 393aa93cc..29e98fc2c 100644 --- a/Server/db/mongo/modules/monitorModule.js +++ b/Server/db/mongo/modules/monitorModule.js @@ -230,7 +230,7 @@ const getMonitorStatsById = async (req) => { } monitorStats.periodIncidents = getIncidents(checksForDateRange); - monitor.periodTotalChecks = checksForDateRange.length; + monitorStats.periodTotalChecks = checksForDateRange.length; // If more than numToDisplay checks, pick every nth check From 70a40589933de0987255ea696457a49743ec4b28 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 4 Sep 2024 13:21:51 -0700 Subject: [PATCH 10/11] add uptimePercentage to aggregate data --- Server/db/mongo/modules/monitorModule.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Server/db/mongo/modules/monitorModule.js b/Server/db/mongo/modules/monitorModule.js index 29e98fc2c..558fbae4d 100644 --- a/Server/db/mongo/modules/monitorModule.js +++ b/Server/db/mongo/modules/monitorModule.js @@ -212,6 +212,7 @@ const getMonitorStatsById = async (req) => { // Map grouped checks to stats const aggregateData = Object.values(groupedChecks).map((group) => { const totalChecks = group.checks.length; + const uptimePercentage = getUptimePercentage(group.checks); const totalIncidents = group.checks.filter( (check) => check.status === false ).length; @@ -221,6 +222,7 @@ const getMonitorStatsById = async (req) => { return { time: group.time, + uptimePercentage, totalChecks, totalIncidents, avgResponseTime, From 6aef23e8a456e5ed7228f88c76d791b9b232a663 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 4 Sep 2024 17:41:28 -0700 Subject: [PATCH 11/11] Use correct array for normalization --- Server/db/mongo/modules/monitorModule.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Server/db/mongo/modules/monitorModule.js b/Server/db/mongo/modules/monitorModule.js index 558fbae4d..bfa4bd73a 100644 --- a/Server/db/mongo/modules/monitorModule.js +++ b/Server/db/mongo/modules/monitorModule.js @@ -249,7 +249,7 @@ const getMonitorStatsById = async (req) => { // Normalize checks if requested if (normalize !== undefined) { - const normailzedChecks = NormalizeData(checksForDateRange, 1, 100); + const normailzedChecks = NormalizeData(nthChecks, 1, 100); monitorStats.checks = normailzedChecks; } else { monitorStats.checks = nthChecks;