This commit is contained in:
Louis Lam
2024-10-09 07:19:05 +08:00
parent 344fd52501
commit d7c3c40d74
2 changed files with 75 additions and 47 deletions

View File

@@ -12,7 +12,6 @@ class UptimeCalculator {
* @private
* @type {{string:UptimeCalculator}}
*/
static list = {};
/**
@@ -55,6 +54,12 @@ class UptimeCalculator {
lastHourlyStatBean = null;
lastMinutelyStatBean = null;
/**
* For migration purposes.
* @type {boolean}
*/
migrationMode = false;
/**
* Get the uptime calculator for a monitor
* Initializes and returns the monitor if it does not exist
@@ -194,6 +199,10 @@ class UptimeCalculator {
* @throws {Error} Invalid status
*/
async update(status, ping = 0, date) {
if (!this.monitorID) {
throw new Error("Monitor ID is required");
}
if (!date) {
date = this.getCurrentDate();
}
@@ -330,17 +339,19 @@ class UptimeCalculator {
}
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")),
]);
if (!this.migrationMode) {
// 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")),
]);
await R.exec("DELETE FROM stat_hourly WHERE monitor_id = ? AND timestamp < ?", [
this.monitorID,
this.getHourlyKey(date.subtract(30, "day")),
]);
await R.exec("DELETE FROM stat_hourly WHERE monitor_id = ? AND timestamp < ?", [
this.monitorID,
this.getHourlyKey(date.subtract(30, "day")),
]);
}
return date;
}
@@ -815,6 +826,14 @@ class UptimeCalculator {
return dayjs.utc();
}
/**
* For migration purposes.
* @param {boolean} value Migration mode on/off
* @returns {void}
*/
setMigrationMode(value) {
this.migrationMode = value;
}
}
class UptimeDataResult {