Skip to content

armgit / maybe

Public Imported from GitHub
0

The personal finance app for everyone

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

Josh Pigfordfd65b5a7472d
diff --git a/app/models/balance_sheet.rb b/app/models/balance_sheet.rb
index a89a9859..9a3d43ea 100644
--- a/app/models/balance_sheet.rb
+++ b/app/models/balance_sheet.rb
@@ -22,65 +22,70 @@ class BalanceSheet
end
def classification_groups
- asset_groups = account_groups("asset")
- liability_groups = account_groups("liability")
-
- [
- ClassificationGroup.new(
- key: "asset",
- display_name: "Assets",
- icon: "plus",
- total_money: total_assets_money,
- account_groups: asset_groups,
- syncing?: asset_groups.any?(&:syncing?)
- ),
- ClassificationGroup.new(
- key: "liability",
- display_name: "Debts",
- icon: "minus",
- total_money: total_liabilities_money,
- account_groups: liability_groups,
- syncing?: liability_groups.any?(&:syncing?)
- )
- ]
+ Rails.cache.fetch(family.build_cache_key("bs_classification_groups")) do
+ asset_groups = account_groups("asset")
+ liability_groups = account_groups("liability")
+
+ [
+ ClassificationGroup.new(
+ key: "asset",
+ display_name: "Assets",
+ icon: "plus",
+ total_money: total_assets_money,
+ account_groups: asset_groups,
+ syncing?: asset_groups.any?(&:syncing?)
+ ),
+ ClassificationGroup.new(
+ key: "liability",
+ display_name: "Debts",
+ icon: "minus",
+ total_money: total_liabilities_money,
+ account_groups: liability_groups,
+ syncing?: liability_groups.any?(&:syncing?)
+ )
+ ]
+ end
end
def account_groups(classification = nil)
- classification_accounts = classification ? totals_query.filter { |t| t.classification == classification } : totals_query
- classification_total = classification_accounts.sum(&:converted_balance)
- account_groups = classification_accounts.group_by(&:accountable_type)
- .transform_keys { |k| Accountable.from_type(k) }
-
- groups = account_groups.map do |accountable, accounts|
- group_total = accounts.sum(&:converted_balance)
-
- key = accountable.model_name.param_key
-
- AccountGroup.new(
- 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
- end.sort_by(&:weight).reverse
- )
- end
-
- groups.sort_by do |group|
- manual_order = Accountable::TYPES
- type_name = group.key.camelize
- manual_order.index(type_name) || Float::INFINITY
+ 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_total = classification_accounts.sum(&:converted_balance)
+
+ account_groups = classification_accounts.group_by(&:accountable_type)
+ .transform_keys { |k| Accountable.from_type(k) }
+
+ groups = account_groups.map do |accountable, accounts|
+ group_total = accounts.sum(&:converted_balance)
+
+ key = accountable.model_name.param_key
+
+ AccountGroup.new(
+ 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
+ end.sort_by(&:weight).reverse
+ )
+ 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
diff --git a/app/views/accountable_sparklines/show.html.erb b/app/views/accountable_sparklines/show.html.erb
index ba915d57..236c3616 100644
--- a/app/views/accountable_sparklines/show.html.erb
+++ b/app/views/accountable_sparklines/show.html.erb
@@ -1,11 +1,13 @@
-<%= turbo_frame_tag "#{@accountable.model_name.param_key}_sparkline" do %>
- <div class="flex items-center justify-end gap-1">
- <div class="w-8 h-3">
- <%= render "shared/sparkline", id: dom_id(@accountable, :sparkline_chart), series: @series %>
- </div>
+<% 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="w-8 h-3">
+ <%= render "shared/sparkline", id: dom_id(@accountable, :sparkline_chart), series: @series %>
+ </div>
- <%= tag.p @series.trend.percent_formatted,
- style: "color: #{@series.trend.color}",
- class: "font-mono text-right text-xs font-medium text-primary" %>
- </div>
+ <%= tag.p @series.trend.percent_formatted,
+ style: "color: #{@series.trend.color}",
+ class: "font-mono text-right text-xs font-medium text-primary" %>
+ </div>
+ <% end %>
<% end %>
diff --git a/app/views/accounts/sparkline.html.erb b/app/views/accounts/sparkline.html.erb
index 3a3316ce..ee7f743b 100644
--- a/app/views/accounts/sparkline.html.erb
+++ b/app/views/accounts/sparkline.html.erb
@@ -1,11 +1,13 @@
-<%= turbo_frame_tag dom_id(@account, :sparkline) do %>
- <div class="flex items-center justify-end gap-1">
- <div class="w-8 h-5">
- <%= render "shared/sparkline", id: dom_id(@account, :sparkline_chart), series: @account.sparkline_series %>
- </div>
+<% 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="w-8 h-5">
+ <%= render "shared/sparkline", id: dom_id(@account, :sparkline_chart), series: @account.sparkline_series %>
+ </div>
- <%= tag.p @account.sparkline_series.trend.percent_formatted,
- style: "color: #{@account.sparkline_series.trend.color}",
- class: "font-mono text-right text-xs font-medium text-primary" %>
- </div>
+ <%= tag.p @account.sparkline_series.trend.percent_formatted,
+ style: "color: #{@account.sparkline_series.trend.color}",
+ class: "font-mono text-right text-xs font-medium text-primary" %>
+ </div>
+ <% end %>
<% end %>