From 6d841994df7ce9d52e6ccf5f1e4ec584a75865b5 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sun, 16 Feb 2020 02:04:55 +0100 Subject: [PATCH] enable views and downloads. #390 --- app/controllers/dois_controller.rb | 8 ++--- app/models/doi.rb | 52 +++++++++++++++--------------- app/serializers/doi_serializer.rb | 2 +- spec/models/doi_spec.rb | 34 +++++++++---------- spec/requests/dois_spec.rb | 21 ++++++------ 5 files changed, 58 insertions(+), 59 deletions(-) diff --git a/app/controllers/dois_controller.rb b/app/controllers/dois_controller.rb index 1ab0f8ac6..3e9505b05 100644 --- a/app/controllers/dois_controller.rb +++ b/app/controllers/dois_controller.rb @@ -200,8 +200,8 @@ def index logger.warn method: "GET", path: "/dois", message: "AggregationsLinkChecks /dois", duration: bm # citations = total.positive? ? metric_facet_by_year(response.aggregations.citations.buckets) : nil - # views = total.positive? ? metric_facet_by_year(response.aggregations.views.buckets) : nil - # downloads = total.positive? ? metric_facet_by_year(response.aggregations.downloads.buckets) : nil + views = total.positive? ? metric_facet_by_year(response.aggregations.views.buckets) : nil + downloads = total.positive? ? metric_facet_by_year(response.aggregations.downloads.buckets) : nil respond_to do |format| format.json do @@ -230,8 +230,8 @@ def index "linkChecksCitationDoi" => link_checks_citation_doi, subjects: subjects, # citations: citations, - # views: views, - # downloads: downloads, + views: views, + downloads: downloads, }.compact options[:links] = { diff --git a/app/models/doi.rb b/app/models/doi.rb index 8388c89df..462c70bf6 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -72,8 +72,8 @@ class Doi < ActiveRecord::Base belongs_to :client, foreign_key: :datacentre has_many :media, -> { order "created DESC" }, foreign_key: :dataset, dependent: :destroy has_many :metadata, -> { order "created DESC" }, foreign_key: :dataset, dependent: :destroy - # has_many :view_events, -> { where target_relation_type_id: "views" }, class_name: "Event", primary_key: :doi, foreign_key: :target_doi, dependent: :destroy - # has_many :download_events, -> { where target_relation_type_id: "downloads" }, class_name: "Event", primary_key: :doi, foreign_key: :target_doi, dependent: :destroy + has_many :view_events, -> { where target_relation_type_id: "views" }, class_name: "Event", primary_key: :doi, foreign_key: :target_doi, dependent: :destroy + has_many :download_events, -> { where target_relation_type_id: "downloads" }, class_name: "Event", primary_key: :doi, foreign_key: :target_doi, dependent: :destroy # has_many :reference_events, -> { where source_relation_type_id: "references" }, class_name: "Event", primary_key: :doi, foreign_key: :source_doi, dependent: :destroy # has_many :citation_events, -> { where target_relation_type_id: "citations" }, class_name: "Event", primary_key: :doi, foreign_key: :target_doi, dependent: :destroy # has_many :part_events, -> { where source_relation_type_id: "parts" }, class_name: "Event", primary_key: :doi, foreign_key: :source_doi, dependent: :destroy @@ -420,16 +420,16 @@ class Doi < ActiveRecord::Base consortium_organizations: { type: :object }, } indexes :resource_type, type: :object - # indexes :view_count, type: :integer - # indexes :download_count, type: :integer + indexes :view_count, type: :integer + indexes :download_count, type: :integer # indexes :reference_count, type: :integer # indexes :citation_count, type: :integer # indexes :part_count, type: :integer # indexes :part_of_count, type: :integer # indexes :version_count, type: :integer # indexes :version_of_count, type: :integer - # indexes :views_over_time, type: :object - # indexes :downloads_over_time, type: :object + indexes :views_over_time, type: :object + indexes :downloads_over_time, type: :object # indexes :reference_ids, type: :keyword # indexes :citation_ids, type: :keyword # indexes :part_ids, type: :keyword @@ -463,10 +463,10 @@ def as_indexed_json(options={}) "consortium_id" => consortium_id, "resource_type_id" => resource_type_id, "media_ids" => media_ids, - # "view_count" => view_count, - # "views_over_time" => views_over_time, - # "download_count" => download_count, - # "downloads_over_time" => downloads_over_time, + "view_count" => view_count, + "views_over_time" => views_over_time, + "download_count" => download_count, + "downloads_over_time" => downloads_over_time, # "reference_ids" => reference_ids, # "reference_count" => reference_count, # "citation_ids" => citation_ids, @@ -979,25 +979,25 @@ def media_ids media.pluck(:id).map { |m| Base32::URL.encode(m, split: 4, length: 16) }.compact end - # def view_count - # view_events.pluck(:total).inject(:+).to_i - # end + def view_count + view_events.pluck(:total).inject(:+).to_i + end - # def views_over_time - # view_events.pluck(:occurred_at, :total) - # .map { |v| { "yearMonth" => v[0].present? ? v[0].utc.iso8601[0..6] : nil, "total" => v[1] } } - # .sort_by { |h| h[:year_month] } - # end + def views_over_time + view_events.pluck(:occurred_at, :total) + .map { |v| { "yearMonth" => v[0].present? ? v[0].utc.iso8601[0..6] : nil, "total" => v[1] } } + .sort_by { |h| h[:year_month] } + end - # def download_count - # download_events.pluck(:total).inject(:+).to_i - # end + def download_count + download_events.pluck(:total).inject(:+).to_i + end - # def downloads_over_time - # download_events.pluck(:occurred_at, :total) - # .map { |v| { "yearMonth" => v[0].present? ? v[0].utc.iso8601[0..6] : nil, "total" => v[1] } } - # .sort_by { |h| h[:year_month] } - # end + def downloads_over_time + download_events.pluck(:occurred_at, :total) + .map { |v| { "yearMonth" => v[0].present? ? v[0].utc.iso8601[0..6] : nil, "total" => v[1] } } + .sort_by { |h| h[:year_month] } + end # def reference_ids # references.pluck(:uuid) diff --git a/app/serializers/doi_serializer.rb b/app/serializers/doi_serializer.rb index 899b9930e..b3e4f9c71 100644 --- a/app/serializers/doi_serializer.rb +++ b/app/serializers/doi_serializer.rb @@ -6,7 +6,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, :view_count, :views_over_time, :download_count, :downloads_over_time, :created, :registered, :published, :updated attributes :prefix, :suffix, if: Proc.new { |object, params| params && params[:detail] } belongs_to :client, record_type: :clients diff --git a/spec/models/doi_spec.rb b/spec/models/doi_spec.rb index 994b3d7cc..eea200efe 100644 --- a/spec/models/doi_spec.rb +++ b/spec/models/doi_spec.rb @@ -550,15 +550,15 @@ sleep 1 end - # it "has views" do - # expect(doi.view_events.count).to eq(3) - # expect(doi.view_count).to eq(75) - # expect(doi.views_over_time.first).to eq("total"=>25, "yearMonth"=>"2015-06") - - # view = doi.view_events.first - # expect(view.target_doi).to eq(doi.uid) - # expect(view.total).to eq(25) - # end + it "has views" do + expect(doi.view_events.count).to eq(3) + expect(doi.view_count).to eq(75) + expect(doi.views_over_time.first).to eq("total"=>25, "yearMonth"=>"2015-06") + + view = doi.view_events.first + expect(view.target_doi).to eq(doi.uid) + expect(view.total).to eq(25) + end end describe "downloads", elasticsearch: true do @@ -571,15 +571,15 @@ sleep 1 end - # it "has downloads" do - # expect(doi.download_events.count).to eq(3) - # expect(doi.download_count).to eq(30) - # expect(doi.downloads_over_time.first).to eq("total"=>10, "yearMonth"=>"2015-06") + it "has downloads" do + expect(doi.download_events.count).to eq(3) + expect(doi.download_count).to eq(30) + expect(doi.downloads_over_time.first).to eq("total"=>10, "yearMonth"=>"2015-06") - # download = doi.download_events.first - # expect(download.target_doi).to eq(doi.uid) - # expect(download.total).to eq(10) - # end + download = doi.download_events.first + expect(download.target_doi).to eq(doi.uid) + expect(download.total).to eq(10) + end end describe "references", elasticsearch: true do diff --git a/spec/requests/dois_spec.rb b/spec/requests/dois_spec.rb index a3a632e04..ec166941b 100644 --- a/spec/requests/dois_spec.rb +++ b/spec/requests/dois_spec.rb @@ -140,16 +140,15 @@ 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', 'views')).to eq([{"count"=>50, "id"=>"2011", "title"=>"2011"}]) - # expect(json.dig('meta', 'downloads')).to eq([{"count"=>20, "id"=>"2011", "title"=>"2011"}]) + expect(json.dig('meta', 'views')).to eq([{"count"=>50, "id"=>"2011", "title"=>"2011"}]) + expect(json.dig('meta', 'downloads')).to eq([{"count"=>20, "id"=>"2011", "title"=>"2011"}]) expect(json.dig('data', 0, 'attributes', 'publicationYear')).to eq(2011) expect(json.dig('data', 0, 'attributes', 'doi')).to eq(doi.doi.downcase) expect(json.dig('data', 0, 'attributes', 'titles')).to eq(doi.titles) - # expect(json.dig('data', 0, 'attributes', 'viewCount')).to eq(50) - # expect(json.dig('data', 0, 'attributes', 'viewsOverTime')).to eq([{"total"=>25, "yearMonth"=>"2015-06"}, {"total"=>25, "yearMonth"=>"2015-06"}]) - # expect(json.dig('data', 0, 'attributes', 'downloadCount')).to eq(20) - # expect(json.dig('data', 0, 'attributes', 'downloadsOverTime')).to eq([{"total"=>10, "yearMonth"=>"2015-06"}, {"total"=>10, "yearMonth"=>"2015-06"}]) - + expect(json.dig('data', 0, 'attributes', 'viewCount')).to eq(50) + expect(json.dig('data', 0, 'attributes', 'viewsOverTime')).to eq([{"total"=>25, "yearMonth"=>"2015-06"}, {"total"=>25, "yearMonth"=>"2015-06"}]) + expect(json.dig('data', 0, 'attributes', 'downloadCount')).to eq(20) + expect(json.dig('data', 0, 'attributes', 'downloadsOverTime')).to eq([{"total"=>10, "yearMonth"=>"2015-06"}, {"total"=>10, "yearMonth"=>"2015-06"}]) end end @@ -170,15 +169,15 @@ expect(json.dig('data', 'attributes', 'url')).to eq(doi.url) expect(json.dig('data', 'attributes', 'doi')).to eq(doi.doi.downcase) expect(json.dig('data', 'attributes', 'titles')).to eq(doi.titles) - # expect(json.dig('data', 'attributes', 'viewCount')).to eq(75) - # expect(json.dig('data', 'attributes', 'viewsOverTime')).to eq([{"total"=>25, "yearMonth"=>"2015-06"}, {"total"=>25, "yearMonth"=>"2015-06"}, {"total"=>25, "yearMonth"=>"2015-06"}]) + expect(json.dig('data', 'attributes', 'viewCount')).to eq(75) + expect(json.dig('data', 'attributes', 'viewsOverTime')).to eq([{"total"=>25, "yearMonth"=>"2015-06"}, {"total"=>25, "yearMonth"=>"2015-06"}, {"total"=>25, "yearMonth"=>"2015-06"}]) end it "has views meta" do get "/dois", nil, headers expect(last_response.status).to eq(200) - # expect(json.dig('meta', 'views')).to eq([{"count"=>75, "id"=>"2011", "title"=>"2011"}]) + expect(json.dig('meta', 'views')).to eq([{"count"=>75, "id"=>"2011", "title"=>"2011"}]) end it "repository shows summary count" do @@ -186,7 +185,7 @@ expect(last_response.status).to eq(200) expect(json.dig('data', 'attributes', 'name')).to eq(client.name) - # expect(json.dig('meta', 'views')).to eq([{"count"=>75, "id"=>"2011", "title"=>"2011"}]) + expect(json.dig('meta', 'views')).to eq([{"count"=>75, "id"=>"2011", "title"=>"2011"}]) end end