Skip to content

Commit

Permalink
citations_over_time. #418
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Feb 17, 2020
1 parent 14a4f97 commit ee4ea05
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
14 changes: 13 additions & 1 deletion app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ class Doi < ActiveRecord::Base
# indexes :version_of_count, type: :integer
indexes :views_over_time, type: :object
indexes :downloads_over_time, type: :object
indexes :citations_over_time, type: :object
indexes :reference_event_ids, type: :keyword
indexes :citation_event_ids, type: :keyword
indexes :reference_events, type: :object
Expand Down Expand Up @@ -475,6 +476,7 @@ def as_indexed_json(options={})
"citation_event_ids" => citation_event_ids,
"citation_count" => citation_count,
"citation_events" => citation_events,
"citations_over_time" => citations_over_time,
# "part_ids" => part_ids,
# "part_count" => part_count,
# "part_of_ids" => part_of_ids,
Expand Down Expand Up @@ -1015,8 +1017,18 @@ def citation_event_ids
citation_events.pluck(:uuid)
end

# remove duplicate citing source dois
def citation_count
citation_events.size
citation_events.pluck(:source_doi).uniq.length
end

# remove duplicate citing source dois,
# then show distribution by year
def citations_over_time
citation_events.pluck(:occurred_at, :source_doi).uniq { |v| v[1] }
.group_by { |v| v[0].utc.iso8601[0..3] }
.map { |k, v| { "year" => k, "total" => v.length } }
.sort_by { |h| h[:year] }
end

# def part_ids
Expand Down
15 changes: 10 additions & 5 deletions spec/models/doi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@
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")
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)
Expand Down Expand Up @@ -607,17 +607,22 @@
let(:client) { create(:client) }
let(:doi) { create(:doi, client: client, aasm_state: "findable") }
let(:source_doi) { create(:doi, client: client, aasm_state: "findable") }
let!(:citation_events) { create(:event_for_datacite_crossref, subj_id: "https://doi.org/#{doi.doi}", obj_id: "https://doi.org/#{source_doi.doi}", relation_type_id: "is-referenced-by") }
let(:source_doi2) { create(:doi, client: client, aasm_state: "findable") }
let!(:citation_event) { create(:event_for_datacite_crossref, subj_id: "https://doi.org/#{doi.doi}", obj_id: "https://doi.org/#{source_doi.doi}", relation_type_id: "is-referenced-by", occurred_at: "2015-06-13T16:14:19Z") }
let!(:citation_event2) { create(:event_for_datacite_crossref, subj_id: "https://doi.org/#{doi.doi}", obj_id: "https://doi.org/#{source_doi2.doi}", relation_type_id: "is-referenced-by", occurred_at: "2016-06-13T16:14:19Z") }
let!(:citation_event3) { create(:event_for_datacite_crossref, subj_id: "https://doi.org/#{doi.doi}", obj_id: "https://doi.org/#{source_doi2.doi}", relation_type_id: "is-cited-by", occurred_at: "2016-06-13T16:14:19Z") }

before do
Doi.import
sleep 1
end

# removing duplicate dois in citation_count and citations_over_time (different relation_type_id)
it "has citations" do
expect(doi.citation_events.count).to eq(1)
expect(doi.citation_event_ids.count).to eq(1)
expect(doi.citation_count).to eq(1)
expect(doi.citation_events.count).to eq(3)
expect(doi.citation_event_ids.count).to eq(3)
expect(doi.citation_count).to eq(2)
expect(doi.citations_over_time).to eq([{"total"=>1, "year"=>"2015"}, {"total"=>1, "year"=>"2016"}])

citation_event = doi.citation_events.first
expect(citation_event.source_doi.downcase).to eq(source_doi.uid)
Expand Down

0 comments on commit ee4ea05

Please sign in to comment.