5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-07-01 22:16:07 +00:00
panel/app/jobs/uri_collection_job.rb

153 lines
3.8 KiB
Ruby
Raw Normal View History

2021-10-20 16:08:21 +00:00
# frozen_string_literal: true
# Procesar una lista de URIs para una lista de dominios. Esto nos
# permite procesar estadísticas a demanda.
#
# Hay varias cosas acá que van a convertirse en métodos propios, como la
# detección de URIs de un sitio (aunque la versión actual detecta todas
# las páginas y no solo las de posts como tenemos planeado, hay que
# resolver eso).
#
# Los hostnames de un sitio van a poder obtenerse a partir de
# Site#hostnames con la garantía de que son únicos.
class UriCollectionJob < PeriodicJob
2022-04-23 23:32:20 +00:00
include RecursiveRollup
# Ignoramos imágenes porque suelen ser demasiadas y no aportan a las
# estadísticas.
2022-04-23 21:56:34 +00:00
IMAGES = %w[.png .jpg .jpeg .gif .webp .jfif].freeze
STAT_NAME = 'uri_collection_job'
def perform(site_id:, once: true)
@site = Site.find site_id
2022-04-26 19:57:16 +00:00
return unless File.directory? destination
2021-10-20 16:08:21 +00:00
2022-04-23 20:50:36 +00:00
# Recordar la última vez que se corrió la tarea
stat = site.stats.create! name: STAT_NAME
2022-04-23 23:32:20 +00:00
name = 'host|uri'
beginning = beginning_of_interval
site.hostnames.each do |host|
2022-04-23 23:32:20 +00:00
break if stop?
2022-04-23 20:50:36 +00:00
2021-10-20 16:08:21 +00:00
uris.each do |uri|
2022-04-23 23:32:20 +00:00
break if stop?
rollup_uri(uri, host, name, beginning)
end
2022-04-23 23:32:20 +00:00
rollup_host(host, name, beginning)
end
2022-04-23 20:50:36 +00:00
stat.touch
run_again! unless once
end
private
2022-04-23 23:32:20 +00:00
def rollup_uri(uri, host, name, beginning)
dimensions = { host: host, uri: uri }
AccessLog.where(**dimensions)
.where('created_at >= ?', beginning)
.completed_requests
.non_robots
.group(*dimensions.keys)
.rollup(name, interval: starting_interval, update: true)
# Reducir las estadísticas calculadas aplicando un rollup sobre el
# intervalo más amplio.
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
end
def rollup_host(host, name, beginning)
dimensions = { host: host }
new_name = 'host'
recursive_rollup(name: name,
new_name: new_name,
interval_previous: starting_interval,
interval: starting_interval,
dimensions: dimensions,
beginning: beginning)
Stat::INTERVALS.reduce do |previous, current|
recursive_rollup(name: new_name,
interval_previous: previous,
interval: current,
dimensions: dimensions,
beginning: beginning)
current
end
end
def stat_name
STAT_NAME
end
# @return [String]
#
# TODO: Cambiar al mergear origin-referer
def destination
@destination ||= site.deploys.find_by(type: 'DeployLocal').destination
end
# Recolecta todas las URIs menos imágenes
#
# @return [Array]
def uris
2022-04-23 21:56:34 +00:00
@uris ||=
locales.map do |locale|
uri = "/#{locale}/".squeeze('/')
dir = File.join(destination, locale)
files(dir).map do |f|
uri + f
end
2022-04-23 21:56:34 +00:00
end.flatten(2)
end
# @return [Array]
def locales
@locales ||= ['', site.locales.map(&:to_s)].flatten(1)
end
# @param :dir [String]
# @return [Array]
def files(dir)
Dir.chdir(dir) do
pages = Dir.glob('**/*.html')
files = Dir.glob('public/**/*')
files = remove_directories files
files = remove_images files
[pages, files].flatten(1)
end
end
# @param :files [Array]
# @return [Array]
def remove_directories(files)
files.reject do |f|
File.directory? f
end
end
def remove_images(files)
files.reject do |f|
IMAGES.include? File.extname(f).downcase
2021-10-20 16:08:21 +00:00
end
end
end