5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-16 08:51:42 +00:00
panel/app/jobs/stat_collection_job.rb

65 lines
1.6 KiB
Ruby
Raw Normal View History

# 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
2022-04-23 23:32:20 +00:00
include RecursiveRollup
STAT_NAME = 'stat_collection_job'
def perform(site_id:, once: true)
@site = Site.find site_id
2022-04-23 23:33:02 +00:00
# Registrar que se hicieron todas las recolecciones
stat = site.stats.create! name: STAT_NAME
2022-04-23 23:33:58 +00:00
beginning = beginning_of_interval
2022-04-23 23:33:02 +00:00
scope.rollup('builds', **options)
scope.rollup('space_used', **options) do |rollup|
rollup.average(:bytes)
end
scope.rollup('build_time', **options) do |rollup|
rollup.average(:seconds)
end
# XXX: Es correcto promediar promedios?
Stat::INTERVALS.reduce do |previous, current|
2022-04-23 23:33:58 +00:00
opts = { interval_previous: previous, interval: current, beginning: beginning, dimensions: { site_id: site.id } }
2022-04-23 23:32:20 +00:00
recursive_rollup(name: 'builds', **opts)
recursive_rollup(name: 'space_used', operation: :average, **opts)
recursive_rollup(name: 'build_time', operation: :average, **opts)
current
end
2022-04-23 23:33:02 +00:00
stat.touch
run_again! unless once
end
private
# Los registros a procesar
#
# @return [ActiveRecord::Relation]
def scope
@scope ||= site.build_stats
.jekyll
2022-04-27 16:57:33 +00:00
.where('created_at >= ?', beginning_of_interval)
.group(:site_id)
end
# Las opciones por defecto
#
# @return [Hash]
def options
@options ||= { interval: starting_interval, update: true }
end
2022-04-26 21:50:11 +00:00
def stat_name
STAT_NAME
end
end