From bdc1cbfffbf53dddbfca931af55079c8750f6b75 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sun, 19 May 2019 08:20:55 +0200 Subject: [PATCH] handle elasticsearch results. #276 --- ...lient_dataset_connection_with_meta_type.rb | 5 +-- app/graphql/types/client_type.rb | 8 ++--- .../types/prefix_connection_with_meta_type.rb | 32 +++++++++++-------- app/graphql/types/provider_type.rb | 5 ++- app/graphql/types/query_type.rb | 10 +++--- 5 files changed, 34 insertions(+), 26 deletions(-) diff --git a/app/graphql/types/client_dataset_connection_with_meta_type.rb b/app/graphql/types/client_dataset_connection_with_meta_type.rb index a5e3457ae..f9f628e2e 100644 --- a/app/graphql/types/client_dataset_connection_with_meta_type.rb +++ b/app/graphql/types/client_dataset_connection_with_meta_type.rb @@ -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 diff --git a/app/graphql/types/client_type.rb b/app/graphql/types/client_type.rb index e32ef3586..e8391f294 100644 --- a/app/graphql/types/client_type.rb +++ b/app/graphql/types/client_type.rb @@ -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 diff --git a/app/graphql/types/prefix_connection_with_meta_type.rb b/app/graphql/types/prefix_connection_with_meta_type.rb index 5dc9cd697..e0eed75d4 100644 --- a/app/graphql/types/prefix_connection_with_meta_type.rb +++ b/app/graphql/types/prefix_connection_with_meta_type.rb @@ -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 @@ -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, @@ -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], @@ -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], diff --git a/app/graphql/types/provider_type.rb b/app/graphql/types/provider_type.rb index 9b0dca276..de1bcfdc4 100644 --- a/app/graphql/types/provider_type.rb +++ b/app/graphql/types/provider_type.rb @@ -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 @@ -40,7 +39,7 @@ 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? @@ -48,6 +47,6 @@ def prefixes(**args) 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 diff --git a/app/graphql/types/query_type.rb b/app/graphql/types/query_type.rb index f37de5f1f..3d825fff9 100644 --- a/app/graphql/types/query_type.rb +++ b/app/graphql/types/query_type.rb @@ -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 @@ -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