2021-09-27 08:40:38 -07:00
|
|
|
const { R } = require("redbean-node");
|
2023-05-12 09:55:48 -07:00
|
|
|
const { log } = require("../../src/util");
|
2023-02-12 00:59:07 -08:00
|
|
|
const Database = require("../database");
|
2024-10-08 22:16:28 -07:00
|
|
|
const { Settings } = require("../settings");
|
2024-10-26 00:37:50 -07:00
|
|
|
const dayjs = require("dayjs");
|
2021-09-27 08:40:38 -07:00
|
|
|
|
2024-10-26 00:37:50 -07:00
|
|
|
const DEFAULT_KEEP_PERIOD = 365;
|
2021-09-27 08:40:38 -07:00
|
|
|
|
2023-05-12 09:55:48 -07:00
|
|
|
/**
|
2024-10-26 00:37:50 -07:00
|
|
|
* Clears old data from the heartbeat table and the stat_daily of the database.
|
2023-08-11 00:46:41 -07:00
|
|
|
* @returns {Promise<void>} A promise that resolves when the data has been cleared.
|
2023-05-12 09:55:48 -07:00
|
|
|
*/
|
|
|
|
const clearOldData = async () => {
|
2024-10-26 00:37:50 -07:00
|
|
|
await Database.clearHeartbeatData();
|
|
|
|
let period = await Settings.get("keepDataPeriodDays");
|
2021-10-09 08:33:47 -07:00
|
|
|
|
|
|
|
// Set Default Period
|
|
|
|
if (period == null) {
|
2024-09-24 02:51:21 -07:00
|
|
|
await Settings.set("keepDataPeriodDays", DEFAULT_KEEP_PERIOD, "general");
|
2021-10-09 08:33:47 -07:00
|
|
|
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..");
|
2024-10-26 00:42:12 -07:00
|
|
|
await Settings.set("keepDataPeriodDays", DEFAULT_KEEP_PERIOD, "general");
|
2021-10-09 08:33:47 -07:00
|
|
|
parsedPeriod = DEFAULT_KEEP_PERIOD;
|
|
|
|
}
|
|
|
|
|
2023-01-08 11:09:06 -08:00
|
|
|
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.`);
|
2023-01-08 11:09:06 -08:00
|
|
|
} 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();
|
|
|
|
|
2023-01-08 11:09:06 -08:00
|
|
|
try {
|
2024-10-26 00:37:50 -07:00
|
|
|
// Heartbeat
|
|
|
|
await R.exec("DELETE FROM heartbeat WHERE time < " + sqlHourOffset, [
|
|
|
|
parsedPeriod * -24,
|
|
|
|
]);
|
|
|
|
|
|
|
|
let timestamp = dayjs().subtract(parsedPeriod, "day").utc().startOf("day").unix();
|
|
|
|
|
|
|
|
// stat_daily
|
|
|
|
await R.exec("DELETE FROM stat_daily WHERE timestamp < ? ", [
|
|
|
|
timestamp,
|
|
|
|
]);
|
2023-07-07 13:57:53 -07:00
|
|
|
|
2023-08-31 14:19:21 -07:00
|
|
|
if (Database.dbConfig.type === "sqlite") {
|
|
|
|
await R.exec("PRAGMA optimize;");
|
|
|
|
}
|
2023-01-08 11:09:06 -08:00
|
|
|
} catch (e) {
|
2023-05-12 09:55:48 -07:00
|
|
|
log.error("clearOldData", `Failed to clear old data: ${e.message}`);
|
2023-01-08 11:09:06 -08:00
|
|
|
}
|
2021-10-09 08:33:47 -07:00
|
|
|
}
|
2024-10-26 00:37:50 -07:00
|
|
|
|
|
|
|
log.debug("clearOldData", "Data cleared.");
|
2023-05-12 09:55:48 -07:00
|
|
|
};
|
2021-09-27 08:40:38 -07:00
|
|
|
|
2023-05-12 09:55:48 -07:00
|
|
|
module.exports = {
|
|
|
|
clearOldData,
|
|
|
|
};
|