mirror of
https://github.com/louislam/uptime-kuma.git
synced 2024-11-27 16:54:04 +00:00
Feat: Implement data clearing logic & frontend
This commit is contained in:
parent
656a4d6270
commit
dca5a59dbc
5 changed files with 90 additions and 18 deletions
|
@ -5,7 +5,7 @@ const { SHARE_ENV } = require("worker_threads");
|
|||
const jobs = [
|
||||
{
|
||||
name: "clear-old-data",
|
||||
interval: "every 1 minute",
|
||||
interval: "every 1 day",
|
||||
}
|
||||
];
|
||||
|
||||
|
@ -17,6 +17,9 @@ const initBackgroundJobs = function (args) {
|
|||
env: SHARE_ENV,
|
||||
workerData: args,
|
||||
},
|
||||
workerMessageHandler: (message) => {
|
||||
console.log("[Background Job]:", message);
|
||||
}
|
||||
});
|
||||
|
||||
bree.start();
|
||||
|
|
|
@ -1,24 +1,40 @@
|
|||
const path = require("path");
|
||||
const { log, exit, connectDb } = require("./util-worker");
|
||||
const { R } = require("redbean-node");
|
||||
const Database = require("../database");
|
||||
const { setSetting, setting } = require("../util-server");
|
||||
|
||||
const dbPath = path.join(
|
||||
process.env.DATA_DIR ||
|
||||
require("worker_threads").workerData["data-dir"] ||
|
||||
"./data/"
|
||||
);
|
||||
|
||||
Database.init({
|
||||
"data-dir": dbPath,
|
||||
});
|
||||
const DEFAULT_KEEP_PERIOD = 30;
|
||||
|
||||
(async () => {
|
||||
await Database.connect();
|
||||
await connectDb();
|
||||
|
||||
console.log(await R.getAll("PRAGMA journal_mode"));
|
||||
console.log(
|
||||
await R.getAll("SELECT * from setting WHERE key = 'database_version'")
|
||||
let period = await setting("keepDataPeriodDays");
|
||||
|
||||
// Set Default Period
|
||||
if (period == null) {
|
||||
await setSetting("keepDataPeriodDays", DEFAULT_KEEP_PERIOD);
|
||||
period = DEFAULT_KEEP_PERIOD;
|
||||
}
|
||||
|
||||
// Try parse setting
|
||||
let parsedPeriod;
|
||||
try {
|
||||
parsedPeriod = parseInt(period);
|
||||
} catch (_) {
|
||||
log("Failed to parse setting, resetting to default..");
|
||||
await setSetting("keepDataPeriodDays", DEFAULT_KEEP_PERIOD);
|
||||
parsedPeriod = DEFAULT_KEEP_PERIOD;
|
||||
}
|
||||
|
||||
log(`Clearing Data older than ${parsedPeriod} days...`);
|
||||
|
||||
try {
|
||||
await R.exec(
|
||||
"DELETE FROM heartbeat WHERE time < DATETIME('now', '-' || ? || ' days') ",
|
||||
[parsedPeriod]
|
||||
);
|
||||
} catch (e) {
|
||||
log(`Failed to clear old data: ${e.message}`);
|
||||
}
|
||||
|
||||
process.exit(0);
|
||||
exit();
|
||||
})();
|
||||
|
|
39
server/jobs/util-worker.js
Normal file
39
server/jobs/util-worker.js
Normal file
|
@ -0,0 +1,39 @@
|
|||
const { parentPort, workerData } = require("worker_threads");
|
||||
const Database = require("../database");
|
||||
const path = require("path");
|
||||
|
||||
const log = function (any) {
|
||||
if (parentPort) {
|
||||
parentPort.postMessage(any);
|
||||
}
|
||||
};
|
||||
|
||||
const exit = function (error) {
|
||||
if (error && error != 0) {
|
||||
process.exit(error);
|
||||
} else {
|
||||
if (parentPort) {
|
||||
parentPort.postMessage("done");
|
||||
} else {
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const connectDb = async function () {
|
||||
const dbPath = path.join(
|
||||
process.env.DATA_DIR || workerData["data-dir"] || "./data/"
|
||||
);
|
||||
|
||||
Database.init({
|
||||
"data-dir": dbPath,
|
||||
});
|
||||
|
||||
await Database.connect();
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
log,
|
||||
exit,
|
||||
connectDb,
|
||||
};
|
|
@ -281,4 +281,6 @@ export default {
|
|||
promosmsPhoneNumber: "Phone number (for Polish recipient You can skip area codes)",
|
||||
promosmsSMSSender: "SMS Sender Name : Pre-registred name or one of defaults: InfoSMS, SMS Info, MaxSMS, INFO, SMS",
|
||||
// End notification form
|
||||
"Monitor History": "Monitor History",
|
||||
clearDataOlderThan: "Remove monitor data older than {0} days",
|
||||
};
|
||||
|
|
|
@ -212,6 +212,14 @@
|
|||
{{ importAlert }}
|
||||
</div>
|
||||
|
||||
<div class="mt-3 mb-3">
|
||||
<h2 class="mb-2">{{ $t("Monitor History") }}</h2>
|
||||
<div class="my-3">
|
||||
<label for="keepDataPeriodDays" class="form-label">{{ $t("clearDataOlderThan", [ settings.keepDataPeriodDays ]) }}</label>
|
||||
<input id="keepDataPeriodDays" v-model="settings.keepDataPeriodDays" type="number" class="form-control" required min="1" step="1">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2 class="mt-5 mb-2">{{ $t("Advanced") }}</h2>
|
||||
|
||||
<div class="mb-3">
|
||||
|
@ -465,6 +473,10 @@ export default {
|
|||
this.settings.entryPage = "dashboard";
|
||||
}
|
||||
|
||||
if (this.settings.keepDataPeriodDays === undefined) {
|
||||
this.settings.keepDataPeriodDays = 30;
|
||||
}
|
||||
|
||||
this.loaded = true;
|
||||
});
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue