1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-09 07:25:19 +02:00

Rename app/sidekiq to app/jobs

This commit is contained in:
Claude Ayitey 2024-01-06 14:47:35 +00:00
parent 1e1c9c2481
commit a7667ba29a
14 changed files with 27 additions and 41 deletions

View file

@ -1,5 +1,4 @@
class AskQuestionJob class AskQuestionJob
include Sidekiq::Job
def perform(conversation_id, reply_id) def perform(conversation_id, reply_id)
conversation = Conversation.find(conversation_id) conversation = Conversation.find(conversation_id)
@ -163,7 +162,7 @@ class AskQuestionJob
5. investing: the user has a specific question about investing and needs real-time data 5. investing: the user has a specific question about investing and needs real-time data
6. accounts: the user has a specific question about their accounts 6. accounts: the user has a specific question about their accounts
7. system: the user wants to know how to do something within the product 7. system: the user wants to know how to do something within the product
Second, remember to keep these things in mind regarding how to resolve: Second, remember to keep these things in mind regarding how to resolve:
- We have access to both historical and real-time data we can query, so we can answer questions about the user's accounts. But if we need to get that data, then content_contains_answer should be false. - We have access to both historical and real-time data we can query, so we can answer questions about the user's accounts. But if we need to get that data, then content_contains_answer should be false.
- If the user is asking for metrics, then resolution should be to query the metrics table. - If the user is asking for metrics, then resolution should be to query the metrics table.
@ -188,7 +187,7 @@ class AskQuestionJob
) )
raw_response = response.dig("choices", 0, "message", "content") raw_response = response.dig("choices", 0, "message", "content")
justification_reply = conversation.messages.new justification_reply = conversation.messages.new
justification_reply.log = raw_response justification_reply.log = raw_response
justification_reply.user = nil justification_reply.user = nil
@ -226,7 +225,7 @@ class AskQuestionJob
resolve_value = last_log_json["resolve"] resolve_value = last_log_json["resolve"]
text_string = '' text_string = ''
response = openai.chat( response = openai.chat(
parameters: { parameters: {
model: "gpt-4-1106-preview", model: "gpt-4-1106-preview",

View file

@ -1,5 +1,4 @@
class EnrichTransactionsJob class EnrichTransactionsJob
include Sidekiq::Job
def perform def perform
enrichment = Faraday.new( enrichment = Faraday.new(

View file

@ -1,5 +1,4 @@
class GenerateBalanceJob class GenerateBalanceJob
include Sidekiq::Job
def perform(account_id) def perform(account_id)
account = Account.find(account_id) account = Account.find(account_id)
@ -11,7 +10,7 @@ class GenerateBalanceJob
# Get current balance and save it to Balance model. Update based on account and date. Don't add last_balance if it's nil. # Get current balance and save it to Balance model. Update based on account and date. Don't add last_balance if it's nil.
Balance.find_or_initialize_by(account_id: account_id, security_id: nil, date: Date.today, kind: 'account', family_id: account.family.id).update(balance: account.current_balance, change: last_balance.nil? ? 0 : account.current_balance - last_balance) Balance.find_or_initialize_by(account_id: account_id, security_id: nil, date: Date.today, kind: 'account', family_id: account.family.id).update(balance: account.current_balance, change: last_balance.nil? ? 0 : account.current_balance - last_balance)
# Check if there holdings # Check if there holdings
if account.holdings.any? if account.holdings.any?
# Get current holdings value and save it to Balance model. Update based on account, security and date. # Get current holdings value and save it to Balance model. Update based on account, security and date.

View file

@ -1,5 +1,4 @@
class GenerateCategoricalMetricsJob class GenerateCategoricalMetricsJob
include Sidekiq::Job
def perform(family_id) def perform(family_id)
family = Family.find(family_id) family = Family.find(family_id)

View file

@ -1,5 +1,4 @@
class GenerateMetricsJob class GenerateMetricsJob
include Sidekiq::Job
def perform(family_id) def perform(family_id)
family = Family.find(family_id) family = Family.find(family_id)
@ -15,7 +14,7 @@ class GenerateMetricsJob
property_accounts_balance = accounts.property.sum { |account| account.current_balance } property_accounts_balance = accounts.property.sum { |account| account.current_balance }
total_assets = depository_accounts_balance + investment_accounts_balance + property_accounts_balance total_assets = depository_accounts_balance + investment_accounts_balance + property_accounts_balance
total_debts = credit_accounts_balance total_debts = credit_accounts_balance
net_worth = total_assets - total_debts net_worth = total_assets - total_debts

View file

@ -1,5 +1,4 @@
class RealTimeSyncJob class RealTimeSyncJob
include Sidekiq::Job
def perform(security_id) def perform(security_id)
security = Security.find(security_id) security = Security.find(security_id)

View file

@ -1,5 +1,4 @@
class SyncPlaidHoldingsJob class SyncPlaidHoldingsJob
include Sidekiq::Job
def perform(item_id) def perform(item_id)
connection = Connection.find_by(source: 'plaid', item_id: item_id) connection = Connection.find_by(source: 'plaid', item_id: item_id)
@ -31,7 +30,7 @@ class SyncPlaidHoldingsJob
# Process all securities first # Process all securities first
securities = holdings_response.securities securities = holdings_response.securities
# upsert_all securities # upsert_all securities
all_securities = [] all_securities = []
@ -59,7 +58,7 @@ class SyncPlaidHoldingsJob
holdings.each do |holding| holdings.each do |holding|
next if account_ids[holding.account_id].nil? next if account_ids[holding.account_id].nil?
next if holding.quantity <= 0 next if holding.quantity <= 0
all_holdings << { all_holdings << {
account_id: account_ids[holding.account_id], account_id: account_ids[holding.account_id],
security_id: Security.find_by(source_id: holding.security_id).id, security_id: Security.find_by(source_id: holding.security_id).id,
@ -74,6 +73,6 @@ class SyncPlaidHoldingsJob
Holding.upsert_all(all_holdings, unique_by: :index_holdings_on_account_id_and_security_id) Holding.upsert_all(all_holdings, unique_by: :index_holdings_on_account_id_and_security_id)
SyncPlaidInvestmentTransactionsJob.perform_async(item_id) SyncPlaidInvestmentTransactionsJob.perform(item_id)
end end
end end

View file

@ -1,12 +1,11 @@
class SyncPlaidInstitutionsJob class SyncPlaidInstitutionsJob < ApplicationJob
include Sidekiq::Job
def perform def perform_now
# Get all institutions from Plaid, which includes paginating through all pages # Get all institutions from Plaid, which includes paginating through all pages
offset = 0 offset = 0
while true while true
institutions = [] institutions = []
institutions_get_request = Plaid::InstitutionsGetRequest.new({ institutions_get_request = Plaid::InstitutionsGetRequest.new({
offset: offset, offset: offset,
count: 500, count: 500,

View file

@ -1,5 +1,4 @@
class SyncPlaidInvestmentTransactionsJob class SyncPlaidInvestmentTransactionsJob
include Sidekiq::Job
def perform(item_id) def perform(item_id)
connection = Connection.find_by(source: 'plaid', item_id: item_id) connection = Connection.find_by(source: 'plaid', item_id: item_id)
@ -37,11 +36,11 @@ class SyncPlaidInvestmentTransactionsJob
return return
end end
end end
# Process all securities first # Process all securities first
securities = response.securities securities = response.securities
# upsert_all securities # upsert_all securities
all_securities = [] all_securities = []
@ -70,7 +69,7 @@ class SyncPlaidInvestmentTransactionsJob
access_token: access_token, access_token: access_token,
start_date: start_date, start_date: start_date,
end_date: Date.today, end_date: Date.today,
options: { options: {
count: 500, count: 500,
offset: investmentTransactions.length() offset: investmentTransactions.length()
} }
@ -83,7 +82,7 @@ class SyncPlaidInvestmentTransactionsJob
if investmentTransactions.any? if investmentTransactions.any?
investmentTransactions_hash = investmentTransactions.map do |transaction| investmentTransactions_hash = investmentTransactions.map do |transaction|
security = Security.find_by(source_id: transaction.security_id) security = Security.find_by(source_id: transaction.security_id)
next if security.blank? next if security.blank?
{ {
account_id: account_ids[transaction.account_id], account_id: account_ids[transaction.account_id],
@ -118,9 +117,9 @@ class SyncPlaidInvestmentTransactionsJob
# Update investments_last_synced_at to the current time # Update investments_last_synced_at to the current time
connection.update(investments_last_synced_at: DateTime.now) connection.update(investments_last_synced_at: DateTime.now)
end end
accounts.each do |account| accounts.each do |account|
GenerateBalanceJob.perform_async(account.id) GenerateBalanceJob.perform(account.id)
end end
end end
end end

View file

@ -1,5 +1,4 @@
class SyncPlaidItemAccountsJob class SyncPlaidItemAccountsJob
include Sidekiq::Job
def perform(item_id) def perform(item_id)
connection = Connection.find_by(source: 'plaid', item_id: item_id) connection = Connection.find_by(source: 'plaid', item_id: item_id)
@ -38,12 +37,12 @@ class SyncPlaidItemAccountsJob
) )
connection_account.save connection_account.save
#GenerateBalanceJob.perform_async(connection_account.id) #GenerateBalanceJob.perform(connection_account.id)
end end
connection.update(sync_status: 'idle') connection.update(sync_status: 'idle')
SyncPlaidTransactionsJob.perform_async(item_id) SyncPlaidTransactionsJob.perform(item_id)
SyncPlaidHoldingsJob.perform_async(item_id) SyncPlaidHoldingsJob.perform(item_id)
end end
end end

View file

@ -1,5 +1,4 @@
class SyncPlaidTransactionsJob class SyncPlaidTransactionsJob
include Sidekiq::Job
def perform(item_id) def perform(item_id)
connection = Connection.find_by(source: 'plaid', item_id: item_id) connection = Connection.find_by(source: 'plaid', item_id: item_id)
@ -27,7 +26,7 @@ class SyncPlaidTransactionsJob
include_personal_finance_category: true include_personal_finance_category: true
} }
}) })
transactions_response = $plaid_api_client.transactions_sync(transactions_request) transactions_response = $plaid_api_client.transactions_sync(transactions_request)
added_transactions += transactions_response.added added_transactions += transactions_response.added
@ -92,10 +91,10 @@ class SyncPlaidTransactionsJob
Transaction.where(source_transaction_id: removed_transactions).destroy_all Transaction.where(source_transaction_id: removed_transactions).destroy_all
end end
EnrichTransactionsJob.perform_async EnrichTransactionsJob.perform
accounts.each do |account| accounts.each do |account|
GenerateBalanceJob.perform_async(account.id) GenerateBalanceJob.perform(account.id)
end end
end end
end end

View file

@ -1,5 +1,4 @@
class SyncPropertyValuesJob class SyncPropertyValuesJob
include Sidekiq::Job
def perform(account_id) def perform(account_id)
account = Account.find(account_id) account = Account.find(account_id)
@ -8,11 +7,11 @@ class SyncPropertyValuesJob
if account.auto_valuation && (account.current_balance_date.nil? || account.current_balance_date < 30.days.ago) if account.auto_valuation && (account.current_balance_date.nil? || account.current_balance_date < 30.days.ago)
url_formatted_address = "#{account.property_details['line_1'].gsub(' ','-')}-#{account.property_details['city']}-#{account.property_details['state_abbreviation']}-#{account.property_details['zip_code']}_rb" url_formatted_address = "#{account.property_details['line_1'].gsub(' ','-')}-#{account.property_details['city']}-#{account.property_details['state_abbreviation']}-#{account.property_details['zip_code']}_rb"
scraper = Faraday.get("https://app.scrapingbee.com/api/v1/?api_key=#{ENV['SCRAPING_BEE_KEY']}&url=https%3A%2F%2Fwww.zillow.com%2Fhomes%2F#{url_formatted_address}%2F&render_js=false&extract_rules=%7B%22value%22%3A'%2F%2Fspan%5B%40data-testid%3D%22zestimate-text%22%5D%2Fspan%2Fspan'%7D") scraper = Faraday.get("https://app.scrapingbee.com/api/v1/?api_key=#{ENV['SCRAPING_BEE_KEY']}&url=https%3A%2F%2Fwww.zillow.com%2Fhomes%2F#{url_formatted_address}%2F&render_js=false&extract_rules=%7B%22value%22%3A'%2F%2Fspan%5B%40data-testid%3D%22zestimate-text%22%5D%2Fspan%2Fspan'%7D")
# If the scraper returns a 200 status code, parse the response body and update the account # If the scraper returns a 200 status code, parse the response body and update the account
if scraper.status == 200 and JSON.parse(scraper.body)['value'].present? if scraper.status == 200 and JSON.parse(scraper.body)['value'].present?
account.update(current_balance: JSON.parse(scraper.body)['value'].gsub('$','').gsub(',','').to_i, current_balance_date: Date.today) account.update(current_balance: JSON.parse(scraper.body)['value'].gsub('$','').gsub(',','').to_i, current_balance_date: Date.today)
end end
end end
end end
end end

View file

@ -1,5 +1,4 @@
class SyncSecurityDetailsJob class SyncSecurityDetailsJob
include Sidekiq::Job
def perform(security_id) def perform(security_id)
security = Security.find_by(id: security_id) security = Security.find_by(id: security_id)
@ -80,4 +79,4 @@ class SyncSecurityDetailsJob
["https://logo.clearbit.com/#{domain}", 'clearbit'] ["https://logo.clearbit.com/#{domain}", 'clearbit']
end end
end end

View file

@ -1,5 +1,4 @@
class SyncSecurityHistoryJob class SyncSecurityHistoryJob
include Sidekiq::Job
def perform(security_id) def perform(security_id)
security = Security.find(security_id) security = Security.find(security_id)
@ -38,7 +37,7 @@ class SyncSecurityHistoryJob
end end
# remove duplicate dates # remove duplicate dates
all_prices.uniq! { |price| price[:date] } all_prices.uniq! { |price| price[:date] }
SecurityPrice.upsert_all(all_prices, unique_by: :index_security_prices_on_security_id_and_date) SecurityPrice.upsert_all(all_prices, unique_by: :index_security_prices_on_security_id_and_date)