diff --git a/server/server.js b/server/server.js index f9366c4ab..5105100fc 100644 --- a/server/server.js +++ b/server/server.js @@ -109,6 +109,7 @@ const { login } = require("./auth"); const passwordHash = require("./password-hash"); const { Prometheus } = require("./prometheus"); +const { UptimeCalculator } = require("./uptime-calculator"); const hostname = config.hostname; @@ -1592,9 +1593,11 @@ let needSetup = false; log.info("manage", `Clear Heartbeats Monitor: ${monitorID} User ID: ${socket.userID}`); - await R.exec("DELETE FROM heartbeat WHERE monitor_id = ?", [ - monitorID - ]); + await UptimeCalculator.clearStatistics(monitorID); + + if (monitorID in server.monitorList) { + await restartMonitor(socket.userID, monitorID); + } await sendHeartbeatList(socket, monitorID, true, true); @@ -1616,10 +1619,7 @@ let needSetup = false; log.info("manage", `Clear Statistics User ID: ${socket.userID}`); - await R.exec("DELETE FROM heartbeat"); - await R.exec("DELETE FROM stat_daily"); - await R.exec("DELETE FROM stat_hourly"); - await R.exec("DELETE FROM stat_minutely"); + await UptimeCalculator.clearAllStatistics(); // Restart all monitors to reset the stats for (let monitorID in server.monitorList) { diff --git a/server/uptime-calculator.js b/server/uptime-calculator.js index 48f7d80ae..7796791a3 100644 --- a/server/uptime-calculator.js +++ b/server/uptime-calculator.js @@ -90,6 +90,14 @@ class UptimeCalculator { delete UptimeCalculator.list[monitorID]; } + /** + * Remove all monitors from the list + * @returns {Promise} + */ + static async removeAll() { + UptimeCalculator.list = {}; + } + /** * */ @@ -845,6 +853,42 @@ class UptimeCalculator { setMigrationMode(value) { this.migrationMode = value; } + + /** + * Clear all statistics and heartbeats for a monitor + * @param {number} monitorID the id of the monitor + * @returns {Promise} + */ + static async clearStatistics(monitorID) { + await R.exec("DELETE FROM heartbeat WHERE monitor_id = ?", [ + monitorID + ]); + + await R.exec("DELETE FROM stat_minutely WHERE monitor_id = ?", [ + monitorID + ]); + await R.exec("DELETE FROM stat_hourly WHERE monitor_id = ?", [ + monitorID + ]); + await R.exec("DELETE FROM stat_daily WHERE monitor_id = ?", [ + monitorID + ]); + + await UptimeCalculator.remove(monitorID); + } + + /** + * Clear all statistics and heartbeats for all monitors + * @returns {Promise} + */ + static async clearAllStatistics() { + await R.exec("DELETE FROM heartbeat"); + await R.exec("DELETE FROM stat_minutely"); + await R.exec("DELETE FROM stat_hourly"); + await R.exec("DELETE FROM stat_daily"); + + await UptimeCalculator.removeAll(); + } } class UptimeDataResult {