Feat: Implement data clearing logic & frontend

This commit is contained in:
Nelson Chan 2021-10-09 23:33:47 +08:00
parent 656a4d6270
commit dca5a59dbc
5 changed files with 90 additions and 18 deletions

View file

@ -5,7 +5,7 @@ const { SHARE_ENV } = require("worker_threads");
const jobs = [ const jobs = [
{ {
name: "clear-old-data", name: "clear-old-data",
interval: "every 1 minute", interval: "every 1 day",
} }
]; ];
@ -17,6 +17,9 @@ const initBackgroundJobs = function (args) {
env: SHARE_ENV, env: SHARE_ENV,
workerData: args, workerData: args,
}, },
workerMessageHandler: (message) => {
console.log("[Background Job]:", message);
}
}); });
bree.start(); bree.start();

View file

@ -1,24 +1,40 @@
const path = require("path"); const { log, exit, connectDb } = require("./util-worker");
const { R } = require("redbean-node"); const { R } = require("redbean-node");
const Database = require("../database"); const { setSetting, setting } = require("../util-server");
const dbPath = path.join( const DEFAULT_KEEP_PERIOD = 30;
process.env.DATA_DIR ||
require("worker_threads").workerData["data-dir"] ||
"./data/"
);
Database.init({
"data-dir": dbPath,
});
(async () => { (async () => {
await Database.connect(); await connectDb();
console.log(await R.getAll("PRAGMA journal_mode")); let period = await setting("keepDataPeriodDays");
console.log(
await R.getAll("SELECT * from setting WHERE key = 'database_version'")
);
process.exit(0); // 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}`);
}
exit();
})(); })();

View 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,
};

View file

@ -281,4 +281,6 @@ export default {
promosmsPhoneNumber: "Phone number (for Polish recipient You can skip area codes)", 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", promosmsSMSSender: "SMS Sender Name : Pre-registred name or one of defaults: InfoSMS, SMS Info, MaxSMS, INFO, SMS",
// End notification form // End notification form
"Monitor History": "Monitor History",
clearDataOlderThan: "Remove monitor data older than {0} days",
}; };

View file

@ -212,6 +212,14 @@
{{ importAlert }} {{ importAlert }}
</div> </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> <h2 class="mt-5 mb-2">{{ $t("Advanced") }}</h2>
<div class="mb-3"> <div class="mb-3">
@ -465,6 +473,10 @@ export default {
this.settings.entryPage = "dashboard"; this.settings.entryPage = "dashboard";
} }
if (this.settings.keepDataPeriodDays === undefined) {
this.settings.keepDataPeriodDays = 30;
}
this.loaded = true; this.loaded = true;
}); });
}, },