implemented uptime duration calculation

This commit is contained in:
Alex Holliday committed 2024-07-15 22:28:03 -07:00
1 parent 6b794f2435
commit 2f4a03ee01
1 file changed
+18 -16
+18 -16
View File
@@ -31,28 +31,30 @@ const DetailsPage = () => {
/** /**
* Function to calculate uptime duration based on the most recent check. * Function to calculate uptime duration based on the most recent check.
* @param {Array} checks Array of check objects. * @param {Array} checks Array of check objects.
* @returns {string} Uptime duration in hours, minutes, and seconds. * @returns {number} Uptime duration in ms.
* TODO - NEED TO REVISIT THIS FOR PROPER LOGIC.
*/ */
const calculateUptimeDuration = (checks) => { const calculateUptimeDuration = (checks) => {
if (!checks || checks.length === 0) { if (!checks || checks.length === 0) {
return "N/A"; return 0;
} }
const mostRecentCheck = checks[0]; let longestDuration = 0;
const currentTime = new Date(); let lastDownTimestamp = null;
const lastCheckedTime = new Date(mostRecentCheck.createdAt); let currentDuration = longestDuration;
let uptimeDuration = currentTime - lastCheckedTime;
// Calculate hours, minutes, and seconds from uptime duration // Loop over the checks to find the most recent uptime duration
// TODO - NEED TO REVISIT THIS FOR PROPER LOGIC. checks.forEach((check) => {
let uptimeHours = Math.floor(uptimeDuration / (1000 * 60 * 60)); if (check.status === false) {
uptimeDuration %= 1000 * 60 * 60; lastDownTimestamp = check.createdAt;
let uptimeMinutes = Math.floor(uptimeDuration / (1000 * 60)); } else if (check.status === true && lastDownTimestamp) {
uptimeDuration %= 1000 * 60; currentDuration = check.createdAt - lastDownTimestamp;
let uptimeSeconds = Math.floor(uptimeDuration / 1000); if (currentDuration > longestDuration) {
longestDuration = currentDuration;
return `${uptimeHours}h ${uptimeMinutes}m ${uptimeSeconds}s`; }
lastDownTimestamp = null;
}
});
return longestDuration;
}; };
/** /**