From b22fe5451152c8b5c740a3f2d2a6034b06c9b65f Mon Sep 17 00:00:00 2001 From: youngcw Date: Tue, 10 Oct 2023 14:39:37 -0700 Subject: [PATCH] Goal speedup2 (#1720) * only use needed priorities * cleanup * cleanup * cleanup * maybe better? * note * note fix * suggestion Co-authored-by: Joel Jeremy Marquez --------- Co-authored-by: Joel Jeremy Marquez --- .../src/server/budget/goaltemplates.ts | 29 ++++++++++++------- upcoming-release-notes/1720.md | 6 ++++ 2 files changed, 24 insertions(+), 11 deletions(-) create mode 100644 upcoming-release-notes/1720.md diff --git a/packages/loot-core/src/server/budget/goaltemplates.ts b/packages/loot-core/src/server/budget/goaltemplates.ts index c23d3f7eb11..9f3b415450b 100644 --- a/packages/loot-core/src/server/budget/goaltemplates.ts +++ b/packages/loot-core/src/server/budget/goaltemplates.ts @@ -63,9 +63,9 @@ async function setGoalBudget({ month, templateBudget }) { async function processTemplate(month, force, category_templates) { let num_applied = 0; let errors = []; - let lowestPriority = 0; let originalCategoryBalance = []; let setToZero = []; + let priority_list = []; let categories = await db.all( 'SELECT * FROM v_categories WHERE tombstone = 0 AND hidden = 0', @@ -81,10 +81,11 @@ async function processTemplate(month, force, category_templates) { let template = category_templates[category.id]; if (template) { for (let l = 0; l < template.length; l++) { - lowestPriority = - template[l].priority > lowestPriority - ? template[l].priority - : lowestPriority; + //add each priority we need to a list. Will sort later + if (template[l].priority == null) { + continue; + } + priority_list.push(template[l].priority); } } if (budgeted) { @@ -107,10 +108,15 @@ async function processTemplate(month, force, category_templates) { templateBudget: setToZero.filter(f => f.isTemplate === true), }); - // find all remainder templates, place them after all other templates + // sort and filter down to just the requested priorities + priority_list = priority_list + .sort() + .filter((item, index, curr) => curr.indexOf(item) === index); + + // find all remainder templates, place them at highest priority let remainder_found; - let remainder_priority = lowestPriority + 1; let remainder_weight_total = 0; + let remainder_priority = priority_list[priority_list.length - 1] + 1; for (let c = 0; c < categories.length; c++) { let category = categories[c]; let templates = category_templates[category.id]; @@ -124,19 +130,20 @@ async function processTemplate(month, force, category_templates) { } } } - // so the remainders don't get skipped - if (remainder_found) lowestPriority = remainder_priority; + if (remainder_found) priority_list.push(remainder_priority); let sheetName = monthUtils.sheetForMonth(month); let available_start = await getSheetValue(sheetName, `to-budget`); let available_remaining = isReflectBudget() ? await getSheetValue(sheetName, `total-saved`) : await getSheetValue(sheetName, `to-budget`); - for (let priority = 0; priority <= lowestPriority; priority++) { + for (let ii = 0; ii < priority_list.length; ii++) { + let priority = priority_list[ii]; let templateBudget = []; + // setup scaling for remainder let remainder_scale = 1; - if (priority === lowestPriority) { + if (priority === remainder_priority && remainder_found) { let available_now = await getSheetValue(sheetName, `to-budget`); remainder_scale = available_now / remainder_weight_total; } diff --git a/upcoming-release-notes/1720.md b/upcoming-release-notes/1720.md new file mode 100644 index 00000000000..0d72060aa8e --- /dev/null +++ b/upcoming-release-notes/1720.md @@ -0,0 +1,6 @@ +--- +category: Enhancements +authors: [youngcw] +--- + +Goals: speedup by only run the requested priority levels, skip others