From cc830827bbdb47ded656abebe01c717120ee0327 Mon Sep 17 00:00:00 2001 From: Raj Nandan Sharma Date: Tue, 30 Apr 2024 11:13:45 +0530 Subject: [PATCH] feat(kener): supports custom threshold for calculations of day uptime CHANGE: monitors now get three new optional parameter dayDegradedMinimumCount, dayDownMinimumCount and includeDegradedInDowntime Requestd on issue #54 --- docs.md | 6 ++++++ scripts/ninety.js | 39 +++++++++++++++++++++++++++++++-------- scripts/startup.js | 20 ++++++++++++++++++++ 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/docs.md b/docs.md index dd73df2..8e1d188 100644 --- a/docs.md +++ b/docs.md @@ -366,6 +366,9 @@ To set the base path of your kener instance you can set the `KENER_BASE_PATH` en It should be present during both build and run time. If you are using docker you will have to do your own build and set this env variable during `docker build` +Please also adjust files in static folder by prefixing them with the base path. For example if you set `KENER_BASE_PATH=/status` then the logo should be `/status/logo.png` + + ## Custom Scripts You can include any script in the `app.html` file like google analytics etc @@ -424,6 +427,9 @@ Sample | defaultStatus | Optional | If no API is given this will be the default status. can be UP/DOWN/DEGRADED | | hidden | Optional | If set to `true` will not show the monitor in the UI | | category | Optional | Use this to group your monitors. Make sure you have defined category in `site.yaml` and use the `name` attribute here | +| dayDegradedMinimumCount | Optional | Default is 1. It means minimum this number of count for the day to be classified as DEGRADED(Yellow Bar) in 90 day view. Has to be `number` greater than 0 | +| dayDownMinimumCount | Optional | Default is 1. It means minimum this number of count for the day to be classified as DOWN(Red Bar) in 90 day view. Has to be `number` greater than 0 | +| includeDegradedInDowntime | Optional | By deafault uptime percentage is calculated as (UP+DEGRADED/UP+DEGRADED+DOWN). Setting it as `true` will change the calculation to (UP/UP+DEGRADED+DOWN) | ## cron diff --git a/scripts/ninety.js b/scripts/ninety.js index 7cf7c3e..2e82de0 100644 --- a/scripts/ninety.js +++ b/scripts/ninety.js @@ -13,7 +13,13 @@ function getDayMessage(type, numOfMinute){ } const NO_DATA = "No Data"; -function getDayData(day0, startTime, endTime) { +function getDayData( + day0, + startTime, + endTime, + dayDownMinimumCount, + dayDegradedMinimumCount +) { let dayData = { UP: 0, DEGRADED: 0, @@ -41,15 +47,15 @@ function getDayData(day0, startTime, endTime) { let cssClass = StatusObj.UP; let message = "Status OK"; - if (dayData.DEGRADED > 0) { - cssClass = StatusObj.DEGRADED; + if (dayData.DEGRADED >= dayDegradedMinimumCount) { + cssClass = StatusObj.DEGRADED; message = getDayMessage("DEGRADED", dayData.DEGRADED); } - if (dayData.DOWN > 0) { + if (dayData.DOWN >= dayDownMinimumCount) { cssClass = StatusObj.DOWN; message = getDayMessage("DOWN", dayData.DOWN); } - if (dayData.DEGRADED + dayData.DOWN + dayData.UP > 0) { + if (dayData.DEGRADED + dayData.DOWN + dayData.UP >= Math.min(dayDownMinimumCount, dayDegradedMinimumCount)) { dayData.message = message; dayData.cssClass = cssClass; } @@ -68,6 +74,7 @@ const Ninety = async (monitor) => { let completeDown = 0; let completeDegraded = 0; + const secondsInDay = 24 * 60 * 60; const now = GetMinuteStartNowTimestampUTC(); const midnight = BeginningOfDay({ timeZone: "GMT" }); @@ -107,7 +114,13 @@ const Ninety = async (monitor) => { } for (let i = midnight90DaysAgo; i < midnightTomorrow; i += secondsInDay) { - _90Day[i] = getDayData(day0, i, i + secondsInDay - 1); + _90Day[i] = getDayData( + day0, + i, + i + secondsInDay - 1, + monitor.dayDownMinimumCount, + monitor.dayDegradedMinimumCount + ); } for (const key in _90Day) { @@ -117,12 +130,22 @@ const Ninety = async (monitor) => { delete _90Day[key].DOWN; if (element.message == NO_DATA) continue; } - uptime0Day = ParseUptime(dailyUps + dailyDegraded, dailyUps + dailyDown + dailyDegraded); + + let uptime0DayNumerator = dailyUps + dailyDegraded; + let uptime0DayDenominator = dailyUps + dailyDown + dailyDegraded; + let uptime90DayNumerator = completeUps + completeDegraded; + let uptime90DayDenominator = completeUps + completeDown + completeDegraded; + + if(monitor.includeDegradedInDowntime === true) { + uptime0DayNumerator = dailyUps; + uptime90DayNumerator = completeUps; + } + uptime0Day = ParseUptime(uptime0DayNumerator, uptime0DayDenominator); const dataToWrite = { _90Day: _90Day, uptime0Day, - uptime90Day: ParseUptime(completeUps + completeDegraded, completeUps + completeDegraded + completeDown), + uptime90Day: ParseUptime(uptime90DayNumerator, uptime90DayDenominator), dailyUps, dailyDown, dailyDegraded, diff --git a/scripts/startup.js b/scripts/startup.js index 81e1cb6..174a8af 100644 --- a/scripts/startup.js +++ b/scripts/startup.js @@ -72,6 +72,24 @@ const Startup = async () => { process.exit(1); } + if(monitor.dayDegradedMinimumCount && (isNaN(monitor.dayDegradedMinimumCount) || monitor.dayDegradedMinimumCount < 1)){ + console.log("dayDegradedMinimumCount is not a number or it is less than 1"); + process.exit(1); + } else if(monitor.dayDegradedMinimumCount === undefined) { + monitors[i].dayDegradedMinimumCount = 1; + } + + if(monitor.dayDownMinimumCount && (isNaN(monitor.dayDownMinimumCount) || monitor.dayDownMinimumCount < 1)){ + console.log("dayDownMinimumCount is not a number or it is less than 1"); + process.exit(1); + } else if(monitor.dayDownMinimumCount === undefined) { + monitors[i].dayDownMinimumCount = 1; + } + + if (monitor.includeDegradedInDowntime === undefined || monitor.includeDegradedInDowntime !== true) { + monitors[i].includeDegradedInDowntime = false; + } + if(hasAPI) { let url = monitor.api.url; let method = monitor.api.method; @@ -184,6 +202,8 @@ const Startup = async () => { }); } } + + } if (site.github === undefined || site.github.owner === undefined || site.github.repo === undefined) { console.log("github owner and repo are required");