uptime-kuma/server/jobs/clear-old-data.js

73 lines
2.2 KiB
JavaScript
Raw Normal View History

const { R } = require("redbean-node");
2023-05-12 09:55:48 -07:00
const { log } = require("../../src/util");
const { setSetting, setting } = require("../util-server");
2023-02-12 00:59:07 -08:00
const Database = require("../database");
2024-10-08 22:15:29 -07:00
const Settings = require("../models/settings");
2021-10-12 08:28:21 -07:00
const DEFAULT_KEEP_PERIOD = 180;
2023-05-12 09:55:48 -07:00
/**
* Clears old data from the heartbeat table of the database.
* @returns {Promise<void>} A promise that resolves when the data has been cleared.
2023-05-12 09:55:48 -07:00
*/
2023-05-12 09:55:48 -07:00
const clearOldData = async () => {
2024-09-01 02:19:18 -07:00
/*
* 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
2024-09-22 01:01:45 -07:00
*
* stat_* tables:
* - keep according to the setting (keepDataPeriodDays) (default 180 days)
2024-09-01 02:19:18 -07:00
*/
let period = await setting("keepDataPeriodDays");
// Set Default Period
if (period == null) {
await Settings.set("keepDataPeriodDays", DEFAULT_KEEP_PERIOD, "general");
period = DEFAULT_KEEP_PERIOD;
}
// Try parse setting
let parsedPeriod;
try {
parsedPeriod = parseInt(period);
} catch (_) {
2023-05-12 09:55:48 -07:00
log.warn("clearOldData", "Failed to parse setting, resetting to default..");
await setSetting("keepDataPeriodDays", DEFAULT_KEEP_PERIOD, "general");
parsedPeriod = DEFAULT_KEEP_PERIOD;
}
if (parsedPeriod < 1) {
2023-05-12 09:55:48 -07:00
log.info("clearOldData", `Data deletion has been disabled as period is less than 1. Period is ${parsedPeriod} days.`);
} else {
2023-05-12 09:55:48 -07:00
log.debug("clearOldData", `Clearing Data older than ${parsedPeriod} days...`);
2023-02-12 00:59:07 -08:00
const sqlHourOffset = Database.sqlHourOffset();
try {
await R.exec(
2023-02-12 00:59:07 -08:00
"DELETE FROM heartbeat WHERE time < " + sqlHourOffset,
[ parsedPeriod * -24 ]
);
if (Database.dbConfig.type === "sqlite") {
await R.exec("PRAGMA optimize;");
}
} catch (e) {
2023-05-12 09:55:48 -07:00
log.error("clearOldData", `Failed to clear old data: ${e.message}`);
}
}
2023-05-12 09:55:48 -07:00
};
2023-05-12 09:55:48 -07:00
module.exports = {
clearOldData,
};