From bb21714b45c44ae76a561c68126e31f2a563133a Mon Sep 17 00:00:00 2001 From: fyears <1142836+fyears@users.noreply.github.com> Date: Wed, 6 Apr 2022 00:35:21 +0800 Subject: [PATCH] auto clean sync plans --- src/localdb.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ src/main.ts | 22 ++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/localdb.ts b/src/localdb.ts index 524dffc..a124f9a 100644 --- a/src/localdb.ts +++ b/src/localdb.ts @@ -552,6 +552,48 @@ export const readAllSyncPlanRecordTextsByVault = async ( } }; +/** + * We remove records that are older than 7 days or 10000 records. + * It's a heavy operation, so we shall not place it in the start up. + * @param db + */ +export const clearExpiredSyncPlanRecords = async (db: InternalDBs) => { + const MILLISECONDS_OLD = 1000 * 60 * 60 * 24 * 7; // 7 days + const COUNT_TO_MANY = 10000; + + const currTs = Date.now(); + const expiredTs = currTs - MILLISECONDS_OLD; + + let records = (await db.syncPlansTbl.keys()).map((key) => { + const ts = parseInt(key.split("\t")[1]); + const expired = ts <= expiredTs; + return { + ts: ts, + key: key, + expired: expired, + }; + }); + + const keysToRemove = new Set( + records.filter((x) => x.expired).map((x) => x.key) + ); + + if (records.length - keysToRemove.size > COUNT_TO_MANY) { + // we need to find out records beyond 10000 records + records = records.filter((x) => !x.expired); // shrink the array + records.sort((a, b) => -(a.ts - b.ts)); // descending + records.slice(COUNT_TO_MANY).forEach((element) => { + keysToRemove.add(element.key); + }); + } + + const ps = [] as Promise[]; + keysToRemove.forEach((element) => { + ps.push(db.syncPlansTbl.removeItem(element)); + }); + await Promise.all(ps); +}; + export const readAllLogRecordTextsByVault = async ( db: InternalDBs, vaultRandomID: string diff --git a/src/main.ts b/src/main.ts index 8d858f2..7a3ef50 100644 --- a/src/main.ts +++ b/src/main.ts @@ -26,6 +26,7 @@ import { InternalDBs, insertLoggerOutputByVault, clearExpiredLoggerOutputRecords, + clearExpiredSyncPlanRecords, } from "./localdb"; import { RemoteClient } from "./remote"; import { @@ -419,6 +420,9 @@ export default class RemotelySavePlugin extends Plugin { this.addOutputToDBIfSet(); this.enableAutoClearOutputToDBHistIfSet(); + // must AFTER preparing DB + this.enableAutoClearSyncPlanHist(); + this.syncStatus = "idle"; this.registerEvent( @@ -948,4 +952,22 @@ export default class RemotelySavePlugin extends Plugin { this.registerInterval(intervalID); }); } + + enableAutoClearSyncPlanHist() { + const initClearSyncPlanHistAfterMilliseconds = 1000 * 45; + const autoClearSyncPlanHistAfterMilliseconds = 1000 * 60 * 5; + + this.app.workspace.onLayoutReady(() => { + // init run + window.setTimeout(() => { + clearExpiredSyncPlanRecords(this.db); + }, initClearSyncPlanHistAfterMilliseconds); + + // scheduled run + const intervalID = window.setInterval(() => { + clearExpiredSyncPlanRecords(this.db); + }, autoClearSyncPlanHistAfterMilliseconds); + this.registerInterval(intervalID); + }); + } }