Skip to content

Commit

Permalink
Avoid database calls in client and prefixes facets. #468
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Apr 25, 2020
1 parent a30c761 commit a68fcb3
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 66 deletions.
4 changes: 2 additions & 2 deletions app/controllers/client_prefixes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def index
total = response.results.total
total_pages = page[:size].positive? ? (total.to_f / page[:size]).ceil : 0
years = total.positive? ? facet_by_year(response.response.aggregations.years.buckets) : nil
providers = total.positive? ? facet_by_provider(response.response.aggregations.providers.buckets) : nil
clients = total.positive? ? facet_by_client(response.response.aggregations.clients.buckets) : nil
providers = total.positive? ? facet_by_combined_key(response.response.aggregations.providers.buckets) : nil
clients = total.positive? ? facet_by_combined_key(response.response.aggregations.clients.buckets) : nil

client_prefixes = response.results

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/clients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def index
total = response.results.total
total_pages = page[:size] > 0 ? (total.to_f / page[:size]).ceil : 0
years = total > 0 ? facet_by_key_as_string(response.response.aggregations.years.buckets) : nil
providers = total > 0 ? facet_by_provider(response.response.aggregations.providers.buckets) : nil
providers = total > 0 ? facet_by_combined_key(response.response.aggregations.providers.buckets) : nil
software = total > 0 ? facet_by_software(response.response.aggregations.software.buckets) : nil
client_types = total > 0 ? facet_by_key(response.response.aggregations.client_types.buckets) : nil
certificates = total > 0 ? facet_by_key(response.response.aggregations.certificates.buckets) : nil
Expand Down
37 changes: 0 additions & 37 deletions app/controllers/concerns/facetable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,6 @@ def facet_by_resource_type(arr)
end
end

def facet_by_provider(arr)
# generate hash with id and name for each provider in facet

ids = arr.map { |hsh| hsh["key"] }.join(",")
providers = Provider.find_by_id(ids, size: 1000).records.pluck(:symbol, :name).to_h

arr.map do |hsh|
{ "id" => hsh["key"],
"title" => providers[hsh["key"].upcase],
"count" => hsh["doc_count"] }
end
end

def facet_by_year_month(arr)
arr.map do |hsh|
month = hsh["key_as_string"][5..6].to_i
Expand Down Expand Up @@ -372,30 +359,6 @@ def clients_totals(arr)
end
end

def facet_by_provider_ids(arr)
# generate hash with id and name for each provider in facet
ids = arr.map { |hsh| hsh["key"] }.join(",")
providers = Provider.find_by_id_list(ids).records.pluck(:symbol, :name).to_h

arr.map do |hsh|
{ "id" => hsh["key"],
"title" => providers[hsh["key"].upcase],
"count" => hsh["doc_count"] }
end
end

def facet_by_client(arr)
# generate hash with id and name for each client in facet
ids = arr.map { |hsh| hsh["key"] }.join(",")
clients = Client.find_by_id(ids).records.pluck(:symbol, :name).to_h

arr.map do |hsh|
{ "id" => hsh["key"],
"title" => clients[hsh["key"].upcase],
"count" => hsh["doc_count"] }
end
end

def facet_by_combined_key(arr)
arr.map do |hsh|
id, title = hsh["key"].split(":", 2)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/data_centers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def index
total = response.results.total
total_pages = page[:size] > 0 ? (total.to_f / page[:size]).ceil : 0
years = total > 0 ? facet_by_year(response.response.aggregations.years.buckets) : nil
providers = total > 0 ? facet_by_provider(response.response.aggregations.providers.buckets) : nil
providers = total > 0 ? facet_by_combined_key(response.response.aggregations.providers.buckets) : nil

@clients = response.results

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/prefixes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def index
total_pages = page[:size].positive? ? (total.to_f / page[:size]).ceil : 0
years = total.positive? ? facet_by_year(response.response.aggregations.years.buckets) : nil
states = total.positive? ? facet_by_key(response.response.aggregations.states.buckets) : nil
providers = total.positive? ? facet_by_provider(response.response.aggregations.providers.buckets) : nil
clients = total.positive? ? facet_by_client(response.response.aggregations.clients.buckets) : nil
providers = total.positive? ? facet_by_combined_key(response.response.aggregations.providers.buckets) : nil
clients = total.positive? ? facet_by_combined_key(response.response.aggregations.clients.buckets) : nil

prefixes = response.results

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/provider_prefixes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def index
total_pages = page[:size].positive? ? (total.to_f / page[:size]).ceil : 0
years = total.positive? ? facet_by_year(response.response.aggregations.years.buckets) : nil
states = total.positive? ? facet_by_key(response.response.aggregations.states.buckets) : nil
providers = total.positive? ? facet_by_provider(response.response.aggregations.providers.buckets) : nil
providers = total.positive? ? facet_by_combined_key(response.response.aggregations.providers.buckets) : nil

provider_prefixes = response.results

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/repositories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def index
total = response.results.total
total_pages = page[:size] > 0 ? (total.to_f / page[:size]).ceil : 0
years = total > 0 ? facet_by_year(response.response.aggregations.years.buckets) : nil
providers = total > 0 ? facet_by_provider(response.response.aggregations.providers.buckets) : nil
providers = total > 0 ? facet_by_combined_key(response.response.aggregations.providers.buckets) : nil
software = total > 0 ? facet_by_software(response.response.aggregations.software.buckets) : nil
certificates = total > 0 ? facet_by_key(response.response.aggregations.certificates.buckets) : nil
client_types = total > 0 ? facet_by_key(response.response.aggregations.client_types.buckets) : nil
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/repository_prefixes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def index
total = response.results.total
total_pages = page[:size].positive? ? (total.to_f / page[:size]).ceil : 0
years = total.positive? ? facet_by_year(response.response.aggregations.years.buckets) : nil
providers = total.positive? ? facet_by_provider(response.response.aggregations.providers.buckets) : nil
repositories = total.positive? ? facet_by_client(response.response.aggregations.clients.buckets) : nil
providers = total.positive? ? facet_by_combined_key(response.response.aggregations.providers.buckets) : nil
repositories = total.positive? ? facet_by_combined_key(response.response.aggregations.clients.buckets) : nil

repository_prefixes = response.results

Expand Down
12 changes: 0 additions & 12 deletions app/graphql/types/base_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,4 @@ def facet_by_combined_key(arr)
"count" => hsh["doc_count"] }
end
end

def facet_by_client(arr)
# generate hash with id and name for each client in facet
ids = arr.map { |hsh| hsh["key"] }.join(",")
clients = Client.find_by_id(ids).records.pluck(:symbol, :name).to_h

arr.map do |hsh|
{ "id" => hsh["key"],
"title" => clients[hsh["key"].upcase],
"count" => hsh["doc_count"] }
end
end
end
2 changes: 1 addition & 1 deletion app/graphql/types/book_chapter_connection_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def repositories
args = prepare_args(object.arguments)

res = response(args)
res.results.total.positive? ? facet_by_client(res.response.aggregations.clients.buckets) : []
res.results.total.positive? ? facet_by_combined_key(res.response.aggregations.clients.buckets) : []
end

def affiliations
Expand Down
12 changes: 10 additions & 2 deletions app/models/client_prefix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def self.query_aggregations
{
years: { date_histogram: { field: 'created_at', interval: 'year', format: 'year', order: { _key: "desc" }, min_doc_count: 1 },
aggs: { bucket_truncate: { bucket_sort: { size: 10 } } } },
providers: { terms: { field: "provider_id", size: 15, min_doc_count: 1 } },
clients: { terms: { field: "client_id", size: 15, min_doc_count: 1 } },
providers: { terms: { field: "provider_id_and_name", size: 10, min_doc_count: 1 } },
clients: { terms: { field: "client_id_and_name", size: 10, min_doc_count: 1 } },
}
end

Expand All @@ -74,6 +74,10 @@ def client_id
client.symbol.downcase
end

def client_id_and_name
"#{client_id}:#{client.name}" if client.present?
end

# convert external id / internal id
def client_id=(value)
logger.warn value.inspect
Expand All @@ -100,6 +104,10 @@ def provider_id
client.provider_id if client.present?
end

def provider_id_and_name
"#{client.provider_id}:#{client.provider.name}" if client.present?
end

def provider
client.provider if client.present?
end
Expand Down
16 changes: 14 additions & 2 deletions app/models/prefix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def self.query_aggregations
states: { terms: { field: 'state', size: 3, min_doc_count: 1 } },
years: { date_histogram: { field: 'created_at', interval: 'year', format: 'year', order: { _key: "desc" }, min_doc_count: 1 },
aggs: { bucket_truncate: { bucket_sort: { size: 10 } } } },
providers: { terms: { field: 'provider_ids', size: 10, min_doc_count: 1 } },
clients: { terms: { field: 'client_ids', size: 10, min_doc_count: 1 } },
providers: { terms: { field: 'provider_ids_and_names', size: 10, min_doc_count: 1 } },
clients: { terms: { field: 'client_ids_and_names', size: 10, min_doc_count: 1 } },
}
end

Expand All @@ -81,10 +81,22 @@ def client_ids
clients.pluck(:symbol).map(&:downcase)
end

def client_ids_and_name
clients.pluck(:symbol, :name).map do |p|
"#{p[0].downcase}:#{p[1]}"
end
end

def provider_ids
providers.pluck(:symbol).map(&:downcase)
end

def provider_ids_and_names
providers.pluck(:symbol, :name).map do |p|
"#{p[0].downcase}:#{p[1]}"
end
end

def client_prefix_ids
client_prefixes.pluck(:uid)
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def self.query_aggregations
{
years: { date_histogram: { field: 'created', interval: 'year', format: 'year', order: { _key: "desc" }, min_doc_count: 1 },
aggs: { bucket_truncate: { bucket_sort: { size: 10 } } } },
cumulative_years: { terms: { field: 'cumulative_years', size: 15, min_doc_count: 1, order: { _count: "asc" } } },
cumulative_years: { terms: { field: 'cumulative_years', size: 10, min_doc_count: 1, order: { _count: "asc" } } },
regions: { terms: { field: 'region', size: 10, min_doc_count: 1 } },
member_types: { terms: { field: 'member_type', size: 10, min_doc_count: 1 } },
organization_types: { terms: { field: 'organization_type', size: 10, min_doc_count: 1 } },
Expand Down
6 changes: 5 additions & 1 deletion app/models/provider_prefix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def self.query_aggregations
states: { terms: { field: 'state', size: 2, min_doc_count: 1 } },
years: { date_histogram: { field: 'created_at', interval: 'year', format: 'year', order: { _key: "desc" }, min_doc_count: 1 },
aggs: { bucket_truncate: { bucket_sort: { size: 10 } } } },
providers: { terms: { field: 'provider_id', size: 15, min_doc_count: 1 } },
providers: { terms: { field: 'provider_id_and_name', size: 10, min_doc_count: 1 } },
}
end

Expand All @@ -83,6 +83,10 @@ def provider_id
provider.symbol.downcase
end

def provider_id_and_name
"#{provider_id}:#{provider.name}" if provider.present?
end

# convert external id / internal id
def provider_id=(value)
r = Provider.where(symbol: value).first
Expand Down

0 comments on commit a68fcb3

Please sign in to comment.