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

Goals: Second speed improvement #1347

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions packages/loot-core/src/server/budget/goaltemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -33,8 +34,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();
Expand Down Expand Up @@ -65,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;
Expand All @@ -88,6 +101,7 @@ async function processTemplate(month, force) {

let sheetName = monthUtils.sheetForMonth(month);
let available_start = 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;
Expand Down Expand Up @@ -148,15 +162,16 @@ async function processTemplate(month, force) {
priority,
remainder_scale,
available_start,
available_remaining,
force,
);
if (to_budget != null) {
num_applied++;
await setBudget({
templateBudget.push({
category: category.id,
month,
amount: to_budget,
});
available_remaining -= to_budget;
}
if (applyErrors != null) {
errors = errors.concat(
Expand All @@ -167,7 +182,9 @@ async function processTemplate(month, force) {
}
}
}
await setGoalBudget({ month, templateBudget });
}

if (!force) {
//if overwrite is not preferred, set cell to original value
for (let l = 0; l < originalCategoryBalance.length; l++) {
Expand Down Expand Up @@ -251,6 +268,7 @@ async function applyCategoryTemplate(
priority,
remainder_scale,
available_start,
budgetAvailable,
force,
) {
let current_month = `${month}-01`;
Expand Down Expand Up @@ -325,7 +343,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;
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/1347.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Enhancements
authors: [shall0pass]
---

Goals: Yet another speed improvement on the calculation of templates and filling cells
Loading