2023-08-31 17:19:21 -04:00
|
|
|
const dayjs = require("dayjs");
|
|
|
|
const { UP, MAINTENANCE, DOWN, PENDING } = require("../src/util");
|
|
|
|
const { LimitQueue } = require("./utils/limit-queue");
|
|
|
|
const { log } = require("../src/util");
|
|
|
|
const { R } = require("redbean-node");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the uptime of a monitor.
|
|
|
|
*/
|
|
|
|
class UptimeCalculator {
|
2023-09-07 03:42:44 -04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @type {{string:UptimeCalculator}}
|
|
|
|
*/
|
2023-08-31 17:19:21 -04:00
|
|
|
|
|
|
|
static list = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For testing purposes, we can set the current date to a specific date.
|
|
|
|
* @type {dayjs.Dayjs}
|
|
|
|
*/
|
|
|
|
static currentDate = null;
|
|
|
|
|
2023-09-07 03:42:44 -04:00
|
|
|
/**
|
|
|
|
* monitorID the id of the monitor
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2023-08-31 17:19:21 -04:00
|
|
|
monitorID;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recent 24-hour uptime, each item is a 1-minute interval
|
|
|
|
* Key: {number} DivisionKey
|
2023-09-07 03:42:44 -04:00
|
|
|
* @type {LimitQueue<number,string>}
|
2023-08-31 17:19:21 -04:00
|
|
|
*/
|
|
|
|
minutelyUptimeDataList = new LimitQueue(24 * 60);
|
|
|
|
|
2024-01-05 07:42:24 -05:00
|
|
|
/**
|
|
|
|
* Recent 30-day uptime, each item is a 1-hour interval
|
|
|
|
* Key: {number} DivisionKey
|
|
|
|
* @type {LimitQueue<number,string>}
|
|
|
|
*/
|
|
|
|
hourlyUptimeDataList = new LimitQueue(30 * 24);
|
|
|
|
|
2023-08-31 17:19:21 -04:00
|
|
|
/**
|
|
|
|
* Daily uptime data,
|
|
|
|
* Key: {number} DailyKey
|
|
|
|
*/
|
|
|
|
dailyUptimeDataList = new LimitQueue(365);
|
|
|
|
|
|
|
|
lastUptimeData = null;
|
2024-01-05 07:42:24 -05:00
|
|
|
lastHourlyUptimeData = null;
|
|
|
|
lastDailyUptimeData = null;
|
2023-08-31 17:19:21 -04:00
|
|
|
|
|
|
|
lastDailyStatBean = null;
|
2024-01-05 07:42:24 -05:00
|
|
|
lastHourlyStatBean = null;
|
2023-08-31 17:19:21 -04:00
|
|
|
lastMinutelyStatBean = null;
|
|
|
|
|
|
|
|
/**
|
2023-09-07 03:42:44 -04:00
|
|
|
* Get the uptime calculator for a monitor
|
|
|
|
* Initializes and returns the monitor if it does not exist
|
|
|
|
* @param {number} monitorID the id of the monitor
|
|
|
|
* @returns {Promise<UptimeCalculator>} UptimeCalculator
|
2023-08-31 17:19:21 -04:00
|
|
|
*/
|
|
|
|
static async getUptimeCalculator(monitorID) {
|
2024-01-05 07:42:24 -05:00
|
|
|
if (!monitorID) {
|
|
|
|
throw new Error("Monitor ID is required");
|
|
|
|
}
|
|
|
|
|
2023-08-31 17:19:21 -04:00
|
|
|
if (!UptimeCalculator.list[monitorID]) {
|
|
|
|
UptimeCalculator.list[monitorID] = new UptimeCalculator();
|
|
|
|
await UptimeCalculator.list[monitorID].init(monitorID);
|
|
|
|
}
|
|
|
|
return UptimeCalculator.list[monitorID];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-09-07 03:42:44 -04:00
|
|
|
* Remove a monitor from the list
|
|
|
|
* @param {number} monitorID the id of the monitor
|
|
|
|
* @returns {Promise<void>}
|
2023-08-31 17:19:21 -04:00
|
|
|
*/
|
|
|
|
static async remove(monitorID) {
|
|
|
|
delete UptimeCalculator.list[monitorID];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
if (process.env.TEST_BACKEND) {
|
|
|
|
// Override the getCurrentDate() method to return a specific date
|
|
|
|
// Only for testing
|
|
|
|
this.getCurrentDate = () => {
|
|
|
|
if (UptimeCalculator.currentDate) {
|
|
|
|
return UptimeCalculator.currentDate;
|
|
|
|
} else {
|
|
|
|
return dayjs.utc();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-09-07 03:42:44 -04:00
|
|
|
* Initialize the uptime calculator for a monitor
|
|
|
|
* @param {number} monitorID the id of the monitor
|
|
|
|
* @returns {Promise<void>}
|
2023-08-31 17:19:21 -04:00
|
|
|
*/
|
|
|
|
async init(monitorID) {
|
|
|
|
this.monitorID = monitorID;
|
|
|
|
|
|
|
|
let now = this.getCurrentDate();
|
|
|
|
|
|
|
|
// Load minutely data from database (recent 24 hours only)
|
|
|
|
let minutelyStatBeans = await R.find("stat_minutely", " monitor_id = ? AND timestamp > ? ORDER BY timestamp", [
|
|
|
|
monitorID,
|
|
|
|
this.getMinutelyKey(now.subtract(24, "hour")),
|
|
|
|
]);
|
|
|
|
|
|
|
|
for (let bean of minutelyStatBeans) {
|
|
|
|
let key = bean.timestamp;
|
|
|
|
this.minutelyUptimeDataList.push(key, {
|
|
|
|
up: bean.up,
|
|
|
|
down: bean.down,
|
|
|
|
avgPing: bean.ping,
|
2024-01-05 07:42:24 -05:00
|
|
|
minPing: bean.pingMin,
|
|
|
|
maxPing: bean.pingMax,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load hourly data from database (recent 30 days only)
|
|
|
|
let hourlyStatBeans = await R.find("stat_hourly", " monitor_id = ? AND timestamp > ? ORDER BY timestamp", [
|
|
|
|
monitorID,
|
|
|
|
this.getHourlyKey(now.subtract(30, "day")),
|
|
|
|
]);
|
|
|
|
|
|
|
|
for (let bean of hourlyStatBeans) {
|
|
|
|
let key = bean.timestamp;
|
|
|
|
this.hourlyUptimeDataList.push(key, {
|
|
|
|
up: bean.up,
|
|
|
|
down: bean.down,
|
|
|
|
avgPing: bean.ping,
|
|
|
|
minPing: bean.pingMin,
|
|
|
|
maxPing: bean.pingMax,
|
2023-08-31 17:19:21 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load daily data from database (recent 365 days only)
|
|
|
|
let dailyStatBeans = await R.find("stat_daily", " monitor_id = ? AND timestamp > ? ORDER BY timestamp", [
|
|
|
|
monitorID,
|
2024-01-05 07:42:24 -05:00
|
|
|
this.getDailyKey(now.subtract(365, "day")),
|
2023-08-31 17:19:21 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
for (let bean of dailyStatBeans) {
|
|
|
|
let key = bean.timestamp;
|
|
|
|
this.dailyUptimeDataList.push(key, {
|
|
|
|
up: bean.up,
|
|
|
|
down: bean.down,
|
|
|
|
avgPing: bean.ping,
|
2024-01-05 07:42:24 -05:00
|
|
|
minPing: bean.pingMin,
|
|
|
|
maxPing: bean.pingMax,
|
2023-08-31 17:19:21 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} status status
|
|
|
|
* @param {number} ping Ping
|
|
|
|
* @returns {dayjs.Dayjs} date
|
|
|
|
* @throws {Error} Invalid status
|
|
|
|
*/
|
|
|
|
async update(status, ping = 0) {
|
|
|
|
let date = this.getCurrentDate();
|
|
|
|
|
|
|
|
// Don't count MAINTENANCE into uptime
|
|
|
|
if (status === MAINTENANCE) {
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
|
|
|
|
let flatStatus = this.flatStatus(status);
|
|
|
|
|
|
|
|
if (flatStatus === DOWN && ping > 0) {
|
|
|
|
log.warn("uptime-calc", "The ping is not effective when the status is DOWN");
|
|
|
|
}
|
|
|
|
|
|
|
|
let divisionKey = this.getMinutelyKey(date);
|
2024-01-05 07:42:24 -05:00
|
|
|
let hourlyKey = this.getHourlyKey(date);
|
|
|
|
let dailyKey = this.getDailyKey(date);
|
2023-08-31 17:19:21 -04:00
|
|
|
|
|
|
|
let minutelyData = this.minutelyUptimeDataList[divisionKey];
|
2024-01-05 07:42:24 -05:00
|
|
|
let hourlyData = this.hourlyUptimeDataList[hourlyKey];
|
2023-08-31 17:19:21 -04:00
|
|
|
let dailyData = this.dailyUptimeDataList[dailyKey];
|
|
|
|
|
|
|
|
if (flatStatus === UP) {
|
|
|
|
minutelyData.up += 1;
|
2024-01-05 07:42:24 -05:00
|
|
|
hourlyData.up += 1;
|
2023-08-31 17:19:21 -04:00
|
|
|
dailyData.up += 1;
|
|
|
|
|
|
|
|
// Only UP status can update the ping
|
|
|
|
if (!isNaN(ping)) {
|
|
|
|
// Add avg ping
|
|
|
|
// The first beat of the minute, the ping is the current ping
|
|
|
|
if (minutelyData.up === 1) {
|
|
|
|
minutelyData.avgPing = ping;
|
2024-01-05 07:42:24 -05:00
|
|
|
minutelyData.minPing = ping;
|
|
|
|
minutelyData.maxPing = ping;
|
2023-08-31 17:19:21 -04:00
|
|
|
} else {
|
|
|
|
minutelyData.avgPing = (minutelyData.avgPing * (minutelyData.up - 1) + ping) / minutelyData.up;
|
2024-01-05 07:42:24 -05:00
|
|
|
minutelyData.minPing = Math.min(minutelyData.minPing, ping);
|
|
|
|
minutelyData.maxPing = Math.max(minutelyData.maxPing, ping);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add avg ping
|
|
|
|
// The first beat of the hour, the ping is the current ping
|
|
|
|
if (hourlyData.up === 1) {
|
|
|
|
hourlyData.avgPing = ping;
|
|
|
|
hourlyData.minPing = ping;
|
|
|
|
hourlyData.maxPing = ping;
|
|
|
|
} else {
|
|
|
|
hourlyData.avgPing = (hourlyData.avgPing * (hourlyData.up - 1) + ping) / hourlyData.up;
|
|
|
|
hourlyData.minPing = Math.min(hourlyData.minPing, ping);
|
|
|
|
hourlyData.maxPing = Math.max(hourlyData.maxPing, ping);
|
2023-08-31 17:19:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add avg ping (daily)
|
|
|
|
// The first beat of the day, the ping is the current ping
|
2024-01-05 07:42:24 -05:00
|
|
|
if (dailyData.up === 1) {
|
2023-08-31 17:19:21 -04:00
|
|
|
dailyData.avgPing = ping;
|
2024-01-05 07:42:24 -05:00
|
|
|
dailyData.minPing = ping;
|
|
|
|
dailyData.maxPing = ping;
|
2023-08-31 17:19:21 -04:00
|
|
|
} else {
|
|
|
|
dailyData.avgPing = (dailyData.avgPing * (dailyData.up - 1) + ping) / dailyData.up;
|
2024-01-05 07:42:24 -05:00
|
|
|
dailyData.minPing = Math.min(dailyData.minPing, ping);
|
|
|
|
dailyData.maxPing = Math.max(dailyData.maxPing, ping);
|
2023-08-31 17:19:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
minutelyData.down += 1;
|
2024-01-05 07:42:24 -05:00
|
|
|
hourlyData.down += 1;
|
2023-08-31 17:19:21 -04:00
|
|
|
dailyData.down += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (minutelyData !== this.lastUptimeData) {
|
|
|
|
this.lastUptimeData = minutelyData;
|
|
|
|
}
|
|
|
|
|
2024-01-05 07:42:24 -05:00
|
|
|
if (hourlyData !== this.lastHourlyUptimeData) {
|
|
|
|
this.lastHourlyUptimeData = hourlyData;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dailyData !== this.lastDailyUptimeData) {
|
|
|
|
this.lastDailyUptimeData = dailyData;
|
|
|
|
}
|
|
|
|
|
2023-08-31 17:19:21 -04:00
|
|
|
// Don't store data in test mode
|
|
|
|
if (process.env.TEST_BACKEND) {
|
|
|
|
log.debug("uptime-calc", "Skip storing data in test mode");
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
|
|
|
|
let dailyStatBean = await this.getDailyStatBean(dailyKey);
|
|
|
|
dailyStatBean.up = dailyData.up;
|
|
|
|
dailyStatBean.down = dailyData.down;
|
2023-09-02 05:11:09 -04:00
|
|
|
dailyStatBean.ping = dailyData.avgPing;
|
2024-01-05 07:42:24 -05:00
|
|
|
dailyStatBean.pingMin = dailyData.minPing;
|
|
|
|
dailyStatBean.pingMax = dailyData.maxPing;
|
2023-08-31 17:19:21 -04:00
|
|
|
await R.store(dailyStatBean);
|
|
|
|
|
2024-01-05 07:42:24 -05:00
|
|
|
let hourlyStatBean = await this.getHourlyStatBean(hourlyKey);
|
|
|
|
hourlyStatBean.up = hourlyData.up;
|
|
|
|
hourlyStatBean.down = hourlyData.down;
|
|
|
|
hourlyStatBean.ping = hourlyData.avgPing;
|
|
|
|
hourlyStatBean.pingMin = hourlyData.minPing;
|
|
|
|
hourlyStatBean.pingMax = hourlyData.maxPing;
|
|
|
|
await R.store(hourlyStatBean);
|
|
|
|
|
2023-08-31 17:19:21 -04:00
|
|
|
let minutelyStatBean = await this.getMinutelyStatBean(divisionKey);
|
|
|
|
minutelyStatBean.up = minutelyData.up;
|
|
|
|
minutelyStatBean.down = minutelyData.down;
|
2023-09-02 05:11:09 -04:00
|
|
|
minutelyStatBean.ping = minutelyData.avgPing;
|
2024-01-05 07:42:24 -05:00
|
|
|
minutelyStatBean.pingMin = minutelyData.minPing;
|
|
|
|
minutelyStatBean.pingMax = minutelyData.maxPing;
|
2023-08-31 17:19:21 -04:00
|
|
|
await R.store(minutelyStatBean);
|
|
|
|
|
|
|
|
// Remove the old data
|
|
|
|
log.debug("uptime-calc", "Remove old data");
|
|
|
|
await R.exec("DELETE FROM stat_minutely WHERE monitor_id = ? AND timestamp < ?", [
|
|
|
|
this.monitorID,
|
|
|
|
this.getMinutelyKey(date.subtract(24, "hour")),
|
|
|
|
]);
|
|
|
|
|
2024-01-05 07:42:24 -05:00
|
|
|
await R.exec("DELETE FROM stat_hourly WHERE monitor_id = ? AND timestamp < ?", [
|
|
|
|
this.monitorID,
|
|
|
|
this.getHourlyKey(date.subtract(30, "day")),
|
|
|
|
]);
|
|
|
|
|
2023-08-31 17:19:21 -04:00
|
|
|
return date;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the daily stat bean
|
|
|
|
* @param {number} timestamp milliseconds
|
|
|
|
* @returns {Promise<import("redbean-node").Bean>} stat_daily bean
|
|
|
|
*/
|
|
|
|
async getDailyStatBean(timestamp) {
|
|
|
|
if (this.lastDailyStatBean && this.lastDailyStatBean.timestamp === timestamp) {
|
|
|
|
return this.lastDailyStatBean;
|
|
|
|
}
|
|
|
|
|
|
|
|
let bean = await R.findOne("stat_daily", " monitor_id = ? AND timestamp = ?", [
|
|
|
|
this.monitorID,
|
|
|
|
timestamp,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (!bean) {
|
|
|
|
bean = R.dispense("stat_daily");
|
|
|
|
bean.monitor_id = this.monitorID;
|
|
|
|
bean.timestamp = timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.lastDailyStatBean = bean;
|
|
|
|
return this.lastDailyStatBean;
|
|
|
|
}
|
|
|
|
|
2024-01-05 07:42:24 -05:00
|
|
|
/**
|
|
|
|
* Get the hourly stat bean
|
|
|
|
* @param {number} timestamp milliseconds
|
|
|
|
* @returns {Promise<import("redbean-node").Bean>} stat_hourly bean
|
|
|
|
*/
|
|
|
|
async getHourlyStatBean(timestamp) {
|
|
|
|
if (this.lastHourlyStatBean && this.lastHourlyStatBean.timestamp === timestamp) {
|
|
|
|
return this.lastHourlyStatBean;
|
|
|
|
}
|
|
|
|
|
|
|
|
let bean = await R.findOne("stat_hourly", " monitor_id = ? AND timestamp = ?", [
|
|
|
|
this.monitorID,
|
|
|
|
timestamp,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (!bean) {
|
|
|
|
bean = R.dispense("stat_hourly");
|
|
|
|
bean.monitor_id = this.monitorID;
|
|
|
|
bean.timestamp = timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.lastHourlyStatBean = bean;
|
|
|
|
return this.lastHourlyStatBean;
|
|
|
|
}
|
|
|
|
|
2023-08-31 17:19:21 -04:00
|
|
|
/**
|
|
|
|
* Get the minutely stat bean
|
|
|
|
* @param {number} timestamp milliseconds
|
|
|
|
* @returns {Promise<import("redbean-node").Bean>} stat_minutely bean
|
|
|
|
*/
|
|
|
|
async getMinutelyStatBean(timestamp) {
|
|
|
|
if (this.lastMinutelyStatBean && this.lastMinutelyStatBean.timestamp === timestamp) {
|
|
|
|
return this.lastMinutelyStatBean;
|
|
|
|
}
|
|
|
|
|
|
|
|
let bean = await R.findOne("stat_minutely", " monitor_id = ? AND timestamp = ?", [
|
|
|
|
this.monitorID,
|
|
|
|
timestamp,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (!bean) {
|
|
|
|
bean = R.dispense("stat_minutely");
|
|
|
|
bean.monitor_id = this.monitorID;
|
|
|
|
bean.timestamp = timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.lastMinutelyStatBean = bean;
|
|
|
|
return this.lastMinutelyStatBean;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-01-05 07:42:24 -05:00
|
|
|
* Convert timestamp to minutely key
|
2023-08-31 17:19:21 -04:00
|
|
|
* @param {dayjs.Dayjs} date The heartbeat date
|
|
|
|
* @returns {number} Timestamp
|
|
|
|
*/
|
|
|
|
getMinutelyKey(date) {
|
2024-01-05 07:42:24 -05:00
|
|
|
// Truncate value to minutes (e.g. 2021-01-01 12:34:56 -> 2021-01-01 12:34:00)
|
2023-08-31 17:19:21 -04:00
|
|
|
date = date.startOf("minute");
|
|
|
|
|
|
|
|
// Convert to timestamp in second
|
|
|
|
let divisionKey = date.unix();
|
|
|
|
|
|
|
|
if (! (divisionKey in this.minutelyUptimeDataList)) {
|
|
|
|
this.minutelyUptimeDataList.push(divisionKey, {
|
|
|
|
up: 0,
|
|
|
|
down: 0,
|
|
|
|
avgPing: 0,
|
2024-01-05 07:42:24 -05:00
|
|
|
minPing: 0,
|
|
|
|
maxPing: 0,
|
2023-08-31 17:19:21 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return divisionKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-01-05 07:42:24 -05:00
|
|
|
* Convert timestamp to hourly key
|
|
|
|
* @param {dayjs.Dayjs} date The heartbeat date
|
2023-08-31 17:19:21 -04:00
|
|
|
* @returns {number} Timestamp
|
|
|
|
*/
|
2024-01-05 07:42:24 -05:00
|
|
|
getHourlyKey(date) {
|
|
|
|
// Truncate value to hours (e.g. 2021-01-01 12:34:56 -> 2021-01-01 12:00:00)
|
|
|
|
date = date.startOf("hour");
|
|
|
|
|
|
|
|
// Convert to timestamp in second
|
|
|
|
let divisionKey = date.unix();
|
|
|
|
|
|
|
|
if (! (divisionKey in this.hourlyUptimeDataList)) {
|
|
|
|
this.hourlyUptimeDataList.push(divisionKey, {
|
|
|
|
up: 0,
|
|
|
|
down: 0,
|
|
|
|
avgPing: 0,
|
|
|
|
minPing: 0,
|
|
|
|
maxPing: 0,
|
|
|
|
});
|
|
|
|
}
|
2023-08-31 17:19:21 -04:00
|
|
|
|
2024-01-05 07:42:24 -05:00
|
|
|
return divisionKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert timestamp to daily key
|
|
|
|
* @param {dayjs.Dayjs} date The heartbeat date
|
|
|
|
* @returns {number} Timestamp
|
|
|
|
*/
|
|
|
|
getDailyKey(date) {
|
|
|
|
// Truncate value to start of day (e.g. 2021-01-01 12:34:56 -> 2021-01-01 00:00:00)
|
2023-08-31 17:19:21 -04:00
|
|
|
// Considering if the user keep changing could affect the calculation, so use UTC time to avoid this problem.
|
|
|
|
date = date.utc().startOf("day");
|
|
|
|
let dailyKey = date.unix();
|
|
|
|
|
|
|
|
if (!this.dailyUptimeDataList[dailyKey]) {
|
|
|
|
this.dailyUptimeDataList.push(dailyKey, {
|
|
|
|
up: 0,
|
|
|
|
down: 0,
|
|
|
|
avgPing: 0,
|
2024-01-05 07:42:24 -05:00
|
|
|
minPing: 0,
|
|
|
|
maxPing: 0,
|
2023-08-31 17:19:21 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return dailyKey;
|
|
|
|
}
|
|
|
|
|
2024-01-05 07:42:24 -05:00
|
|
|
/**
|
|
|
|
* Convert timestamp to key
|
|
|
|
* @param {dayjs.Dayjs} datetime Datetime
|
|
|
|
* @param {"day" | "hour" | "minute"} type the type of data which is expected to be returned
|
|
|
|
* @returns {number} Timestamp
|
|
|
|
* @throws {Error} If the type is invalid
|
|
|
|
*/
|
|
|
|
getKey(datetime, type) {
|
|
|
|
switch (type) {
|
|
|
|
case "day":
|
|
|
|
return this.getDailyKey(datetime);
|
|
|
|
case "hour":
|
|
|
|
return this.getHourlyKey(datetime);
|
|
|
|
case "minute":
|
|
|
|
return this.getMinutelyKey(datetime);
|
|
|
|
default:
|
|
|
|
throw new Error("Invalid type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-31 17:19:21 -04:00
|
|
|
/**
|
|
|
|
* Flat status to UP or DOWN
|
2023-09-07 03:42:44 -04:00
|
|
|
* @param {number} status the status which schould be turned into a flat status
|
|
|
|
* @returns {UP|DOWN|PENDING} The flat status
|
2023-08-31 17:19:21 -04:00
|
|
|
* @throws {Error} Invalid status
|
|
|
|
*/
|
|
|
|
flatStatus(status) {
|
|
|
|
switch (status) {
|
|
|
|
case UP:
|
|
|
|
// case MAINTENANCE:
|
|
|
|
return UP;
|
|
|
|
case DOWN:
|
|
|
|
case PENDING:
|
|
|
|
return DOWN;
|
|
|
|
}
|
|
|
|
throw new Error("Invalid status");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-09-07 03:42:44 -04:00
|
|
|
* @param {number} num the number of data points which are expected to be returned
|
2024-01-05 07:42:24 -05:00
|
|
|
* @param {"day" | "hour" | "minute"} type the type of data which is expected to be returned
|
2023-09-07 03:42:44 -04:00
|
|
|
* @returns {UptimeDataResult} UptimeDataResult
|
|
|
|
* @throws {Error} The maximum number of minutes greater than 1440
|
2023-08-31 17:19:21 -04:00
|
|
|
*/
|
|
|
|
getData(num, type = "day") {
|
|
|
|
|
2024-01-05 07:42:24 -05:00
|
|
|
if (type === "hour" && num > 24 * 30) {
|
|
|
|
throw new Error("The maximum number of hours is 720");
|
|
|
|
}
|
|
|
|
if (type === "minute" && num > 24 * 60) {
|
|
|
|
throw new Error("The maximum number of minutes is 1440");
|
2023-08-31 17:19:21 -04:00
|
|
|
}
|
|
|
|
|
2024-01-05 07:42:24 -05:00
|
|
|
// Get the current time period key based on the type
|
|
|
|
let key = this.getKey(this.getCurrentDate(), type);
|
|
|
|
|
2023-08-31 17:19:21 -04:00
|
|
|
let total = {
|
|
|
|
up: 0,
|
|
|
|
down: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
let totalPing = 0;
|
|
|
|
let endTimestamp;
|
|
|
|
|
2024-01-05 07:42:24 -05:00
|
|
|
// Get the eariest timestamp of the required period based on the type
|
|
|
|
switch (type) {
|
|
|
|
case "day":
|
|
|
|
endTimestamp = key - 86400 * (num - 1);
|
|
|
|
break;
|
|
|
|
case "hour":
|
|
|
|
endTimestamp = key - 3600 * (num - 1);
|
|
|
|
break;
|
|
|
|
case "minute":
|
|
|
|
endTimestamp = key - 60 * (num - 1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error("Invalid type");
|
2023-08-31 17:19:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sum up all data in the specified time range
|
|
|
|
while (key >= endTimestamp) {
|
|
|
|
let data;
|
|
|
|
|
2024-01-05 07:42:24 -05:00
|
|
|
switch (type) {
|
|
|
|
case "day":
|
|
|
|
data = this.dailyUptimeDataList[key];
|
|
|
|
break;
|
|
|
|
case "hour":
|
|
|
|
data = this.hourlyUptimeDataList[key];
|
|
|
|
break;
|
|
|
|
case "minute":
|
|
|
|
data = this.minutelyUptimeDataList[key];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error("Invalid type");
|
2023-08-31 17:19:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
total.up += data.up;
|
|
|
|
total.down += data.down;
|
|
|
|
totalPing += data.avgPing * data.up;
|
|
|
|
}
|
|
|
|
|
2024-01-05 07:42:24 -05:00
|
|
|
// Set key to the pervious time period
|
|
|
|
switch (type) {
|
|
|
|
case "day":
|
|
|
|
key -= 86400;
|
|
|
|
break;
|
|
|
|
case "hour":
|
|
|
|
key -= 3600;
|
|
|
|
break;
|
|
|
|
case "minute":
|
|
|
|
key -= 60;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error("Invalid type");
|
2023-08-31 17:19:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let uptimeData = new UptimeDataResult();
|
|
|
|
|
2024-01-05 07:42:24 -05:00
|
|
|
// If there is no data in the previous time ranges, use the last data?
|
2023-08-31 17:19:21 -04:00
|
|
|
if (total.up === 0 && total.down === 0) {
|
2024-01-05 07:42:24 -05:00
|
|
|
switch (type) {
|
|
|
|
case "day":
|
|
|
|
if (this.lastDailyUptimeData) {
|
|
|
|
total = this.lastDailyUptimeData;
|
|
|
|
totalPing = total.avgPing * total.up;
|
|
|
|
} else {
|
|
|
|
return uptimeData;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "hour":
|
|
|
|
if (this.lastHourlyUptimeData) {
|
|
|
|
total = this.lastHourlyUptimeData;
|
|
|
|
totalPing = total.avgPing * total.up;
|
|
|
|
} else {
|
|
|
|
return uptimeData;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "minute":
|
|
|
|
if (this.lastUptimeData) {
|
|
|
|
total = this.lastUptimeData;
|
|
|
|
totalPing = total.avgPing * total.up;
|
|
|
|
} else {
|
|
|
|
return uptimeData;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error("Invalid type");
|
2023-08-31 17:19:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let avgPing;
|
|
|
|
|
|
|
|
if (total.up === 0) {
|
|
|
|
avgPing = null;
|
|
|
|
} else {
|
|
|
|
avgPing = totalPing / total.up;
|
|
|
|
}
|
|
|
|
|
|
|
|
uptimeData.uptime = total.up / (total.up + total.down);
|
|
|
|
uptimeData.avgPing = avgPing;
|
|
|
|
return uptimeData;
|
|
|
|
}
|
|
|
|
|
2024-01-05 07:42:24 -05:00
|
|
|
/**
|
|
|
|
* Get data in form of an array
|
|
|
|
* @param {number} num the number of data points which are expected to be returned
|
|
|
|
* @param {"day" | "hour" | "minute"} type the type of data which is expected to be returned
|
|
|
|
* @returns {Array<object>} uptime data
|
|
|
|
* @throws {Error} The maximum number of minutes greater than 1440
|
|
|
|
*/
|
|
|
|
getDataArray(num, type = "day") {
|
|
|
|
if (type === "hour" && num > 24 * 30) {
|
|
|
|
throw new Error("The maximum number of hours is 720");
|
|
|
|
}
|
|
|
|
if (type === "minute" && num > 24 * 60) {
|
|
|
|
throw new Error("The maximum number of minutes is 1440");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current time period key based on the type
|
|
|
|
let key = this.getKey(this.getCurrentDate(), type);
|
|
|
|
|
|
|
|
let result = [];
|
|
|
|
|
|
|
|
let endTimestamp;
|
|
|
|
|
|
|
|
// Get the eariest timestamp of the required period based on the type
|
|
|
|
switch (type) {
|
|
|
|
case "day":
|
|
|
|
endTimestamp = key - 86400 * (num - 1);
|
|
|
|
break;
|
|
|
|
case "hour":
|
|
|
|
endTimestamp = key - 3600 * (num - 1);
|
|
|
|
break;
|
|
|
|
case "minute":
|
|
|
|
endTimestamp = key - 60 * (num - 1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error("Invalid type");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get datapoints in the specified time range
|
|
|
|
while (key >= endTimestamp) {
|
|
|
|
let data;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case "day":
|
|
|
|
data = this.dailyUptimeDataList[key];
|
|
|
|
break;
|
|
|
|
case "hour":
|
|
|
|
data = this.hourlyUptimeDataList[key];
|
|
|
|
break;
|
|
|
|
case "minute":
|
|
|
|
data = this.minutelyUptimeDataList[key];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error("Invalid type");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
data.timestamp = key;
|
|
|
|
result.push(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set key to the pervious time period
|
|
|
|
switch (type) {
|
|
|
|
case "day":
|
|
|
|
key -= 86400;
|
|
|
|
break;
|
|
|
|
case "hour":
|
|
|
|
key -= 3600;
|
|
|
|
break;
|
|
|
|
case "minute":
|
|
|
|
key -= 60;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error("Invalid type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-08-31 17:19:21 -04:00
|
|
|
/**
|
|
|
|
* Get the uptime data by duration
|
|
|
|
* @param {'24h'|'30d'|'1y'} duration Only accept 24h, 30d, 1y
|
|
|
|
* @returns {UptimeDataResult} UptimeDataResult
|
|
|
|
* @throws {Error} Invalid duration
|
|
|
|
*/
|
|
|
|
getDataByDuration(duration) {
|
|
|
|
if (duration === "24h") {
|
|
|
|
return this.get24Hour();
|
|
|
|
} else if (duration === "30d") {
|
|
|
|
return this.get30Day();
|
|
|
|
} else if (duration === "1y") {
|
|
|
|
return this.get1Year();
|
|
|
|
} else {
|
|
|
|
throw new Error("Invalid duration");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 1440 = 24 * 60mins
|
|
|
|
* @returns {UptimeDataResult} UptimeDataResult
|
|
|
|
*/
|
|
|
|
get24Hour() {
|
|
|
|
return this.getData(1440, "minute");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {UptimeDataResult} UptimeDataResult
|
|
|
|
*/
|
|
|
|
get7Day() {
|
2024-01-05 07:42:24 -05:00
|
|
|
return this.getData(168, "hour");
|
2023-08-31 17:19:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {UptimeDataResult} UptimeDataResult
|
|
|
|
*/
|
|
|
|
get30Day() {
|
|
|
|
return this.getData(30);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {UptimeDataResult} UptimeDataResult
|
|
|
|
*/
|
|
|
|
get1Year() {
|
|
|
|
return this.getData(365);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-01-05 07:42:24 -05:00
|
|
|
* @returns {dayjs.Dayjs} Current datetime in UTC
|
2023-08-31 17:19:21 -04:00
|
|
|
*/
|
|
|
|
getCurrentDate() {
|
|
|
|
return dayjs.utc();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class UptimeDataResult {
|
|
|
|
/**
|
|
|
|
* @type {number} Uptime
|
|
|
|
*/
|
2024-01-05 07:42:24 -05:00
|
|
|
uptime = 0;
|
2023-08-31 17:19:21 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {number} Average ping
|
|
|
|
*/
|
2024-01-05 07:42:24 -05:00
|
|
|
avgPing = null;
|
2023-08-31 17:19:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
UptimeCalculator,
|
|
|
|
UptimeDataResult,
|
|
|
|
};
|