Skip to content

Commit

Permalink
Goal speedup2 (actualbudget#1720)
Browse files Browse the repository at this point in the history
* only use needed priorities

* cleanup

* cleanup

* cleanup

* maybe better?

* note

* note fix

* suggestion

Co-authored-by: Joel Jeremy Marquez <[email protected]>

---------

Co-authored-by: Joel Jeremy Marquez <[email protected]>
  • Loading branch information
youngcw and joel-jeremy authored Oct 10, 2023
1 parent b5f706f commit b22fe54
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
29 changes: 18 additions & 11 deletions packages/loot-core/src/server/budget/goaltemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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) {
Expand All @@ -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];
Expand All @@ -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;
}
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/1720.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Enhancements
authors: [youngcw]
---

Goals: speedup by only run the requested priority levels, skip others

0 comments on commit b22fe54

Please sign in to comment.