Skip to content

Commit

Permalink
revert to previous state for totals aggregations. datacite/datacite#359
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Nov 22, 2019
1 parent 598f3e6 commit af8e854
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 49 deletions.
4 changes: 3 additions & 1 deletion app/controllers/clients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 22 additions & 8 deletions app/controllers/concerns/facetable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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|
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/export_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/prefixes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -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
Expand Down
11 changes: 10 additions & 1 deletion app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 16 additions & 36 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit af8e854

Please sign in to comment.