diff --git a/app/jobs/sync_market_data_job.rb b/app/jobs/import_market_data_job.rb similarity index 95% rename from app/jobs/sync_market_data_job.rb rename to app/jobs/import_market_data_job.rb index 9580a467..294d5836 100644 --- a/app/jobs/sync_market_data_job.rb +++ b/app/jobs/import_market_data_job.rb @@ -7,7 +7,7 @@ # Each individual account sync will still fetch any missing market data that isn't yet synced, but by running # this job daily, we significantly reduce overlapping account syncs that both need the same market data (e.g. common security like `AAPL`) # -class SyncMarketDataJob < ApplicationJob +class ImportMarketDataJob < ApplicationJob queue_as :scheduled def perform(opts) diff --git a/app/models/account/market_data_importer.rb b/app/models/account/market_data_importer.rb index b344c1cb..af538314 100644 --- a/app/models/account/market_data_importer.rb +++ b/app/models/account/market_data_importer.rb @@ -33,7 +33,7 @@ class Account::MarketDataImporter end pair_dates.each do |(source, target), start_date| - ExchangeRate.sync_provider_rates( + ExchangeRate.import_provider_rates( from: source, to: target, start_date: start_date, @@ -50,12 +50,12 @@ class Account::MarketDataImporter return if account_securities.empty? account_securities.each do |security| - security.sync_provider_prices( + security.import_provider_prices( start_date: first_required_price_date(security), end_date: Date.current ) - security.sync_provider_details + security.import_provider_details end end diff --git a/app/models/account/syncer.rb b/app/models/account/syncer.rb index f5c12175..ab198a95 100644 --- a/app/models/account/syncer.rb +++ b/app/models/account/syncer.rb @@ -7,8 +7,8 @@ class Account::Syncer def perform_sync(sync) Rails.logger.info("Processing balances (#{account.linked? ? 'reverse' : 'forward'})") - sync_market_data - sync_balances + import_market_data + materialize_balances end def perform_post_sync @@ -16,9 +16,9 @@ class Account::Syncer end private - def sync_balances + def materialize_balances strategy = account.linked? ? :reverse : :forward - Balance::Syncer.new(account, strategy: strategy).sync_balances + Balance::Materializer.new(account, strategy: strategy).materialize_balances end # Syncs all the exchange rates + security prices this account needs to display historical chart data @@ -28,7 +28,7 @@ class Account::Syncer # # We rescue errors here because if this operation fails, we don't want to fail the entire sync since # we have reasonable fallbacks for missing market data. - def sync_market_data + def import_market_data Account::MarketDataImporter.new(account).import_all rescue => e Rails.logger.error("Error syncing market data for account #{account.id}: #{e.message}") diff --git a/app/models/exchange_rate/importer.rb b/app/models/exchange_rate/importer.rb index b55ea73b..133106bc 100644 --- a/app/models/exchange_rate/importer.rb +++ b/app/models/exchange_rate/importer.rb @@ -12,7 +12,7 @@ class ExchangeRate::Importer end # Constructs a daily series of rates for the given currency pair for date range - def sync_provider_rates + def import_provider_rates if !clear_cache && all_rates_exist? Rails.logger.info("No new rates to sync for #{from} to #{to} between #{start_date} and #{end_date}, skipping") return diff --git a/app/models/exchange_rate/provided.rb b/app/models/exchange_rate/provided.rb index 1d111a49..defee421 100644 --- a/app/models/exchange_rate/provided.rb +++ b/app/models/exchange_rate/provided.rb @@ -28,9 +28,9 @@ module ExchangeRate::Provided end # @return [Integer] The number of exchange rates synced - def sync_provider_rates(from:, to:, start_date:, end_date:, clear_cache: false) + def import_provider_rates(from:, to:, start_date:, end_date:, clear_cache: false) unless provider.present? - Rails.logger.warn("No provider configured for ExchangeRate.sync_provider_rates") + Rails.logger.warn("No provider configured for ExchangeRate.import_provider_rates") return 0 end @@ -41,7 +41,7 @@ module ExchangeRate::Provided start_date: start_date, end_date: end_date, clear_cache: clear_cache - ).sync_provider_rates + ).import_provider_rates end end end diff --git a/app/models/market_data_importer.rb b/app/models/market_data_importer.rb index a16eb515..9eaf5964 100644 --- a/app/models/market_data_importer.rb +++ b/app/models/market_data_importer.rb @@ -23,13 +23,13 @@ class MarketDataImporter end Security.where.not(exchange_operating_mic: nil).find_each do |security| - security.sync_provider_prices( + security.import_provider_prices( start_date: get_first_required_price_date(security), end_date: end_date, clear_cache: clear_cache ) - security.sync_provider_details(clear_cache: clear_cache) + security.import_provider_details(clear_cache: clear_cache) end end @@ -43,7 +43,7 @@ class MarketDataImporter # pair is a Hash with keys :source, :target, and :start_date start_date = snapshot? ? default_start_date : pair[:start_date] - ExchangeRate.sync_provider_rates( + ExchangeRate.import_provider_rates( from: pair[:source], to: pair[:target], start_date: start_date, diff --git a/app/models/security/price/importer.rb b/app/models/security/price/importer.rb index c632867f..4e6bee2f 100644 --- a/app/models/security/price/importer.rb +++ b/app/models/security/price/importer.rb @@ -12,7 +12,7 @@ class Security::Price::Importer # Constructs a daily series of prices for a single security over the date range. # Returns the number of rows upserted. - def sync_provider_prices + def import_provider_prices if !clear_cache && all_prices_exist? Rails.logger.info("No new prices to sync for #{security.ticker} between #{start_date} and #{end_date}, skipping") return 0 diff --git a/app/models/security/provided.rb b/app/models/security/provided.rb index e5acf5b2..2fdcc607 100644 --- a/app/models/security/provided.rb +++ b/app/models/security/provided.rb @@ -49,9 +49,9 @@ module Security::Provided price end - def sync_provider_details(clear_cache: false) + def import_provider_details(clear_cache: false) unless provider.present? - Rails.logger.warn("No provider configured for Security.sync_provider_details") + Rails.logger.warn("No provider configured for Security.import_provider_details") return end @@ -76,9 +76,9 @@ module Security::Provided end end - def sync_provider_prices(start_date:, end_date:, clear_cache: false) + def import_provider_prices(start_date:, end_date:, clear_cache: false) unless provider.present? - Rails.logger.warn("No provider configured for Security.sync_provider_prices") + Rails.logger.warn("No provider configured for Security.import_provider_prices") return 0 end @@ -88,7 +88,7 @@ module Security::Provided start_date: start_date, end_date: end_date, clear_cache: clear_cache - ).sync_provider_prices + ).import_provider_prices end private diff --git a/config/schedule.yml b/config/schedule.yml index 8eb8ef0a..28078e4d 100644 --- a/config/schedule.yml +++ b/config/schedule.yml @@ -1,8 +1,8 @@ -sync_market_data: +import_market_data: cron: "0 22 * * 1-5" # 5:00 PM EST / 6:00 PM EDT (NY time) - class: "SyncMarketDataJob" + class: "ImportMarketDataJob" queue: "scheduled" - description: "Syncs market data daily at 5:00 PM EST (1 hour after market close)" + description: "Imports market data daily at 5:00 PM EST (1 hour after market close)" args: mode: "full" clear_cache: false diff --git a/test/models/account/market_data_importer_test.rb b/test/models/account/market_data_importer_test.rb index c4eea68e..74c42d36 100644 --- a/test/models/account/market_data_importer_test.rb +++ b/test/models/account/market_data_importer_test.rb @@ -32,7 +32,7 @@ class Account::MarketDataImporterTest < ActiveSupport::TestCase accountable: Depository.new ) - # Seed a rate for the first required day so that the syncer only needs the next day forward + # Seed a rate for the first required day so that the importer only needs the next day forward existing_date = account.start_date ExchangeRate.create!(from_currency: "CAD", to_currency: "USD", date: existing_date, rate: 2.0) diff --git a/test/models/exchange_rate/importer_test.rb b/test/models/exchange_rate/importer_test.rb index 90f6e7b7..dab40fa8 100644 --- a/test/models/exchange_rate/importer_test.rb +++ b/test/models/exchange_rate/importer_test.rb @@ -27,7 +27,7 @@ class ExchangeRate::ImporterTest < ActiveSupport::TestCase to: "EUR", start_date: 2.days.ago.to_date, end_date: Date.current - ).sync_provider_rates + ).import_provider_rates db_rates = ExchangeRate.where(from_currency: "USD", to_currency: "EUR", date: 2.days.ago.to_date..Date.current) .order(:date) @@ -59,7 +59,7 @@ class ExchangeRate::ImporterTest < ActiveSupport::TestCase to: "EUR", start_date: 3.days.ago.to_date, end_date: Date.current - ).sync_provider_rates + ).import_provider_rates db_rates = ExchangeRate.order(:date) assert_equal 4, db_rates.count @@ -81,7 +81,7 @@ class ExchangeRate::ImporterTest < ActiveSupport::TestCase to: "EUR", start_date: 3.days.ago.to_date, end_date: Date.current - ).sync_provider_rates + ).import_provider_rates end # A helpful "reset" option for when we need to refresh provider data @@ -110,7 +110,7 @@ class ExchangeRate::ImporterTest < ActiveSupport::TestCase start_date: 2.days.ago.to_date, end_date: Date.current, clear_cache: true - ).sync_provider_rates + ).import_provider_rates db_rates = ExchangeRate.where(from_currency: "USD", to_currency: "EUR").order(:date) assert_equal [ 1.3, 1.4, 1.5 ], db_rates.map(&:rate) @@ -135,7 +135,7 @@ class ExchangeRate::ImporterTest < ActiveSupport::TestCase to: "EUR", start_date: Date.current, end_date: future_date - ).sync_provider_rates + ).import_provider_rates assert_equal 1, ExchangeRate.count end diff --git a/test/models/security/price/importer_test.rb b/test/models/security/price/importer_test.rb index 9e769d08..665a91f6 100644 --- a/test/models/security/price/importer_test.rb +++ b/test/models/security/price/importer_test.rb @@ -28,7 +28,7 @@ class Security::Price::ImporterTest < ActiveSupport::TestCase security_provider: @provider, start_date: 2.days.ago.to_date, end_date: Date.current - ).sync_provider_prices + ).import_provider_prices db_prices = Security::Price.where(security: @security, date: 2.days.ago.to_date..Date.current).order(:date) @@ -57,7 +57,7 @@ class Security::Price::ImporterTest < ActiveSupport::TestCase security_provider: @provider, start_date: 3.days.ago.to_date, end_date: Date.current - ).sync_provider_prices + ).import_provider_prices db_prices = Security::Price.where(security: @security).order(:date) assert_equal 4, db_prices.count @@ -78,7 +78,7 @@ class Security::Price::ImporterTest < ActiveSupport::TestCase security_provider: @provider, start_date: 3.days.ago.to_date, end_date: Date.current - ).sync_provider_prices + ).import_provider_prices end test "full upsert if clear_cache is true" do @@ -106,7 +106,7 @@ class Security::Price::ImporterTest < ActiveSupport::TestCase start_date: 2.days.ago.to_date, end_date: Date.current, clear_cache: true - ).sync_provider_prices + ).import_provider_prices db_prices = Security::Price.where(security: @security).order(:date) assert_equal [ 150, 155, 160 ], db_prices.map(&:price) @@ -131,7 +131,7 @@ class Security::Price::ImporterTest < ActiveSupport::TestCase security_provider: @provider, start_date: Date.current, end_date: future_date - ).sync_provider_prices + ).import_provider_prices assert_equal 1, Security::Price.count end