From dca5a59dbc17715ddc98ffb0f7430d773f19bd05 Mon Sep 17 00:00:00 2001 From: Nelson Chan Date: Sat, 9 Oct 2021 23:33:47 +0800 Subject: [PATCH] Feat: Implement data clearing logic & frontend --- server/jobs.js | 5 +++- server/jobs/clear-old-data.js | 50 +++++++++++++++++++++++------------ server/jobs/util-worker.js | 39 +++++++++++++++++++++++++++ src/languages/en.js | 2 ++ src/pages/Settings.vue | 12 +++++++++ 5 files changed, 90 insertions(+), 18 deletions(-) create mode 100644 server/jobs/util-worker.js diff --git a/server/jobs.js b/server/jobs.js index 18460a1a..d8435ba3 100644 --- a/server/jobs.js +++ b/server/jobs.js @@ -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(); diff --git a/server/jobs/clear-old-data.js b/server/jobs/clear-old-data.js index 5b109da0..2b1ed47c 100644 --- a/server/jobs/clear-old-data.js +++ b/server/jobs/clear-old-data.js @@ -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(); + + 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...`); - console.log(await R.getAll("PRAGMA journal_mode")); - console.log( - await R.getAll("SELECT * from setting WHERE key = 'database_version'") - ); + 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(); })(); diff --git a/server/jobs/util-worker.js b/server/jobs/util-worker.js new file mode 100644 index 00000000..9426840d --- /dev/null +++ b/server/jobs/util-worker.js @@ -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, +}; diff --git a/src/languages/en.js b/src/languages/en.js index 2ce8f46b..31eaee0e 100644 --- a/src/languages/en.js +++ b/src/languages/en.js @@ -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", }; diff --git a/src/pages/Settings.vue b/src/pages/Settings.vue index 259f334b..b5f0f7a7 100644 --- a/src/pages/Settings.vue +++ b/src/pages/Settings.vue @@ -212,6 +212,14 @@ {{ importAlert }} +
+

{{ $t("Monitor History") }}

+
+ + +
+
+

{{ $t("Advanced") }}

@@ -465,6 +473,10 @@ export default { this.settings.entryPage = "dashboard"; } + if (this.settings.keepDataPeriodDays === undefined) { + this.settings.keepDataPeriodDays = 30; + } + this.loaded = true; }); },