The personal finance app for everyone (by everyone)
Ruby
class Balance::SyncCache
# 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
# How many entries were left out of the balance calculation because no exchange rate was
# available to convert them into the account's currency. Populated once `converted_entries`
# has run; zero on a healthy sync.
def unconvertible_entry_count
entries_by_date
@unconvertible_entry_count
end
# How many holdings were counted at a 1:1 rate for the same reason, overstating or
# understating the holdings total by the FX gap. Populated once `holdings_value_by_date`
# has run; zero on a healthy sync.
def unconvertible_holding_count
holdings_value_by_date
@unconvertible_holding_count
end
# Every entry the calculation uses: settled, without split parents or
# future-dated ones, and converted into the account's currency.
def entries
converted_entries
end
def get_valuation(date)
entries_by_date[date]&.find { |e| e.valuation? }
end
def get_holdings_value(date)
holdings_value_by_date[date] || 0
end
def get_entries(date)
entries_by_date[date]&.select { |e| e.transaction? || e.trade? } || []
end
# Every entry converted to the account currency, in date order.
def entries
converted_entries
end
# One replay per loan per cache, so a calculator that asks twice does not
# walk the ledger twice.
def loan_principal_payments(loan)
@loan_principal_payments ||= {}
@loan_principal_payments[loan.id] ||= Loan::PrincipalPayments.for(loan, entries: entries)
end
private
attr_reader :account
def entries_by_date
@entries_by_date ||= converted_entries.group_by(&:date)
end
def holdings_value_by_date
@holdings_value_by_date ||= begin
missing_rate_pairs = []
totals = account.holdings.each_with_object(Hash.new(0)) do |h, acc|
begin
converted = Money.new(h.amount, h.currency).exchange_to(account.currency, date: h.date).amount
rescue Money::ConversionError => error
# Fall back to a 1:1 rate, which misstates the holding by the FX gap. Excluding it
# instead would be worse here: `BaseCalculator#derive_cash_balance_on_date_from_total`
# derives cash as `total_balance - holdings_value`, so a dropped holding reappears
# as phantom cash. Report it rather than silently accepting the wrong number.
converted = h.amount
@unconvertible_holding_count += 1
missing_rate_pairs |= [ [ error.from_currency, error.to_currency ] ]
end
acc[h.date] += converted
end
if @unconvertible_holding_count.positive?
report_missing_rates(
message: "Valued #{@unconvertible_holding_count} #{"holding".pluralize(@unconvertible_holding_count)} at a 1:1 rate: no exchange rate available",
counts: { unconvertible_holding_count: @unconvertible_holding_count },
missing_rate_pairs: missing_rate_pairs
)
end
totals
end
end
def converted_entries
# Scheduled (future-dated) entries are excluded from balance calculations
# entirely -- they must not affect the account's current balance until
# their date arrives. A daily sync naturally picks them up once "today"
# reaches their date (see Entry#scheduled?).
@converted_entries ||= begin
missing_rate_pairs = []
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.
# Mutate the entry in place rather than dup'ing — these instances are scoped to
# this sync-cache only and never persisted, so avoiding the dup eliminates a
# large amount of ActiveModel::Attribute allocations during sync.
# to_a materializes independent instances; no AR identity map is active during sync,
# so callers holding a reference to the same association will never see these mutations.
begin
new_amount = e.amount_money.exchange_to(
account.currency,
date: e.date,
custom_rate: custom_rate
).amount
rescue Money::ConversionError => error
# Drop the entry instead of converting it at a made-up rate. A 1:1 fallback here
# would silently misstate the balance by the size of the FX gap (see #1143), and
# raising would abandon the whole account's balances over a single entry.
@unconvertible_entry_count += 1
missing_rate_pairs |= [ [ error.from_currency, error.to_currency ] ]
next
end
e.amount = new_amount
e.currency = account.currency
e
end
if @unconvertible_entry_count.positive?
report_missing_rates(
message: "Excluded #{@unconvertible_entry_count} #{"entry".pluralize(@unconvertible_entry_count)} from balance calculation: no exchange rate available",
counts: { unconvertible_entry_count: @unconvertible_entry_count },
missing_rate_pairs: missing_rate_pairs
)
end
converted
end
end
def entries_scope
scope = account.entries.excluding_pending.excluding_split_parents.where(date: ..Date.current).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",
level: "warn",
source: "Balance::SyncCache",
message: message,
account: account,
family: account.family,
provider_key: exchange_rate_provider_key,
metadata: {
account_currency: account.currency,
missing_rate_pairs: missing_rate_pairs.map { |from, to| "#{from}->#{to}" }
}.merge(counts)
)
end
# Which FX backend was in use when the rate came up missing, so support can filter
# these entries by provider. `ExchangeRate.provider_name` raises on an unrecognized
# configuration — diagnostics must never fail the sync.
def exchange_rate_provider_key
ExchangeRate.provider_name
rescue StandardError
nil
end
end