1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-27 00:59:39 +02:00

Implement caching for classification and account groups in BalanceSheet model and optimize sparkline rendering in views

- Add caching for classification groups and account groups in the BalanceSheet model to improve performance.
- Update views for accountable sparklines to utilize caching for rendered HTML, enhancing load times and reducing database queries.
This commit is contained in:
Josh Pigford 2025-05-23 11:46:12 -05:00
parent 6d4509fbe6
commit fd65b5a747
3 changed files with 77 additions and 68 deletions

View file

@ -22,6 +22,7 @@ class BalanceSheet
end end
def classification_groups def classification_groups
Rails.cache.fetch(family.build_cache_key("bs_classification_groups")) do
asset_groups = account_groups("asset") asset_groups = account_groups("asset")
liability_groups = account_groups("liability") liability_groups = account_groups("liability")
@ -44,10 +45,13 @@ class BalanceSheet
) )
] ]
end end
end
def account_groups(classification = nil) def account_groups(classification = nil)
Rails.cache.fetch(family.build_cache_key("bs_account_groups_#{classification || 'all'}")) do
classification_accounts = classification ? totals_query.filter { |t| t.classification == classification } : totals_query classification_accounts = classification ? totals_query.filter { |t| t.classification == classification } : totals_query
classification_total = classification_accounts.sum(&:converted_balance) classification_total = classification_accounts.sum(&:converted_balance)
account_groups = classification_accounts.group_by(&:accountable_type) account_groups = classification_accounts.group_by(&:accountable_type)
.transform_keys { |k| Accountable.from_type(k) } .transform_keys { |k| Accountable.from_type(k) }
@ -83,6 +87,7 @@ class BalanceSheet
manual_order.index(type_name) || Float::INFINITY manual_order.index(type_name) || Float::INFINITY
end end
end end
end
def net_worth_series(period: Period.last_30_days) def net_worth_series(period: Period.last_30_days)
active_accounts.balance_series(currency: currency, period: period, favorable_direction: "up") active_accounts.balance_series(currency: currency, period: period, favorable_direction: "up")

View file

@ -1,4 +1,5 @@
<%= turbo_frame_tag "#{@accountable.model_name.param_key}_sparkline" do %> <% cache Current.family.build_cache_key("#{@accountable.name}_sparkline_html") do %>
<%= turbo_frame_tag "#{@accountable.model_name.param_key}_sparkline" do %>
<div class="flex items-center justify-end gap-1"> <div class="flex items-center justify-end gap-1">
<div class="w-8 h-3"> <div class="w-8 h-3">
<%= render "shared/sparkline", id: dom_id(@accountable, :sparkline_chart), series: @series %> <%= render "shared/sparkline", id: dom_id(@accountable, :sparkline_chart), series: @series %>
@ -8,4 +9,5 @@
style: "color: #{@series.trend.color}", style: "color: #{@series.trend.color}",
class: "font-mono text-right text-xs font-medium text-primary" %> class: "font-mono text-right text-xs font-medium text-primary" %>
</div> </div>
<% end %>
<% end %> <% end %>

View file

@ -1,4 +1,5 @@
<%= turbo_frame_tag dom_id(@account, :sparkline) do %> <% cache Current.family.build_cache_key("account_#{@account.id}_sparkline_html") do %>
<%= turbo_frame_tag dom_id(@account, :sparkline) do %>
<div class="flex items-center justify-end gap-1"> <div class="flex items-center justify-end gap-1">
<div class="w-8 h-5"> <div class="w-8 h-5">
<%= render "shared/sparkline", id: dom_id(@account, :sparkline_chart), series: @account.sparkline_series %> <%= render "shared/sparkline", id: dom_id(@account, :sparkline_chart), series: @account.sparkline_series %>
@ -8,4 +9,5 @@
style: "color: #{@account.sparkline_series.trend.color}", style: "color: #{@account.sparkline_series.trend.color}",
class: "font-mono text-right text-xs font-medium text-primary" %> class: "font-mono text-right text-xs font-medium text-primary" %>
</div> </div>
<% end %>
<% end %> <% end %>