Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Goal speedup2 #1720

Merged
merged 12 commits into from
Oct 10, 2023
32 changes: 21 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,14 @@ 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 ||
template[l].priority === undefined
) {
youngcw marked this conversation as resolved.
Show resolved Hide resolved
continue;
}
priority_list.push(template[l].priority);
}
}
if (budgeted) {
Expand All @@ -107,10 +111,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 +133,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