mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-10-04 22:49:21 +08:00
Clean up data
This commit is contained in:
@@ -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;
|
||||
|
Reference in New Issue
Block a user