Skip to content

Commit

Permalink
handle elasticsearch results. #276
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed May 19, 2019
1 parent a4bb6d8 commit bdc1cbf
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 26 deletions.
5 changes: 3 additions & 2 deletions app/graphql/types/client_dataset_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

class ClientDatasetConnectionWithMetaType < BaseConnection
edge_type(DatasetEdgeType)

field :total_count, Integer, null: false
field_class GraphQL::Cache::Field

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

def total_count
args = object.arguments
Expand Down
8 changes: 4 additions & 4 deletions app/graphql/types/client_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ class ClientType < GraphQL::Schema::Object
end

def prefixes(**args)
collection = object.client_prefixes.joins(:prefix)
collection = ClientPrefix.joins(:client, :prefix).where('datacentre.symbol = ?', object.uid)
collection = collection.query(args[:query]) if args[:query].present?
collection = collection.where('YEAR(datacentre_prefixes.created_at) = ?', args[:year]) if args[:year].present?
collection
end

def datasets(**args)
Doi.query(args[:query], client_id: object.uid, resource_type_id: "Dataset", page: { number: 1, size: args[:first] }).records.to_a
Doi.query(args[:query], client_id: object.uid, resource_type_id: "Dataset", page: { number: 1, size: args[:first] }).results.to_a
end

def publications(**args)
Doi.query(args[:query], client_id: object.uid, resource_type_id: "Text", page: { number: 1, size: args[:first] }).records.to_a
Doi.query(args[:query], client_id: object.uid, resource_type_id: "Text", page: { number: 1, size: args[:first] }).results.to_a
end

def softwares(**args)
Doi.query(args[:query], client_id: object.uid, resource_type_id: "Software", page: { number: 1, size: args[:first] }).records.to_a
Doi.query(args[:query], client_id: object.uid, resource_type_id: "Software", page: { number: 1, size: args[:first] }).results.to_a
end
end
32 changes: 19 additions & 13 deletions app/graphql/types/prefix_connection_with_meta_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

class PrefixConnectionWithMetaType < BaseConnection
edge_type(PrefixEdgeType)

field :total_count, Integer, null: false
field :states, [FacetType], null: false
field :years, [FacetType], null: false
field_class GraphQL::Cache::Field

field :total_count, Integer, null: false, cache: true
field :states, [FacetType], null: false, cache: true
field :years, [FacetType], null: false, cache: true

def total_count
object.nodes.size
Expand All @@ -14,10 +15,13 @@ def total_count
def states
args = self.object.arguments

if object.parent.class.name == "Provider"
collection = object.parent.provider_prefixes.joins(:prefix)
if object.parent._index == "providers"
collection = ProviderPrefix.joins(:provider, :prefix).where('allocator.symbol = ?', object.parent.symbol)
collection = collection.where('YEAR(allocator_prefixes.created_at) = ?', args[:year]) if args[:year].present?

collection = collection.state(args[:state].underscore.dasherize) if args[:state].present?
collection = collection.query(args[:query]) if args[:query].present?

puts collection.inspect
if args[:state].present?
[{ id: args[:state],
title: args[:state].underscore.humanize,
Expand All @@ -38,10 +42,11 @@ def states
def years
args = self.object.arguments

if object.parent.class.name == "Provider"
collection = object.parent.provider_prefixes.joins(:prefix)
if object.parent._index == "providers"
collection = ProviderPrefix.joins(:provider, :prefix).where('allocator.symbol = ?', object.parent.symbol)
collection = collection.state(args[:state].underscore.dasherize) if args[:state].present?

collection = collection.query(args[:query]) if args[:query].present?

if args[:year].present?
[{ id: args[:year],
title: args[:year],
Expand All @@ -50,9 +55,10 @@ def years
years = collection.where.not(prefixes: nil).order("YEAR(allocator_prefixes.created_at) DESC").group("YEAR(allocator_prefixes.created_at)").count
years.map { |k,v| { id: k.to_s, title: k.to_s, count: v } }
end
elsif object.parent.class.name == "Client"
collection = object.parent.client_prefixes.joins(:prefix)

elsif object.parent._index == "clients"
collection = ClientPrefix.joins(:client, :prefix).where('datacentre.symbol = ?', object.parent.symbol)
collection = collection.query(args[:query]) if args[:query].present?

if args[:year].present?
[{ id: args[:year],
title: args[:year],
Expand Down
5 changes: 2 additions & 3 deletions app/graphql/types/provider_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class ProviderType < BaseObject
argument :year, String, required: false
argument :software, String, required: false
argument :first, Int, required: false, default_value: 25
argument :after, String, required: false
end

def country
Expand All @@ -40,14 +39,14 @@ def country
end

def prefixes(**args)
collection = object.provider_prefixes.joins(:prefix)
collection = ProviderPrefix.joins(:provider, :prefix).where('allocator.symbol = ?', object.uid)
collection = collection.state(args[:state].underscore.dasherize) if args[:state].present?
collection = collection.query(args[:query]) if args[:query].present?
collection = collection.where('YEAR(allocator_prefixes.created_at) = ?', args[:year]) if args[:year].present?
collection
end

def clients(**args)
Client.query(args[:query], provider_id: object.uid, year: args[:year], software: args[:software], page: { number: 1, size: 500 }).records.to_a
Client.query(args[:query], provider_id: object.uid, year: args[:year], software: args[:software], page: { number: 1, size: args[:first] }).results.to_a
end
end
10 changes: 6 additions & 4 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
class QueryType < BaseObject
field :providers, ProviderConnectionWithMetaType, null: false, connection: true, max_page_size: 100 do
argument :query, String, required: false
argument :first, Int, required: false, default_value: 25
end

def providers(query: nil)
Provider.query(query, page: { number: 1, size: 250 }).records.to_a
def providers(query: nil, year: nil, first: nil)
Provider.query(query, year: year, page: { number: 1, size: first }).results.to_a
end

field :provider, ProviderType, null: false do
Expand All @@ -21,10 +22,11 @@ def provider(id:)
argument :query, String, required: false
argument :year, String, required: false
argument :software, String, required: false
argument :first, Int, required: false, default_value: 25
end

def clients(query: nil, year: nil, software: nil)
Client.query(query, year: year, software: software, page: { number: 1, size: 2000 }).records.to_a
def clients(query: nil, year: nil, software: nil, first: nil)
Client.query(query, year: year, software: software, page: { number: 1, size: first }).results.to_a
end

field :client, ClientType, null: false do
Expand Down

0 comments on commit bdc1cbf

Please sign in to comment.