Skip to content

Commit

Permalink
CV2-3435 updates to alegre to work locally
Browse files Browse the repository at this point in the history
  • Loading branch information
DGaffney committed Sep 21, 2023
1 parent 45ba1ba commit 23ff0ec
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
14 changes: 12 additions & 2 deletions app/models/bot/alegre.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ def get_language_from_transcription
end
end

def self.valid_request?(request)
request.query_parameters['token'] == CheckConfig.get('alegre_token')
end


def self.default_model
CheckConfig.get('alegre_default_model') || Bot::Alegre::ELASTICSEARCH_MODEL
end
Expand Down Expand Up @@ -474,7 +479,7 @@ def self.request_api(method, path, params = {}, query_or_body = 'body', retries
ActiveRecord::Base.clear_active_connections!
ActiveRecord::Base.connection.close
end
uri = URI(CheckConfig.get('alegre_host') + path)
uri = URI(CheckConfig.get('alegre_host') + path+ '?token=' + CheckConfig.get('alegre_token'))
klass = 'Net::HTTP::' + method.capitalize
request = klass.constantize.new(uri.path, 'Content-Type' => 'application/json')
if query_or_body == 'query'
Expand All @@ -491,7 +496,12 @@ def self.request_api(method, path, params = {}, query_or_body = 'body', retries
response_body = response.body
Rails.logger.info("[Alegre Bot] Alegre response: #{response_body.inspect}")
ActiveRecord::Base.connection.reconnect! if RequestStore.store[:pause_database_connection]
JSON.parse(response_body)
parsed_response = JSON.parse(response_body)
if parsed_response.dig("queue") == 'audio__Model' && parsed_response.dig("body", "callback_url") != nil
redis = Redis.new(REDIS_CONFIG)
redis_response = redis.blpop(parsed_response.dig("body", "id"), timeout=120)
return JSON.parse(redis_response[1])
end
rescue StandardError => e
if retries > 0
sleep 1
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/alegre_similarity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def get_similar_items(pm)
type = Bot::Alegre.get_pm_type(pm)
Rails.logger.info "[Alegre Bot] [ProjectMedia ##{pm.id}] [Similarity 2/5] Type is #{type.blank? ? "blank" : type}"
unless type.blank?
if !self.should_get_similar_items_of_type?('master', pm.team_id) || !self.should_get_similar_items_of_type?(type, pm.team_id)
if !Bot::Alegre.should_get_similar_items_of_type?('master', pm.team_id) || !Bot::Alegre.should_get_similar_items_of_type?(type, pm.team_id)
Rails.logger.info "[Alegre Bot] [ProjectMedia ##{pm.id}] [Similarity 3/5] ProjectMedia cannot be checked for similar items"
return {}
else
Expand Down
13 changes: 10 additions & 3 deletions app/models/concerns/alegre_webhooks.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class AlegreCallbackError < StandardError
end
module AlegreWebhooks
extend ActiveSupport::Concern

module ClassMethods
def webhook(request)
#at this point, we know that the data is stored and ready for querying, so we can safely query based on package of:
Expand All @@ -9,8 +10,14 @@ def webhook(request)
# "model_type": model_type,
# "data": data,
# })
pm = ProjectMedia.find(request.params["data"]["requested"]["body"]["context"]["project_media_id"])
relate_project_media_to_similar_items(pm)
begin
doc_id = request.params["data"]["requested"]["body"]["id"]
redis = Redis.new(REDIS_CONFIG)
redis.lpush(doc_id, request.params.to_json)
redis.expire(doc_id, 1.day.to_i)
rescue StandardError => e
CheckSentry.notify(AlegreCallbackError.new(e.message), { alegre_response: body })
end
end
end
end

0 comments on commit 23ff0ec

Please sign in to comment.