Skip to content

Commit

Permalink
Merge pull request #177 from datacite/feature_queries_forstats
Browse files Browse the repository at this point in the history
Feature queries for stats
  • Loading branch information
kjgarza authored Jan 9, 2019
2 parents 8a8683e + 32a6a8b commit 2a6a344
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
18 changes: 18 additions & 0 deletions app/controllers/clients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,24 @@ def destroy
end
end

def totals
page = { size: 25, number: 1 }
page_prov = { size: 2000, number: 1 }

ttl = Client.query("", page: page_prov).map do |client|
response = Doi.query("", client_id: client.symbol.downcase, page: page)
total = response.results.total
states = total > 0 ? facet_by_key(response.response.aggregations.states.buckets) : nil
temporal ={}
temporal[:this_month] = total > 0 ? facet_by_date(response.response.aggregations.this_month.buckets) : nil
temporal[:this_year] = total > 0 ? facet_anual(response.response.aggregations.this_year.buckets) : nil
temporal[:last_year] = total > 0 ? facet_anual(response.response.aggregations.last_year.buckets) : nil
id = client.symbol
{id: id, title: id, count: total, states: states, temporal: temporal}
end
render json: ttl, status: :ok
end

protected

def set_include
Expand Down
30 changes: 30 additions & 0 deletions app/controllers/concerns/facetable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ def facet_by_year(arr)
end
end

def facet_anual(arr)
arr.map do |hsh|
{ "id" => hsh["key"][0..3],
"title" => hsh["key"][0..3],
"count" => hsh["doc_count"] }
end
end

def facet_by_date(arr)
arr.map do |hsh|
{ "id" => hsh["key"][0..9],
"title" => hsh["key"][0..9],
"count" => hsh["doc_count"] }
end
end

def facet_by_cumulative_year(arr)
arr.map do |hsh|
{ "id" => hsh["key"].to_s,
Expand Down Expand Up @@ -118,6 +134,20 @@ def facet_by_client(arr)
"count" => hsh["doc_count"] }
end
end

# def get_all_providers_aggs
# page = { size: 25, number: 1}
# response = Doi.query("", page: page)
# after = response.response.aggregations.providers_x.after_key.doi ||=""
# aggs = response.response.aggregations.providers_x.buckets
# loop do
# resp = Doi.query("", {after_key: after })
# aggs = aggs.concat resp.response.aggregations.providers_x.buckets
# after = response.response.aggregations.providers_x.after_key.doi
# break if after.nil?
# end
# aggs
# end
end
end

20 changes: 20 additions & 0 deletions app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,26 @@ def update
end
end

def totals
page = { size: 25, number: 1}
page_prov = { size: 500, number: 1}

ttl = Provider.query("", page: page_prov).map do |provider|
response = Doi.query("", provider_id: provider.symbol.downcase, page: page)
total = response.results.total
states = total > 0 ? facet_by_key(response.response.aggregations.states.buckets) : nil
temporal ={}
temporal[:this_month] = total > 0 ? facet_by_date(response.response.aggregations.this_month.buckets) : nil
temporal[:this_year] = total > 0 ? facet_anual(response.response.aggregations.this_year.buckets) : nil
temporal[:last_year] = total > 0 ? facet_anual(response.response.aggregations.last_year.buckets) : nil
# temporal[:providers] = total > 0 ? facet_by_key(response.response.aggregations.provider_x.buckets) : nil
id = provider.symbol
{id: id, title: id, count: total, states: states, temporal: temporal}
end
render json: ttl, status: :ok
end


# don't delete, but set deleted_at timestamp
# a provider with clients or prefixes can't be deleted
def destroy
Expand Down
7 changes: 7 additions & 0 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ def as_indexed_json(options={})
end

def self.query_aggregations
beginning_of_year = DateTime.current.beginning_of_year.strftime('%Y-%m-%d')
beginning_of_last_year = DateTime.current.beginning_of_year.last_year.strftime('%Y-%m-%d')
beginning_of_month = DateTime.current.beginning_of_month.strftime('%Y-%m-%d')

{
resource_types: { terms: { field: 'types.resourceTypeGeneral', size: 15, min_doc_count: 1 } },
states: { terms: { field: 'aasm_state', size: 15, min_doc_count: 1 } },
Expand All @@ -316,6 +320,9 @@ def self.query_aggregations
link_checks_citation_doi: { value_count: { field: "landing_page.citationDoi" } },
links_checked: { value_count: { field: "landing_page.checked" } },
sources: { terms: { field: 'source', size: 15, min_doc_count: 1 } },
this_month: { date_range: { field: 'created', time_zone: "CET", ranges: {from: beginning_of_month, to: "now/d"} } },
this_year: { date_range: { field: 'created', time_zone: "CET", ranges: {from: beginning_of_year, to: "now/d"} } },
last_year: { date_range: { field: 'created', time_zone: "CET", ranges: {from: beginning_of_last_year, to: beginning_of_year} } }
}
end

Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
get 'dois/random', :to => 'dois#random'
get 'dois/:id/get-url', :to => 'dois#get_url', constraints: { :id => /.+/ }
get 'dois/get-dois', :to => 'dois#get_dois'
get 'providers/totals', :to => 'providers#totals'
get 'clients/totals', :to => 'clients#totals'

resources :heartbeat, only: [:index]
resources :index, only: [:index]
Expand Down

0 comments on commit 2a6a344

Please sign in to comment.