The personal finance app for everyone (by everyone)
Ruby
Skip the rollover recompute when its inputs are unchanged (#4678)
Nikolay Kopylovc372b3b98cef
diff --git a/app/models/budget/rollover_calculator.rb b/app/models/budget/rollover_calculator.rbindex b8e9aab962..b014a8f865 100644--- a/app/models/budget/rollover_calculator.rb+++ b/app/models/budget/rollover_calculator.rb@@ -29,11 +29,16 @@ class Budget::RolloverCalculator# may have changed while we waited for it.def recompute!return if first_relevant_budget_date.nil?+ return if Rails.cache.read(inputs_cache_key) == inputs_fingerprintBudgetCategory.transaction dolock_chain!+ fingerprint = inputs_fingerprintfrom = first_relevant_budget_date- recompute_chain!(from) if from+ wrote = from ? recompute_chain!(from) : false++ # Only a pass that changed nothing proves the stored amounts match these inputs.+ Rails.cache.write(inputs_cache_key, fingerprint) unless wroteendend@@ -113,8 +118,35 @@ class Budget::RolloverCalculatorif updates.any?BudgetCategory.upsert_all(updates, unique_by: :id, update_only: %w[rolled_over_amount updated_at])end++ updates.any?+ end++ def inputs_cache_key+ "#{chain_key}:inputs"end+ # Only what the chain reads. Rows are digested, not versioned by max(updated_at), so a+ # late-committing or update_all write still counts; family.updated_at moves with every sync.+ def inputs_fingerprint+ budgets = family.budgets.where(user_id: user&.id)+ accounts = user ? family.accounts.where(owner_id: user.id) : family.accounts++ [+ digest_of(budgets),+ digest_of(BudgetCategory.where(budget_id: budgets.select(:id))),+ digest_of(accounts.select(:id, :owner_id, :status, :exclude_from_reports)),+ digest_of(AccountShare.where(account_id: accounts.select(:id)).select(:id, :account_id, :user_id, :include_in_finances)),+ family.tax_advantaged_account_ids.sort.join(","),+ family.entries_cache_version,+ ExchangeRate.where(to_currency: family.currency).maximum(:updated_at)&.to_f,+ family.updated_at.to_f+ ].join("|")+ end++ def digest_of(scope)+ scope.klass.unscoped.from(scope, :rows).pick(Arel.sql("md5(string_agg(rows::text, '|' ORDER BY rows.id))"))+ end# Earliest month this chain has anything to say about: rollover switched# on, or an amount left behind by a toggle that was switched off anddiff --git a/test/models/budget_category_test.rb b/test/models/budget_category_test.rbindex c469a224a9..759d9cf590 100644--- a/test/models/budget_category_test.rb+++ b/test/models/budget_category_test.rb@@ -766,6 +766,158 @@ class BudgetCategoryRolloverTest < ActiveSupport::TestCaseassert_equal 0, stored_rollover(second)end+ test "an unchanged chain is not recomputed" do+ Rails.stubs(:cache).returns(ActiveSupport::Cache::MemoryStore.new)+ first = initialized_budget(2.months.ago)+ allocate(first, 100)+ second = initialized_budget(1.month.ago)+ allocate(second, 100)+ recompute!+ recompute!++ Budget::RolloverCalculator.any_instance.expects(:recompute_chain!).never+ recompute!+ end++ test "new spending recomputes a chain that was already settled" do+ Rails.stubs(:cache).returns(ActiveSupport::Cache::MemoryStore.new)+ first = initialized_budget(2.months.ago)+ allocate(first, 100)+ second = initialized_budget(1.month.ago)+ allocate(second, 100)+ recompute!+ recompute!++ spend(30, budget: first)+ recompute!++ assert_equal 70, stored_rollover(second)+ end++ test "an allocation change recomputes a chain that was already settled" do+ Rails.stubs(:cache).returns(ActiveSupport::Cache::MemoryStore.new)+ first = initialized_budget(2.months.ago)+ allocate(first, 100)+ second = initialized_budget(1.month.ago)+ allocate(second, 100)+ recompute!+ recompute!++ allocate(first, 40)+ recompute!++ assert_equal 40, stored_rollover(second)+ end++ test "a sync recomputes a chain that was already settled" do+ Rails.stubs(:cache).returns(ActiveSupport::Cache::MemoryStore.new)+ first = initialized_budget(2.months.ago)+ allocate(first, 100)+ second = initialized_budget(1.month.ago)+ allocate(second, 100)+ recompute!+ recompute!++ @family.touch(:latest_sync_completed_at)+ Budget::RolloverCalculator.any_instance.expects(:recompute_chain!).once.returns(false)+ recompute!+ end++ test "an allocation committed behind a later timestamp recomputes a settled chain" do+ Rails.stubs(:cache).returns(ActiveSupport::Cache::MemoryStore.new)+ first = initialized_budget(2.months.ago)+ allocate(first, 100)+ second = initialized_budget(1.month.ago)+ allocate(second, 100)+ recompute!+ recompute!++ budget_category_for(first).update_columns(budgeted_spending: 40, updated_at: 1.day.ago)+ recompute!++ assert_equal 40, stored_rollover(second)+ end++ test "an account reassigned without a touch recomputes a settled chain" do+ Rails.stubs(:cache).returns(ActiveSupport::Cache::MemoryStore.new)+ first = initialized_budget(2.months.ago)+ allocate(first, 100)+ second = initialized_budget(1.month.ago)+ allocate(second, 100)+ recompute!+ recompute!++ new_owner = @family.users.where.not(id: @account.owner_id).first!+ Account.where(id: @account.id).update_all(owner_id: new_owner.id)+ Budget::RolloverCalculator.any_instance.expects(:recompute_chain!).once.returns(false)+ recompute!+ end++ test "a balance update keeps a settled chain" do+ settle_chain++ @account.update!(balance: 1)++ assert_no_recompute+ end++ test "excluding an account from reports recomputes a settled chain" do+ second = settle_chain { |first| spend(30, budget: first) }.last+ assert_equal 70, stored_rollover(second)++ Account.where(id: @account.id).update_all(exclude_from_reports: true)+ @family = Family.find(@family.id)+ recompute!++ assert_equal 100, stored_rollover(second)+ end++ test "a tax-advantaged subtype recomputes a settled chain" do+ investment = @family.accounts.create!(accountable: Investment.new(subtype: "brokerage"), name: "Brokerage", currency: "USD", balance: 0, status: "active")+ second = settle_chain { |first| create_transaction(account: investment, date: first.start_date, amount: 30, category: @category) }.last+ assert_equal 70, stored_rollover(second)++ investment.accountable.update!(subtype: "401k")+ @family = Family.find(@family.id)+ recompute!++ assert_equal 100, stored_rollover(second)+ end++ test "a share permission change keeps a settled chain" do+ share = AccountShare.create!(account: @account, user: @family.users.first!, include_in_finances: true)+ settle_chain++ share.update!(permission: "full_control")++ assert_no_recompute+ end++ test "a share's include in finances flag recomputes a settled chain" do+ share = AccountShare.create!(account: @account, user: @family.users.first!, include_in_finances: true)+ settle_chain++ share.update!(include_in_finances: false)++ assert_recompute+ end++ test "a rate into another currency keeps a settled chain" do+ settle_chain++ ExchangeRate.create!(from_currency: "JPY", to_currency: "KRW", date: Date.current, rate: 9)++ assert_no_recompute+ end++ test "a rate into the family currency recomputes a settled chain" do+ settle_chain++ ExchangeRate.create!(from_currency: "EUR", to_currency: @family.currency, date: Date.current, rate: 1.1)++ assert_recompute+ end+test "recompute does not clobber an allocation changed underneath it" dofirst = initialized_budget(2.months.ago)allocate(first, 100)@@ -1085,6 +1237,31 @@ class BudgetCategoryRolloverTest < ActiveSupport::TestCaseBudget::RolloverCalculator.new(family: @family, user: user).recompute!end+ def settle_chain+ Rails.stubs(:cache).returns(ActiveSupport::Cache::MemoryStore.new)+ first = initialized_budget(2.months.ago)+ allocate(first, 100)+ yield first if block_given?+ second = initialized_budget(1.month.ago)+ allocate(second, 100)+ recompute!+ recompute!+ [ first, second ]+ end++ # A fresh family, as on the next request: Family memoizes derived reads.+ def assert_recompute+ @family = Family.find(@family.id)+ Budget::RolloverCalculator.any_instance.expects(:recompute_chain!).once.returns(false)+ recompute!+ end++ def assert_no_recompute+ @family = Family.find(@family.id)+ Budget::RolloverCalculator.any_instance.expects(:recompute_chain!).never+ recompute!+ end+def budget_category_for(budget)BudgetCategory.find_by!(budget_id: budget.id, category: @category)end