Skip to content

Commit

Permalink
added usage_report graphql type. #260
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed May 16, 2019
1 parent 74d2535 commit d5d29ce
Show file tree
Hide file tree
Showing 12 changed files with 376 additions and 1 deletion.
19 changes: 19 additions & 0 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,25 @@ def other(id:)
set_doi(id)
end

field :usage_reports, UsageReportConnectionWithMetaType, null: false, connection: true, max_page_size: 100 do
argument :first, Int, required: false, default_value: 25
end

def usage_reports(first: nil)
UsageReport.query(nil, page: { number: 1, size: first }).fetch(:data, [])
end

field :usage_report, UsageReportType, null: false do
argument :id, ID, required: true
end

def usage_report(id:)
result = UsageReport.find_by_id(id).fetch(:data, []).first
fail ActiveRecord::RecordNotFound if result.nil?

result
end

def set_doi(id)
doi = doi_from_url(id)
fail ActiveRecord::RecordNotFound if doi.nil?
Expand Down
8 changes: 8 additions & 0 deletions app/graphql/types/reporting_period_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class ReportingPeriodType < BaseObject
description "Information about reporting periods"

field :begin_date, String, null: true, description: "Begin reporting period"
field :end_date, String, null: true, description: "End reporting period"
end
12 changes: 12 additions & 0 deletions app/graphql/types/usage_report_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class UsageReportConnectionWithMetaType < BaseConnection
edge_type(UsageReportEdgeType)
field_class GraphQL::Cache::Field

field :total_count, Integer, null: false, cache: true

def total_count
UsageReport.query(nil, limit: 0).dig(:meta, "total").to_i
end
end
5 changes: 5 additions & 0 deletions app/graphql/types/usage_report_edge_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class UsageReportEdgeType < GraphQL::Types::Relay::BaseEdge
node_type(UsageReportType)
end
9 changes: 9 additions & 0 deletions app/graphql/types/usage_report_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class UsageReportType < BaseObject
description "Information about usage reports"

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"
end
51 changes: 51 additions & 0 deletions app/models/usage_report.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

class UsageReport
# include helper module for PORO models
include Modelable

def self.find_by_id(id)
url = "https://api.datacite.org/reports/#{id}"
response = Maremma.get(url)

return {} if response.status != 200

message = response.body.dig("data", "report")
data = [parse_message(id: id, message: message)]

errors = response.body.fetch("errors", nil)

{ data: data, errors: errors }
end

def self.query(query, options={})
number = (options.dig(:page, :number) || 1).to_i
size = (options.dig(:page, :size) || 25).to_i

url = "https://api.datacite.org/reports?page[size]=#{size}&page[number]=#{number}"

response = Maremma.get(url)

return {} if response.status != 200

data = response.body.dig("data", "reports").map do |message|
parse_message(id: message['id'], message: message)
end
meta = { "total" => response.body.dig("data", "meta", "total") }
errors = response.body.fetch("errors", nil)

{ data: data, meta: meta, errors: errors }
end

def self.parse_message(id: nil, message: nil)
reporting_period = {
begin_date: message.dig("report-header", "reporting-period", "begin-date"),
end_date: message.dig("report-header", "reporting-period", "end-date")
}

{
id: id,
reporting_period: reporting_period,
date_modified: message["created"] }.compact
end
end
49 changes: 49 additions & 0 deletions spec/fixtures/vcr_cassettes/UsageReport/find_by_id/found.yml

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions spec/fixtures/vcr_cassettes/UsageReport/find_by_id/not_found.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions spec/fixtures/vcr_cassettes/UsageReport/query/all.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions spec/fixtures/vcr_cassettes/UsageReport/query/size.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion spec/models/researcher_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

# frozen_string_literal: true

require 'rails_helper'

Expand Down
Loading

0 comments on commit d5d29ce

Please sign in to comment.