Skip to content

Commit

Permalink
Merge pull request #364 from datacite/refractor_multiple_dois_in_events
Browse files Browse the repository at this point in the history
Refractor multiple dois in events
  • Loading branch information
kjgarza authored Nov 28, 2019
2 parents a4879f0 + 20cbac3 commit 18369e2
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 151 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ doc/dependencies*
docker-compose.override.yml
.ruby-version
.vscode
.solargraph
2 changes: 1 addition & 1 deletion app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def index
dois_usage = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_dois(response.response.aggregations.dois_usage.dois.buckets) : nil
dois_citations = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_citations_by_year_v1(response.response.aggregations.dois_citations) : nil
citations_histogram = total.positive? && params[:doi].present? && aggregations.include?("citations_aggregations") ? facet_citations_by_year(response.response.aggregations.citations_histogram) : nil
citations = total.positive? && params[:doi].present? && aggregations.include?("citations_aggregations") ? facet_citations_by_dois(response.response.aggregations.citations.dois.buckets) : nil
citations = params[:doi].present? ? EventsQuery.new.citations(params[:doi]) : []
references = total.positive? && params[:doi].present? && aggregations.include?("citations_aggregations") ? facet_citations_by_dois(response.response.aggregations.references.dois.buckets) : nil
relations = total.positive? && params[:doi].present? && aggregations.include?("citations_aggregations") ? facet_citations_by_dois(response.response.aggregations.relations.dois.buckets) : nil
views_histogram = total.positive? && aggregations.include?("metrics_aggregations") ? facet_counts_by_year_month(response.response.aggregations.views_histogram) : nil
Expand Down
3 changes: 1 addition & 2 deletions app/graphql/types/metric_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ def download_count
end

def citation_count
meta = citations_aggs
meta.first.fetch("total", {}).fetch("value", nil) if meta.any?
EventsQuery.new.doi_citations(doi_from_url(object.identifier))
end

def reference_count
Expand Down
140 changes: 73 additions & 67 deletions app/models/event.rb

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions app/queries/events_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

class EventsQuery

include Facetable

ACTIVE_RELATION_TYPES = [
"cites",
"is-supplement-to",
"references"
]

PASSIVE_RELATION_TYPES = [
"is-cited-by",
"is-supplemented-by",
"is-referenced-by"
]

def initialize()
end

def doi_citations(doi)
pid = Event.new.normalize_doi(doi)
query = "(subj_id:\"#{pid}\" AND (relation_type_id:#{PASSIVE_RELATION_TYPES.join(' OR relation_type_id:')})) OR (obj_id:\"#{pid}\" AND (relation_type_id:#{ACTIVE_RELATION_TYPES.join(' OR relation_type_id:')}))"
results = Event.query(query, doi:doi, aggregations: "citation_count_aggregation", page: { size: 1, cursor: [] }).response.aggregations.citations.buckets
results.any? ? results.first.total.value : 0
end

def citations(doi)
doi.downcase.split(",").map do |item|
{ id: item, count: EventsQuery.new.doi_citations(item) }
end
end
end
33 changes: 33 additions & 0 deletions spec/queries/events_query_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require "rails_helper"

describe EventsQuery, elasticsearch: true do

context "citation events" do
let!(:event) { create(:event_for_datacite_related, subj_id:"http://doi.org/10.0260/co.2004960.v2", obj_id:"http://doi.org/10.0260/co.2004960.v1") }
let!(:event_references) { create_list(:event_for_datacite_related, 3, obj_id:"10.5061/dryad.47sd5/2", relation_type_id: "references") }
let!(:copies) { create(:event_for_datacite_related, subj_id:"http://doi.org/10.0260/co.2004960.v2", obj_id:"http://doi.org/10.0260/co.2004960.v1", relation_type_id: "cites") }

before do
Event.import
sleep 1
end

it "doi_citations" do
expect(EventsQuery.new.doi_citations("10.0260/co.2004960.v1")).to eq(1)
end

it "doi_citations wiht 0 citations" do
expect(EventsQuery.new.doi_citations("10.5061/dryad.dd47sd5/1")).to eq(0)
end

it "citations" do
results = EventsQuery.new.citations("10.5061/dryad.47sd5/1,10.5061/dryad.47sd5/2,10.0260/co.2004960.v1")
citations = results.select { |item| item[:id] == "10.5061/dryad.47sd5/2" }.first
no_citations = results.select { |item| item[:id] == "10.5061/dryad.47sd5/1" }.first
expect(citations[:count]).to eq(3)
expect(no_citations[:count]).to eq(0)
end
end
end
Loading

0 comments on commit 18369e2

Please sign in to comment.