2023-08-31 14:19:21 -07:00
|
|
|
const dayjs = require("dayjs");
|
2024-06-13 08:39:12 -07:00
|
|
|
const { UP, MAINTENANCE, DOWN, PENDING } = require("../src/util");
|
2023-08-31 14:19:21 -07:00
|
|
|
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 00:42:44 -07:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @type {{string:UptimeCalculator}}
|
|
|
|
*/
|
2023-08-31 14:19:21 -07: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 00:42:44 -07:00
|
|
|
/**
|
|
|
|
* monitorID the id of the monitor
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2023-08-31 14:19:21 -07:00
|
|
|
monitorID;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recent 24-hour uptime, each item is a 1-minute interval
|
|
|
|
* Key: {number} DivisionKey
|
2023-09-07 00:42:44 -07:00
|
|
|
* @type {LimitQueue<number,string>}
|
2023-08-31 14:19:21 -07:00
|
|
|
*/
|
|
|
|
minutelyUptimeDataList = new LimitQueue(24 * 60);
|
|
|
|
|
2024-01-05 04:42:24 -08: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 14:19:21 -07:00
|
|
|
/**
|
|
|
|
* Daily uptime data,
|
|
|
|
* Key: {number} DailyKey
|
|
|
|
*/
|
|
|
|
dailyUptimeDataList = new LimitQueue(365);
|
|
|
|
|
|
|
|
lastUptimeData = null;
|
2024-01-05 04:42:24 -08:00
|
|
|
lastHourlyUptimeData = null;
|
|
|
|
lastDailyUptimeData = null;
|
2023-08-31 14:19:21 -07:00
|
|
|
|
|
|
|
lastDailyStatBean = null;
|
2024-01-05 04:42:24 -08:00
|
|
|
lastHourlyStatBean = null;
|
2023-08-31 14:19:21 -07:00
|
|
|
lastMinutelyStatBean = null;
|
|
|
|
|
2024-10-26 05:50:29 -07:00
|
|
|
/**
|
|
|
|
* For migration purposes.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
migrationMode = false;
|
|
|
|
|
|
|
|
statMinutelyKeepHour = 24;
|
|
|
|
statHourlyKeepDay = 30;
|
|
|
|
|
2023-08-31 14:19:21 -07:00
|
|
|
/**
|
2023-09-07 00:42:44 -07: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 14:19:21 -07:00
|
|
|
*/
|
|
|
|
static async getUptimeCalculator(monitorID) {
|
2024-01-05 04:42:24 -08:00
|
|
|
if (!monitorID) {
|
|
|
|
throw new Error("Monitor ID is required");
|
|
|
|
}
|
|
|
|
|
2023-08-31 14:19:21 -07:00
|
|
|
if (!UptimeCalculator.list[monitorID]) {
|
|
|
|
UptimeCalculator.list[monitorID] = new UptimeCalculator();
|
|
|
|
await UptimeCalculator.list[monitorID].init(monitorID);
|
|
|
|
}
|
|
|
|
return UptimeCalculator.list[monitorID];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-09-07 00:42:44 -07:00
|
|
|
* Remove a monitor from the list
|
|
|
|
* @param {number} monitorID the id of the monitor
|
|
|
|
* @returns {Promise<void>}
|
2023-08-31 14:19:21 -07: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 00:42:44 -07:00
|
|
|
* Initialize the uptime calculator for a monitor
|
|
|
|
* @param {number} monitorID the id of the monitor
|
|
|
|
* @returns {Promise<void>}
|
2023-08-31 14:19:21 -07: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) {
|
2024-03-26 19:19:25 -07:00
|
|
|
let data = {
|
2023-08-31 14:19:21 -07:00
|
|
|
up: bean.up,
|
|
|
|
down: bean.down,
|
|
|
|
avgPing: bean.ping,
|
2024-01-05 04:42:24 -08:00
|
|
|
minPing: bean.pingMin,
|
|
|
|
maxPing: bean.pingMax,
|
2024-03-26 19:19:25 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (bean.extras != null) {
|
|
|
|
data = {
|
|
|
|
...data,
|
|
|
|
...JSON.parse(bean.extras),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let key = bean.timestamp;
|
|
|
|
this.minutelyUptimeDataList.push(key, data);
|
2024-01-05 04:42:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2024-03-26 19:19:25 -07:00
|
|
|
let data = {
|
2024-01-05 04:42:24 -08:00
|
|
|
up: bean.up,
|
|
|
|
down: bean.down,
|
|
|
|
avgPing: bean.ping,
|
|
|
|
minPing: bean.pingMin,
|
|
|
|
maxPing: bean.pingMax,
|
2024-03-26 19:19:25 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (bean.extras != null) {
|
|
|
|
data = {
|
|
|
|
...data,
|
|
|
|
...JSON.parse(bean.extras),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
this.hourlyUptimeDataList.push(bean.timestamp, data);
|
2023-08-31 14:19:21 -07: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 04:42:24 -08:00
|
|
|
this.getDailyKey(now.subtract(365, "day")),
|
2023-08-31 14:19:21 -07:00
|
|
|
]);
|
|
|
|
|
|
|
|
for (let bean of dailyStatBeans) {
|
2024-03-26 19:19:25 -07:00
|
|
|
let data = {
|
2023-08-31 14:19:21 -07:00
|
|
|
up: bean.up,
|
|
|
|
down: bean.down,
|
|
|
|
avgPing: bean.ping,
|
2024-01-05 04:42:24 -08:00
|
|
|
minPing: bean.pingMin,
|
|
|
|
maxPing: bean.pingMax,
|
2024-03-26 19:19:25 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (bean.extras != null) {
|
|
|
|
data = {
|
|
|
|
...data,
|
|
|
|
...JSON.parse(bean.extras),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
this.dailyUptimeDataList.push(bean.timestamp, data);
|
2023-08-31 14:19:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} status status
|
|
|
|
* @param {number} ping Ping
|
2024-10-26 05:50:29 -07:00
|
|
|
* @param {dayjs.Dayjs} date Date (Only for migration)
|
2023-08-31 14:19:21 -07:00
|
|
|
* @returns {dayjs.Dayjs} date
|
|
|
|
* @throws {Error} Invalid status
|
|
|
|
*/
|
2024-10-26 05:50:29 -07:00
|
|
|
async update(status, ping = 0, date) {
|
|
|
|
if (!date) {
|
|
|
|
date = this.getCurrentDate();
|
|
|
|
}
|
2023-08-31 14:19:21 -07:00
|
|
|
|
|
|
|
let flatStatus = this.flatStatus(status);
|
|
|
|
|
|
|
|
if (flatStatus === DOWN && ping > 0) {
|
2024-10-26 05:50:29 -07:00
|
|
|
log.debug("uptime-calc", "The ping is not effective when the status is DOWN");
|
2023-08-31 14:19:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let divisionKey = this.getMinutelyKey(date);
|
2024-01-05 04:42:24 -08:00
|
|
|
let hourlyKey = this.getHourlyKey(date);
|
|
|
|
let dailyKey = this.getDailyKey(date);
|
2023-08-31 14:19:21 -07:00
|
|
|
|
|
|
|
let minutelyData = this.minutelyUptimeDataList[divisionKey];
|
2024-01-05 04:42:24 -08:00
|
|
|
let hourlyData = this.hourlyUptimeDataList[hourlyKey];
|
2023-08-31 14:19:21 -07:00
|
|
|
let dailyData = this.dailyUptimeDataList[dailyKey];
|
|
|
|
|
2024-03-26 19:19:25 -07:00
|
|
|
if (status === MAINTENANCE) {
|
|
|
|
minutelyData.maintenance = minutelyData.maintenance ? minutelyData.maintenance + 1 : 1;
|
|
|
|
hourlyData.maintenance = hourlyData.maintenance ? hourlyData.maintenance + 1 : 1;
|
|
|
|
dailyData.maintenance = dailyData.maintenance ? dailyData.maintenance + 1 : 1;
|
|
|
|
|
|
|
|
} else if (flatStatus === UP) {
|
2023-08-31 14:19:21 -07:00
|
|
|
minutelyData.up += 1;
|
2024-01-05 04:42:24 -08:00
|
|
|
hourlyData.up += 1;
|
2023-08-31 14:19:21 -07: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 04:42:24 -08:00
|
|
|
minutelyData.minPing = ping;
|
|
|
|
minutelyData.maxPing = ping;
|
2023-08-31 14:19:21 -07:00
|
|
|
} else {
|
|
|
|
minutelyData.avgPing = (minutelyData.avgPing * (minutelyData.up - 1) + ping) / minutelyData.up;
|
2024-01-05 04:42:24 -08: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 14:19:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add avg ping (daily)
|
|
|
|
// The first beat of the day, the ping is the current ping
|
2024-01-05 04:42:24 -08:00
|
|
|
if (dailyData.up === 1) {
|
2023-08-31 14:19:21 -07:00
|
|
|
dailyData.avgPing = ping;
|
2024-01-05 04:42:24 -08:00
|
|
|
dailyData.minPing = ping;
|
|
|
|
dailyData.maxPing = ping;
|
2023-08-31 14:19:21 -07:00
|
|
|
} else {
|
|
|
|
dailyData.avgPing = (dailyData.avgPing * (dailyData.up - 1) + ping) / dailyData.up;
|
2024-01-05 04:42:24 -08:00
|
|
|
dailyData.minPing = Math.min(dailyData.minPing, ping);
|
|
|
|
dailyData.maxPing = Math.max(dailyData.maxPing, ping);
|
2023-08-31 14:19:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-26 19:19:25 -07:00
|
|
|
} else if (flatStatus === DOWN) {
|
2023-08-31 14:19:21 -07:00
|
|
|
minutelyData.down += 1;
|
2024-01-05 04:42:24 -08:00
|
|
|
hourlyData.down += 1;
|
2023-08-31 14:19:21 -07:00
|
|
|
dailyData.down += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (minutelyData !== this.lastUptimeData) {
|
|
|
|
this.lastUptimeData = minutelyData;
|
|
|
|
}
|
|
|
|
|
2024-01-05 04:42:24 -08:00
|
|
|
if (hourlyData !== this.lastHourlyUptimeData) {
|
|
|
|
this.lastHourlyUptimeData = hourlyData;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dailyData !== this.lastDailyUptimeData) {
|
|
|
|
this.lastDailyUptimeData = dailyData;
|
|
|
|
}
|
|
|
|
|
2023-08-31 14:19:21 -07: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 02:11:09 -07:00
|
|
|
dailyStatBean.ping = dailyData.avgPing;
|
2024-01-05 04:42:24 -08:00
|
|
|
dailyStatBean.pingMin = dailyData.minPing;
|
|
|
|
dailyStatBean.pingMax = dailyData.maxPing;
|
2024-03-26 19:19:25 -07:00
|
|
|
{
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
2024-05-19 13:03:32 -07:00
|
|
|
const { up, down, avgPing, minPing, maxPing, timestamp, ...extras } = dailyData;
|
2024-03-26 19:19:25 -07:00
|
|
|
if (Object.keys(extras).length > 0) {
|
|
|
|
dailyStatBean.extras = JSON.stringify(extras);
|
|
|
|
}
|
|
|
|
}
|
2023-08-31 14:19:21 -07:00
|
|
|
await R.store(dailyStatBean);
|
|
|
|
|
2024-10-26 05:50:29 -07:00
|
|
|
let currentDate = this.getCurrentDate();
|
|
|
|
|
|
|
|
// For migration mode, we don't need to store old hourly and minutely data, but we need 30-day's hourly data
|
|
|
|
// Run anyway for non-migration mode
|
|
|
|
if (!this.migrationMode || date.isAfter(currentDate.subtract(this.statHourlyKeepDay, "day"))) {
|
|
|
|
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;
|
|
|
|
{
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
const { up, down, avgPing, minPing, maxPing, timestamp, ...extras } = hourlyData;
|
|
|
|
if (Object.keys(extras).length > 0) {
|
|
|
|
hourlyStatBean.extras = JSON.stringify(extras);
|
|
|
|
}
|
2024-03-26 19:19:25 -07:00
|
|
|
}
|
2024-10-26 05:50:29 -07:00
|
|
|
await R.store(hourlyStatBean);
|
|
|
|
}
|
|
|
|
|
|
|
|
// For migration mode, we don't need to store old hourly and minutely data, but we need 24-hour's minutely data
|
|
|
|
// Run anyway for non-migration mode
|
|
|
|
if (!this.migrationMode || date.isAfter(currentDate.subtract(this.statMinutelyKeepHour, "hour"))) {
|
|
|
|
let minutelyStatBean = await this.getMinutelyStatBean(divisionKey);
|
|
|
|
minutelyStatBean.up = minutelyData.up;
|
|
|
|
minutelyStatBean.down = minutelyData.down;
|
|
|
|
minutelyStatBean.ping = minutelyData.avgPing;
|
|
|
|
minutelyStatBean.pingMin = minutelyData.minPing;
|
|
|
|
minutelyStatBean.pingMax = minutelyData.maxPing;
|
|
|
|
{
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
const { up, down, avgPing, minPing, maxPing, timestamp, ...extras } = minutelyData;
|
|
|
|
if (Object.keys(extras).length > 0) {
|
|
|
|
minutelyStatBean.extras = JSON.stringify(extras);
|
|
|
|
}
|
2024-03-26 19:19:25 -07:00
|
|
|
}
|
2024-10-26 05:50:29 -07:00
|
|
|
await R.store(minutelyStatBean);
|
2024-03-26 19:19:25 -07:00
|
|
|
}
|
2023-08-31 14:19:21 -07:00
|
|
|
|
2024-10-26 05:50:29 -07:00
|
|
|
// No need to remove old data in migration mode
|
|
|
|
if (!this.migrationMode) {
|
|
|
|
// Remove the old data
|
|
|
|
// TODO: Improvement: Convert it to a job?
|
|
|
|
log.debug("uptime-calc", "Remove old data");
|
|
|
|
await R.exec("DELETE FROM stat_minutely WHERE monitor_id = ? AND timestamp < ?", [
|
|
|
|
this.monitorID,
|
|
|
|
this.getMinutelyKey(currentDate.subtract(this.statMinutelyKeepHour, "hour")),
|
|
|
|
]);
|
2023-08-31 14:19:21 -07:00
|
|
|
|
2024-10-26 05:50:29 -07:00
|
|
|
await R.exec("DELETE FROM stat_hourly WHERE monitor_id = ? AND timestamp < ?", [
|
|
|
|
this.monitorID,
|
|
|
|
this.getHourlyKey(currentDate.subtract(this.statHourlyKeepDay, "day")),
|
|
|
|
]);
|
|
|
|
}
|
2024-01-05 04:42:24 -08:00
|
|
|
|
2023-08-31 14:19:21 -07: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 04:42:24 -08: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 14:19:21 -07: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 04:42:24 -08:00
|
|
|
* Convert timestamp to minutely key
|
2023-08-31 14:19:21 -07:00
|
|
|
* @param {dayjs.Dayjs} date The heartbeat date
|
|
|
|
* @returns {number} Timestamp
|
|
|
|
*/
|
|
|
|
getMinutelyKey(date) {
|
2024-01-05 04:42:24 -08:00
|
|
|
// Truncate value to minutes (e.g. 2021-01-01 12:34:56 -> 2021-01-01 12:34:00)
|
2023-08-31 14:19:21 -07: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 04:42:24 -08:00
|
|
|
minPing: 0,
|
|
|
|
maxPing: 0,
|
2023-08-31 14:19:21 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return divisionKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-01-05 04:42:24 -08:00
|
|
|
* Convert timestamp to hourly key
|
|
|
|
* @param {dayjs.Dayjs} date The heartbeat date
|
2023-08-31 14:19:21 -07:00
|
|
|
* @returns {number} Timestamp
|
|
|
|
*/
|
2024-01-05 04:42:24 -08: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 14:19:21 -07:00
|
|
|
|
2024-01-05 04:42:24 -08: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 14:19:21 -07: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 04:42:24 -08:00
|
|
|
minPing: 0,
|
|
|
|
maxPing: 0,
|
2023-08-31 14:19:21 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return dailyKey;
|
|
|
|
}
|
|
|
|
|
2024-01-05 04:42:24 -08: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 14:19:21 -07:00
|
|
|
/**
|
|
|
|
* Flat status to UP or DOWN
|
2024-04-16 07:59:07 -07:00
|
|
|
* @param {number} status the status which should be turned into a flat status
|
2023-09-07 00:42:44 -07:00
|
|
|
* @returns {UP|DOWN|PENDING} The flat status
|
2023-08-31 14:19:21 -07:00
|
|
|
* @throws {Error} Invalid status
|
|
|
|
*/
|
|
|
|
flatStatus(status) {
|
|
|
|
switch (status) {
|
|
|
|
case UP:
|
2024-03-26 19:19:25 -07:00
|
|
|
case MAINTENANCE:
|
2023-08-31 14:19:21 -07:00
|
|
|
return UP;
|
|
|
|
case DOWN:
|
|
|
|
case PENDING:
|
|
|
|
return DOWN;
|
|
|
|
}
|
|
|
|
throw new Error("Invalid status");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-09-07 00:42:44 -07:00
|
|
|
* @param {number} num the number of data points which are expected to be returned
|
2024-01-05 04:42:24 -08:00
|
|
|
* @param {"day" | "hour" | "minute"} type the type of data which is expected to be returned
|
2023-09-07 00:42:44 -07:00
|
|
|
* @returns {UptimeDataResult} UptimeDataResult
|
|
|
|
* @throws {Error} The maximum number of minutes greater than 1440
|
2023-08-31 14:19:21 -07:00
|
|
|
*/
|
|
|
|
getData(num, type = "day") {
|
|
|
|
|
2024-01-05 04:42:24 -08: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 14:19:21 -07:00
|
|
|
}
|
2024-06-13 00:29:35 -07:00
|
|
|
if (type === "day" && num > 365) {
|
|
|
|
throw new Error("The maximum number of days is 365");
|
|
|
|
}
|
2024-01-05 04:42:24 -08:00
|
|
|
// Get the current time period key based on the type
|
|
|
|
let key = this.getKey(this.getCurrentDate(), type);
|
|
|
|
|
2023-08-31 14:19:21 -07:00
|
|
|
let total = {
|
|
|
|
up: 0,
|
|
|
|
down: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
let totalPing = 0;
|
|
|
|
let endTimestamp;
|
|
|
|
|
2024-01-05 04:42:24 -08: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 14:19:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sum up all data in the specified time range
|
|
|
|
while (key >= endTimestamp) {
|
|
|
|
let data;
|
|
|
|
|
2024-01-05 04:42:24 -08: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 14:19:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
total.up += data.up;
|
|
|
|
total.down += data.down;
|
|
|
|
totalPing += data.avgPing * data.up;
|
|
|
|
}
|
|
|
|
|
2024-04-16 07:59:07 -07:00
|
|
|
// Set key to the previous time period
|
2024-01-05 04:42:24 -08:00
|
|
|
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 14:19:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let uptimeData = new UptimeDataResult();
|
|
|
|
|
2024-01-05 04:42:24 -08:00
|
|
|
// If there is no data in the previous time ranges, use the last data?
|
2023-08-31 14:19:21 -07:00
|
|
|
if (total.up === 0 && total.down === 0) {
|
2024-01-05 04:42:24 -08: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 14:19:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let avgPing;
|
|
|
|
|
|
|
|
if (total.up === 0) {
|
|
|
|
avgPing = null;
|
|
|
|
} else {
|
|
|
|
avgPing = totalPing / total.up;
|
|
|
|
}
|
|
|
|
|
2024-03-26 19:19:25 -07:00
|
|
|
if (total.up + total.down === 0) {
|
|
|
|
uptimeData.uptime = 0;
|
|
|
|
} else {
|
|
|
|
uptimeData.uptime = total.up / (total.up + total.down);
|
|
|
|
}
|
2023-08-31 14:19:21 -07:00
|
|
|
uptimeData.avgPing = avgPing;
|
|
|
|
return uptimeData;
|
|
|
|
}
|
|
|
|
|
2024-01-05 04:42:24 -08: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);
|
|
|
|
}
|
|
|
|
|
2024-04-16 07:59:07 -07:00
|
|
|
// Set key to the previous time period
|
2024-01-05 04:42:24 -08:00
|
|
|
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 14:19:21 -07:00
|
|
|
/**
|
2024-06-12 03:17:16 -07:00
|
|
|
* Get the uptime data for given duration.
|
2024-06-13 00:29:35 -07:00
|
|
|
* @param {string} duration A string with a number and a unit (m,h,d,w,M,y), such as 24h, 30d, 1y.
|
2023-08-31 14:19:21 -07:00
|
|
|
* @returns {UptimeDataResult} UptimeDataResult
|
2024-06-13 00:29:35 -07:00
|
|
|
* @throws {Error} Invalid duration / Unsupported unit
|
2023-08-31 14:19:21 -07:00
|
|
|
*/
|
|
|
|
getDataByDuration(duration) {
|
2024-06-13 00:29:35 -07:00
|
|
|
const durationNumStr = duration.slice(0, -1);
|
|
|
|
|
2024-06-13 08:39:12 -07:00
|
|
|
if (!/^[0-9]+$/.test(durationNumStr)) {
|
2024-06-13 00:29:35 -07:00
|
|
|
throw new Error(`Invalid duration: ${duration}`);
|
|
|
|
}
|
|
|
|
const num = Number(durationNumStr);
|
|
|
|
const unit = duration.slice(-1);
|
|
|
|
|
|
|
|
switch (unit) {
|
2024-06-13 08:39:12 -07:00
|
|
|
case "m":
|
2024-06-13 00:29:35 -07:00
|
|
|
return this.getData(num, "minute");
|
2024-06-13 08:39:12 -07:00
|
|
|
case "h":
|
2024-06-13 00:29:35 -07:00
|
|
|
return this.getData(num, "hour");
|
2024-06-13 08:39:12 -07:00
|
|
|
case "d":
|
2024-06-13 00:29:35 -07:00
|
|
|
return this.getData(num, "day");
|
2024-06-13 08:39:12 -07:00
|
|
|
case "w":
|
2024-06-13 00:29:35 -07:00
|
|
|
return this.getData(7 * num, "day");
|
2024-06-13 08:39:12 -07:00
|
|
|
case "M":
|
2024-06-13 00:29:35 -07:00
|
|
|
return this.getData(30 * num, "day");
|
2024-06-13 08:39:12 -07:00
|
|
|
case "y":
|
2024-06-13 00:29:35 -07:00
|
|
|
return this.getData(365 * num, "day");
|
|
|
|
default:
|
|
|
|
throw new Error(`Unsupported unit (${unit}) for badge duration ${duration}`
|
|
|
|
);
|
2023-08-31 14:19:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 1440 = 24 * 60mins
|
|
|
|
* @returns {UptimeDataResult} UptimeDataResult
|
|
|
|
*/
|
|
|
|
get24Hour() {
|
|
|
|
return this.getData(1440, "minute");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {UptimeDataResult} UptimeDataResult
|
|
|
|
*/
|
|
|
|
get7Day() {
|
2024-01-05 04:42:24 -08:00
|
|
|
return this.getData(168, "hour");
|
2023-08-31 14:19:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {UptimeDataResult} UptimeDataResult
|
|
|
|
*/
|
|
|
|
get30Day() {
|
|
|
|
return this.getData(30);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {UptimeDataResult} UptimeDataResult
|
|
|
|
*/
|
|
|
|
get1Year() {
|
|
|
|
return this.getData(365);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-01-05 04:42:24 -08:00
|
|
|
* @returns {dayjs.Dayjs} Current datetime in UTC
|
2023-08-31 14:19:21 -07:00
|
|
|
*/
|
|
|
|
getCurrentDate() {
|
|
|
|
return dayjs.utc();
|
|
|
|
}
|
|
|
|
|
2024-10-26 05:50:29 -07:00
|
|
|
/**
|
|
|
|
* For migration purposes.
|
|
|
|
* @param {boolean} value Migration mode on/off
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
setMigrationMode(value) {
|
|
|
|
this.migrationMode = value;
|
|
|
|
}
|
2023-08-31 14:19:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class UptimeDataResult {
|
|
|
|
/**
|
|
|
|
* @type {number} Uptime
|
|
|
|
*/
|
2024-01-05 04:42:24 -08:00
|
|
|
uptime = 0;
|
2023-08-31 14:19:21 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {number} Average ping
|
|
|
|
*/
|
2024-01-05 04:42:24 -08:00
|
|
|
avgPing = null;
|
2023-08-31 14:19:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
UptimeCalculator,
|
|
|
|
UptimeDataResult,
|
|
|
|
};
|