From e2170981e418748fcafe381980f2004b3e200f23 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 12 Feb 2025 15:18:27 -0800 Subject: [PATCH] correctly calculate grouped uptime percentage --- .../Details/Components/ChartBoxes/index.jsx | 12 +- Server/db/mongo/modules/monitorModule.js | 143 +++++++++--------- 2 files changed, 80 insertions(+), 75 deletions(-) diff --git a/Client/src/Pages/Uptime/Details/Components/ChartBoxes/index.jsx b/Client/src/Pages/Uptime/Details/Components/ChartBoxes/index.jsx index 77fffd497..99d17b6f8 100644 --- a/Client/src/Pages/Uptime/Details/Components/ChartBoxes/index.jsx +++ b/Client/src/Pages/Uptime/Details/Components/ChartBoxes/index.jsx @@ -30,6 +30,12 @@ const ChartBoxes = ({ return ; } + const totalUpChecks = monitor?.upChecks?.totalChecks ?? 0; + const totalDownChecks = monitor?.downChecks?.totalChecks ?? 0; + const denominator = + totalUpChecks + totalDownChecks > 0 ? totalUpChecks + totalDownChecks : 1; + const groupedUptimePercentage = (totalUpChecks / denominator) * 100; + return ( {hoveredUptimeData !== null ? Math.floor(hoveredUptimeData?.avgResponseTime ?? 0) - : Math.floor( - ((monitor?.upChecks?.totalChecks ?? 0) / - (monitor?.totalChecks ?? 1)) * - 100 - )} + : Math.floor(groupedUptimePercentage)} {hoveredUptimeData !== null ? " ms" : " %"} diff --git a/Server/db/mongo/modules/monitorModule.js b/Server/db/mongo/modules/monitorModule.js index e1db09f0a..14d883c23 100644 --- a/Server/db/mongo/modules/monitorModule.js +++ b/Server/db/mongo/modules/monitorModule.js @@ -313,7 +313,7 @@ const calculateGroupStats = (group) => { avgResponseTime: checksWithResponseTime.length > 0 ? checksWithResponseTime.reduce((sum, check) => sum + check.responseTime, 0) / - checksWithResponseTime.length + checksWithResponseTime.length : 0, }; }; @@ -372,7 +372,10 @@ const getUptimeDetailsById = async (req) => { const getDistributedUptimeDetailsById = async (req) => { try { - const { monitorId } = req.params; + const { monitorId } = req?.params ?? {}; + if (typeof monitorId === "undefined") { + throw new Error(); + } const monitor = await Monitor.findById(monitorId); if (monitor === null || monitor === undefined) { throw new Error(this.stringService.dbFindMonitorById(monitorId)); @@ -605,98 +608,98 @@ const getMonitorsByTeamId = async (req) => { filteredMonitors: [ ...(filter !== undefined ? [ - { - $match: { - $or: [ - { name: { $regex: filter, $options: "i" } }, - { url: { $regex: filter, $options: "i" } }, - ], + { + $match: { + $or: [ + { name: { $regex: filter, $options: "i" } }, + { url: { $regex: filter, $options: "i" } }, + ], + }, }, - }, - ] + ] : []), { $sort: sort }, { $skip: skip }, ...(rowsPerPage ? [{ $limit: rowsPerPage }] : []), ...(limit ? [ - { - $lookup: { - from: "checks", - let: { monitorId: "$_id" }, - pipeline: [ - { - $match: { - $expr: { $eq: ["$monitorId", "$$monitorId"] }, + { + $lookup: { + from: "checks", + let: { monitorId: "$_id" }, + pipeline: [ + { + $match: { + $expr: { $eq: ["$monitorId", "$$monitorId"] }, + }, }, - }, - { $sort: { createdAt: -1 } }, - ...(limit ? [{ $limit: limit }] : []), - ], - as: "standardchecks", + { $sort: { createdAt: -1 } }, + ...(limit ? [{ $limit: limit }] : []), + ], + as: "standardchecks", + }, }, - }, - ] + ] : []), ...(limit ? [ - { - $lookup: { - from: "pagespeedchecks", - let: { monitorId: "$_id" }, - pipeline: [ - { - $match: { - $expr: { $eq: ["$monitorId", "$$monitorId"] }, + { + $lookup: { + from: "pagespeedchecks", + let: { monitorId: "$_id" }, + pipeline: [ + { + $match: { + $expr: { $eq: ["$monitorId", "$$monitorId"] }, + }, }, - }, - { $sort: { createdAt: -1 } }, - ...(limit ? [{ $limit: limit }] : []), - ], - as: "pagespeedchecks", + { $sort: { createdAt: -1 } }, + ...(limit ? [{ $limit: limit }] : []), + ], + as: "pagespeedchecks", + }, }, - }, - ] + ] : []), ...(limit ? [ - { - $lookup: { - from: "hardwarechecks", - let: { monitorId: "$_id" }, - pipeline: [ - { - $match: { - $expr: { $eq: ["$monitorId", "$$monitorId"] }, + { + $lookup: { + from: "hardwarechecks", + let: { monitorId: "$_id" }, + pipeline: [ + { + $match: { + $expr: { $eq: ["$monitorId", "$$monitorId"] }, + }, }, - }, - { $sort: { createdAt: -1 } }, - ...(limit ? [{ $limit: limit }] : []), - ], - as: "hardwarechecks", + { $sort: { createdAt: -1 } }, + ...(limit ? [{ $limit: limit }] : []), + ], + as: "hardwarechecks", + }, }, - }, - ] + ] : []), ...(limit ? [ - { - $lookup: { - from: "distributeduptimechecks", - let: { monitorId: "$_id" }, - pipeline: [ - { - $match: { - $expr: { $eq: ["$monitorId", "$$monitorId"] }, + { + $lookup: { + from: "distributeduptimechecks", + let: { monitorId: "$_id" }, + pipeline: [ + { + $match: { + $expr: { $eq: ["$monitorId", "$$monitorId"] }, + }, }, - }, - { $sort: { createdAt: -1 } }, - ...(limit ? [{ $limit: limit }] : []), - ], - as: "distributeduptimechecks", + { $sort: { createdAt: -1 } }, + ...(limit ? [{ $limit: limit }] : []), + ], + as: "distributeduptimechecks", + }, }, - }, - ] + ] : []), {