|
|
@ -8,6 +8,10 @@ const { R } = require("redbean-node");
|
|
|
|
* Calculates the uptime of a monitor.
|
|
|
|
* Calculates the uptime of a monitor.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
class UptimeCalculator {
|
|
|
|
class UptimeCalculator {
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @private
|
|
|
|
|
|
|
|
* @type {{string:UptimeCalculator}}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
static list = {};
|
|
|
|
static list = {};
|
|
|
|
|
|
|
|
|
|
|
@ -17,11 +21,16 @@ class UptimeCalculator {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
static currentDate = null;
|
|
|
|
static currentDate = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* monitorID the id of the monitor
|
|
|
|
|
|
|
|
* @type {number}
|
|
|
|
|
|
|
|
*/
|
|
|
|
monitorID;
|
|
|
|
monitorID;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Recent 24-hour uptime, each item is a 1-minute interval
|
|
|
|
* Recent 24-hour uptime, each item is a 1-minute interval
|
|
|
|
* Key: {number} DivisionKey
|
|
|
|
* Key: {number} DivisionKey
|
|
|
|
|
|
|
|
* @type {LimitQueue<number,string>}
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
minutelyUptimeDataList = new LimitQueue(24 * 60);
|
|
|
|
minutelyUptimeDataList = new LimitQueue(24 * 60);
|
|
|
|
|
|
|
|
|
|
|
@ -38,8 +47,10 @@ class UptimeCalculator {
|
|
|
|
lastMinutelyStatBean = null;
|
|
|
|
lastMinutelyStatBean = null;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param monitorID
|
|
|
|
* Get the uptime calculator for a monitor
|
|
|
|
* @returns {Promise<UptimeCalculator>}
|
|
|
|
* Initializes and returns the monitor if it does not exist
|
|
|
|
|
|
|
|
* @param {number} monitorID the id of the monitor
|
|
|
|
|
|
|
|
* @returns {Promise<UptimeCalculator>} UptimeCalculator
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
static async getUptimeCalculator(monitorID) {
|
|
|
|
static async getUptimeCalculator(monitorID) {
|
|
|
|
if (!UptimeCalculator.list[monitorID]) {
|
|
|
|
if (!UptimeCalculator.list[monitorID]) {
|
|
|
@ -50,7 +61,9 @@ class UptimeCalculator {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param monitorID
|
|
|
|
* Remove a monitor from the list
|
|
|
|
|
|
|
|
* @param {number} monitorID the id of the monitor
|
|
|
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
static async remove(monitorID) {
|
|
|
|
static async remove(monitorID) {
|
|
|
|
delete UptimeCalculator.list[monitorID];
|
|
|
|
delete UptimeCalculator.list[monitorID];
|
|
|
@ -74,7 +87,9 @@ class UptimeCalculator {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param {number} monitorID
|
|
|
|
* Initialize the uptime calculator for a monitor
|
|
|
|
|
|
|
|
* @param {number} monitorID the id of the monitor
|
|
|
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async init(monitorID) {
|
|
|
|
async init(monitorID) {
|
|
|
|
this.monitorID = monitorID;
|
|
|
|
this.monitorID = monitorID;
|
|
|
@ -300,8 +315,8 @@ class UptimeCalculator {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Flat status to UP or DOWN
|
|
|
|
* Flat status to UP or DOWN
|
|
|
|
* @param {number} status
|
|
|
|
* @param {number} status the status which schould be turned into a flat status
|
|
|
|
* @returns {number}
|
|
|
|
* @returns {UP|DOWN|PENDING} The flat status
|
|
|
|
* @throws {Error} Invalid status
|
|
|
|
* @throws {Error} Invalid status
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
flatStatus(status) {
|
|
|
|
flatStatus(status) {
|
|
|
@ -317,8 +332,10 @@ class UptimeCalculator {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param {number} num
|
|
|
|
* @param {number} num the number of data points which are expected to be returned
|
|
|
|
* @param {string} type "day" | "minute"
|
|
|
|
* @param {"day" | "minute"} type the type of data which is expected to be returned
|
|
|
|
|
|
|
|
* @returns {UptimeDataResult} UptimeDataResult
|
|
|
|
|
|
|
|
* @throws {Error} The maximum number of minutes greater than 1440
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
getData(num, type = "day") {
|
|
|
|
getData(num, type = "day") {
|
|
|
|
let key;
|
|
|
|
let key;
|
|
|
|