From 9234064730b820cc3c08e151460a962a05db6984 Mon Sep 17 00:00:00 2001 From: Richard Hallett Date: Fri, 12 Apr 2019 17:27:57 +0200 Subject: [PATCH 1/2] Avoid using an instance variable for results. This was a performance related change. My belief is that the serializer has to do a method call for every single DOI in the result set and as the size is increased, this adds a significant overhead of 2-3 seconds overall. --- app/controllers/dois_controller.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/app/controllers/dois_controller.rb b/app/controllers/dois_controller.rb index 3c7356396..70130031a 100644 --- a/app/controllers/dois_controller.rb +++ b/app/controllers/dois_controller.rb @@ -135,7 +135,6 @@ def index respond_to do |format| format.json do - @dois = results options = {} options[:meta] = { total: total, @@ -161,11 +160,11 @@ def index options[:links] = { self: request.original_url, - next: @dois.size < page[:size] ? nil : request.base_url + "/dois?" + { + next: results.size < page[:size] ? nil : request.base_url + "/dois?" + { query: params[:query], "provider-id" => params[:provider_id], "client-id" => params[:client_id], - "page[cursor]" => page[:cursor].present? ? Array.wrap(@dois.to_a.last[:sort]).first : nil, + "page[cursor]" => page[:cursor].present? ? Array.wrap(results.to_a.last[:sort]).first : nil, "page[number]" => page[:cursor].blank? && page[:number].present? ? page[:number] + 1 : nil, "page[size]" => page[:size] }.compact.to_query }.compact @@ -176,7 +175,7 @@ def index } logger.info "[Benchmark] render " + Benchmark.ms { - render json: DoiSerializer.new(@dois, options).serialized_json, status: :ok + render json: DoiSerializer.new(results, options).serialized_json, status: :ok }.to_s + " ms" end format.citation do @@ -187,7 +186,7 @@ def index end rescue Elasticsearch::Transport::Transport::Errors::BadRequest => exception Raven.capture_exception(exception) - + message = JSON.parse(exception.message[6..-1]).to_h.dig("error", "root_cause", 0, "reason") render json: { "errors" => { "title" => message }}.to_json, status: :bad_request @@ -395,7 +394,7 @@ def get_dois head :no_content and return unless client_prefix.present? dois = Doi.get_dois(prefix: client_prefix.prefix, username: current_user.uid.upcase, password: current_user.password) - if dois.length > 0 + if dois.length > 0 render json: { dois: dois }.to_json, status: :ok else head :no_content From 1d973ba9ac34479f07676439d4d7d4e0e07ce312 Mon Sep 17 00:00:00 2001 From: Richard Hallett Date: Fri, 12 Apr 2019 17:38:15 +0200 Subject: [PATCH 2/2] Merge --- app/controllers/dois_controller.rb | 2 -- app/models/concerns/indexable.rb | 4 ++++ config/application.rb | 1 + config/initializers/lograge.rb | 1 - 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/controllers/dois_controller.rb b/app/controllers/dois_controller.rb index 70130031a..c28b41134 100644 --- a/app/controllers/dois_controller.rb +++ b/app/controllers/dois_controller.rb @@ -185,8 +185,6 @@ def index format.any(:bibtex, :citeproc, :codemeta, :crosscite, :datacite, :datacite_json, :jats, :ris, :csv, :schema_org) { render request.format.to_sym => response.records.to_a } end rescue Elasticsearch::Transport::Transport::Errors::BadRequest => exception - Raven.capture_exception(exception) - message = JSON.parse(exception.message[6..-1]).to_h.dig("error", "root_cause", 0, "reason") render json: { "errors" => { "title" => message }}.to_json, status: :bad_request diff --git a/app/models/concerns/indexable.rb b/app/models/concerns/indexable.rb index eadb1b2b3..d1ddc9583 100644 --- a/app/models/concerns/indexable.rb +++ b/app/models/concerns/indexable.rb @@ -108,6 +108,10 @@ def query(query, options={}) from = 0 search_after = [options.dig(:page, :cursor)] sort = [{ _id: { order: 'asc' }}] + elsif self.name == "Activity" && options.dig(:page, :cursor).present? + from = 0 + search_after = [options.dig(:page, :cursor)] + sort = [{ created: { order: 'asc' }}] else from = (options.dig(:page, :number) - 1) * options.dig(:page, :size) search_after = nil diff --git a/config/application.rb b/config/application.rb index bd7b0b127..ccde6de10 100644 --- a/config/application.rb +++ b/config/application.rb @@ -77,6 +77,7 @@ class Application < Rails::Application logger = ActiveSupport::Logger.new(STDOUT) logger.formatter = config.log_formatter config.logger = ActiveSupport::TaggedLogging.new(logger) + config.log_level = ENV['LOG_LEVEL'].to_sym # configure caching config.cache_store = :dalli_store, nil, { :namespace => ENV['APPLICATION'] } diff --git a/config/initializers/lograge.rb b/config/initializers/lograge.rb index 16598d544..85ea987cf 100644 --- a/config/initializers/lograge.rb +++ b/config/initializers/lograge.rb @@ -10,7 +10,6 @@ event.payload.inspect.length > 100000 end config.lograge.base_controller_class = 'ActionController::API' - config.log_level = ENV['LOG_LEVEL'].to_sym config.lograge.custom_options = lambda do |event| exceptions = %w(controller action format id)