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
|
2022-04-23 23:32:20 +00:00
|
|
|
include RecursiveRollup
|
|
|
|
|
2021-10-26 14:33:15 +00:00
|
|
|
STAT_NAME = 'stat_collection_job'
|
2021-10-09 17:45:34 +00:00
|
|
|
|
2024-04-10 14:00:18 +00:00
|
|
|
def perform(site:, once: true)
|
|
|
|
@site = site
|
2022-04-23 23:33:58 +00:00
|
|
|
beginning = beginning_of_interval
|
2022-04-30 17:39:31 +00:00
|
|
|
stat = site.stats.create! name: STAT_NAME
|
2022-04-23 23:33:02 +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
|
|
|
|
2024-04-10 14:00:18 +00:00
|
|
|
dimensions = { site_id: site.id }
|
2022-04-30 22:35:14 +00:00
|
|
|
|
|
|
|
reduce_rollup(name: 'builds', operation: :sum, dimensions: dimensions)
|
|
|
|
reduce_rollup(name: 'space_used', operation: :average, dimensions: dimensions)
|
|
|
|
reduce_rollup(name: 'build_time', operation: :average, dimensions: dimensions)
|
2021-10-09 17:45:34 +00:00
|
|
|
|
2022-04-23 23:33:02 +00:00
|
|
|
stat.touch
|
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
|
|
|
# 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
|
2022-04-30 17:48:42 +00:00
|
|
|
@scope ||= site.build_stats.jekyll.where('build_stats.created_at >= ?', beginning_of_interval).group(:site_id)
|
2021-10-26 14:33:15 +00:00
|
|
|
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
|
2022-04-26 21:50:11 +00:00
|
|
|
|
|
|
|
def stat_name
|
|
|
|
STAT_NAME
|
|
|
|
end
|
2021-10-09 17:45:34 +00:00
|
|
|
end
|