mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-04 21:15:19 +02:00
Data provider simplification, tests, and documentation (#1997)
* Ignore env.test from source control * Simplification of providers interface * Synth tests * Update money to use new find rates method * Remove unused issues code * Additional issue feature removals * Update price data fetching and tests * Update documentation for providers * Security test fixes * Fix self host test * Update synth usage data access * Remove AI pr schema changes
This commit is contained in:
parent
dd75cadebc
commit
f65b93a352
95 changed files with 2014 additions and 1638 deletions
|
@ -1,28 +1,65 @@
|
|||
module Security::Provided
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
include Synthable
|
||||
|
||||
class_methods do
|
||||
def provider
|
||||
synth_client
|
||||
Providers.synth
|
||||
end
|
||||
|
||||
def search_provider(query)
|
||||
return [] if query[:search].blank? || query[:search].length < 2
|
||||
def search_provider(symbol, country_code: nil, exchange_operating_mic: nil)
|
||||
return [] if symbol.blank? || symbol.length < 2
|
||||
|
||||
response = provider.search_securities(
|
||||
query: query[:search],
|
||||
dataset: "limited",
|
||||
country_code: query[:country],
|
||||
exchange_operating_mic: query[:exchange_operating_mic]
|
||||
)
|
||||
response = provider.search_securities(symbol, country_code: country_code, exchange_operating_mic: exchange_operating_mic)
|
||||
|
||||
if response.success?
|
||||
response.securities.map { |attrs| new(**attrs) }
|
||||
response.data.securities
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def sync_provider_prices(start_date:, end_date: Date.current)
|
||||
unless has_prices?
|
||||
Rails.logger.warn("Security id=#{id} ticker=#{ticker} is not known by provider, skipping price sync")
|
||||
return 0
|
||||
end
|
||||
|
||||
unless provider.present?
|
||||
Rails.logger.warn("No security provider configured, cannot sync prices for id=#{id} ticker=#{ticker}")
|
||||
return 0
|
||||
end
|
||||
|
||||
response = provider.fetch_security_prices(self, start_date: start_date, end_date: end_date)
|
||||
|
||||
unless response.success?
|
||||
Rails.logger.error("Provider error for sync_provider_prices with id=#{id} ticker=#{ticker}: #{response.error}")
|
||||
return 0
|
||||
end
|
||||
|
||||
fetched_prices = response.data.prices.map do |price|
|
||||
price.attributes.slice("security_id", "date", "price", "currency")
|
||||
end
|
||||
|
||||
Security::Price.upsert_all(fetched_prices, unique_by: %i[security_id date currency])
|
||||
end
|
||||
|
||||
def find_or_fetch_price(date: Date.current, cache: true)
|
||||
price = prices.find_by(date: date)
|
||||
|
||||
return price if price.present?
|
||||
|
||||
response = provider.fetch_security_price(self, date: date)
|
||||
|
||||
return nil unless response.success? # Provider error
|
||||
|
||||
price = response.data.price
|
||||
price.save! if cache
|
||||
price
|
||||
end
|
||||
|
||||
private
|
||||
def provider
|
||||
self.class.provider
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue