Skip to content

Commit

Permalink
connection usage datasets. #260
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed May 18, 2019
1 parent ce9f5ce commit fd9476c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/graphql/types/dataset_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions app/graphql/types/usage_report_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/models/usage_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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

0 comments on commit fd9476c

Please sign in to comment.