From 257aa89a0aa643fc255947eed785b8962c059365 Mon Sep 17 00:00:00 2001 From: kjgarza Date: Mon, 20 Jan 2020 13:18:44 +0100 Subject: [PATCH 1/2] move citations counts to doi object --- app/controllers/dois_controller.rb | 16 +++++++--------- app/serializers/doi_serializer.rb | 14 +++++++++++++- spec/requests/dois_spec.rb | 9 +++++---- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/app/controllers/dois_controller.rb b/app/controllers/dois_controller.rb index 843f82e08..24d293a0f 100644 --- a/app/controllers/dois_controller.rb +++ b/app/controllers/dois_controller.rb @@ -192,12 +192,13 @@ def index } logger.warn method: "GET", path: "/dois", message: "AggregationsLinkChecks /dois", duration: bm - if params[:mix_in].present? - dois_result = results.map { |result| result.dig(:_source, :doi) }.join(',') if total.positive? - citations = total.positive? ? EventsQuery.new.citations(dois_result) : nil - views = total.positive? ? EventsQuery.new.views(dois_result) : nil - downloads = total.positive? ? EventsQuery.new.downloads(dois_result) : nil - end + results = results.map do |result| + result.merge( + citations: EventsQuery.new.doi_citations( result.dig(:_source, :doi) ), + views: EventsQuery.new.doi_views( result.dig(:_source, :doi) ), + downloads: EventsQuery.new.doi_downloads( result.dig(:_source, :doi) ), + ) + end if total.positive? && params[:mix_in].present? respond_to do |format| format.json do @@ -225,9 +226,6 @@ def index "linkChecksDcIdentifier" => link_checks_dc_identifier, "linkChecksCitationDoi" => link_checks_citation_doi, subjects: subjects, - citations: citations, - views: views, - downloads: downloads, }.compact options[:links] = { diff --git a/app/serializers/doi_serializer.rb b/app/serializers/doi_serializer.rb index 1290b1b7a..3d61f6822 100644 --- a/app/serializers/doi_serializer.rb +++ b/app/serializers/doi_serializer.rb @@ -5,7 +5,7 @@ class DoiSerializer set_id :uid # don't cache dois, as works are cached using the doi model - attributes :doi, :prefix, :suffix, :identifiers, :creators, :titles, :publisher, :container, :publication_year, :subjects, :contributors, :dates, :language, :types, :related_identifiers, :sizes, :formats, :version, :rights_list, :descriptions, :geo_locations, :funding_references, :xml, :url, :content_url, :metadata_version, :schema_version, :source, :is_active, :state, :reason, :landing_page, :created, :registered, :published, :updated + attributes :doi, :prefix, :suffix, :identifiers, :creators, :titles, :publisher, :container, :publication_year, :subjects, :contributors, :dates, :language, :types, :related_identifiers, :sizes, :formats, :version, :rights_list, :descriptions, :geo_locations, :funding_references, :xml, :url, :content_url, :metadata_version, :schema_version, :source, :is_active, :state, :reason, :landing_page, :created, :registered, :published, :updated, :citations, :views, :downloads attributes :prefix, :suffix, if: Proc.new { |object, params| params && params[:detail] } belongs_to :client, record_type: :clients @@ -84,4 +84,16 @@ class DoiSerializer attribute :landing_page, if: Proc.new { |object, params| params[:current_ability] && params[:current_ability].can?(:read_landing_page_results, object) == true } do |object| object.landing_page end + + attribute :citations do |object| + object[:citations] || {} + end + + attribute :views do |object| + object[:views] || {} + end + + attribute :downloads do |object| + object[:downloads] || {} + end end diff --git a/spec/requests/dois_spec.rb b/spec/requests/dois_spec.rb index 31665d933..8de5f5508 100644 --- a/spec/requests/dois_spec.rb +++ b/spec/requests/dois_spec.rb @@ -126,20 +126,21 @@ before do Event.import Doi.import - sleep 3 + sleep 4 end context 'when the record exists' do it 'returns the Doi' do get "/dois?mix-in=metrics", nil, headers + puts json.dig('data',0) expect(last_response.status).to eq(200) expect(json['data'].size).to eq(1) expect(json.dig('meta', 'total')).to eq(1) - expect(json.dig('meta', 'citations').first.dig('count')).to eq(3) - expect(json.dig('meta', 'views').first.dig('count')).to be > 0 - expect(json.dig('meta', 'downloads').first.dig('count')).to eq(0) + expect(json.dig('data',0,'attributes','citations')).to eq(3) + expect(json.dig('data',0,'attributes','views')).to be > 0 + expect(json.dig('data',0,'attributes','downloads')).to eq(0) end end end From 34b34b77eb7dd9626dbd631d32fb61de17f6c364 Mon Sep 17 00:00:00 2001 From: kjgarza Date: Mon, 20 Jan 2020 16:15:03 +0100 Subject: [PATCH 2/2] Revert "move citations counts to doi object" This reverts commit 257aa89a0aa643fc255947eed785b8962c059365. --- app/controllers/dois_controller.rb | 16 +++++++++------- app/serializers/doi_serializer.rb | 14 +------------- spec/requests/dois_spec.rb | 9 ++++----- 3 files changed, 14 insertions(+), 25 deletions(-) diff --git a/app/controllers/dois_controller.rb b/app/controllers/dois_controller.rb index 24d293a0f..843f82e08 100644 --- a/app/controllers/dois_controller.rb +++ b/app/controllers/dois_controller.rb @@ -192,13 +192,12 @@ def index } logger.warn method: "GET", path: "/dois", message: "AggregationsLinkChecks /dois", duration: bm - results = results.map do |result| - result.merge( - citations: EventsQuery.new.doi_citations( result.dig(:_source, :doi) ), - views: EventsQuery.new.doi_views( result.dig(:_source, :doi) ), - downloads: EventsQuery.new.doi_downloads( result.dig(:_source, :doi) ), - ) - end if total.positive? && params[:mix_in].present? + if params[:mix_in].present? + dois_result = results.map { |result| result.dig(:_source, :doi) }.join(',') if total.positive? + citations = total.positive? ? EventsQuery.new.citations(dois_result) : nil + views = total.positive? ? EventsQuery.new.views(dois_result) : nil + downloads = total.positive? ? EventsQuery.new.downloads(dois_result) : nil + end respond_to do |format| format.json do @@ -226,6 +225,9 @@ def index "linkChecksDcIdentifier" => link_checks_dc_identifier, "linkChecksCitationDoi" => link_checks_citation_doi, subjects: subjects, + citations: citations, + views: views, + downloads: downloads, }.compact options[:links] = { diff --git a/app/serializers/doi_serializer.rb b/app/serializers/doi_serializer.rb index 3d61f6822..1290b1b7a 100644 --- a/app/serializers/doi_serializer.rb +++ b/app/serializers/doi_serializer.rb @@ -5,7 +5,7 @@ class DoiSerializer set_id :uid # don't cache dois, as works are cached using the doi model - attributes :doi, :prefix, :suffix, :identifiers, :creators, :titles, :publisher, :container, :publication_year, :subjects, :contributors, :dates, :language, :types, :related_identifiers, :sizes, :formats, :version, :rights_list, :descriptions, :geo_locations, :funding_references, :xml, :url, :content_url, :metadata_version, :schema_version, :source, :is_active, :state, :reason, :landing_page, :created, :registered, :published, :updated, :citations, :views, :downloads + attributes :doi, :prefix, :suffix, :identifiers, :creators, :titles, :publisher, :container, :publication_year, :subjects, :contributors, :dates, :language, :types, :related_identifiers, :sizes, :formats, :version, :rights_list, :descriptions, :geo_locations, :funding_references, :xml, :url, :content_url, :metadata_version, :schema_version, :source, :is_active, :state, :reason, :landing_page, :created, :registered, :published, :updated attributes :prefix, :suffix, if: Proc.new { |object, params| params && params[:detail] } belongs_to :client, record_type: :clients @@ -84,16 +84,4 @@ class DoiSerializer attribute :landing_page, if: Proc.new { |object, params| params[:current_ability] && params[:current_ability].can?(:read_landing_page_results, object) == true } do |object| object.landing_page end - - attribute :citations do |object| - object[:citations] || {} - end - - attribute :views do |object| - object[:views] || {} - end - - attribute :downloads do |object| - object[:downloads] || {} - end end diff --git a/spec/requests/dois_spec.rb b/spec/requests/dois_spec.rb index 8de5f5508..31665d933 100644 --- a/spec/requests/dois_spec.rb +++ b/spec/requests/dois_spec.rb @@ -126,21 +126,20 @@ before do Event.import Doi.import - sleep 4 + sleep 3 end context 'when the record exists' do it 'returns the Doi' do get "/dois?mix-in=metrics", nil, headers - puts json.dig('data',0) expect(last_response.status).to eq(200) expect(json['data'].size).to eq(1) expect(json.dig('meta', 'total')).to eq(1) - expect(json.dig('data',0,'attributes','citations')).to eq(3) - expect(json.dig('data',0,'attributes','views')).to be > 0 - expect(json.dig('data',0,'attributes','downloads')).to eq(0) + expect(json.dig('meta', 'citations').first.dig('count')).to eq(3) + expect(json.dig('meta', 'views').first.dig('count')).to be > 0 + expect(json.dig('meta', 'downloads').first.dig('count')).to eq(0) end end end