1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-05 05:25:24 +02:00
Maybe/app/models/account/chartable.rb
Josh Pigford 6f67827f14 Implement error handling and logging for sparkline and series methods
- Added rescue blocks to handle exceptions in the Accounts and AccountableSparklines controllers, logging errors and rendering error partials.
- Enhanced error handling in the Account::Chartable and Balance::ChartSeriesBuilder models, logging specific error messages for series generation failures.
- Updated the accounts view to include a timeout for Turbo frame loading.
- Added a test to ensure graceful handling of sparkline errors in the AccountsController.

In reference to bug #2315
2025-05-26 20:05:16 -05:00

43 lines
1.2 KiB
Ruby

module Account::Chartable
extend ActiveSupport::Concern
def favorable_direction
classification == "asset" ? "up" : "down"
end
def balance_series(period: Period.last_30_days, view: :balance, interval: nil)
raise ArgumentError, "Invalid view type" unless [ :balance, :cash_balance, :holdings_balance ].include?(view.to_sym)
@balance_series ||= {}
memo_key = [ period.start_date, period.end_date, interval ].compact.join("_")
builder = (@balance_series[memo_key] ||= Balance::ChartSeriesBuilder.new(
account_ids: [ id ],
currency: self.currency,
period: period,
favorable_direction: favorable_direction,
interval: interval
))
builder.send("#{view}_series")
end
def sparkline_series
cache_key = family.build_cache_key("#{id}_sparkline")
Rails.cache.fetch(cache_key) do
balance_series
end
rescue => e
Rails.logger.error "Sparkline series error for account #{id}: #{e.message}"
# Return empty series as fallback
Series.new(
start_date: 30.days.ago.to_date,
end_date: Date.current,
interval: "1 day",
values: [],
favorable_direction: favorable_direction
)
end
end