diff --git a/app/jobs/concerns/recursive_rollup.rb b/app/jobs/concerns/recursive_rollup.rb index 3539d7d2..28c2e66a 100644 --- a/app/jobs/concerns/recursive_rollup.rb +++ b/app/jobs/concerns/recursive_rollup.rb @@ -46,6 +46,30 @@ module RecursiveRollup end end + # Reducir las estadísticas calculadas aplicando un rollup sobre el + # intervalo más amplio. + # + # @param :name [String] + # @param :beginning [Time] + # @param :operation [Symbol] + # @param :dimensions [Hash] + # @return [nil] + def reduce_rollup(name, beginning, operation, **dimensions) + Stat::INTERVALS.reduce do |previous, current| + recursive_rollup(name: name, + interval_previous: previous, + interval: current, + dimensions: dimensions, + beginning: beginning, + operation: operation) + + # Devolver el intervalo actual + current + end + + nil + end + # @param :dimensions [Hash] # @return [Array] def dimensions_to_jsonb_query(dimensions) diff --git a/app/jobs/stat_collection_job.rb b/app/jobs/stat_collection_job.rb index a67a3c50..65226b5c 100644 --- a/app/jobs/stat_collection_job.rb +++ b/app/jobs/stat_collection_job.rb @@ -9,10 +9,8 @@ class StatCollectionJob < PeriodicJob def perform(site_id:, once: true) @site = Site.find site_id - - # Registrar que se hicieron todas las recolecciones - stat = site.stats.create! name: STAT_NAME beginning = beginning_of_interval + stat = site.stats.create! name: STAT_NAME scope.rollup('builds', **options) @@ -24,16 +22,9 @@ class StatCollectionJob < PeriodicJob rollup.average(:seconds) end - # XXX: Es correcto promediar promedios? - Stat::INTERVALS.reduce do |previous, current| - opts = { interval_previous: previous, interval: current, beginning: beginning, dimensions: { site_id: site.id } } - - recursive_rollup(name: 'builds', **opts) - recursive_rollup(name: 'space_used', operation: :average, **opts) - recursive_rollup(name: 'build_time', operation: :average, **opts) - - current - end + reduce_rollup('builds', beginning, :sum, site_id: site_id) + reduce_rollup('space_used', beginning, :average, site_id: site_id) + reduce_rollup('build_time', beginning, :average, site_id: site_id) stat.touch run_again! unless once @@ -45,10 +36,7 @@ class StatCollectionJob < PeriodicJob # # @return [ActiveRecord::Relation] def scope - @scope ||= site.build_stats - .jekyll - .where('created_at >= ?', beginning_of_interval) - .group(:site_id) + @scope ||= site.build_stats.jekyll.where('created_at >= ?', beginning_of_interval) end # Las opciones por defecto diff --git a/app/jobs/uri_collection_job.rb b/app/jobs/uri_collection_job.rb index 620123db..1553fb22 100644 --- a/app/jobs/uri_collection_job.rb +++ b/app/jobs/uri_collection_job.rb @@ -52,7 +52,7 @@ class UriCollectionJob < PeriodicJob dimensions = { host: host, uri: uri } rollup(name, beginning, **dimensions) - reduce_rollup(name, beginning, **dimensions) + reduce_rollup(name, beginning, :sum, **dimensions) columns.each_pair do |column, values| # Obtener orígenes de visitas por host @@ -62,7 +62,7 @@ class UriCollectionJob < PeriodicJob column_dimensions[column] = value rollup(column_name, beginning, **column_dimensions) - reduce_rollup(column_name, beginning, **column_dimensions) + reduce_rollup(column_name, beginning, :sum, **column_dimensions) end end end @@ -75,7 +75,7 @@ class UriCollectionJob < PeriodicJob beginning: beginning) # Acumular por mes y año - reduce_rollup('host', beginning, **host_dimensions) + reduce_rollup('host', beginning, :sum, **host_dimensions) columns.each_key do |column| square_rollup(name: "host|uri|#{column}", @@ -84,7 +84,7 @@ class UriCollectionJob < PeriodicJob dimensions: host_dimensions, beginning: beginning) - reduce_rollup("host|#{column}", beginning, **host_dimensions) + reduce_rollup("host|#{column}", beginning, :sum, **host_dimensions) end end @@ -110,28 +110,6 @@ class UriCollectionJob < PeriodicJob .rollup(name, interval: starting_interval, update: true) end - # Reducir las estadísticas calculadas aplicando un rollup sobre el - # intervalo más amplio. - # - # @param :name [String] - # @param :beginning [Time] - # @param :dimensions [Hash] - # @return [nil] - def reduce_rollup(name, beginning, **dimensions) - Stat::INTERVALS.reduce do |previous, current| - recursive_rollup(name: name, - interval_previous: previous, - interval: current, - dimensions: dimensions, - beginning: beginning) - - # Devolver el intervalo actual - current - end - - nil - end - def stat_name STAT_NAME end diff --git a/app/views/stats/index.haml b/app/views/stats/index.haml index 2b5d643e..02683781 100644 --- a/app/views/stats/index.haml +++ b/app/views/stats/index.haml @@ -6,8 +6,8 @@ %p %small = t('.last_update') - %time{ datetime: @last_stat.created_at } - #{time_ago_in_words @last_stat.created_at}. + %time{ datetime: @last_stat.updated_at } + #{time_ago_in_words @last_stat.updated_at}. .mb-5 - Stat::INTERVALS.each do |interval|