Skip to content

Commit

Permalink
add cache for the fair_score and submission endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
syphax-bouazzouni committed Apr 10, 2024
1 parent 9c659e5 commit 17888ef
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
3 changes: 1 addition & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -710,9 +710,8 @@ def init_trial_license

# Get the submission metadata from the REST API.
def submission_metadata
@metadata ||= JSON.parse(LinkedData::Client::HTTP.get("#{REST_URI}/submission_metadata", {}, raw: true))
@metadata ||= helpers.submission_metadata
end
helper_method :submission_metadata

def request_lang
helpers.request_lang
Expand Down
28 changes: 18 additions & 10 deletions app/helpers/fair_score_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,24 @@ def fairness_service_enabled?
def get_fairness_service_url(apikey = user_apikey)
"#{$FAIRNESS_URL}?portal=#{$HOSTNAME.split('.')[0]}#{apikey.nil? || apikey.empty? ? '' : "&apikey=#{apikey}"}"
end

def get_fairness_json(ontologies_acronyms, apikey = user_apikey)
begin
conn = Faraday.new do |conn|
conn.options.timeout = 30
Rails.cache.fetch("fairness-#{ontologies_acronyms.gsub(',', '-')}", expires: 24.hours) do
begin
out = {}
time = Benchmark.realtime do
conn = Faraday.new do |conn|
conn.options.timeout = 30
end
response = conn.get(get_fairness_service_url(apikey) + "&ontologies=#{ontologies_acronyms}&combined")
out = MultiJson.load(response.body.force_encoding('ISO-8859-1').encode('UTF-8'))
end
puts "Call fairness service for: #{ontologies_acronyms} (#{time}s)"
rescue
Rails.logger.warn t('fair_score.fairness_unreachable_warning')
end
response = conn.get(get_fairness_service_url(apikey) + "&ontologies=#{ontologies_acronyms}&combined")
MultiJson.load(response.body.force_encoding('ISO-8859-1').encode('UTF-8'))
rescue
Rails.logger.warn t('fair_score.fairness_unreachable_warning')
{}

out
end
end

Expand Down Expand Up @@ -72,7 +80,7 @@ def create_fair_scores_data(fair_scores, count = nil)
end

def get_not_obtained_score(fair_scores_data, index)
fair_scores_data[:criteria][:portalMaxCredits][index] - fair_scores_data[:criteria][:scores][index]
fair_scores_data[:criteria][:portalMaxCredits][index] - fair_scores_data[:criteria][:scores][index]
end

def get_not_obtained_score_normalized(fair_scores_data, index)
Expand All @@ -84,7 +92,7 @@ def get_not_obtained_score_normalized(fair_scores_data, index)
elsif score_rest.zero?
100 - fair_scores_data[:criteria][:normalizedScores][index]
else
0
0
end

end
Expand Down
4 changes: 3 additions & 1 deletion app/helpers/metadata_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ def input_type?(attr, type)
end

def submission_metadata
@metadata ||= JSON.parse(Net::HTTP.get(URI.parse("#{$REST_URL}/submission_metadata?apikey=#{$API_KEY}")))
@metadata ||= Rails.cache.fetch('submission_metadata') do
JSON.parse(LinkedData::Client::HTTP.get("/submission_metadata", {}, {raw: true}))
end
end

def attr_metadata(attr_key)
Expand Down

0 comments on commit 17888ef

Please sign in to comment.