Skip to content

Commit

Permalink
enable views and downloads. #390
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Feb 16, 2020
1 parent e3ead45 commit 6d84199
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 59 deletions.
8 changes: 4 additions & 4 deletions app/controllers/dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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] = {
Expand Down
52 changes: 26 additions & 26 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/doi_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 17 additions & 17 deletions spec/models/doi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
21 changes: 10 additions & 11 deletions spec/requests/dois_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -170,23 +169,23 @@
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
get "/repositories/#{client.uid}", nil, headers

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

Expand Down

0 comments on commit 6d84199

Please sign in to comment.