From ce9f5ce2a44e6ee698a2985b97e9189b72339b0f Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Fri, 17 May 2019 07:53:09 +0200 Subject: [PATCH] don't use source_id for usage report events. #260 --- .../usage_report_dataset_connection_with_meta_type.rb | 2 +- app/graphql/types/usage_report_type.rb | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/graphql/types/usage_report_dataset_connection_with_meta_type.rb b/app/graphql/types/usage_report_dataset_connection_with_meta_type.rb index 75900edba..c4be0d4ed 100644 --- a/app/graphql/types/usage_report_dataset_connection_with_meta_type.rb +++ b/app/graphql/types/usage_report_dataset_connection_with_meta_type.rb @@ -7,6 +7,6 @@ class UsageReportDatasetConnectionWithMetaType < BaseConnection field :total_count, Integer, null: false, cache: true def total_count - Event.query(nil, subj_id: object.parent[:id], source_id: "datacite-usage").dig(:meta, "total").to_i + Event.query(nil, subj_id: object.parent[:id]).dig(:meta, "total").to_i end end diff --git a/app/graphql/types/usage_report_type.rb b/app/graphql/types/usage_report_type.rb index 44067138e..41802bb05 100644 --- a/app/graphql/types/usage_report_type.rb +++ b/app/graphql/types/usage_report_type.rb @@ -11,9 +11,16 @@ class UsageReportType < BaseObject end def datasets(**args) - ids = Event.query(nil, subj_id: object[:id], source_id: "datacite-usage").fetch(:data, []).map do |e| + ids = Event.query(nil, subj_id: object[:id]).fetch(:data, []).map do |e| doi_from_url(e[:obj_id]) 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