From 24bffacaeb4775fb4cab29533fee8359c29ca5d1 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Mon, 7 Oct 2024 20:53:32 +1300 Subject: [PATCH] GC functional, config values now actually used --- client/homebrew/utils/versionHistory.js | 40 +++++++++++++++---------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/client/homebrew/utils/versionHistory.js b/client/homebrew/utils/versionHistory.js index b72ab6db6..9dfeab8fd 100644 --- a/client/homebrew/utils/versionHistory.js +++ b/client/homebrew/utils/versionHistory.js @@ -15,9 +15,8 @@ const DEFAULT_HISTORY_SAVE_DELAYS = { const DEFAULT_GARBAGE_COLLECT_DELAY = 28 * 24 * 60; -const HISTORY_SAVE_DELAYS = global.config?.historyData?.HISTORY_SAVE_DELAYS ?? DEFAULT_HISTORY_SAVE_DELAYS; -const GARBAGE_COLLECT_DELAY = global.config?.historyData?.GARBAGE_COLLECT_DELAY ?? DEFAULT_GARBAGE_COLLECT_DELAY; - +let HISTORY_SAVE_DELAYS = DEFAULT_HISTORY_SAVE_DELAYS; +let GARBAGE_COLLECT_DELAY = DEFAULT_GARBAGE_COLLECT_DELAY; function getKeyBySlot(brew, slot){ @@ -48,6 +47,10 @@ function parseBrewForStorage(brew, slot = 0) { expireAt : new Date() }; + if(global.config?.history?.HISTORY_SAVE_DELAYS){ + HISTORY_SAVE_DELAYS = global.config?.history?.HISTORY_SAVE_DELAYS; + } + archiveBrew.expireAt.setMinutes(archiveBrew.expireAt.getMinutes() + HISTORY_SAVE_DELAYS[slot]); const key = getKeyBySlot(brew, slot); @@ -128,17 +131,22 @@ export async function updateHistory(brew) { }; }; -export function versionHistoryGarbageCollection(){ - console.log('Version History Garbage Collection'); - // Object.keys(IDB) - // .filter((key)=>{ - // return key.startsWith(HISTORY_PREFIX); - // }) - // .forEach((key)=>{ - // const collectAt = new Date(JSON.parse(IDB.get(key)).savedAt); - // collectAt.setMinutes(collectAt.getMinutes() + GARBAGE_COLLECT_DELAY); - // if(new Date() > collectAt){ - // IDB.removeItem(key); - // } - // }); +export async function versionHistoryGarbageCollection(){ + if(global.config?.history?.GARBAGE_COLLECT_DELAY != GARBAGE_COLLECT_DELAY) GARBAGE_COLLECT_DELAY = global.config?.history?.GARBAGE_COLLECT_DELAY; + + await IDB.entries() + .then((entries)=>{ + entries.forEach((entry)=>{ + const key = entry[0]; + const value = entry[1]; + + if(key.startsWith(`${HISTORY_PREFIX}`)) { + const collectAt = new Date(value.savedAt); + collectAt.setMinutes(collectAt.getMinutes() + GARBAGE_COLLECT_DELAY); + if(new Date() > collectAt){ + IDB.del(key); + }; + } + }); + }); }; \ No newline at end of file