Compare commits

..

1 commit

Author SHA1 Message Date
Louis Lam
8404a9144b
Merge 5e55215c9c into 79a26180af 2024-10-23 04:47:16 +00:00
3 changed files with 32 additions and 55 deletions

View file

@ -829,7 +829,8 @@ class Database {
i++; i++;
} }
await Database.clearHeartbeatData(true); // TODO: Remove all non-important heartbeats from heartbeat table
log.info("db", "[DON'T STOP] Deleting all data from heartbeat table");
await Settings.set("migrateAggregateTableState", "migrated"); await Settings.set("migrateAggregateTableState", "migrated");
@ -840,40 +841,6 @@ 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 Log detailed information
* @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; module.exports = Database;

View file

@ -1,18 +1,35 @@
const { R } = require("redbean-node"); const { R } = require("redbean-node");
const { log } = require("../../src/util"); const { log } = require("../../src/util");
const { setSetting, setting } = require("../util-server");
const Database = require("../database"); const Database = require("../database");
const { Settings } = require("../settings"); const { Settings } = require("../settings");
const dayjs = require("dayjs");
const DEFAULT_KEEP_PERIOD = 365; const DEFAULT_KEEP_PERIOD = 180;
/** /**
* Clears old data from the heartbeat table and the stat_daily of the database. * Clears old data from the heartbeat table of the database.
* @returns {Promise<void>} A promise that resolves when the data has been cleared. * @returns {Promise<void>} A promise that resolves when the data has been cleared.
*/ */
const clearOldData = async () => { const clearOldData = async () => {
await Database.clearHeartbeatData();
let period = await Settings.get("keepDataPeriodDays"); // TODO: Temporary disable for testing
return;
/*
* TODO:
* Since we have aggregated table now, we don't need so much data in heartbeat table.
* But we still need to keep the important rows, because they contain the message.
*
* In the heartbeat table:
* - important rows: keep according to the setting (keepDataPeriodDays) (default 180 days)
* - not important rows: keep 2 days
*
* stat_* tables:
* - keep according to the setting (keepDataPeriodDays) (default 180 days)
*/
let period = await setting("keepDataPeriodDays");
// Set Default Period // Set Default Period
if (period == null) { if (period == null) {
@ -26,28 +43,23 @@ const clearOldData = async () => {
parsedPeriod = parseInt(period); parsedPeriod = parseInt(period);
} catch (_) { } catch (_) {
log.warn("clearOldData", "Failed to parse setting, resetting to default.."); log.warn("clearOldData", "Failed to parse setting, resetting to default..");
await Settings.set("keepDataPeriodDays", DEFAULT_KEEP_PERIOD, "general"); await setSetting("keepDataPeriodDays", DEFAULT_KEEP_PERIOD, "general");
parsedPeriod = DEFAULT_KEEP_PERIOD; parsedPeriod = DEFAULT_KEEP_PERIOD;
} }
if (parsedPeriod < 1) { if (parsedPeriod < 1) {
log.info("clearOldData", `Data deletion has been disabled as period is less than 1. Period is ${parsedPeriod} days.`); log.info("clearOldData", `Data deletion has been disabled as period is less than 1. Period is ${parsedPeriod} days.`);
} else { } else {
log.debug("clearOldData", `Clearing Data older than ${parsedPeriod} days...`); log.debug("clearOldData", `Clearing Data older than ${parsedPeriod} days...`);
const sqlHourOffset = Database.sqlHourOffset(); const sqlHourOffset = Database.sqlHourOffset();
try { try {
// Heartbeat await R.exec(
await R.exec("DELETE FROM heartbeat WHERE time < " + sqlHourOffset, [ "DELETE FROM heartbeat WHERE time < " + sqlHourOffset,
parsedPeriod * -24, [ parsedPeriod * -24 ]
]); );
let timestamp = dayjs().subtract(parsedPeriod, "day").utc().startOf("day").unix();
// stat_daily
await R.exec("DELETE FROM stat_daily WHERE timestamp < ? ", [
timestamp,
]);
if (Database.dbConfig.type === "sqlite") { if (Database.dbConfig.type === "sqlite") {
await R.exec("PRAGMA optimize;"); await R.exec("PRAGMA optimize;");
@ -56,8 +68,6 @@ const clearOldData = async () => {
log.error("clearOldData", `Failed to clear old data: ${e.message}`); log.error("clearOldData", `Failed to clear old data: ${e.message}`);
} }
} }
log.debug("clearOldData", "Data cleared.");
}; };
module.exports = { module.exports = {

View file

@ -209,7 +209,7 @@ class UptimeCalculator {
let flatStatus = this.flatStatus(status); let flatStatus = this.flatStatus(status);
if (flatStatus === DOWN && ping > 0) { if (flatStatus === DOWN && ping > 0) {
log.debug("uptime-calc", "The ping is not effective when the status is DOWN"); log.warn("uptime-calc", "The ping is not effective when the status is DOWN");
} }
let divisionKey = this.getMinutelyKey(date); let divisionKey = this.getMinutelyKey(date);