Skip to content

Commit

Permalink
include events for datasets. #261
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jun 9, 2019
1 parent ed77c53 commit 20c4131
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 14 deletions.
6 changes: 6 additions & 0 deletions app/graphql/types/base_connection.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# frozen_string_literal: true

class BaseConnection < GraphQL::Types::Relay::BaseConnection
def doi_from_url(url)
if /\A(?:(http|https):\/\/(dx\.)?(doi.org|handle.test.datacite.org)\/)?(doi:)?(10\.\d{4,5}\/.+)\z/.match?(url)
uri = Addressable::URI.parse(url)
uri.path.gsub(/^\//, "").downcase
end
end
end
7 changes: 7 additions & 0 deletions app/graphql/types/base_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@

class BaseObject < GraphQL::Schema::Object
field_class GraphQL::Cache::Field

def doi_from_url(url)
if /\A(?:(http|https):\/\/(dx\.)?(doi.org|handle.test.datacite.org)\/)?(doi:)?(10\.\d{4,5}\/.+)\z/.match?(url)
uri = Addressable::URI.parse(url)
uri.path.gsub(/^\//, "").downcase
end
end
end
12 changes: 12 additions & 0 deletions app/graphql/types/dataset_dataset_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class DatasetDatasetConnectionWithMetaType < BaseConnection
edge_type(EventDataEdgeType, edge_class: EventDataEdge)
field_class GraphQL::Cache::Field

field :total_count, Integer, null: false, cache: true

def total_count
Event.query(nil, doi_id: doi_from_url(object.parent.identifier), citation_type: "Dataset-Dataset").results.total
end
end
12 changes: 12 additions & 0 deletions app/graphql/types/dataset_publication_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class DatasetPublicationConnectionWithMetaType < BaseConnection
edge_type(EventDataEdgeType, edge_class: EventDataEdge)
field_class GraphQL::Cache::Field

field :total_count, Integer, null: false, cache: true

def total_count
Event.query(nil, doi_id: doi_from_url(object.parent.identifier), citation_type: "Dataset-JournalArticle").results.total
end
end
12 changes: 12 additions & 0 deletions app/graphql/types/dataset_software_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class DatasetSoftwareConnectionWithMetaType < BaseConnection
edge_type(EventDataEdgeType, edge_class: EventDataEdge)
field_class GraphQL::Cache::Field

field :total_count, Integer, null: false, cache: true

def total_count
Event.query(nil, doi_id: doi_from_url(object.parent.identifier), citation_type: "Dataset-SoftwareSourceCode").results.total
end
end
32 changes: 32 additions & 0 deletions app/graphql/types/dataset_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,43 @@ class DatasetType < BaseObject
field :usage_reports, DatasetUsageReportConnectionWithMetaType, null: false, description: "Usage reports for this dataset", connection: true, max_page_size: 100 do
argument :first, Int, required: false, default_value: 25
end
field :datasets, DatasetDatasetConnectionWithMetaType, null: false, description: "Funded datasets", connection: true, max_page_size: 100 do
argument :first, Int, required: false, default_value: 25
end
field :publications, DatasetPublicationConnectionWithMetaType, null: false, description: "Funded publications", connection: true do
argument :query, String, required: false
argument :first, Int, required: false, default_value: 25
end
field :softwares, DatasetSoftwareConnectionWithMetaType, null: false, description: "Funded software", connection: true, max_page_size: 100 do
argument :first, Int, required: false, default_value: 25
end

def usage_reports(**args)
ids = Event.query(nil, obj_id: object.id).results.to_a.map do |e|
e[:subj_id]
end
UsageReport.find_by_id(ids, page: { number: 1, size: args[:first] }).fetch(:data, [])
end

def datasets(**args)
ids = Event.query(nil, doi_id: doi_from_url(object.identifier), citation_type: "Dataset-Dataset").results.to_a.map do |e|
object.identifier == e.subj_id ? doi_from_url(e.obj_id) : doi_from_url(e.subj_id)
end

ElasticsearchLoader.for(Doi).load_many(ids)
end

def publications(**args)
ids = Event.query(nil, doi_id: doi_from_url(object.identifier), citation_type: "Dataset-ScholarlyArticle").results.to_a.map do |e|
object.identifier == e.subj_id ? doi_from_url(e.obj_id) : doi_from_url(e.subj_id)
end
ElasticsearchLoader.for(Doi).load_many(ids)
end

def softwares(**args)
ids = Event.query(nil, doi_id: doi_from_url(object.identifier), citation_type: "Dataset-SoftwareSourceCode").results.to_a.map do |e|
object.identifier == e.subj_id ? doi_from_url(e.obj_id) : doi_from_url(e.subj_id)
end
ElasticsearchLoader.for(Doi).load_many(ids)
end
end
7 changes: 0 additions & 7 deletions app/graphql/types/funder_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,4 @@ def softwares(**args)
end
ElasticsearchLoader.for(Doi).load_many(ids)
end

def doi_from_url(url)
if /\A(?:(http|https):\/\/(dx\.)?(doi.org|handle.test.datacite.org)\/)?(doi:)?(10\.\d{4,5}\/.+)\z/.match?(url)
uri = Addressable::URI.parse(url)
uri.path.gsub(/^\//, "").downcase
end
end
end
7 changes: 0 additions & 7 deletions app/graphql/types/researcher_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@ def softwares(**args)
ElasticsearchLoader.for(Doi).load_many(ids)
end

def doi_from_url(url)
if /\A(?:(http|https):\/\/(dx\.)?(doi.org|handle.test.datacite.org)\/)?(doi:)?(10\.\d{4,5}\/.+)\z/.match?(url)
uri = Addressable::URI.parse(url)
uri.path.gsub(/^\//, "").downcase
end
end

def https_to_http(url)
uri = Addressable::URI.parse(url)
uri.scheme = "http"
Expand Down

0 comments on commit 20c4131

Please sign in to comment.