From 7e36ea2bbd7f4628673eefa577f0a740e278c7ee Mon Sep 17 00:00:00 2001 From: shall0pass <20625555+shall0pass@users.noreply.github.com> Date: Sat, 15 Jul 2023 18:03:34 -0500 Subject: [PATCH 1/4] speed improvements --- .../src/server/budget/goaltemplates.ts | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/packages/loot-core/src/server/budget/goaltemplates.ts b/packages/loot-core/src/server/budget/goaltemplates.ts index e17effde803..514ea3cf17f 100644 --- a/packages/loot-core/src/server/budget/goaltemplates.ts +++ b/packages/loot-core/src/server/budget/goaltemplates.ts @@ -6,6 +6,7 @@ import { import { amountToInteger, integerToAmount } from '../../shared/util'; import * as db from '../db'; import { getRuleForSchedule, getNextDate } from '../schedules/app'; +import { batchMessages } from '../sync'; import { setBudget, setZero, getSheetValue } from './actions'; import { parse } from './goal-template.pegjs'; @@ -34,7 +35,20 @@ function checkScheduleTemplates(template) { return { lowPriority, errorNotice }; } +async function setGoalBudget({ month, templateBudget }) { + await batchMessages(async () => { + templateBudget.forEach(element => { + setBudget({ + category: element.category, + month, + amount: element.amount, + }); + }); + }); +} + async function processTemplate(month, force) { + let templateBudget = []; let num_applied = 0; let errors = []; let category_templates = await getCategoryTemplates(); @@ -88,6 +102,7 @@ async function processTemplate(month, force) { let sheetName = monthUtils.sheetForMonth(month); let available_start = await getSheetValue(sheetName, `to-budget`); + let available_remaining = await getSheetValue(sheetName, `to-budget`); for (let priority = 0; priority <= lowestPriority; priority++) { // setup scaling for remainder let remainder_scale = 1; @@ -148,15 +163,18 @@ async function processTemplate(month, force) { priority, remainder_scale, available_start, + available_remaining, force, ); if (to_budget != null) { num_applied++; - await setBudget({ - category: category.id, - month, - amount: to_budget, - }); + templateBudget.push( + { + category: category.id, + amount: to_budget, + } + ) + available_remaining -= to_budget; } if (applyErrors != null) { errors = errors.concat( @@ -167,7 +185,9 @@ async function processTemplate(month, force) { } } } + setGoalBudget({ month, templateBudget }); } + if (!force) { //if overwrite is not preferred, set cell to original value for (let l = 0; l < originalCategoryBalance.length; l++) { @@ -251,6 +271,7 @@ async function applyCategoryTemplate( priority, remainder_scale, available_start, + budgetAvailable, force, ) { let current_month = `${month}-01`; @@ -325,7 +346,6 @@ async function applyCategoryTemplate( let budgeted = await getSheetValue(sheetName, `budget-${category.id}`); let spent = await getSheetValue(sheetName, `sum-amount-${category.id}`); let balance = await getSheetValue(sheetName, `leftover-${category.id}`); - let budgetAvailable = await getSheetValue(sheetName, `to-budget`); let to_budget = budgeted; let limit; let hold; From a0e63b8cbd6f35dddd13464cbd42a6a924629b05 Mon Sep 17 00:00:00 2001 From: shall0pass <20625555+shall0pass@users.noreply.github.com> Date: Sat, 15 Jul 2023 18:09:48 -0500 Subject: [PATCH 2/4] lint --- packages/loot-core/src/server/budget/goaltemplates.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/loot-core/src/server/budget/goaltemplates.ts b/packages/loot-core/src/server/budget/goaltemplates.ts index 514ea3cf17f..e79c51e8e4c 100644 --- a/packages/loot-core/src/server/budget/goaltemplates.ts +++ b/packages/loot-core/src/server/budget/goaltemplates.ts @@ -168,12 +168,10 @@ async function processTemplate(month, force) { ); if (to_budget != null) { num_applied++; - templateBudget.push( - { - category: category.id, - amount: to_budget, - } - ) + templateBudget.push({ + category: category.id, + amount: to_budget, + }); available_remaining -= to_budget; } if (applyErrors != null) { From 2580eb6586592a6d873e116f950255dafd3a3c3b Mon Sep 17 00:00:00 2001 From: shall0pass <20625555+shall0pass@users.noreply.github.com> Date: Sat, 15 Jul 2023 18:17:46 -0500 Subject: [PATCH 3/4] release note --- upcoming-release-notes/1347.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 upcoming-release-notes/1347.md diff --git a/upcoming-release-notes/1347.md b/upcoming-release-notes/1347.md new file mode 100644 index 00000000000..68768d4cd2c --- /dev/null +++ b/upcoming-release-notes/1347.md @@ -0,0 +1,6 @@ +--- +category: Enhancements +authors: [shall0pass] +--- + +Goals: Yet another speed improvement on the calculation of templates and filling cells From 8454448373db3487d7e1cc1df6cd63039e4a1535 Mon Sep 17 00:00:00 2001 From: shall0pass <20625555+shall0pass@users.noreply.github.com> Date: Sat, 15 Jul 2023 19:10:10 -0500 Subject: [PATCH 4/4] fixup for async --- packages/loot-core/src/server/budget/goaltemplates.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/loot-core/src/server/budget/goaltemplates.ts b/packages/loot-core/src/server/budget/goaltemplates.ts index e79c51e8e4c..4354c381163 100644 --- a/packages/loot-core/src/server/budget/goaltemplates.ts +++ b/packages/loot-core/src/server/budget/goaltemplates.ts @@ -34,7 +34,6 @@ function checkScheduleTemplates(template) { } return { lowPriority, errorNotice }; } - async function setGoalBudget({ month, templateBudget }) { await batchMessages(async () => { templateBudget.forEach(element => { @@ -79,7 +78,7 @@ async function processTemplate(month, force) { } } } - setZero({ month }); + await setZero({ month }); // find all remainder templates, place them after all other templates let remainder_found; let remainder_priority = lowestPriority + 1; @@ -102,7 +101,7 @@ async function processTemplate(month, force) { let sheetName = monthUtils.sheetForMonth(month); let available_start = await getSheetValue(sheetName, `to-budget`); - let available_remaining = await getSheetValue(sheetName, `to-budget`); + let available_remaining = available_start; for (let priority = 0; priority <= lowestPriority; priority++) { // setup scaling for remainder let remainder_scale = 1; @@ -183,7 +182,7 @@ async function processTemplate(month, force) { } } } - setGoalBudget({ month, templateBudget }); + await setGoalBudget({ month, templateBudget }); } if (!force) {