The personal finance app for everyone (by everyone)
Ruby
Stop SimpleFIN backfill on empty windows, not on payload growth (#3780)
Matrayuc2d83e777b06
diff --git a/app/models/simplefin_item.rb b/app/models/simplefin_item.rbindex bb1200eae5..a80e29d5fc 100644--- a/app/models/simplefin_item.rb+++ b/app/models/simplefin_item.rb@@ -45,6 +45,14 @@ class SimplefinItem < ApplicationRecord.uniqend+ # Accounts linked to a Sure account, via the legacy FK or AccountProvider.+ # Fresh query on purpose: the importer rewrites payloads through separate+ # SimplefinAccount instances, so a loaded simplefin_accounts association+ # would keep serving stale raw payloads during a run.+ def linked_simplefin_accounts+ simplefin_accounts.includes(:account, :linked_account).select { |sfa| sfa.current_account.present? }+ end+def import_latest_simplefin_data(sync: nil)SimplefinItem::Importer.new(self, simplefin_provider: simplefin_provider, sync: sync).importenddiff --git a/app/models/simplefin_item/importer.rb b/app/models/simplefin_item/importer.rbindex 2869c5fefc..914b9cafeb 100644--- a/app/models/simplefin_item/importer.rb+++ b/app/models/simplefin_item/importer.rb@@ -38,7 +38,7 @@ class SimplefinItem::Importer## Check for linked accounts via BOTH legacy FK (accounts.simplefin_account_id) AND# the new AccountProvider system. An account is "linked" if either association exists.- linked_accounts = simplefin_item.simplefin_accounts.select { |sfa| sfa.current_account.present? }+ linked_accounts = simplefin_item.linked_simplefin_accountsno_txns_yet = linked_accounts.any? && linked_accounts.all? { |sfa| sfa.raw_transactions_payload.blank? }if simplefin_item.last_synced_at.nil? || no_txns_yet@@ -416,6 +416,9 @@ class SimplefinItem::Importer# chunked window would otherwise filter out newly added, inactive accounts.perform_account_discovery+ # Accounts found during the walk are created unlinked, so the set is fixed here.+ linked_account_ids = simplefin_item.linked_simplefin_accounts.map(&:account_id).to_set+total_accounts_imported = 0chunk_count = 0consecutive_empty_chunks = 0@@ -483,15 +486,28 @@ class SimplefinItem::Importerendend- # Count new transactions in this chunk (adaptive stopping)+ # Count new transactions in this chunk (reported in stats)post_chunk_tx_count = count_linked_transactionsnew_txns_in_chunk = [ post_chunk_tx_count - pre_chunk_tx_count, 0 ].maxtotal_new_transactions += new_txns_in_chunk- Rails.logger.info "SimpleFin chunked sync: chunk #{chunk_count} added #{new_txns_in_chunk} new transactions"+ # Adaptive stopping is driven by what SimpleFIN returned for this window,+ # not by growth of the stored payloads: rows already stored (for example+ # by account discovery) come back again and would read as no growth.+ # Only settled rows dated inside the window count, and only for linked+ # accounts, so pending rows, out-of-window rows and accounts the user+ # skipped cannot keep the walk going.+ window = chunk_start_date.to_i..chunk_end_date.to_i+ returned_txns_in_chunk = accounts_data[:accounts].to_a.sum do |a|+ next 0 unless linked_account_ids.include?(a[:id].to_s)++ a[:transactions].to_a.count { |t| settled_in_window?(t, window) }+ end++ Rails.logger.info "SimpleFin chunked sync: chunk #{chunk_count} returned #{returned_txns_in_chunk} transactions, #{new_txns_in_chunk} new"- # Adaptive stopping: if chunk returned no new transactions, increment counter- if new_txns_in_chunk.zero?+ # Adaptive stopping: if the window returned no history, increment counter+ if returned_txns_in_chunk.zero?consecutive_empty_chunks += 1# Stop after 3 consecutive empty chunks (allow for gaps in bank data)if consecutive_empty_chunks >= 3@@ -526,11 +542,20 @@ class SimplefinItem::ImporterRails.logger.info "SimpleFin chunked sync completed: #{chunk_count} chunks processed, #{total_accounts_imported} account records, #{total_new_transactions} new transactions#{stopped_early ? " (stopped early)" : ""}"end- # Count total transactions in linked SimpleFIN accounts (for adaptive chunking)+ # Count total transactions in linked SimpleFIN accounts (for chunked sync stats)def count_linked_transactions- simplefin_item.simplefin_accounts- .select { |sfa| sfa.current_account.present? }- .sum { |sfa| sfa.raw_transactions_payload.to_a.size }+ simplefin_item.linked_simplefin_accounts.sum { |sfa| sfa.raw_transactions_payload.to_a.size }+ end++ # Whether a returned transaction is settled history dated inside the window.+ # Pending detection is shared with the entry processor. Dating falls back to+ # transacted_at because some providers omit posted on settled transactions.+ def settled_in_window?(transaction, window)+ return false if SimplefinEntry::Processor.pending?(transaction)++ time = Simplefin::DateUtils.parse_provider_time(transaction[:posted]) ||+ Simplefin::DateUtils.parse_provider_time(transaction[:transacted_at])+ time.present? && window.cover?(time.to_i)enddef import_regular_syncdiff --git a/test/models/simplefin_item/importer_chunked_history_test.rb b/test/models/simplefin_item/importer_chunked_history_test.rbnew file mode 100644index 0000000000..317a797327--- /dev/null+++ b/test/models/simplefin_item/importer_chunked_history_test.rb@@ -0,0 +1,219 @@+require "test_helper"++# The chunked backfill walks back in 60-day windows and stops after two+# consecutive windows with no history. Whether a window "had history" must come+# from what SimpleFIN returned for the linked accounts in it, not from how much+# the stored payloads grew: windows that re-fetch rows already stored (for+# example by account discovery) would otherwise read as empty.+class SimplefinItem::ImporterChunkedHistoryTest < ActiveSupport::TestCase+ setup do+ @family = families(:dylan_family)+ @item = SimplefinItem.create!(+ family: @family,+ name: "SimpleFIN Chunked History Test",+ access_url: "https://example.com/access"+ )+ @checking = link_account("sf_checking", "Checking", accounts(:depository))+ @importer = SimplefinItem::Importer.new(@item, simplefin_provider: nil)+ @importer.stubs(:perform_account_discovery)+ @item.stubs(:upsert_simplefin_snapshot!)+ end++ test "import keeps walking back and reports the transactions it stored" do+ # The linked account starts empty, so the public entry point chooses the+ # chunked path; three windows of history, then nothing older.+ @item.stubs(:last_synced_at).returns(2.days.ago)+ stub_windows(+ [ tx("t1", 10.days.ago) ],+ [ tx("t2", 70.days.ago) ],+ [ tx("t3", 130.days.ago) ],+ [],+ []+ )++ @importer.import++ history = @importer.send(:stats)["chunked_history"]+ assert_equal 5, history["chunks_processed"]+ assert_equal "no_new_data", history["reason"]+ assert_equal 3, history["total_new_transactions"]+ assert_equal %w[t1 t2 t3], stored_transaction_ids+ end++ test "an account linked through AccountProvider counts as linked" do+ # Linking to an existing account creates only an AccountProvider row, with+ # no legacy simplefin_account_id on the account.+ accounts(:depository).update!(simplefin_account_id: nil)+ AccountProvider.create!(account: accounts(:depository), provider: @checking)+ @item.stubs(:last_synced_at).returns(2.days.ago)+ stub_windows(+ [ tx("t1", 10.days.ago) ],+ [ tx("t2", 70.days.ago) ],+ [],+ []+ )++ @importer.import++ history = @importer.send(:stats)["chunked_history"]+ assert_equal 4, history["chunks_processed"]+ assert_equal 2, history["total_new_transactions"]+ end++ test "keeps walking back when the first windows re-fetch transactions already stored" do+ # Discovery (or an earlier sync) already stored the two most recent windows.+ @checking.update!(raw_transactions_payload: [ tx("t1", 10.days.ago), tx("t2", 70.days.ago) ])++ stub_windows(+ [ tx("t1", 10.days.ago) ],+ [ tx("t2", 70.days.ago) ],+ [ tx("t3", 130.days.ago) ],+ [],+ []+ )++ @importer.send(:import_with_chunked_history)++ assert_equal 5, @importer.send(:stats)["chunked_history"]["chunks_processed"]+ assert_includes stored_transaction_ids, "t3"+ end++ test "stops after two consecutive windows that return no history" do+ stub_windows([ tx("t1", 10.days.ago) ], [], [], [ tx("t4", 190.days.ago) ])++ @importer.send(:import_with_chunked_history)++ history = @importer.send(:stats)["chunked_history"]+ assert_equal 3, history["chunks_processed"]+ assert history["stopped_early"]+ assert_equal %w[t1], stored_transaction_ids+ end++ test "counts history from every linked account in a window, not just the first" do+ link_account("sf_savings", "Savings", accounts(:investment))+ quiet = ->(txns) { { id: "sf_savings", name: "Savings", currency: "USD", balance: "5.00", "balance-date": Time.current.to_i, transactions: txns } }+ busy = ->(txns) { { id: "sf_checking", name: "Checking", currency: "USD", balance: "100.00", "balance-date": Time.current.to_i, transactions: txns } }+ @importer.stubs(:fetch_accounts_data).returns(+ { accounts: [ quiet.call([]), busy.call([ tx("t1", 10.days.ago) ]) ] },+ { accounts: [ quiet.call([]), busy.call([ tx("t2", 70.days.ago) ]) ] },+ { accounts: [ quiet.call([]), busy.call([ tx("t3", 130.days.ago) ]) ] },+ { accounts: [ quiet.call([]), busy.call([]) ] },+ { accounts: [ quiet.call([]), busy.call([]) ] }+ )++ @importer.send(:import_with_chunked_history)++ assert_equal %w[t1 t2 t3], stored_transaction_ids+ end++ test "history from an account that is not linked does not extend the walk" do+ # The linked account is quiet; an unlinked (skipped) account has history in every window.+ @item.simplefin_accounts.create!(name: "Unlinked", account_id: "sf_unlinked", account_type: "checking", currency: "USD", current_balance: 1)+ responses = 6.times.map do |i|+ { accounts: [+ { id: "sf_checking", name: "Checking", currency: "USD", balance: "100.00", "balance-date": Time.current.to_i, transactions: [] },+ { id: "sf_unlinked", name: "Unlinked", currency: "USD", balance: "1.00", "balance-date": Time.current.to_i, transactions: [ tx("u#{i}", (10 + 60 * i).days.ago) ] }+ ] }+ end+ @importer.stubs(:fetch_accounts_data).returns(*responses)++ @importer.send(:import_with_chunked_history)++ history = @importer.send(:stats)["chunked_history"]+ assert_equal 2, history["chunks_processed"]+ assert_equal "no_new_data", history["reason"]+ end++ test "settled rows dated outside the window do not count as history" do+ # A bridge that returns the same old row for every request must not keep the walk going.+ old = tx("old", 400.days.ago)+ stub_windows([ tx("t1", 10.days.ago), old ], [ old ], [ old ], [ old ], [ old ], [ old ])++ @importer.send(:import_with_chunked_history)++ history = @importer.send(:stats)["chunked_history"]+ assert_equal 3, history["chunks_processed"]+ assert_equal "no_new_data", history["reason"]+ end++ test "pending rows returned with every window do not keep the walk going" do+ pending = { id: "p1", posted: 0, amount: "-5.00", description: "Pending", pending: true }+ stub_windows(+ [ tx("t1", 10.days.ago), pending ],+ [ pending ],+ [ pending ],+ [ tx("t4", 190.days.ago), pending ]+ )++ @importer.send(:import_with_chunked_history)++ history = @importer.send(:stats)["chunked_history"]+ assert_equal 3, history["chunks_processed"]+ assert history["stopped_early"]+ end++ test "settled rows without a posted date count toward the window of their transaction date" do+ # Some bridges omit posted on settled transactions and only send transacted_at.+ unposted = ->(id, time) { { id: id, transacted_at: time.to_i, amount: "-10.00", description: "Purchase #{id}" } }+ stub_windows(+ [ unposted.call("t1", 10.days.ago) ],+ [ unposted.call("t2", 70.days.ago) ],+ [ unposted.call("t3", 130.days.ago) ],+ [],+ []+ )++ @importer.send(:import_with_chunked_history)++ history = @importer.send(:stats)["chunked_history"]+ assert_equal 5, history["chunks_processed"]+ assert_equal %w[t1 t2 t3], stored_transaction_ids+ end++ test "rows flagged pending do not count as history even with an in-window posted date" do+ flagged = ->(id, time) { tx(id, time).merge(pending: true) }+ stub_windows(+ [ tx("t1", 10.days.ago) ],+ [ flagged.call("p2", 70.days.ago) ],+ [ flagged.call("p3", 130.days.ago) ],+ [ tx("t4", 190.days.ago) ]+ )++ @importer.send(:import_with_chunked_history)++ history = @importer.send(:stats)["chunked_history"]+ assert_equal 3, history["chunks_processed"]+ assert history["stopped_early"]+ end++ private+ def link_account(account_id, name, account)+ sfa = @item.simplefin_accounts.create!(+ name: name, account_id: account_id, account_type: "checking",+ currency: "USD", current_balance: 100+ )+ account.update!(simplefin_account_id: sfa.id)+ sfa+ end++ def stub_windows(*windows)+ responses = windows.map do |transactions|+ {+ accounts: [+ { id: "sf_checking", name: "Checking", currency: "USD", balance: "100.00",+ "balance-date": Time.current.to_i, transactions: transactions }+ ]+ }+ end+ @importer.stubs(:fetch_accounts_data).returns(*responses)+ end++ def tx(id, time)+ { id: id, posted: time.to_i, amount: "-10.00", description: "Purchase #{id}" }+ end++ def stored_transaction_ids+ @item.simplefin_accounts.find_by!(account_id: "sf_checking")+ .raw_transactions_payload.map { |t| t.with_indifferent_access[:id] }.sort+ end+end