diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb index 3603a9a38..8bdaaf99d 100644 --- a/app/controllers/clients_controller.rb +++ b/app/controllers/clients_controller.rb @@ -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 diff --git a/app/controllers/concerns/facetable.rb b/app/controllers/concerns/facetable.rb index 623febd63..85eddd55d 100644 --- a/app/controllers/concerns/facetable.rb +++ b/app/controllers/concerns/facetable.rb @@ -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, @@ -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 diff --git a/app/controllers/providers_controller.rb b/app/controllers/providers_controller.rb index bbe7f0a5e..e1e8e3f0d 100644 --- a/app/controllers/providers_controller.rb +++ b/app/controllers/providers_controller.rb @@ -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 diff --git a/app/models/doi.rb b/app/models/doi.rb index 86da76702..21a4d030f 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -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 } }, @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 21662017a..ac8e2c787 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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]