Clean up data

This commit is contained in:
Louis Lam
2024-10-26 15:37:50 +08:00
parent e91b2efe9a
commit 65b49384e0
3 changed files with 55 additions and 31 deletions

View File

@@ -829,8 +829,7 @@ class Database {
i++;
}
// TODO: Remove all non-important heartbeats from heartbeat table
log.info("db", "[DON'T STOP] Deleting all data from heartbeat table");
await Database.clearHeartbeatData(true);
await Settings.set("migrateAggregateTableState", "migrated");
@@ -841,6 +840,40 @@ class Database {
}
}
/**
* Remove all non-important heartbeats from heartbeat table, keep last 24-hour or {KEEP_LAST_ROWS} rows for each monitor
* @param {boolean} detailedLog
* @returns {Promise<void>}
*/
static async clearHeartbeatData(detailedLog = false) {
let monitors = await R.getAll("SELECT id FROM monitor");
const sqlHourOffset = Database.sqlHourOffset();
for (let monitor of monitors) {
if (detailedLog) {
log.info("db", "Deleting non-important heartbeats for monitor " + monitor.id);
}
await R.exec(`
DELETE FROM heartbeat
WHERE monitor_id = ?
AND important = 0
AND time < ${sqlHourOffset}
AND id NOT IN (
SELECT id
FROM heartbeat
WHERE monitor_id = ?
ORDER BY time DESC
LIMIT ?
)
`, [
monitor.id,
-24,
monitor.id,
100,
]);
}
}
}
module.exports = Database;