Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix: exclude stale provider holdings from investment views (#3484)

Howard Wu36cd7e999b8f
diff --git a/app/models/holding/current_for_investment_accounts.rb b/app/models/holding/current_for_investment_accounts.rb
new file mode 100644
index 0000000000..ee96168e20
--- /dev/null
+++ b/app/models/holding/current_for_investment_accounts.rb
@@ -0,0 +1,50 @@
+class Holding::CurrentForInvestmentAccounts
+ CURRENT_HOLDINGS_SQL = <<~SQL.squish.freeze
+ (
+ holdings.account_provider_id IS NOT NULL
+ AND holdings.updated_at::date = (
+ SELECT MAX(provider_holdings.updated_at::date)
+ FROM holdings provider_holdings
+ WHERE provider_holdings.account_id = holdings.account_id
+ AND provider_holdings.account_provider_id IS NOT NULL
+ )
+ ) OR (
+ NOT EXISTS (
+ SELECT 1
+ FROM holdings provider_holdings
+ WHERE provider_holdings.account_id = holdings.account_id
+ AND provider_holdings.account_provider_id IS NOT NULL
+ )
+ AND holdings.currency = (
+ SELECT accounts.currency
+ FROM accounts
+ WHERE accounts.id = holdings.account_id
+ )
+ AND holdings.id = (
+ SELECT latest_holdings.id
+ FROM holdings latest_holdings
+ WHERE latest_holdings.account_id = holdings.account_id
+ AND latest_holdings.security_id = holdings.security_id
+ AND latest_holdings.currency = holdings.currency
+ ORDER BY latest_holdings.date DESC
+ LIMIT 1
+ )
+ )
+ SQL
+
+ def initialize(account_ids)
+ @account_ids = account_ids
+ end
+
+ def relation
+ return Holding.none if account_ids.empty?
+
+ Holding
+ .where(account_id: account_ids)
+ .where.not(qty: 0)
+ .where(Arel.sql(CURRENT_HOLDINGS_SQL))
+ end
+
+ private
+ attr_reader :account_ids
+end
diff --git a/app/models/investment_statement.rb b/app/models/investment_statement.rb
index 66c8d58144..96d52b8d17 100644
--- a/app/models/investment_statement.rb
+++ b/app/models/investment_statement.rb
@@ -69,25 +69,10 @@ class InvestmentStatement
# and day_change each call this, so an unmemoized version ran the same
# DISTINCT ON query up to 5x per dashboard/report request.
def current_holdings
- @current_holdings ||= begin
- account_ids = investment_account_ids
-
- if account_ids.any?
- # Get the latest holding for each security per account
- Holding
- .where(account_id: account_ids)
- .where.not(qty: 0)
- .where(
- id: Holding
- .where(account_id: account_ids)
- .select("DISTINCT ON (holdings.account_id, holdings.security_id) holdings.id")
- .order(Arel.sql("holdings.account_id, holdings.security_id, holdings.date DESC"))
- )
- .includes(:security, :account)
- else
- Holding.none
- end
- end
+ @current_holdings ||= Holding::CurrentForInvestmentAccounts
+ .new(investment_account_ids)
+ .relation
+ .includes(:security, :account)
end
# Top holdings by family-currency value
diff --git a/test/models/holding/current_for_investment_accounts_test.rb b/test/models/holding/current_for_investment_accounts_test.rb
new file mode 100644
index 0000000000..6e93c063b3
--- /dev/null
+++ b/test/models/holding/current_for_investment_accounts_test.rb
@@ -0,0 +1,57 @@
+require "test_helper"
+
+class Holding::CurrentForInvestmentAccountsTest < ActiveSupport::TestCase
+ setup do
+ @family = families(:empty)
+ @account = @family.accounts.create!(
+ name: "Investment",
+ balance: 2100,
+ currency: "USD",
+ accountable: Investment.new
+ )
+ end
+
+ test "returns the latest provider import day while preserving per-security price dates" do
+ coinstats_item = @family.coinstats_items.create!(name: "CoinStats", api_key: "test-key")
+ coinstats_account = coinstats_item.coinstats_accounts.create!(name: "Brokerage", currency: "USD")
+ account_provider = AccountProvider.create!(account: @account, provider: coinstats_account)
+
+ current_holding = create_holding("AAPL", date: Date.current, account_provider: account_provider)
+ stale_holding = create_holding("STALE", date: 1.day.ago.to_date, account_provider: account_provider)
+ older_price_current_holding = create_holding("MSFT", date: 1.day.ago.to_date, account_provider: account_provider)
+ stale_holding.update_columns(created_at: 2.days.ago, updated_at: 2.days.ago)
+
+ assert_equal [ current_holding.id, older_price_current_holding.id ].sort,
+ described_query.pluck(:id).sort
+ end
+
+ test "returns the latest holding per security for a manual account" do
+ security = Security.create!(ticker: "AAPL", name: "Apple")
+ create_holding_for_security(security, date: 2.days.ago.to_date, qty: 1)
+ latest = create_holding_for_security(security, date: Date.current, qty: 2)
+
+ assert_equal [ latest.id ], described_query.pluck(:id)
+ end
+
+ private
+ def described_query
+ Holding::CurrentForInvestmentAccounts.new([ @account.id ]).relation
+ end
+
+ def create_holding(ticker, date:, account_provider: nil)
+ security = Security.create!(ticker: ticker, name: ticker)
+ create_holding_for_security(security, date: date, account_provider: account_provider)
+ end
+
+ def create_holding_for_security(security, date:, qty: 1, account_provider: nil)
+ @account.holdings.create!(
+ security: security,
+ date: date,
+ qty: qty,
+ price: 100,
+ amount: qty * 100,
+ currency: "USD",
+ account_provider: account_provider
+ )
+ end
+end
diff --git a/test/models/investment_statement_test.rb b/test/models/investment_statement_test.rb
index 232eecc7ab..dbf27a8428 100644
--- a/test/models/investment_statement_test.rb
+++ b/test/models/investment_statement_test.rb
@@ -284,9 +284,9 @@ class InvestmentStatementTest < ActiveSupport::TestCase
@statement.day_change
end
- holdings_queries = queries.grep(/DISTINCT ON \(holdings\.account_id, holdings\.security_id\)/)
+ holdings_queries = queries.grep(/MAX\(provider_holdings\.updated_at::date\)/)
assert_equal 1, holdings_queries.size,
- "current_holdings should only run its DISTINCT ON query once per instance, not once per caller"
+ "current_holdings should only run its holdings query once per instance, not once per caller"
end
test "current_holdings memoizes the empty (no investment accounts) case too" do