From c6d1956c2950117fc0c8a8eb99d46be4ad28dd2a Mon Sep 17 00:00:00 2001 From: Kristian Garza Date: Fri, 29 Nov 2019 14:51:45 +0100 Subject: [PATCH] get metrics only if results --- app/controllers/events_controller.rb | 30 ++++++++++++++++++++-------- app/models/event.rb | 10 +--------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index db96d6672..c7c2a36ab 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -4,6 +4,8 @@ class EventsController < ApplicationController include Facetable include BatchLoaderHelper + require 'benchmark' + prepend_before_action :authenticate_user!, except: [:index, :show] @@ -120,21 +122,33 @@ def index 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 - dois_usage = params[:doi].present? ? EventsQuery.new.usage(params[:doi]) : [] - dois_citations = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_citations_by_year_v1(response.response.aggregations.dois_citations) : nil - citations = params[:doi].present? ? EventsQuery.new.citations(params[:doi]) : [] + dois_usage = total.positive? && params[:doi].present? ? EventsQuery.new.usage(params[:doi]) : [] + # dois_citations = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_citations_by_year_v1(response.response.aggregations.dois_citations) : nil + citations = total.positive? && params[:doi].present? ? EventsQuery.new.citations(params[:doi]) : [] citations_histogram = params[:doi].present? ? EventsQuery.new.citations_histogram(params[:doi]) : [] references = total.positive? && params[:doi].present? && aggregations.include?("citations_aggregations") ? facet_citations_by_dois(response.response.aggregations.references.dois.buckets) : nil relations = total.positive? && params[:doi].present? && aggregations.include?("citations_aggregations") ? facet_citations_by_dois(response.response.aggregations.relations.dois.buckets) : nil - views_histogram = params[:doi].present? ? EventsQuery.new.views_histogram(params[:doi]) : [] - downloads_histogram = params[:doi].present? ? EventsQuery.new.downloads_histogram(params[:doi]) : [] + views_histogram = total.positive? && params[:doi].present? ? EventsQuery.new.views_histogram(params[:doi]) : [] + downloads_histogram = total.positive? && params[:doi].present? ? EventsQuery.new.downloads_histogram(params[:doi]) : [] - views = params[:doi].present? ? EventsQuery.new.views(params[:doi]) : [] - downloads = params[:doi].present? ? EventsQuery.new.downloads(params[:doi]) : [] + views = total.positive? && params[:doi].present? ? EventsQuery.new.views(params[:doi]) : [] + downloads = total.positive? && params[:doi].present? ? EventsQuery.new.downloads(params[:doi]) : [] unique_obj_count = total.positive? && aggregations.include?("advanced_aggregations") ? response.response.aggregations.unique_obj_count.value : nil unique_subj_count = total.positive? && aggregations.include?("advanced_aggregations") ? response.response.aggregations.unique_subj_count.value : nil + + + bmt = Benchmark.ms { + total.positive? && params[:doi].present? ? EventsQuery.new.citations(params[:doi]) : [] + } + if bmt > 10000 + logger.warn "[Benchmark Warning] citations " + bmt.to_s + " ms" + else + logger.info "[Benchmark] citations " + bmt.to_s + " ms" + end + + results = response.results options = {} @@ -150,7 +164,7 @@ def index registrants: registrants, "doisRelationTypes": dois, "doisUsageTypes": dois_usage, - "doisCitations": dois_citations, + # "doisCitations": dois_citations, "citationsHistogram": citations_histogram, "uniqueCitations": citations, "references": references, diff --git a/app/models/event.rb b/app/models/event.rb index f6a0d345d..c3ae71373 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -220,15 +220,7 @@ def self.query_aggregations pairings: { terms: { field: "registrant_id", size: 50, min_doc_count: 1 }, aggs: { recipient: { terms: { field: "registrant_id", size: 50, min_doc_count: 1 }, aggs: { "total" => { sum: { field: "total" } } } } } }, citation_types: { terms: { field: "citation_type", size: 50, min_doc_count: 1 }, aggs: { year_months: { date_histogram: { field: "occurred_at", interval: "month", min_doc_count: 1 }, aggs: { "total_by_year_month" => { sum: { field: "total" } } } } } }, relation_types: { terms: { field: "relation_type_id", size: 50, min_doc_count: 1 }, aggs: { year_months: { date_histogram: { field: "occurred_at", interval: "month", min_doc_count: 1 }, aggs: { "total_by_year_month" => { sum: { field: "total" } } } }, "sum_distribution" => sum_distribution } }, - dois: { terms: { field: "obj_id", size: 50, min_doc_count: 1 }, aggs: { relation_types: { terms: { field: "relation_type_id", size: 50, min_doc_count: 1 }, aggs: { "total_by_type" => { sum: { field: "total" } } } } } }, - dois_citations: { - filter: { - script: { - script: "#{INCLUDED_RELATION_TYPES}.contains(doc['relation_type_id'].value)" - } - }, - aggs: { years: { date_histogram: { field: "occurred_at", interval: "year", min_doc_count: 1 }, aggs: { "total_by_year" => { sum: { field: "total" } } } }, "sum_distribution" => sum_year_distribution } - } + dois: { terms: { field: "obj_id", size: 50, min_doc_count: 1 }, aggs: { relation_types: { terms: { field: "relation_type_id", size: 50, min_doc_count: 1 }, aggs: { "total_by_type" => { sum: { field: "total" } } } } } } } end