From af8e8549ba2e13bc5f8e0837346b9d09ae742e14 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Fri, 22 Nov 2019 14:28:46 +0100 Subject: [PATCH] revert to previous state for totals aggregations. datacite/datacite#359 --- app/controllers/clients_controller.rb | 4 +- app/controllers/concerns/facetable.rb | 30 ++++++++++---- app/controllers/export_controller.rb | 2 +- app/controllers/prefixes_controller.rb | 4 +- app/controllers/providers_controller.rb | 2 +- app/models/concerns/indexable.rb | 11 +++++- app/models/doi.rb | 52 ++++++++----------------- 7 files changed, 56 insertions(+), 49 deletions(-) diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb index 04ec67567..54701fdcb 100644 --- a/app/controllers/clients_controller.rb +++ b/app/controllers/clients_controller.rb @@ -155,9 +155,11 @@ def destroy end def totals + return [] unless params[:provider_id].present? + page = { size: 0, number: 1 } state = current_user.present? && current_user.is_admin_or_staff? && params[:state].present? ? params[:state] : "registered,findable" - response = Doi.query(nil, provider_id: params[:provider_id], state: state, page: page, totals_agg: true) + response = Doi.query(nil, provider_id: params[:provider_id], state: state, page: page, totals_agg: "client") registrant = clients_totals(response.response.aggregations.clients_totals.buckets) render json: registrant, status: :ok diff --git a/app/controllers/concerns/facetable.rb b/app/controllers/concerns/facetable.rb index ce79659c4..e4889feb7 100644 --- a/app/controllers/concerns/facetable.rb +++ b/app/controllers/concerns/facetable.rb @@ -35,8 +35,8 @@ def facet_by_year(arr) def facet_annual(arr) arr.map do |hsh| - { "id" => hsh["key_as_string"], - "title" => hsh["key_as_string"], + { "id" => hsh["key"][0..3], + "title" => hsh["key"][0..3], "count" => hsh["doc_count"] } end end @@ -189,8 +189,6 @@ def facet_counts_by_year_month(hash) "yearMonths" => arr } end - - def facet_by_relation_type(arr) arr.map do |hsh| arr = hsh.dig("year_months", "buckets").map do |h| @@ -323,7 +321,13 @@ def providers_totals(arr) { "id" => hsh["key"], "title" => providers[hsh["key"].upcase], "count" => hsh["doc_count"], - "year" => facet_annual(hsh.year.buckets) + "temporal" => { + "this_month" => facet_annual(hsh.this_month.buckets), + "this_year" => facet_annual(hsh.this_year.buckets), + "last_year" => facet_annual(hsh.last_year.buckets), + "two_years_ago" => facet_annual(hsh.two_years_ago.buckets) + }, + "states" => facet_by_key(hsh.states.buckets) } end end @@ -333,12 +337,16 @@ def prefixes_totals(arr) { "id" => hsh["key"], "title" => hsh["key"], "count" => hsh["doc_count"], - "year" => facet_annual(hsh.year.buckets) + "temporal" => { + "this_month" => facet_annual(hsh.this_month.buckets), + "this_year" => facet_annual(hsh.this_year.buckets), + "last_year" => facet_annual(hsh.last_year.buckets) + }, + "states" => facet_by_key(hsh.states.buckets) } end end - def clients_totals(arr) logger = Logger.new(STDOUT) @@ -348,7 +356,13 @@ def clients_totals(arr) { "id" => hsh["key"], "title" => clients[hsh["key"].upcase], "count" => hsh["doc_count"], - "year" => facet_annual(hsh.year.buckets) + "temporal" => { + "this_month" => facet_annual(hsh.this_month.buckets), + "this_year" => facet_annual(hsh.this_year.buckets), + "last_year" => facet_annual(hsh.last_year.buckets), + "two_years_ago" => facet_annual(hsh.two_years_ago.buckets) + }, + "states" => facet_by_key(hsh.states.buckets) } end end diff --git a/app/controllers/export_controller.rb b/app/controllers/export_controller.rb index a856dbaac..d2012d1b7 100644 --- a/app/controllers/export_controller.rb +++ b/app/controllers/export_controller.rb @@ -147,7 +147,7 @@ def organizations end # Get doi counts via DOIS query and combine next to clients. - response = Doi.query(nil, state: "registered,findable", page: { size: 0, number: 1}, totals_agg: true) + response = Doi.query(nil, state: "registered,findable", page: { size: 0, number: 1}, totals_agg: "client") client_totals = {} totals_buckets = response.response.aggregations.clients_totals.buckets diff --git a/app/controllers/prefixes_controller.rb b/app/controllers/prefixes_controller.rb index ec5316901..ac668df83 100644 --- a/app/controllers/prefixes_controller.rb +++ b/app/controllers/prefixes_controller.rb @@ -136,8 +136,10 @@ def update end def totals + return [] unless params[:client_id].present? + page = { size: 0, number: 1} - response = Doi.query(nil, client_id: params[:client_id], state: "findable,registered", page: page, totals_agg: true) + response = Doi.query(nil, client_id: params[:client_id], state: "findable,registered", page: page, totals_agg: "prefix") registrant = prefixes_totals(response.response.aggregations.prefixes_totals.buckets) render json: registrant, status: :ok diff --git a/app/controllers/providers_controller.rb b/app/controllers/providers_controller.rb index 5fe9b3926..4b01243b2 100644 --- a/app/controllers/providers_controller.rb +++ b/app/controllers/providers_controller.rb @@ -279,7 +279,7 @@ def totals page = { size: 0, number: 1 } state = current_user.present? && current_user.is_admin_or_staff? && params[:state].present? ? params[:state] : "registered,findable" - response = Doi.query(nil, state: state, page: page, totals_agg: true) + response = Doi.query(nil, state: state, page: page, totals_agg: "provider") registrant = providers_totals(response.response.aggregations.providers_totals.buckets) render json: registrant, status: :ok diff --git a/app/models/concerns/indexable.rb b/app/models/concerns/indexable.rb index f5b433f11..15a49e804 100644 --- a/app/models/concerns/indexable.rb +++ b/app/models/concerns/indexable.rb @@ -110,7 +110,16 @@ def get_aggregations_hash(options={}) end def query(query, options={}) - aggregations = options[:totals_agg] == true ? totals_aggregations : get_aggregations_hash(options) + if options[:totals_agg] == "provider" + aggregations = provider_aggregations + elsif options[:totals_agg] == "client" + aggregations = client_aggregations + elsif options[:totals_agg] == "prefix" + aggregations = prefix_aggregations + else + aggregations = get_aggregations_hash(options) + end + options[:page] ||= {} options[:page][:number] ||= 1 options[:page][:size] ||= 25 diff --git a/app/models/doi.rb b/app/models/doi.rb index 3b1d0fb43..d262ae2f8 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -483,46 +483,26 @@ def self.query_aggregations } end - def self.totals_aggregations - # { - # providers_totals: { - # composite: { - # size: 50, - # sources: [ - # { provider: { terms: { field: "provider_id", size: ::Provider.__elasticsearch__.count, min_doc_count: 1 } } }, - # { year: { date_histogram: { field: "created", interval: "year", format: "yyyy" } } } - # ] - # } - # }, - # clients_totals: { - # composite: { - # size: 50, - # sources: [ - # { client: { terms: { field: "client_id", size: ::Client.__elasticsearch__.count, min_doc_count: 1 } } }, - # { year: { date_histogram: { field: "created", interval: "year", format: "yyyy" } } } - # ] - # } - # }, - # prefixes_totals: { - # composite: { - # size: 50, - # sources: [ - # { prefix: { terms: { field: "prefix", size: ::Prefix.count, min_doc_count: 1 } } }, - # { year: { date_histogram: { field: "created", interval: "year", format: "yyyy" } } } - # ] - # } - # } - # } + def self.provider_aggregations + { providers_totals: { terms: { field: 'provider_id', size: ::Provider.__elasticsearch__.count, min_doc_count: 1 }, aggs: sub_aggregations} } + end - { - providers_totals: { terms: { field: 'provider_id', size: 250, min_doc_count: 1 }, aggs: sub_aggregations }, - clients_totals: { terms: { field: 'client_id', size: 2000, min_doc_count: 1 }, aggs: sub_aggregations }, - prefixes_totals: { terms: { field: 'prefix', size: 3000, min_doc_count: 1 }, aggs: sub_aggregations }, - } + def self.client_aggregations + { clients_totals: { terms: { field: 'client_id', size: ::Client.__elasticsearch__.count, min_doc_count: 1 }, aggs: sub_aggregations } } + end + + def self.prefix_aggregations + { prefixes_totals: { terms: { field: 'prefix', size: ::Prefix.count, min_doc_count: 1 }, aggs: sub_aggregations } } end def self.sub_aggregations - { year: { date_histogram: { field: "created", interval: "year", format: "yyyy" } } } + { + states: { terms: { field: 'aasm_state', size: 4, min_doc_count: 1 } }, + this_month: { date_range: { field: 'created', ranges: { from: "now/M", to: "now/d" } } }, + this_year: { date_range: { field: 'created', ranges: { from: "now/y", to: "now/d" } } }, + last_year: { date_range: { field: 'created', ranges: { from: "now-1y/y", to: "now/y-1d" } } }, + two_years_ago: { date_range: { field: 'created', ranges: { from: "now-2y/y", to: "now-1y/y-1d" } } } + } end def self.query_fields