mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-25 16:19:40 +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:
parent
6d4509fbe6
commit
fd65b5a747
3 changed files with 77 additions and 68 deletions
|
@ -22,65 +22,70 @@ class BalanceSheet
|
||||||
end
|
end
|
||||||
|
|
||||||
def classification_groups
|
def classification_groups
|
||||||
asset_groups = account_groups("asset")
|
Rails.cache.fetch(family.build_cache_key("bs_classification_groups")) do
|
||||||
liability_groups = account_groups("liability")
|
asset_groups = account_groups("asset")
|
||||||
|
liability_groups = account_groups("liability")
|
||||||
|
|
||||||
[
|
[
|
||||||
ClassificationGroup.new(
|
ClassificationGroup.new(
|
||||||
key: "asset",
|
key: "asset",
|
||||||
display_name: "Assets",
|
display_name: "Assets",
|
||||||
icon: "plus",
|
icon: "plus",
|
||||||
total_money: total_assets_money,
|
total_money: total_assets_money,
|
||||||
account_groups: asset_groups,
|
account_groups: asset_groups,
|
||||||
syncing?: asset_groups.any?(&:syncing?)
|
syncing?: asset_groups.any?(&:syncing?)
|
||||||
),
|
),
|
||||||
ClassificationGroup.new(
|
ClassificationGroup.new(
|
||||||
key: "liability",
|
key: "liability",
|
||||||
display_name: "Debts",
|
display_name: "Debts",
|
||||||
icon: "minus",
|
icon: "minus",
|
||||||
total_money: total_liabilities_money,
|
total_money: total_liabilities_money,
|
||||||
account_groups: liability_groups,
|
account_groups: liability_groups,
|
||||||
syncing?: liability_groups.any?(&:syncing?)
|
syncing?: liability_groups.any?(&:syncing?)
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def account_groups(classification = nil)
|
def account_groups(classification = nil)
|
||||||
classification_accounts = classification ? totals_query.filter { |t| t.classification == classification } : totals_query
|
Rails.cache.fetch(family.build_cache_key("bs_account_groups_#{classification || 'all'}")) do
|
||||||
classification_total = classification_accounts.sum(&:converted_balance)
|
classification_accounts = classification ? totals_query.filter { |t| t.classification == classification } : totals_query
|
||||||
account_groups = classification_accounts.group_by(&:accountable_type)
|
classification_total = classification_accounts.sum(&:converted_balance)
|
||||||
.transform_keys { |k| Accountable.from_type(k) }
|
|
||||||
|
|
||||||
groups = account_groups.map do |accountable, accounts|
|
account_groups = classification_accounts.group_by(&:accountable_type)
|
||||||
group_total = accounts.sum(&:converted_balance)
|
.transform_keys { |k| Accountable.from_type(k) }
|
||||||
|
|
||||||
key = accountable.model_name.param_key
|
groups = account_groups.map do |accountable, accounts|
|
||||||
|
group_total = accounts.sum(&:converted_balance)
|
||||||
|
|
||||||
AccountGroup.new(
|
key = accountable.model_name.param_key
|
||||||
id: classification ? "#{classification}_#{key}_group" : "#{key}_group",
|
|
||||||
key: key,
|
|
||||||
name: accountable.display_name,
|
|
||||||
classification: accountable.classification,
|
|
||||||
total: group_total,
|
|
||||||
total_money: Money.new(group_total, currency),
|
|
||||||
weight: classification_total.zero? ? 0 : group_total / classification_total.to_d * 100,
|
|
||||||
missing_rates?: accounts.any? { |a| a.missing_rates? },
|
|
||||||
color: accountable.color,
|
|
||||||
syncing?: accounts.any?(&:is_syncing),
|
|
||||||
accounts: accounts.map do |account|
|
|
||||||
account.define_singleton_method(:weight) do
|
|
||||||
classification_total.zero? ? 0 : account.converted_balance / classification_total.to_d * 100
|
|
||||||
end
|
|
||||||
|
|
||||||
account
|
AccountGroup.new(
|
||||||
end.sort_by(&:weight).reverse
|
id: classification ? "#{classification}_#{key}_group" : "#{key}_group",
|
||||||
)
|
key: key,
|
||||||
end
|
name: accountable.display_name,
|
||||||
|
classification: accountable.classification,
|
||||||
|
total: group_total,
|
||||||
|
total_money: Money.new(group_total, currency),
|
||||||
|
weight: classification_total.zero? ? 0 : group_total / classification_total.to_d * 100,
|
||||||
|
missing_rates?: accounts.any? { |a| a.missing_rates? },
|
||||||
|
color: accountable.color,
|
||||||
|
syncing?: accounts.any?(&:is_syncing),
|
||||||
|
accounts: accounts.map do |account|
|
||||||
|
account.define_singleton_method(:weight) do
|
||||||
|
classification_total.zero? ? 0 : account.converted_balance / classification_total.to_d * 100
|
||||||
|
end
|
||||||
|
|
||||||
groups.sort_by do |group|
|
account
|
||||||
manual_order = Accountable::TYPES
|
end.sort_by(&:weight).reverse
|
||||||
type_name = group.key.camelize
|
)
|
||||||
manual_order.index(type_name) || Float::INFINITY
|
end
|
||||||
|
|
||||||
|
groups.sort_by do |group|
|
||||||
|
manual_order = Accountable::TYPES
|
||||||
|
type_name = group.key.camelize
|
||||||
|
manual_order.index(type_name) || Float::INFINITY
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
<%= turbo_frame_tag "#{@accountable.model_name.param_key}_sparkline" do %>
|
<% cache Current.family.build_cache_key("#{@accountable.name}_sparkline_html") do %>
|
||||||
<div class="flex items-center justify-end gap-1">
|
<%= turbo_frame_tag "#{@accountable.model_name.param_key}_sparkline" do %>
|
||||||
<div class="w-8 h-3">
|
<div class="flex items-center justify-end gap-1">
|
||||||
<%= render "shared/sparkline", id: dom_id(@accountable, :sparkline_chart), series: @series %>
|
<div class="w-8 h-3">
|
||||||
</div>
|
<%= render "shared/sparkline", id: dom_id(@accountable, :sparkline_chart), series: @series %>
|
||||||
|
</div>
|
||||||
|
|
||||||
<%= tag.p @series.trend.percent_formatted,
|
<%= tag.p @series.trend.percent_formatted,
|
||||||
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 %>
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
<%= turbo_frame_tag dom_id(@account, :sparkline) do %>
|
<% cache Current.family.build_cache_key("account_#{@account.id}_sparkline_html") do %>
|
||||||
<div class="flex items-center justify-end gap-1">
|
<%= turbo_frame_tag dom_id(@account, :sparkline) do %>
|
||||||
<div class="w-8 h-5">
|
<div class="flex items-center justify-end gap-1">
|
||||||
<%= render "shared/sparkline", id: dom_id(@account, :sparkline_chart), series: @account.sparkline_series %>
|
<div class="w-8 h-5">
|
||||||
</div>
|
<%= render "shared/sparkline", id: dom_id(@account, :sparkline_chart), series: @account.sparkline_series %>
|
||||||
|
</div>
|
||||||
|
|
||||||
<%= tag.p @account.sparkline_series.trend.percent_formatted,
|
<%= tag.p @account.sparkline_series.trend.percent_formatted,
|
||||||
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 %>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue