mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-25 16:19:40 +02:00
Enhance HoldingCalculator with Logging and Error Handling
This commit is contained in:
parent
983729cbdf
commit
50e5ffb257
1 changed files with 17 additions and 1 deletions
|
@ -46,11 +46,22 @@ class Account::HoldingCalculator
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate_holding_records(portfolio, date)
|
def generate_holding_records(portfolio, date)
|
||||||
|
Rails.logger.info "[HoldingCalculator] Generating holdings for #{portfolio.size} securities on #{date}"
|
||||||
|
|
||||||
portfolio.map do |security_id, qty|
|
portfolio.map do |security_id, qty|
|
||||||
security = securities_cache[security_id]
|
security = securities_cache[security_id]
|
||||||
|
|
||||||
|
if security.blank?
|
||||||
|
Rails.logger.error "[HoldingCalculator] Security #{security_id} not found in cache for account #{account.id}"
|
||||||
|
next
|
||||||
|
end
|
||||||
|
|
||||||
price = security.dig(:prices)&.find { |p| p.date == date }
|
price = security.dig(:prices)&.find { |p| p.date == date }
|
||||||
|
|
||||||
next if price.blank?
|
if price.blank?
|
||||||
|
Rails.logger.info "[HoldingCalculator] No price found for security #{security_id} on #{date}"
|
||||||
|
next
|
||||||
|
end
|
||||||
|
|
||||||
converted_price = Money.new(price.price, price.currency).exchange_to(account.currency, fallback_rate: 1).amount
|
converted_price = Money.new(price.price, price.currency).exchange_to(account.currency, fallback_rate: 1).amount
|
||||||
|
|
||||||
|
@ -107,14 +118,19 @@ class Account::HoldingCalculator
|
||||||
|
|
||||||
def preload_securities
|
def preload_securities
|
||||||
securities = trades.map(&:entryable).map(&:security).uniq
|
securities = trades.map(&:entryable).map(&:security).uniq
|
||||||
|
Rails.logger.info "[HoldingCalculator] Preloading #{securities.size} securities for account #{account.id}"
|
||||||
|
|
||||||
securities.each do |security|
|
securities.each do |security|
|
||||||
|
Rails.logger.info "[HoldingCalculator] Loading prices for security #{security.id} (#{security.symbol})"
|
||||||
|
|
||||||
prices = Security::Price.find_prices(
|
prices = Security::Price.find_prices(
|
||||||
security: security,
|
security: security,
|
||||||
start_date: portfolio_start_date,
|
start_date: portfolio_start_date,
|
||||||
end_date: Date.current
|
end_date: Date.current
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Rails.logger.info "[HoldingCalculator] Found #{prices.size} prices for security #{security.id}"
|
||||||
|
|
||||||
@securities_cache[security.id] = {
|
@securities_cache[security.id] = {
|
||||||
security: security,
|
security: security,
|
||||||
prices: prices
|
prices: prices
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue