Skip to content

Commit

Permalink
optimize performance of facets. #224
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Apr 8, 2019
1 parent 95a28b5 commit 573d4b0
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 46 deletions.
2 changes: 1 addition & 1 deletion app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def index
total_for_pages = page[:cursor].present? ? total.to_f : [total.to_f, 10000].min
total_pages = page[:size] > 0 ? (total_for_pages / page[:size]).ceil : 0

@activities = response.results.results
@activities = response.results

options = {}
options[:meta] = {
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 @@ -35,7 +35,7 @@ def index
providers = total > 0 ? facet_by_provider(response.response.aggregations.providers.buckets) : nil
software = total > 0 ? facet_by_software(response.response.aggregations.software.buckets) : nil

@clients = response.results.results
@clients = response.results

options = {}
options[:meta] = {
Expand Down
73 changes: 35 additions & 38 deletions app/controllers/concerns/facetable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,11 @@ 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_ids(ids, size: 1000).results.reduce({}) do |sum, p|
sum[p.symbol.downcase] = p.name
sum
end
providers = Provider.find_by_ids(ids, size: 1000).records.pluck(:symbol, :name).to_h

arr.map do |hsh|
{ "id" => hsh["key"],
"title" => providers[hsh["key"]],
"title" => providers[hsh["key"].upcase],
"count" => hsh["doc_count"] }
end
end
Expand All @@ -104,14 +101,11 @@ def providers_totals(arr)
# generate hash with id and name for each provider in facet

ids = arr.map { |hsh| hsh["key"] }.join(",")
providers = Provider.find_by_ids(ids, size: 1000).results.reduce({}) do |sum, p|
sum[p.symbol.downcase] = p.name
sum
end
providers = Provider.find_by_ids(ids, size: 1000).records.pluck(:symbol, :name).to_h

arr.map do |hsh|
{ "id" => hsh["key"],
"title" => providers[hsh["key"]],
"title" => providers[hsh["key"].upcase],
"count" => hsh["doc_count"],
"temporal" => {
"this_month" => facet_anual(hsh.this_month.buckets),
Expand Down Expand Up @@ -140,53 +134,56 @@ def prefixes_totals(arr)


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

ids = arr.map { |hsh| hsh["key"] }.join(",")
clients = Client.find_by_ids(ids, size: 2000).results.reduce({}) do |sum, p|
sum[p.symbol.downcase] = p.name
sum
end
logger = Logger.new(STDOUT)

arr.map do |hsh|
{ "id" => hsh["key"],
"title" => clients[hsh["key"]],
"count" => hsh["doc_count"],
"temporal" => {
"this_month" => facet_anual(hsh.this_month.buckets),
"this_year" => facet_anual(hsh.this_year.buckets),
"last_year" => facet_anual(hsh.last_year.buckets)},
"states" => facet_by_key(hsh.states.buckets)
}
end
# generate hash with id and name for each client in facet
ids = nil
logger.info "[Benchmark] clients_totals ids " + Benchmark.ms {
ids = arr.map { |hsh| hsh["key"] }.join(",")
}.to_s + " ms"

clients = nil
logger.info "[Benchmark] clients_totals find_by_ids " + Benchmark.ms {
clients = Client.find_by_ids(ids, size: 2000).records.pluck(:symbol, :name).to_h
}.to_s + " ms"

logger.info "[Benchmark] clients_totals map " + Benchmark.ms {
arr = arr.map do |hsh|
{ "id" => hsh["key"],
"title" => clients[hsh["key"].upcase],
"count" => hsh["doc_count"],
"temporal" => {
"this_month" => facet_anual(hsh.this_month.buckets),
"this_year" => facet_anual(hsh.this_year.buckets),
"last_year" => facet_anual(hsh.last_year.buckets)
},
"states" => facet_by_key(hsh.states.buckets)
}
end
}.to_s + " ms"
arr
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).results.reduce({}) do |sum, p|
sum[p.id] = p.name
sum
end
providers = Provider.find_by_id_list(ids).records.pluck(:symbol, :name).to_h

arr.map do |hsh|
{ "id" => hsh["key"],
"title" => providers[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_ids(ids).results.reduce({}) do |sum, p|
sum[p.symbol.downcase] = p.name
sum
end
clients = Client.find_by_ids(ids).records.pluck(:symbol, :name).to_h

arr.map do |hsh|
{ "id" => hsh["key"],
"title" => clients[hsh["key"]],
"title" => clients[hsh["key"].upcase],
"count" => hsh["doc_count"] }
end
end
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 @@ -28,7 +28,7 @@ def index
years = total > 0 ? facet_by_year(response.response.aggregations.years.buckets) : nil
providers = total > 0 ? facet_by_provider(response.response.aggregations.providers.buckets) : nil

@clients = response.results.results
@clients = response.results

options = {}
options[:meta] = {
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def index
else
results = nil
logger.info "[Benchmark] results " + Benchmark.ms {
results = response.results.results
results = response.results
}.to_s + " ms"
total = response.results.total
total_for_pages = page[:cursor].present? ? total.to_f : [total.to_f, 10000].min
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/members_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def index
organization_types = total > 0 ? facet_by_key(response.response.aggregations.organization_types.buckets) : nil
focus_areas = total > 0 ? facet_by_key(response.response.aggregations.focus_areas.buckets) : nil

@members = response.results.results
@members = response.results

options = {}
options[:meta] = {
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def index
organization_types = total > 0 ? facet_by_key(response.response.aggregations.organization_types.buckets) : nil
focus_areas = total > 0 ? facet_by_key(response.response.aggregations.focus_areas.buckets) : nil

@providers = response.results.results
@providers = response.results

options = {}
options[:meta] = {
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def index
providers = total > 0 ? facet_by_provider(response.response.aggregations.providers.buckets) : nil
clients = total > 0 ? facet_by_client(response.response.aggregations.clients.buckets) : nil

@dois = response.results.results
@dois = response.results

options = {}
options[:meta] = {
Expand Down Expand Up @@ -100,7 +100,7 @@ def index
if sample_dois
@dois = sample_dois
else
@dois = response.results.results
@dois = response.results
end
render json: WorkSerializer.new(@dois, options).serialized_json, status: :ok
rescue Elasticsearch::Transport::Transport::Errors::BadRequest => exception
Expand Down

0 comments on commit 573d4b0

Please sign in to comment.