Files
sure/app/models/investment_statement.rb
T
3e79de5b64 perf: memoize Family#balance_sheet/investment_statement, cache transactions-index side queries (#3058)
* perf: memoize Family#balance_sheet/investment_statement, cache transactions-index side queries

The account sidebar renders on every page (mobile + desktop, 3 tabs each)
and calls Family#balance_sheet multiple times per render; neither it nor
Family#investment_statement/InvestmentStatement#current_holdings were
memoized, so each call rebuilt the underlying query from scratch. Memoize
both per-user (family sharing means different users must not share a
cached BalanceSheet/InvestmentStatement).

Also cache TransactionsController#index's uncategorized_count and
projected_recurring lookups, which run unconditionally on every request
regardless of whether the underlying data changed, using the same
entries_cache_version-keyed pattern already used elsewhere in the codebase
(e.g. Transaction::Search#totals).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix: address PR #3058 review feedback on cache invalidation and query reuse

- Invalidate transactions-index caches when the current user's AccountShare
  access changes, not just on entries/recurring updates (CodeRabbit/Codex
  flagged revoked users could see stale data for up to a day).
- Use full-precision timestamps instead of to_i in the cache keys so
  same-second updates aren't missed.
- Reuse the already-memoized investment_account_ids in
  InvestmentStatement#current_holdings instead of an extra any? query.
- Assert the rendered response instead of a controller instance variable in
  the uncategorized_count test, per CodeRabbit nitpick.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix: assert rendered response, not implementation details, in PR #3058 tests

Two CodeRabbit nitpicks from the second review round: the uncategorized-count
cache-reuse assertion matched a scope name that never appears in generated SQL
(making it vacuous), and the recurring-cache revocation test read the
controller's private @projected_recurring ivar instead of the rendered page.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix: bust transactions-index caches on entry/recurring deletion and account status change

jjmata's PR review flagged two invalidation gaps: hard-deleting an
uncategorized entry or recurring transaction left the previous max
updated_at unchanged (cache never busted), and toggling an account's
active status doesn't touch entries/AccountShare at all. Fold in
counts (like account_share_version already did) and a new
Family#accounts_status_version, and move the version helpers onto
Family/Current per the "fat models, skinny controllers" nit.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix: include merchant version in projected_recurring cache key

Editing or deleting a FamilyMerchant doesn't touch recurring_transactions,
so the cached projected-recurring list (rendered with merchant name/logo,
expires_in: 1.day) could show stale merchant data for up to a day.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(transactions): version projected-recurring cache by referenced merchants, not just FamilyMerchant

recurring_transactions.merchant_id can point at a shared ProviderMerchant
(recurring detection copies transaction.merchant_id), not just a
family-owned FamilyMerchant. merchants_version only tracked
Family#merchants (FamilyMerchant), so a ProviderMerchant update (e.g.
ProviderMerchant::Enhancer setting name/logo) left the cached projected
recurring list stale for up to a day.

Replace Family#merchants_version with #recurring_transaction_merchants_version,
scoped to the merchant records actually referenced by the family's recurring
transactions (both FamilyMerchant and ProviderMerchant), and bump the cache
key version. Also fix a flaky test assertion that matched all
recurring_transactions-table queries instead of the actual projection query,
since computing the cache key itself still runs small COUNT/MAX queries
against that table on a cache hit.

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Co-authored-by: Gerald <248542187+gfr-free@users.noreply.github.com>
2026-08-25 03:40:57 +02:00

333 lines
11 KiB
Ruby

require "digest/md5"
class InvestmentStatement
include Monetizable
monetize :total_contributions, :total_dividends, :total_interest, :unrealized_gains
attr_reader :family, :user
def initialize(family, user: nil)
@family = family
@user = user || Current.user
end
# Get totals for a specific period
def totals(period: Period.current_month)
account_ids = investment_account_ids
result = totals_query(account_ids: account_ids, date_range: period.date_range)
PeriodTotals.new(
contributions: Money.new(result[:contributions], family.currency),
withdrawals: Money.new(result[:withdrawals], family.currency),
dividends: Money.new(result[:dividends], family.currency),
interest: Money.new(result[:interest], family.currency),
trades_count: result[:trades_count],
currency: family.currency
)
end
# Net contributions (contributions - withdrawals)
def net_contributions(period: Period.current_month)
t = totals(period: period)
t.contributions - t.withdrawals
end
# Total portfolio value across all investment accounts
def portfolio_value
investment_accounts.sum { |a| convert_to_family_currency(a.balance, a.currency) }
end
def portfolio_value_money
Money.new(portfolio_value, family.currency)
end
# Total cash in investment accounts
def cash_balance
investment_accounts.sum { |a| convert_to_family_currency(a.cash_balance, a.currency) }
end
def cash_balance_money
Money.new(cash_balance, family.currency)
end
# Total holdings value
def holdings_value
portfolio_value - cash_balance
end
def holdings_value_money
Money.new(holdings_value, family.currency)
end
# All current holdings across investment accounts. Holdings are returned in
# their native currency; callers that aggregate across accounts must convert
# to family currency via convert_to_family_currency.
#
# Memoized: top_holdings, allocation, unrealized_gains, unrealized_gains_trend,
# and day_change each call this, so an unmemoized version ran the same
# DISTINCT ON query up to 5x per dashboard/report request.
def current_holdings
@current_holdings ||= begin
account_ids = investment_account_ids
if account_ids.any?
# Get the latest holding for each security per account
Holding
.where(account_id: account_ids)
.where.not(qty: 0)
.where(
id: Holding
.where(account_id: account_ids)
.select("DISTINCT ON (holdings.account_id, holdings.security_id) holdings.id")
.order(Arel.sql("holdings.account_id, holdings.security_id, holdings.date DESC"))
)
.includes(:security, :account)
else
Holding.none
end
end
end
# Top holdings by family-currency value
def top_holdings(limit: 5)
current_holdings
.to_a
.sort_by { |h| -convert_to_family_currency(h.amount, h.currency) }
.first(limit)
end
# Portfolio allocation by security. Weights and amounts are computed in the
# family's currency so cross-currency holdings compare correctly.
def allocation
converted = current_holdings.to_a.map do |holding|
[ holding, convert_to_family_currency(holding.amount, holding.currency) ]
end
total = converted.sum { |_, value| value }
return [] if total.zero?
converted
.sort_by { |_, value| -value }
.map do |holding, value|
HoldingAllocation.new(
security: holding.security,
amount: Money.new(value, family.currency),
weight: (value / total * 100).round(2),
trend: holding.trend
)
end
end
# Unrealized gains across all holdings, summed in family currency
def unrealized_gains
current_holdings.sum do |holding|
trend = holding.trend
trend ? convert_to_family_currency(trend.value, holding.currency) : 0
end
end
# Total contributions (all time) - returns numeric for monetize
def total_contributions
all_time_totals.contributions&.amount || 0
end
# Total dividends (all time) - returns numeric for monetize
def total_dividends
all_time_totals.dividends&.amount || 0
end
# Total interest (all time) - returns numeric for monetize
def total_interest
all_time_totals.interest&.amount || 0
end
def unrealized_gains_trend
holdings = current_holdings.to_a
return nil if holdings.empty?
# Only include holdings with known cost basis in the calculation
holdings_with_cost_basis = holdings.select(&:avg_cost)
return nil if holdings_with_cost_basis.empty?
current = holdings_with_cost_basis.sum do |h|
convert_to_family_currency(h.amount, h.currency)
end
previous = holdings_with_cost_basis.sum do |h|
convert_to_family_currency(h.qty * h.avg_cost.amount, h.currency)
end
Trend.new(
current: Money.new(current, family.currency),
previous: Money.new(previous, family.currency)
)
end
def period_return_trend(period: Period.current_month)
currency = family.currency
account_ids = investment_account_ids
return nil if account_ids.empty?
absolute_return = ActiveRecord::Base.connection.select_value(
ActiveRecord::Base.sanitize_sql_array([
<<~SQL.squish,
SELECT COALESCE(SUM(b.net_market_flows * COALESCE(er.rate, 1)), 0)
FROM balances b
JOIN accounts a ON a.id = b.account_id
LEFT JOIN exchange_rates er ON (
er.date = b.date
AND er.from_currency = b.currency
AND er.to_currency = :currency
)
WHERE a.id IN (:account_ids)
AND a.family_id = :family_id
AND a.status IN ('draft', 'active')
AND b.date BETWEEN :start_date AND :end_date
SQL
{
currency: currency,
account_ids: account_ids,
family_id: family.id,
start_date: period.date_range.begin,
end_date: period.date_range.end
}
])
).to_d
period_start = period.date_range.begin
# Single query for all accounts' most recent pre-period balance (strict < to avoid
# double-counting the first day's net_market_flows in both the denominator and absolute_return).
# FX conversion is done in SQL (matching absolute_return) so balance rows whose currency
# differs from the account's current currency (e.g. after a currency change) are still picked up.
start_value = ActiveRecord::Base.connection.select_value(
ActiveRecord::Base.sanitize_sql_array([
<<~SQL.squish,
SELECT COALESCE(SUM(b.end_balance * COALESCE(er.rate, 1)), 0)
FROM accounts a
INNER JOIN balances b ON b.account_id = a.id
LEFT JOIN exchange_rates er ON (
er.date = :period_start
AND er.from_currency = b.currency
AND er.to_currency = :currency
)
INNER JOIN (
SELECT b2.account_id, MAX(b2.date) AS max_date
FROM balances b2
WHERE b2.account_id IN (:account_ids)
AND b2.date < :period_start
GROUP BY b2.account_id
) latest ON latest.account_id = b.account_id AND b.date = latest.max_date
WHERE a.id IN (:account_ids)
AND a.family_id = :family_id
AND a.status IN ('draft', 'active')
SQL
{ account_ids: account_ids, period_start: period_start, family_id: family.id, currency: currency }
])
).to_d
return nil if start_value.zero?
Trend.new(
current: Money.new(start_value + absolute_return, currency),
previous: Money.new(start_value, currency)
)
end
# Day change across portfolio, summed in family currency
def day_change
changes = current_holdings.to_a.filter_map do |h|
t = h.day_change
next nil unless t
curr = t.current.is_a?(Money) ? t.current.amount : t.current
prev = t.previous.is_a?(Money) ? t.previous.amount : t.previous
[
convert_to_family_currency(curr, h.currency),
convert_to_family_currency(prev, h.currency)
]
end
return nil if changes.empty?
Trend.new(
current: Money.new(changes.sum { |c, _| c }, family.currency),
previous: Money.new(changes.sum { |_, p| p }, family.currency)
)
end
# Investment accounts
def investment_accounts
@investment_accounts ||= begin
scope = family.accounts.visible.included_in_reports.where(accountable_type: %w[Investment Crypto])
scope = scope.included_in_finances_for(user) if user
scope
end
end
private
# Today's rates for every currency present on the family's investment
# accounts and their holdings. Mirrors BalanceSheet::AccountTotals#exchange_rates.
def exchange_rates
@exchange_rates ||= begin
account_currencies = investment_accounts.map(&:currency)
holding_currencies = Holding.where(account_id: investment_account_ids).distinct.pluck(:currency)
foreign = (account_currencies + holding_currencies)
.compact
.uniq
.reject { |c| c == family.currency }
ExchangeRate.rates_for(foreign, to: family.currency, date: Date.current)
end
end
# Unwrap Money first because this codebase's Money (lib/money.rb) ignores
# the currency arg of `Money.new` when the payload is already a Money, and
# `Money * numeric` preserves the source currency — so multiplying a
# foreign-currency Money by a rate would FX-scale the amount but keep the
# wrong currency label, corrupting downstream sums.
def convert_to_family_currency(amount, from_currency)
return amount if amount.nil?
numeric = amount.is_a?(Money) ? amount.amount : amount
return numeric if from_currency == family.currency
rate = exchange_rates[from_currency] || 1
numeric * rate
end
def all_time_totals
@all_time_totals ||= totals(period: Period.all_time)
end
PeriodTotals = Data.define(:contributions, :withdrawals, :dividends, :interest, :trades_count, :currency) do
def net_flow
contributions - withdrawals
end
def total_income
dividends + interest
end
end
HoldingAllocation = Data.define(:security, :amount, :weight, :trend)
def investment_account_ids
@investment_account_ids ||= investment_accounts.pluck(:id)
end
def totals_query(account_ids:, date_range:)
if account_ids.empty?
return Totals.new(family, account_ids: account_ids, date_range: date_range).call
end
account_ids_hash = Digest::MD5.hexdigest(account_ids.sort.join(","))
Rails.cache.fetch([
"investment_statement", "totals_query", family.id, user&.id,
account_ids_hash, date_range.begin, date_range.end, family.entries_cache_version
]) { Totals.new(family, account_ids: account_ids, date_range: date_range).call }
end
def monetizable_currency
family.currency
end
end