From 5e55215c9c32129835a3107107fae618a46abdca Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Wed, 16 Oct 2024 05:12:06 +0800 Subject: [PATCH] wip --- server/database.js | 2 -- server/uptime-calculator.js | 19 +++++++++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/server/database.js b/server/database.js index 8ff88604..ce4e04e9 100644 --- a/server/database.js +++ b/server/database.js @@ -785,8 +785,6 @@ class Database { let part = 100 / monitors.length; let i = 1; for (let monitor of monitors) { - - // TODO: Get two or three days at the same to speed up??? // Get a list of unique dates from the heartbeat table, using raw sql let dates = await R.getAll(` SELECT DISTINCT DATE(time) AS date diff --git a/server/uptime-calculator.js b/server/uptime-calculator.js index 916f4c4c..52a34fc2 100644 --- a/server/uptime-calculator.js +++ b/server/uptime-calculator.js @@ -60,6 +60,9 @@ class UptimeCalculator { */ migrationMode = false; + statMinutelyKeepHour = 24; + statHourlyKeepDay = 30; + /** * Get the uptime calculator for a monitor * Initializes and returns the monitor if it does not exist @@ -305,8 +308,11 @@ class UptimeCalculator { } await R.store(dailyStatBean); - // TODO: For migration mode, we don't need to store old hourly and minutely data, but we need 24-hour's minutely data and 30-day's hourly data - if (false) { + 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; @@ -321,7 +327,11 @@ class UptimeCalculator { } } 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; @@ -341,15 +351,16 @@ class UptimeCalculator { // 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(date.subtract(24, "hour")), + this.getMinutelyKey(currentDate.subtract(this.statMinutelyKeepHour, "hour")), ]); await R.exec("DELETE FROM stat_hourly WHERE monitor_id = ? AND timestamp < ?", [ this.monitorID, - this.getHourlyKey(date.subtract(30, "day")), + this.getHourlyKey(currentDate.subtract(this.statHourlyKeepDay, "day")), ]); }