From be72efa1287f81ca5d2e32ae60f8a45499505415 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Sun, 21 Jul 2024 20:07:18 -0700 Subject: [PATCH] Fixed detail page calcuations --- Client/src/Pages/Monitors/Details/index.jsx | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/Client/src/Pages/Monitors/Details/index.jsx b/Client/src/Pages/Monitors/Details/index.jsx index 496ea2034..8eaac61ab 100644 --- a/Client/src/Pages/Monitors/Details/index.jsx +++ b/Client/src/Pages/Monitors/Details/index.jsx @@ -149,17 +149,25 @@ const DetailsPage = () => { if (!checks || checks.length === 0) { return 0; } - const latestCheck = new Date(checks[0].createdAt); + + const latestCheck = new Date(checks[checks.length - 1].createdAt); let latestDownCheck = 0; - for (let i = 0; i < checks.length; i++) { + + // Checks are ordered oldest -> newest + // So we iterate backwards and find the first down check + for (let i = checks.length - 1; i >= 0; i--) { if (checks[i].status === false) { latestDownCheck = new Date(checks[i].createdAt); break; } } + + // If no down check is found, uptime is from the first check to now if (latestDownCheck === 0) { - return Date.now() - new Date(checks[checks.length - 1].createdAt); + return Date.now() - new Date(checks[0].createdAt); } + + // Otherwise the uptime is from the last check to the last down check return latestCheck - latestDownCheck; }; @@ -172,7 +180,8 @@ const DetailsPage = () => { if (!checks || checks.length === 0) { return 0; // Handle case when no checks are available } - return new Date() - new Date(checks[0].createdAt); + // Data is sorted oldest -> newest, so last check is the most recent + return new Date() - new Date(checks[checks.length - 1].createdAt); }; /**