From 48ca9d46590c54487907f1819edf3d69e13275ab Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Wed, 19 Feb 2020 07:23:16 +0100 Subject: [PATCH] Remove benchmarking code. #392 --- app/controllers/dois_controller.rb | 172 +++++++++++---------------- app/controllers/events_controller.rb | 121 +++++++------------ app/controllers/works_controller.rb | 104 ++++++---------- 3 files changed, 146 insertions(+), 251 deletions(-) diff --git a/app/controllers/dois_controller.rb b/app/controllers/dois_controller.rb index 47c1e5bfc..cdf6a132a 100644 --- a/app/controllers/dois_controller.rb +++ b/app/controllers/dois_controller.rb @@ -1,6 +1,5 @@ require 'uri' require 'base64' -require 'benchmark' class DoisController < ApplicationController include ActionController::MimeResponds @@ -40,54 +39,49 @@ def index else nil end - response = nil - # only show findable DOIs to anonymous users and role user params[:state] = "findable" if current_user.nil? || current_user.role_id == "user" - bm = Benchmark.ms { - if params[:id].present? - response = Doi.find_by_id(params[:id]) - elsif params[:ids].present? - response = Doi.find_by_ids(params[:ids], page: page, sort: sort) - else - response = Doi.query(params[:query], - state: params[:state], - exclude_registration_agencies: params[:exclude_registration_agencies], - created: params[:created], - registered: params[:registered], - provider_id: params[:provider_id], - consortium_id: params[:consortium_id], - client_id: params[:client_id], - affiliation_id: params[:affiliation_id], - re3data_id: params[:re3data_id], - opendoar_id: params[:opendoar_id], - certificate: params[:certificate], - prefix: params[:prefix], - user_id: params[:user_id], - resource_type_id: params[:resource_type_id], - schema_version: params[:schema_version], - subject: params[:subject], - has_citations: params[:has_citations], - has_views: params[:has_views], - has_downloads: params[:has_downloads], - link_check_status: params[:link_check_status], - link_check_has_schema_org: params[:link_check_has_schema_org], - link_check_body_has_pid: params[:link_check_body_has_pid], - link_check_found_schema_org_id: params[:link_check_found_schema_org_id], - link_check_found_dc_identifier: params[:link_check_found_dc_identifier], - link_check_found_citation_doi: params[:link_check_found_citation_doi], - link_check_redirect_count_gte: params[:link_check_redirect_count_gte], - sample_group: sample_group_field, - sample_size: params[:sample], - source: params[:source], - scroll_id: params[:scroll_id], - page: page, - sort: sort, - random: params[:random]) - end - } - Rails.logger.warn method: "GET", path: "/dois", message: "Request /dois", duration: bm + if params[:id].present? + response = Doi.find_by_id(params[:id]) + elsif params[:ids].present? + response = Doi.find_by_ids(params[:ids], page: page, sort: sort) + else + response = Doi.query(params[:query], + state: params[:state], + exclude_registration_agencies: params[:exclude_registration_agencies], + created: params[:created], + registered: params[:registered], + provider_id: params[:provider_id], + consortium_id: params[:consortium_id], + client_id: params[:client_id], + affiliation_id: params[:affiliation_id], + re3data_id: params[:re3data_id], + opendoar_id: params[:opendoar_id], + certificate: params[:certificate], + prefix: params[:prefix], + user_id: params[:user_id], + resource_type_id: params[:resource_type_id], + schema_version: params[:schema_version], + subject: params[:subject], + has_citations: params[:has_citations], + has_views: params[:has_views], + has_downloads: params[:has_downloads], + link_check_status: params[:link_check_status], + link_check_has_schema_org: params[:link_check_has_schema_org], + link_check_body_has_pid: params[:link_check_body_has_pid], + link_check_found_schema_org_id: params[:link_check_found_schema_org_id], + link_check_found_dc_identifier: params[:link_check_found_dc_identifier], + link_check_found_citation_doi: params[:link_check_found_citation_doi], + link_check_redirect_count_gte: params[:link_check_redirect_count_gte], + sample_group: sample_group_field, + sample_size: params[:sample], + source: params[:source], + scroll_id: params[:scroll_id], + page: page, + sort: sort, + random: params[:random]) + end begin # If we're using sample groups we need to unpack the results from the aggregation bucket hits. @@ -148,56 +142,27 @@ def index render json: DoiSerializer.new(results, options).serialized_json, status: :ok end else - states = nil - resource_types = nil - years = nil - created = nil - registered = nil - providers = nil - clients = nil - affiliations = nil - prefixes = nil - schema_versions = nil - sources = nil - link_checks_status = nil - links_with_schema_org = nil - link_checks_schema_org_id = nil - link_checks_dc_identifier = nil - link_checks_citation_doi = nil - links_checked = nil - subjects = nil - certificates = nil - - bm = Benchmark.ms { - states = total.positive? ? facet_by_key(response.aggregations.states.buckets) : nil - resource_types = total.positive? ? facet_by_resource_type(response.aggregations.resource_types.buckets) : nil - years = total.positive? ? facet_by_year(response.aggregations.years.buckets) : nil - created = total.positive? ? facet_by_year(response.aggregations.created.buckets) : nil - registered = total.positive? ? facet_by_year(response.aggregations.registered.buckets) : nil - providers = total.positive? ? facet_by_provider(response.aggregations.providers.buckets) : nil - clients = total.positive? ? facet_by_client(response.aggregations.clients.buckets) : nil - prefixes = total.positive? ? facet_by_key(response.aggregations.prefixes.buckets) : nil - schema_versions = total.positive? ? facet_by_schema(response.aggregations.schema_versions.buckets) : nil - } - logger.warn method: "GET", path: "/dois", message: "AggregationsBasic /dois", duration: bm - - bm = Benchmark.ms { - affiliations = total.positive? ? facet_by_affiliation(response.aggregations.affiliations.buckets) : nil - sources = total.positive? ? facet_by_key(response.aggregations.sources.buckets) : nil - subjects = total.positive? ? facet_by_key(response.aggregations.subjects.buckets) : nil - certificates = total.positive? ? facet_by_key(response.aggregations.certificates.buckets) : nil - } - logger.warn method: "GET", path: "/dois", message: "AggregationsExtended /dois", duration: bm - - bm = Benchmark.ms { - link_checks_status = total.positive? ? facet_by_cumulative_year(response.aggregations.link_checks_status.buckets) : nil - links_with_schema_org = total.positive? ? facet_by_cumulative_year(response.aggregations.link_checks_has_schema_org.buckets) : nil - link_checks_schema_org_id = total.positive? ? response.aggregations.link_checks_schema_org_id.value : nil - link_checks_dc_identifier = total.positive? ? response.aggregations.link_checks_dc_identifier.value : nil - link_checks_citation_doi = total.positive? ? response.aggregations.link_checks_citation_doi.value : nil - links_checked = total.positive? ? response.aggregations.links_checked.value : nil - } - logger.warn method: "GET", path: "/dois", message: "AggregationsLinkChecks /dois", duration: bm + states = total.positive? ? facet_by_key(response.aggregations.states.buckets) : nil + resource_types = total.positive? ? facet_by_resource_type(response.aggregations.resource_types.buckets) : nil + years = total.positive? ? facet_by_year(response.aggregations.years.buckets) : nil + created = total.positive? ? facet_by_year(response.aggregations.created.buckets) : nil + registered = total.positive? ? facet_by_year(response.aggregations.registered.buckets) : nil + providers = total.positive? ? facet_by_provider(response.aggregations.providers.buckets) : nil + clients = total.positive? ? facet_by_client(response.aggregations.clients.buckets) : nil + prefixes = total.positive? ? facet_by_key(response.aggregations.prefixes.buckets) : nil + schema_versions = total.positive? ? facet_by_schema(response.aggregations.schema_versions.buckets) : nil + + affiliations = total.positive? ? facet_by_affiliation(response.aggregations.affiliations.buckets) : nil + sources = total.positive? ? facet_by_key(response.aggregations.sources.buckets) : nil + subjects = total.positive? ? facet_by_key(response.aggregations.subjects.buckets) : nil + certificates = total.positive? ? facet_by_key(response.aggregations.certificates.buckets) : nil + + link_checks_status = total.positive? ? facet_by_cumulative_year(response.aggregations.link_checks_status.buckets) : nil + links_with_schema_org = total.positive? ? facet_by_cumulative_year(response.aggregations.link_checks_has_schema_org.buckets) : nil + link_checks_schema_org_id = total.positive? ? response.aggregations.link_checks_schema_org_id.value : nil + link_checks_dc_identifier = total.positive? ? response.aggregations.link_checks_dc_identifier.value : nil + link_checks_citation_doi = total.positive? ? response.aggregations.link_checks_citation_doi.value : nil + links_checked = total.positive? ? response.aggregations.links_checked.value : nil citations = total.positive? ? metric_facet_by_year(response.aggregations.citations.buckets) : nil views = total.positive? ? metric_facet_by_year(response.aggregations.views.buckets) : nil @@ -258,14 +223,11 @@ def index # sparse fieldsets fields = fields_from_params(params) - bm = Benchmark.ms { - if fields - render json: DoiSerializer.new(results, options.merge(fields: fields)).serialized_json, status: :ok - else - render json: DoiSerializer.new(results, options).serialized_json, status: :ok - end - } - logger.warn method: "GET", path: "/dois", message: "Render /dois", duration: bm + if fields + render json: DoiSerializer.new(results, options.merge(fields: fields)).serialized_json, status: :ok + else + render json: DoiSerializer.new(results, options).serialized_json, status: :ok + end end format.citation do @@ -289,7 +251,7 @@ def show # use current_user role to determine permissions to access draft and registered dois # instead of using ability response = Doi.find_by_id(params[:id]) - + respond_to do |format| format.json do doi = response.results.first diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index e15d0791a..99a2d0a5e 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -1,5 +1,3 @@ -require 'benchmark' - class EventsController < ApplicationController include Identifiable @@ -82,41 +80,37 @@ def index page = page_from_params(params) - response = nil - bm = Benchmark.ms { - if params[:id].present? - response = Event.find_by_id(params[:id]) - elsif params[:ids].present? - response = Event.find_by_id(params[:ids], page: page, sort: sort) - else - response = Event.query(params[:query], - subj_id: params[:subj_id], - obj_id: params[:obj_id], - source_doi: params[:source_doi], - target_doi: params[:target_doi], - doi: params[:doi_id] || params[:doi], - orcid: params[:orcid], - prefix: params[:prefix], - subtype: params[:subtype], - citation_type: params[:citation_type], - source_id: params[:source_id], - registrant_id: params[:registrant_id], - relation_type_id: params[:relation_type_id], - source_relation_type_id: params[:source_relation_type_id], - target_relation_type_id: params[:target_relation_type_id], - issn: params[:issn], - publication_year: params[:publication_year], - occurred_at: params[:occurred_at], - year_month: params[:year_month], - aggregations: params[:aggregations], - unique: params[:unique], - state_event: params[:state], - scroll_id: params[:scroll_id], - page: page, - sort: sort) - end - } - Rails.logger.warn method: "GET", path: "/events", message: "Request /events", duration: bm + if params[:id].present? + response = Event.find_by_id(params[:id]) + elsif params[:ids].present? + response = Event.find_by_id(params[:ids], page: page, sort: sort) + else + response = Event.query(params[:query], + subj_id: params[:subj_id], + obj_id: params[:obj_id], + source_doi: params[:source_doi], + target_doi: params[:target_doi], + doi: params[:doi_id] || params[:doi], + orcid: params[:orcid], + prefix: params[:prefix], + subtype: params[:subtype], + citation_type: params[:citation_type], + source_id: params[:source_id], + registrant_id: params[:registrant_id], + relation_type_id: params[:relation_type_id], + source_relation_type_id: params[:source_relation_type_id], + target_relation_type_id: params[:target_relation_type_id], + issn: params[:issn], + publication_year: params[:publication_year], + occurred_at: params[:occurred_at], + year_month: params[:year_month], + aggregations: params[:aggregations], + unique: params[:unique], + state_event: params[:state], + scroll_id: params[:scroll_id], + page: page, + sort: sort) + end if page[:scroll].present? results = response.results @@ -144,29 +138,15 @@ def index render json: EventSerializer.new(results, options).serialized_json, status: :ok else - aggregations = nil - sources = nil - prefixes = nil - citation_types = nil - relation_types = nil - registrants = nil - pairings = nil - dois = nil - states = nil - - bm = Benchmark.ms { - aggregations = params.fetch(:aggregations, "") || "" - - sources = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_source(response.response.aggregations.sources.buckets) : nil - prefixes = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_source(response.response.aggregations.prefixes.buckets) : nil - citation_types = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_citation_type(response.response.aggregations.citation_types.buckets) : nil - relation_types = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_relation_type(response.response.aggregations.relation_types.buckets) : nil - registrants = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_registrants(response.response.aggregations.registrants.buckets) : nil - pairings = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_pairings(response.response.aggregations.pairings.buckets) : nil - dois = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_dois(response.response.aggregations.dois.buckets) : nil - states = total.positive? && aggregations.include?("state_aggregations") ? facet_by_source(response.response.aggregations.states.buckets) : nil - } - Rails.logger.warn method: "GET", path: "/events", message: "Aggregations /events", duration: bm + aggregations = params.fetch(:aggregations, "") || "" + + sources = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_source(response.response.aggregations.sources.buckets) : nil + prefixes = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_source(response.response.aggregations.prefixes.buckets) : nil + citation_types = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_citation_type(response.response.aggregations.citation_types.buckets) : nil + relation_types = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_relation_type(response.response.aggregations.relation_types.buckets) : nil + registrants = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_registrants(response.response.aggregations.registrants.buckets) : nil + pairings = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_pairings(response.response.aggregations.pairings.buckets) : nil + states = total.positive? && aggregations.include?("state_aggregations") ? facet_by_source(response.response.aggregations.states.buckets) : nil results = response.results @@ -208,26 +188,7 @@ def index options[:is_collection] = true - events_serialized = nil - bm = Benchmark.ms { - events_serialized = EventSerializer.new(results, options).serializable_hash - } - Rails.logger.warn method: "GET", path: "/events", message: "Serialize /events", duration: bm - - if @include.include?(:dois) - doi_names = "" - bm = Benchmark.ms { - options[:include] = [] - doi_names = (results.map { |event| event.doi}).uniq().join(",") - events_serialized[:included] = DoiSerializer.new((Doi.find_by_id(doi_names).results), is_collection: true).serializable_hash.dig(:data) - } - Rails.logger.warn method: "GET", path: "/events", message: "IncludeDois /events", duration: bm - end - - bm = Benchmark.ms { - render json: events_serialized, status: :ok - } - Rails.logger.warn method: "GET", path: "/events", message: "Render /events", duration: bm + render json: EventSerializer.new(results, options).serialized_json, status: :ok end end diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index f8632009c..2bbc21f3d 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -1,5 +1,3 @@ -require 'benchmark' - class WorksController < ApplicationController before_action :set_doi, only: [:show] before_action :set_include, only: [:index, :show] @@ -26,59 +24,43 @@ def index else nil end - response = nil - bm = Benchmark.ms { - if params[:id].present? - response = Doi.find_by_id(params[:id]) - elsif params[:ids].present? - response = Doi.find_by_ids(params[:ids], page: page, sort: sort) - else - response = Doi.query(params[:query], - exclude_registration_agencies: true, - state: "findable", - created: params[:created], - registered: params[:registered], - provider_id: params[:member_id], - client_id: params[:data_center_id], - affiliation_id: params[:affiliation_id], - prefix: params[:prefix], - user_id: params[:person_id], - resource_type_id: params[:resource_type_id], - has_citations: params[:has_citations], - has_views: params[:has_views], - has_downloads: params[:has_downloads], - schema_version: params[:schema_version], - sample_group: sample_group_field, - sample_size: params[:sample], - page: page, - sort: sort, - random: params[:sample].present? ? true : false) - end - } - Rails.logger.warn method: "GET", path: "/works", message: "Request /works", duration: bm + if params[:id].present? + response = Doi.find_by_id(params[:id]) + elsif params[:ids].present? + response = Doi.find_by_ids(params[:ids], page: page, sort: sort) + else + response = Doi.query(params[:query], + exclude_registration_agencies: true, + state: "findable", + created: params[:created], + registered: params[:registered], + provider_id: params[:member_id], + client_id: params[:data_center_id], + affiliation_id: params[:affiliation_id], + prefix: params[:prefix], + user_id: params[:person_id], + resource_type_id: params[:resource_type_id], + has_citations: params[:has_citations], + has_views: params[:has_views], + has_downloads: params[:has_downloads], + schema_version: params[:schema_version], + sample_group: sample_group_field, + sample_size: params[:sample], + page: page, + sort: sort, + random: params[:sample].present? ? true : false) + end begin total = response.results.total - total_pages = page[:size] > 0 ? ([total.to_f, 10000].min / page[:size]).ceil : 0 - - resource_types = nil - registered = nil - providers = nil - clients = nil - affiliations = nil - - bm = Benchmark.ms { - resource_types = total > 0 ? facet_by_resource_type(response.response.aggregations.resource_types.buckets) : nil - registered = total > 0 ? facet_by_year(response.response.aggregations.registered.buckets) : nil - providers = total > 0 ? facet_by_provider(response.response.aggregations.providers.buckets) : nil - clients = total > 0 ? facet_by_client(response.response.aggregations.clients.buckets) : nil - } - Rails.logger.warn method: "GET", path: "/works", message: "AggregationsBasic /works", duration: bm + total_pages = page[:size].positive? ? ([total.to_f, 10000].min / page[:size]).ceil : 0 - bm = Benchmark.ms { - affiliations = total > 0 ? facet_by_affiliation(response.response.aggregations.affiliations.buckets) : nil - } - Rails.logger.warn method: "GET", path: "/works", message: "AggregationsAffiliations /works", duration: bm + resource_types = total > 0 ? facet_by_resource_type(response.response.aggregations.resource_types.buckets) : nil + registered = total > 0 ? facet_by_year(response.response.aggregations.registered.buckets) : nil + providers = total > 0 ? facet_by_provider(response.response.aggregations.providers.buckets) : nil + clients = total > 0 ? facet_by_client(response.response.aggregations.clients.buckets) : nil + + affiliations = total > 0 ? facet_by_affiliation(response.response.aggregations.affiliations.buckets) : nil @dois = response.results @@ -117,10 +99,7 @@ def index @dois = response.results end - bm = Benchmark.ms { - render json: WorkSerializer.new(@dois, options).serialized_json, status: :ok - } - Rails.logger.warn method: "GET", path: "/works", message: "Render /works", duration: bm + render json: WorkSerializer.new(@dois, options).serialized_json, status: :ok rescue Elasticsearch::Transport::Transport::Errors::BadRequest => exception message = JSON.parse(exception.message[6..-1]).to_h.dig("error", "root_cause", 0, "reason") @@ -134,26 +113,19 @@ def show options[:is_collection] = false options[:params] = { current_ability: current_ability, - detail: true + detail: true, } - bm = Benchmark.ms { - render json: WorkSerializer.new(@doi, options).serialized_json, status: :ok - } - Rails.logger.warn method: "GET", path: "/works/#{@doi.doi}", message: "Render /works/#{@doi.doi}", duration: bm + render json: WorkSerializer.new(@doi, options).serialized_json, status: :ok end protected def set_doi - @doi = nil + response = Doi.find_by_id(params[:id]) + @doi = response.results.first - bm = Benchmark.ms { - response = Doi.find_by_id(params[:id]) - @doi = response.results.first - } fail ActiveRecord::RecordNotFound if not_allowed_by_doi_and_user(doi: @doi, user: current_user) - Rails.logger.warn method: "GET", path: "/works/#{@doi.doi}", message: "Request ES /works/#{@doi.doi}", duration: bm end def set_include