From 654b9d2038a8588f90503c22a72d1b9680fac1d8 Mon Sep 17 00:00:00 2001 From: M M Date: Sat, 31 Aug 2024 17:58:23 -0700 Subject: [PATCH] Add real percentages for the uptime of monitors to the main dashboard. --- Server/db/mongo/modules/checkModule.js | 20 ++++++++++++++++++++ Server/models/Monitor.js | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/Server/db/mongo/modules/checkModule.js b/Server/db/mongo/modules/checkModule.js index 7736ca3c1..9f5a68c1e 100644 --- a/Server/db/mongo/modules/checkModule.js +++ b/Server/db/mongo/modules/checkModule.js @@ -22,7 +22,27 @@ const dateRangeLookup = { const createCheck = async (checkData) => { try { + const { monitorId, status } = checkData; + const check = await new Check({ ...checkData }).save(); + + const n = await Check.countDocuments({ monitorId }) + 1; + + const monitor = await Monitor.findById(monitorId); + + if (!monitor) { + throw new Error("Monitor not found"); + } + + if (monitor.uptimePercentage === undefined) { + monitor.uptimePercentage = status ? 100 : 0; + } else { + monitor.uptimePercentage = + (monitor.uptimePercentage * (n - 1) + (status ? 100 : 0)) / n; + } + + await monitor.save(); + return check; } catch (error) { throw error; diff --git a/Server/models/Monitor.js b/Server/models/Monitor.js index ee4eb890d..fdc147d1b 100644 --- a/Server/models/Monitor.js +++ b/Server/models/Monitor.js @@ -44,6 +44,10 @@ const MonitorSchema = mongoose.Schema( type: Number, default: 60000, }, + uptimePercentage: { + type: Number, + default: undefined, + }, notifications: [ { type: mongoose.Schema.Types.ObjectId,