The personal finance app for everyone (by everyone)
Ruby
Key account sparklines on the account's own data (#4675)
Nikolay Kopylovc712b6a39792
diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rbindex 9b45de96b1..7eb9dc39f4 100644--- a/app/controllers/accounts_controller.rb+++ b/app/controllers/accounts_controller.rb@@ -234,11 +234,9 @@ class AccountsController < ApplicationControllerenddef sparkline- etag_key = @account.family.build_cache_key("#{@account.id}_sparkline_#{Account::Chartable::SPARKLINE_CACHE_VERSION}", invalidate_on_data_updates: true)-# Short-circuit with 304 Not Modified when the client already has the latest version.# We defer the expensive series computation until we know the content is stale.- if stale?(etag: etag_key, last_modified: @account.family.latest_sync_completed_at)+ if stale?(etag: @account.sparkline_cache_key)@sparkline_series = @account.sparkline_seriesrender layout: falseenddiff --git a/app/models/account/chartable.rb b/app/models/account/chartable.rbindex 12a804b4a2..12e7944ca2 100644--- a/app/models/account/chartable.rb+++ b/app/models/account/chartable.rb@@ -27,14 +27,27 @@ module Account::Chartableenddef sparkline_series- cache_key = family.build_cache_key("#{id}_sparkline_#{SPARKLINE_CACHE_VERSION}", invalidate_on_data_updates: true)-- Rails.cache.fetch(cache_key, expires_in: 24.hours) do+ Rails.cache.fetch(sparkline_cache_key, expires_in: 24.hours) dobalance_seriesendend+ # Built from this account's own data, so a sync of another account keeps it cached.+ # Investment series are normalized from holdings and entries that can change outside+ # this account's syncs, so those keep the family-wide sync timestamp.+ def sparkline_cache_key+ @sparkline_cache_key ||= [+ "account_sparkline", SPARKLINE_CACHE_VERSION, id, updated_at.to_f, last_synced_at&.to_f,+ rows_version(balances), rows_version(account_providers), Date.current,+ (family.latest_sync_completed_at&.to_f if balance_type == :investment)+ ].join("_")+ end+private+ def rows_version(scope)+ scope.pick(Arel.sql("count(*)"), Arel.sql("max(#{scope.table_name}.updated_at)")).map { |v| v&.to_f }.join("-")+ end+def normalize_linked_investment_series(series, view: :balance)Balance::LinkedInvestmentSeriesNormalizer.new(account: self, series: series, view: view).normalizeenddiff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rbindex 2b994573ef..abb2e5dd38 100644--- a/test/controllers/accounts_controller_test.rb+++ b/test/controllers/accounts_controller_test.rb@@ -1227,6 +1227,75 @@ class AccountsControllerTest < ActionDispatch::IntegrationTestassert_response :successend+ test "sparkline stays fresh when another account syncs" do+ get sparkline_account_url(@account)+ etag = response.headers["ETag"]++ @account.family.touch(:latest_sync_completed_at)+ get sparkline_account_url(@account), headers: { "If-None-Match" => etag }++ assert_response :not_modified+ end++ test "sparkline is re-rendered when the account's balances change" do+ Rails.stubs(:cache).returns(ActiveSupport::Cache::MemoryStore.new)+ get sparkline_account_url(@account)+ etag = response.headers["ETag"]+ before = response.body++ travel 1.minute do+ @account.balances.create!(date: 3.days.ago.to_date, balance: 1234, start_cash_balance: 1234, currency: @account.currency)+ get sparkline_account_url(@account), headers: { "If-None-Match" => etag }+ end++ assert_response :success+ assert_not_equal before, response.body+ end++ test "sparkline is re-rendered after the account's own sync completes" do+ get sparkline_account_url(@account)+ etag = response.headers["ETag"]++ travel 1.minute do+ @account.syncs.create!(status: "completed", completed_at: Time.current)+ get sparkline_account_url(@account), headers: { "If-None-Match" => etag }+ end++ assert_response :success+ end++ # AccountProvider rows don't touch the account, and swapping one keeps it linked.+ test "sparkline is re-rendered when the account's provider link changes" do+ AccountProvider.create!(account: @account, provider: plaid_accounts(:one))+ get sparkline_account_url(@account)+ etag = response.headers["ETag"]++ travel 1.minute do+ @account.account_providers.destroy_all+ other = PlaidAccount.create!(+ plaid_item: plaid_items(:one), plaid_id: "acc_mock_swap", name: "Swapped", plaid_type: "depository",+ currency: "USD", current_balance: 1+ )+ AccountProvider.create!(account: @account, provider: other)+ get sparkline_account_url(@account), headers: { "If-None-Match" => etag }+ end++ assert_response :success+ end++ test "an investment sparkline is re-rendered after any family sync" do+ investment = accounts(:investment)+ get sparkline_account_url(investment)+ etag = response.headers["ETag"]++ travel 1.minute do+ investment.family.touch(:latest_sync_completed_at)+ get sparkline_account_url(investment), headers: { "If-None-Match" => etag }+ end++ assert_response :success+ end+test "sparkline renders an empty series without a trend" doempty_series = Series.new(start_date: 1.day.ago.to_date,