From 20c4131768ea05b3c7e7add76ce0d40ae66a3410 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sun, 9 Jun 2019 09:27:55 +0200 Subject: [PATCH] include events for datasets. #261 --- app/graphql/types/base_connection.rb | 6 ++++ app/graphql/types/base_object.rb | 7 ++++ ...taset_dataset_connection_with_meta_type.rb | 12 +++++++ ...t_publication_connection_with_meta_type.rb | 12 +++++++ ...aset_software_connection_with_meta_type.rb | 12 +++++++ app/graphql/types/dataset_type.rb | 32 +++++++++++++++++++ app/graphql/types/funder_type.rb | 7 ---- app/graphql/types/researcher_type.rb | 7 ---- 8 files changed, 81 insertions(+), 14 deletions(-) create mode 100644 app/graphql/types/dataset_dataset_connection_with_meta_type.rb create mode 100644 app/graphql/types/dataset_publication_connection_with_meta_type.rb create mode 100644 app/graphql/types/dataset_software_connection_with_meta_type.rb diff --git a/app/graphql/types/base_connection.rb b/app/graphql/types/base_connection.rb index 63d5c0a46..751dd8193 100644 --- a/app/graphql/types/base_connection.rb +++ b/app/graphql/types/base_connection.rb @@ -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 diff --git a/app/graphql/types/base_object.rb b/app/graphql/types/base_object.rb index 068d1556f..7a35fad54 100644 --- a/app/graphql/types/base_object.rb +++ b/app/graphql/types/base_object.rb @@ -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 diff --git a/app/graphql/types/dataset_dataset_connection_with_meta_type.rb b/app/graphql/types/dataset_dataset_connection_with_meta_type.rb new file mode 100644 index 000000000..29b93eff7 --- /dev/null +++ b/app/graphql/types/dataset_dataset_connection_with_meta_type.rb @@ -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 \ No newline at end of file diff --git a/app/graphql/types/dataset_publication_connection_with_meta_type.rb b/app/graphql/types/dataset_publication_connection_with_meta_type.rb new file mode 100644 index 000000000..3d0d54080 --- /dev/null +++ b/app/graphql/types/dataset_publication_connection_with_meta_type.rb @@ -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 diff --git a/app/graphql/types/dataset_software_connection_with_meta_type.rb b/app/graphql/types/dataset_software_connection_with_meta_type.rb new file mode 100644 index 000000000..20ca00ee7 --- /dev/null +++ b/app/graphql/types/dataset_software_connection_with_meta_type.rb @@ -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 diff --git a/app/graphql/types/dataset_type.rb b/app/graphql/types/dataset_type.rb index 39c021e22..3551929e0 100644 --- a/app/graphql/types/dataset_type.rb +++ b/app/graphql/types/dataset_type.rb @@ -6,6 +6,16 @@ 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| @@ -13,4 +23,26 @@ def usage_reports(**args) 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 diff --git a/app/graphql/types/funder_type.rb b/app/graphql/types/funder_type.rb index 45bb9d61c..521f90218 100644 --- a/app/graphql/types/funder_type.rb +++ b/app/graphql/types/funder_type.rb @@ -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 diff --git a/app/graphql/types/researcher_type.rb b/app/graphql/types/researcher_type.rb index 58b893a3b..8e89257fa 100644 --- a/app/graphql/types/researcher_type.rb +++ b/app/graphql/types/researcher_type.rb @@ -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"