The personal finance app for everyone (by everyone)
Ruby
perf(api): avoid transfer lookups in transaction index (#2127)
ghost4224ff717ce9
diff --git a/app/views/api/v1/transactions/_transaction.json.jbuilder b/app/views/api/v1/transactions/_transaction.json.jbuilderindex 488ecbe5b8..aa8674822a 100644--- a/app/views/api/v1/transactions/_transaction.json.jbuilder+++ b/app/views/api/v1/transactions/_transaction.json.jbuilder@@ -58,19 +58,24 @@ json.tags transaction.tags do |tag|end# Transfer information (if this transaction is part of a transfer)-if transaction.transfer.present?+transfer = transaction.transfer+if transfer.present?json.transfer do- json.id transaction.transfer.id- json.amount transaction.transfer.amount_abs.format- json.currency transaction.transfer.inflow_transaction.entry.currency+ json.id transfer.id# Other transaction in the transfer- if transaction.transfer.inflow_transaction == transaction- other_transaction = transaction.transfer.outflow_transaction+ if transfer.inflow_transaction_id == transaction.id+ inflow_transaction = transaction+ other_transaction = transfer.outflow_transactionelse- other_transaction = transaction.transfer.inflow_transaction+ inflow_transaction = transfer.inflow_transaction+ # When rendering the outflow, the inflow is the counterparty transaction.+ other_transaction = inflow_transactionend+ json.amount inflow_transaction.entry.amount_money.abs.format+ json.currency inflow_transaction.entry.currency+if other_transaction.present?json.other_account dojson.id other_transaction.entry.account.iddiff --git a/test/controllers/api/v1/transactions_controller_test.rb b/test/controllers/api/v1/transactions_controller_test.rbindex 1f5123d9ed..ce7398e3cd 100644--- a/test/controllers/api/v1/transactions_controller_test.rb+++ b/test/controllers/api/v1/transactions_controller_test.rb@@ -723,28 +723,7 @@ endendtest "transactions with transfers should include transfer information" do- # Create a transfer between two accounts to test transfer rendering- from_account = @family.accounts.create!(- name: "Transfer From Account",- balance: 1000,- currency: "USD",- accountable: Depository.new- )-- to_account = @family.accounts.create!(- name: "Transfer To Account",- balance: 0,- currency: "USD",- accountable: Depository.new- )-- transfer = Transfer::Creator.new(- family: @family,- source_account_id: from_account.id,- destination_account_id: to_account.id,- date: Date.current,- amount: 100- ).create+ transfer = create_transfer_between_accountsget api_v1_transaction_url(transfer.inflow_transaction), headers: api_headers(@api_key)assert_response :success@@ -757,12 +736,74 @@ endassert transaction_data["transfer"].key?("other_account")end+ test "index renders transfer rows without per-transfer transaction lookups" do+ transfer = create_transfer_between_accounts++ queries = capture_sql_queries do+ get api_v1_transactions_url,+ params: { per_page: 100 },+ headers: api_headers(@api_key)+ end++ assert_response :success++ response_data = JSON.parse(response.body)+ transfer_transaction_ids = [ transfer.inflow_transaction_id, transfer.outflow_transaction_id ]+ transfer_rows = response_data["transactions"].select { |transaction| transfer_transaction_ids.include?(transaction["id"]) }++ assert_equal 2, transfer_rows.size+ assert transfer_rows.all? { |transaction| transaction["transfer"].present? }+ assert_empty queries.grep(/SELECT "transactions"\.\* FROM "transactions" WHERE "transactions"\."id" =/)+ assert_empty queries.grep(/SELECT "entries"\.\* FROM "entries" WHERE "entries"\."id" =/)+ assert_empty queries.grep(/SELECT "accounts"\.\* FROM "accounts" WHERE "accounts"\."id" =/)+ end+privatedef api_headers(api_key){ "X-Api-Key" => api_key.display_key }end+ def create_transfer_between_accounts+ from_account = @family.accounts.create!(+ name: "Transfer From Account",+ balance: 1000,+ currency: "USD",+ accountable: Depository.new+ )++ to_account = @family.accounts.create!(+ name: "Transfer To Account",+ balance: 0,+ currency: "USD",+ accountable: Depository.new+ )++ Transfer::Creator.new(+ family: @family,+ source_account_id: from_account.id,+ destination_account_id: to_account.id,+ date: Date.current,+ amount: 100+ ).create+ end++ def capture_sql_queries+ queries = []+ callback = lambda do |_name, _started, _finished, _unique_id, payload|+ next if payload[:cached]+ next if %w[SCHEMA TRANSACTION].include?(payload[:name])++ queries << payload[:sql].squish+ end++ ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do+ yield+ end++ queries+ end+# Validates agent-friendly numeric fields: type, sign invariantsdef assert_amount_cents_fields(txn_json)assert txn_json.key?("amount_cents"), "Expected amount_cents field"