diff --git a/app/models/doi.rb b/app/models/doi.rb index d5a65b3a2..767d644a7 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -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 @@ -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, @@ -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 diff --git a/spec/models/doi_spec.rb b/spec/models/doi_spec.rb index c000e1e92..33826655e 100644 --- a/spec/models/doi_spec.rb +++ b/spec/models/doi_spec.rb @@ -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) @@ -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)