diff --git a/app/graphql/types/dataset_type.rb b/app/graphql/types/dataset_type.rb index c474ef61d..809ecee5e 100644 --- a/app/graphql/types/dataset_type.rb +++ b/app/graphql/types/dataset_type.rb @@ -2,4 +2,15 @@ class DatasetType < BaseObject implements DoiItem + + 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 + + def usage_reports(**args) + ids = Event.query(nil, obj_id: object.id).fetch(:data, []).map do |e| + e[:subj_id] + end + UsageReport.find_by_id(ids, page: { number: 1, size: args[:first] }).fetch(:data, []) + end end diff --git a/app/graphql/types/dataset_usage_report_connection_with_meta_type.rb b/app/graphql/types/dataset_usage_report_connection_with_meta_type.rb new file mode 100644 index 000000000..340e38034 --- /dev/null +++ b/app/graphql/types/dataset_usage_report_connection_with_meta_type.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class DatasetUsageReportConnectionWithMetaType < 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, obj_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 41802bb05..056ff34a1 100644 --- a/app/graphql/types/usage_report_type.rb +++ b/app/graphql/types/usage_report_type.rb @@ -4,6 +4,7 @@ class UsageReportType < BaseObject description "Information about usage reports" field :id, ID, null: false, description: "Usage report ID" + field :client_id, String, null: true, description: "Client who created the report" field :reporting_period, ReportingPeriodType, null: false, description: "Time period covered by the report" field :date_created, String, null: false, description: "Date information was created" field :datasets, UsageReportDatasetConnectionWithMetaType, null: false, description: "Datasets included in usage report", connection: true, max_page_size: 100 do diff --git a/app/models/usage_report.rb b/app/models/usage_report.rb index 482f232d9..9c6ec3044 100644 --- a/app/models/usage_report.rb +++ b/app/models/usage_report.rb @@ -5,6 +5,7 @@ class UsageReport include Modelable def self.find_by_id(id) + ids = id.split(",") base_url = Rails.env.production? ? "https://api.datacite.org/reports" : "https://api.test.datacite.org/reports" return {} unless id.starts_with?(base_url) @@ -51,6 +52,9 @@ def self.parse_message(id: nil, message: nil) { id: id, reporting_period: reporting_period, + client_id: message["client_id"], + year: message["year"], + month: message["month"], date_modified: message["created"] }.compact end end \ No newline at end of file