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 new file mode 100644 index 000000000..75900edba --- /dev/null +++ b/app/graphql/types/usage_report_dataset_connection_with_meta_type.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class UsageReportDatasetConnectionWithMetaType < 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, subj_id: object.parent[:id], source_id: "datacite-usage").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 d723e48d3..44067138e 100644 --- a/app/graphql/types/usage_report_type.rb +++ b/app/graphql/types/usage_report_type.rb @@ -6,4 +6,14 @@ class UsageReportType < BaseObject field :id, ID, null: false, description: "Usage report ID" 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 + argument :first, Int, required: false, default_value: 25 + end + + def datasets(**args) + ids = Event.query(nil, subj_id: object[:id], source_id: "datacite-usage").fetch(:data, []).map do |e| + doi_from_url(e[:obj_id]) + end + ElasticsearchLoader.for(Doi).load_many(ids) + end end