|
|
@ -1,35 +1,19 @@
|
|
|
|
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 { setSetting } = 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 = 180;
|
|
|
|
const DEFAULT_KEEP_PERIOD = 365;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Clears old data from the heartbeat table of the database.
|
|
|
|
* Clears old data from the heartbeat table and the stat_daily 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();
|
|
|
|
// TODO: Temporary disable for testing
|
|
|
|
let period = await Settings.get("keepDataPeriodDays");
|
|
|
|
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) {
|
|
|
@ -50,16 +34,21 @@ const clearOldData = async () => {
|
|
|
|
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 {
|
|
|
|
await R.exec(
|
|
|
|
// Heartbeat
|
|
|
|
"DELETE FROM heartbeat WHERE time < " + sqlHourOffset,
|
|
|
|
await R.exec("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;");
|
|
|
@ -68,6 +57,8 @@ 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 = {
|
|
|
|