2021-10-09 17:45:34 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Genera resúmenes de información para poder mostrar estadísticas y se
|
|
|
|
# corre regularmente a sí misma.
|
2022-04-23 18:06:00 +00:00
|
|
|
class StatCollectionJob < PeriodicJob
|
2021-10-26 14:33:15 +00:00
|
|
|
STAT_NAME = 'stat_collection_job'
|
2021-10-09 17:45:34 +00:00
|
|
|
|
2021-10-26 14:33:15 +00:00
|
|
|
def perform(site_id:, once: true)
|
|
|
|
@site = Site.find site_id
|
2021-10-09 17:45:34 +00:00
|
|
|
|
2021-10-26 14:33:15 +00:00
|
|
|
scope.rollup('builds', **options)
|
2021-10-20 16:14:19 +00:00
|
|
|
|
2021-10-26 14:33:15 +00:00
|
|
|
scope.rollup('space_used', **options) do |rollup|
|
|
|
|
rollup.average(:bytes)
|
|
|
|
end
|
2021-10-09 17:45:34 +00:00
|
|
|
|
2021-10-26 14:33:15 +00:00
|
|
|
scope.rollup('build_time', **options) do |rollup|
|
|
|
|
rollup.average(:seconds)
|
|
|
|
end
|
2021-10-20 16:14:19 +00:00
|
|
|
|
2021-10-26 14:33:15 +00:00
|
|
|
# XXX: Es correcto promediar promedios?
|
|
|
|
Stat::INTERVALS.reduce do |previous, current|
|
|
|
|
rollup(name: 'builds', interval_previous: previous, interval: current)
|
|
|
|
rollup(name: 'space_used', interval_previous: previous, interval: current, operation: :average)
|
|
|
|
rollup(name: 'build_time', interval_previous: previous, interval: current, operation: :average)
|
2021-10-09 17:45:34 +00:00
|
|
|
|
2021-10-26 14:33:15 +00:00
|
|
|
current
|
2021-10-09 17:45:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Registrar que se hicieron todas las recolecciones
|
2021-10-26 14:33:15 +00:00
|
|
|
site.stats.create! name: STAT_NAME
|
2021-10-09 17:45:34 +00:00
|
|
|
|
2021-10-26 14:33:15 +00:00
|
|
|
run_again! unless once
|
2021-10-09 17:45:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-10-26 14:33:15 +00:00
|
|
|
# Genera un rollup recursivo en base al período anterior y aplica una
|
|
|
|
# operación.
|
|
|
|
#
|
|
|
|
# @return [NilClass]
|
|
|
|
def rollup(name:, interval_previous:, interval:, operation: :sum)
|
|
|
|
Rollup.where(name: name, interval: interval_previous)
|
2022-04-23 22:37:08 +00:00
|
|
|
.where('time >= ?', beginning_of_interval.try(:"beginning_of_#{interval}"))
|
2021-10-26 14:33:15 +00:00
|
|
|
.where_dimensions(site_id: site.id)
|
|
|
|
.group("dimensions->'site_id'")
|
|
|
|
.rollup(name, interval: interval, update: true) do |rollup|
|
2022-04-23 18:06:17 +00:00
|
|
|
rollup.try(operation, :value)
|
2021-10-26 14:33:15 +00:00
|
|
|
end
|
2021-10-09 17:45:34 +00:00
|
|
|
end
|
|
|
|
|
2021-10-26 14:33:15 +00:00
|
|
|
# Los registros a procesar
|
2021-10-09 17:45:34 +00:00
|
|
|
#
|
2021-10-26 14:33:15 +00:00
|
|
|
# @return [ActiveRecord::Relation]
|
|
|
|
def scope
|
|
|
|
@scope ||= site.build_stats
|
|
|
|
.jekyll
|
|
|
|
.where('created_at => ?', beginning_of_interval)
|
|
|
|
.group(:site_id)
|
|
|
|
end
|
2021-10-09 17:45:34 +00:00
|
|
|
|
2021-10-26 14:33:15 +00:00
|
|
|
# Las opciones por defecto
|
|
|
|
#
|
|
|
|
# @return [Hash]
|
|
|
|
def options
|
|
|
|
@options ||= { interval: starting_interval, update: true }
|
2021-10-09 17:45:34 +00:00
|
|
|
end
|
|
|
|
end
|