Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Load only window entries in incremental forward syncs (#4653)

Nikolay Kopylov4a8cb1bf1ef1
diff --git a/app/models/balance/forward_calculator.rb b/app/models/balance/forward_calculator.rb
index 22094a3c75..b0d0a135ea 100644
--- a/app/models/balance/forward_calculator.rb
+++ b/app/models/balance/forward_calculator.rb
@@ -130,6 +130,10 @@ class Balance::ForwardCalculator < Balance::BaseCalculator
.find_by(date: @window_start_date - 1)
end
+ def sync_cache
+ @sync_cache ||= Balance::SyncCache.new(account, start_date: (@window_start_date if incremental?))
+ end
+
def calc_start_date
incremental? ? @window_start_date : calculation_start_date
end
diff --git a/app/models/balance/sync_cache.rb b/app/models/balance/sync_cache.rb
index 3ec9e40309..f2d9ae06cd 100644
--- a/app/models/balance/sync_cache.rb
+++ b/app/models/balance/sync_cache.rb
@@ -1,6 +1,8 @@
class Balance::SyncCache
- def initialize(account)
+ # start_date skips entries before it; only safe when the caller seeds balances from a persisted prior day.
+ def initialize(account, start_date: nil)
@account = account
+ @start_date = start_date
@unconvertible_entry_count = 0
@unconvertible_holding_count = 0
end
@@ -75,7 +77,7 @@ class Balance::SyncCache
@converted_entries ||= begin
missing_rate_pairs = []
- converted = account.entries.excluding_pending.excluding_split_parents.includes(:entryable).order(:date).to_a.filter_map do |e|
+ converted = entries_scope.to_a.filter_map do |e|
custom_rate = e.entryable.exchange_rate if e.entryable.respond_to?(:exchange_rate)
# Use Money#exchange_to with custom rate if available, standard lookup otherwise.
@@ -116,6 +118,11 @@ class Balance::SyncCache
end
end
+ def entries_scope
+ scope = account.entries.excluding_pending.excluding_split_parents.includes(:entryable).order(:date)
+ @start_date ? scope.where(date: @start_date..) : scope
+ end
+
def report_missing_rates(message:, counts:, missing_rate_pairs:)
DebugLogEntry.capture(
category: "sync",
diff --git a/test/models/balance/forward_calculator_test.rb b/test/models/balance/forward_calculator_test.rb
index db0f7613a4..11ae65fe51 100644
--- a/test/models/balance/forward_calculator_test.rb
+++ b/test/models/balance/forward_calculator_test.rb
@@ -713,6 +713,45 @@ class Balance::ForwardCalculatorTest < ActiveSupport::TestCase
)
end
+ test "incremental sync reads entries from the window start" do
+ account = create_account_with_ledger(
+ account: { type: Depository, currency: "USD" },
+ entries: [
+ { type: "opening_anchor", date: 5.days.ago.to_date, balance: 20000 },
+ { type: "transaction", date: 4.days.ago.to_date, amount: -500 },
+ { type: "transaction", date: 2.days.ago.to_date, amount: 100 }
+ ]
+ )
+ Balance::Materializer.new(account, strategy: :forward).materialize_balances
+
+ window_start = 3.days.ago.to_date
+ sync_cache = Balance::SyncCache.new(account, start_date: window_start)
+ Balance::SyncCache.expects(:new).with(account, start_date: window_start).returns(sync_cache)
+
+ calculator = Balance::ForwardCalculator.new(account, window_start_date: window_start)
+ calculator.calculate
+
+ assert calculator.incremental?
+ end
+
+ test "falling back to full recalculation reads every entry" do
+ account = create_account_with_ledger(
+ account: { type: Depository, currency: "USD" },
+ entries: [
+ { type: "opening_anchor", date: 3.days.ago.to_date, balance: 20000 },
+ { type: "transaction", date: 2.days.ago.to_date, amount: -500 }
+ ]
+ )
+
+ sync_cache = Balance::SyncCache.new(account)
+ Balance::SyncCache.expects(:new).with(account, start_date: nil).returns(sync_cache)
+
+ calculator = Balance::ForwardCalculator.new(account, window_start_date: 2.days.ago.to_date)
+ calculator.calculate
+
+ assert_not calculator.incremental?
+ end
+
test "falls back to full recalculation when prior balance has a non-cash component" do
account = create_account_with_ledger(
account: { type: Depository, currency: "USD" },
diff --git a/test/models/balance/sync_cache_test.rb b/test/models/balance/sync_cache_test.rb
index 94600b1037..d0c5741647 100644
--- a/test/models/balance/sync_cache_test.rb
+++ b/test/models/balance/sync_cache_test.rb
@@ -388,4 +388,16 @@ class Balance::SyncCacheTest < ActiveSupport::TestCase
converted_entry = converted_entries.first
assert_equal 150.0, converted_entry.amount
end
+
+ test "start_date leaves out entries dated before it" do
+ @account.entries.create!(date: 3.days.ago.to_date, name: "Before", amount: 10, currency: "USD", entryable: Transaction.new)
+ @account.entries.create!(date: 3.days.ago.to_date, name: "Before value", amount: 900, currency: "USD", entryable: Valuation.new)
+ @account.entries.create!(date: 1.day.ago.to_date, name: "Inside", amount: 20, currency: "USD", entryable: Transaction.new)
+
+ sync_cache = Balance::SyncCache.new(@account, start_date: 2.days.ago.to_date)
+
+ assert_empty sync_cache.get_entries(3.days.ago.to_date)
+ assert_nil sync_cache.get_valuation(3.days.ago.to_date)
+ assert_equal [ "Inside" ], sync_cache.get_entries(1.day.ago.to_date).map(&:name)
+ end
end