2021-10-20 16:08:21 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Procesar una lista de URIs para una lista de dominios. Esto nos
|
2021-10-26 14:33:15 +00:00
|
|
|
# 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
|
|
|
|
# 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
|
2021-10-26 14:33:15 +00:00
|
|
|
STAT_NAME = 'uri_collection_job'
|
|
|
|
|
|
|
|
def perform(site_id:, once: true)
|
|
|
|
@site = Site.find site_id
|
2021-10-20 16:08:21 +00:00
|
|
|
|
2022-04-30 13:23:58 +00:00
|
|
|
# Obtener el principio del intervalo anterior
|
2022-04-30 22:35:14 +00:00
|
|
|
beginning_of_interval
|
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-30 13:24:21 +00:00
|
|
|
# Columnas a agrupar
|
2022-04-30 20:55:53 +00:00
|
|
|
columns = Stat::COLUMNS.zip([nil]).to_h
|
2022-04-23 23:32:20 +00:00
|
|
|
|
2022-04-30 22:38:32 +00:00
|
|
|
# Las URIs son la fuente de verdad de las visitas, porque son las
|
|
|
|
# que indican las páginas y recursos descargables, el resto son
|
|
|
|
# imágenes, CSS, JS y tipografías que no nos aportan números
|
|
|
|
# significativos.
|
|
|
|
uri_dimensions = { host: site.hostnames, uri: uris }
|
|
|
|
host_dimensions = { host: site.hostnames }
|
|
|
|
|
2022-04-29 18:04:42 +00:00
|
|
|
# Recorremos todos los hostnames y uris posibles y luego agrupamos
|
|
|
|
# recursivamente para no tener que recalcular, asumiendo que es más
|
|
|
|
# rápido buscar en los rollups indexados que en la tabla en bruto.
|
|
|
|
#
|
|
|
|
# Los referers solo se agrupan por host.
|
2022-04-30 22:35:14 +00:00
|
|
|
columns.each_key do |column|
|
|
|
|
columns[column] = AccessLog.where(**host_dimensions).distinct(column).pluck(column)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Cantidad de visitas por host
|
|
|
|
rollup(name: 'host', dimensions: host_dimensions, filter: uri_dimensions)
|
|
|
|
reduce_rollup(name: 'host', operation: :sum, dimensions: uri_dimensions)
|
|
|
|
|
|
|
|
# Cantidad de visitas por página/recurso
|
|
|
|
rollup(name: 'host|uri', dimensions: uri_dimensions)
|
|
|
|
reduce_rollup(name: 'host|uri', operation: :sum, dimensions: uri_dimensions)
|
|
|
|
|
|
|
|
# Cantidad de visitas host y parámetro
|
|
|
|
columns.each_pair do |column, values|
|
|
|
|
column_name = "host|#{column}"
|
|
|
|
column_dimensions = { host: site.hostnames }
|
|
|
|
column_dimensions[column] = values
|
|
|
|
|
|
|
|
rollup(name: column_name, dimensions: column_dimensions, filter: uri_dimensions)
|
|
|
|
reduce_rollup(name: column_name, dimensions: column_dimensions)
|
2021-10-26 14:33:15 +00:00
|
|
|
end
|
|
|
|
|
2022-04-23 20:50:36 +00:00
|
|
|
stat.touch
|
2021-10-26 14:33:15 +00:00
|
|
|
|
|
|
|
run_again! unless once
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-04-29 18:04:42 +00:00
|
|
|
# Generar un rollup de access logs
|
2022-04-26 19:59:02 +00:00
|
|
|
#
|
|
|
|
# @param :name [String]
|
|
|
|
# @param :beginning [Time]
|
|
|
|
# @param :dimensions [Hash]
|
2022-04-30 22:35:14 +00:00
|
|
|
# @param :filter [Hash]
|
2022-04-26 19:59:02 +00:00
|
|
|
# @return [nil]
|
2022-04-30 23:40:54 +00:00
|
|
|
def rollup(name:, dimensions:, interval: starting_interval, filter: nil)
|
2022-04-30 22:35:14 +00:00
|
|
|
AccessLog.where(**(filter || dimensions))
|
2022-04-30 22:44:02 +00:00
|
|
|
.where('created_at >= ?', beginning_of_interval)
|
2022-04-23 23:32:20 +00:00
|
|
|
.completed_requests
|
|
|
|
.non_robots
|
|
|
|
.group(*dimensions.keys)
|
2022-04-30 23:40:54 +00:00
|
|
|
.rollup(name, interval: interval, update: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Generar rollups con el resto de la información
|
|
|
|
#
|
|
|
|
# @param :name [String]
|
|
|
|
# @param :dimensions [Hash]
|
|
|
|
# @param :filter [Hash]
|
|
|
|
# @return [nil]
|
|
|
|
def reduce_rollup(name:, dimensions:, filter: nil)
|
|
|
|
Stat::INTERVALS.reduce do |_previous, current|
|
|
|
|
rollup(name: name, dimensions: dimensions, filter: filter, interval: current)
|
|
|
|
|
|
|
|
current
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
2022-04-29 18:04:42 +00:00
|
|
|
end
|
2022-04-23 23:32:20 +00:00
|
|
|
|
2021-10-26 14:33:15 +00:00
|
|
|
def stat_name
|
|
|
|
STAT_NAME
|
|
|
|
end
|
|
|
|
|
2022-04-29 20:43:06 +00:00
|
|
|
# Obtiene todas las ubicaciones de archivos
|
|
|
|
#
|
2021-10-26 14:33:15 +00:00
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# TODO: Cambiar al mergear origin-referer
|
2022-04-29 20:43:06 +00:00
|
|
|
def destinations
|
|
|
|
@destinations ||= site.deploys.map(&:destination).select do |d|
|
|
|
|
File.directory?(d)
|
|
|
|
end.map do |d|
|
|
|
|
File.realpath(d)
|
|
|
|
end.uniq
|
2021-10-26 14:33:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Recolecta todas las URIs menos imágenes
|
|
|
|
#
|
2022-04-30 13:03:14 +00:00
|
|
|
# TODO: Para los sitios con DeployLocalizedDomain estamos buscando
|
|
|
|
# URIs de más.
|
|
|
|
#
|
2021-10-26 14:33:15 +00:00
|
|
|
# @return [Array]
|
|
|
|
def uris
|
2022-04-23 21:56:34 +00:00
|
|
|
@uris ||=
|
2022-04-29 20:43:06 +00:00
|
|
|
destinations.map do |destination|
|
|
|
|
locales.map do |locale|
|
|
|
|
uri = "/#{locale}/".squeeze('/')
|
|
|
|
dir = File.join(destination, locale)
|
|
|
|
|
2022-04-29 21:04:43 +00:00
|
|
|
next unless File.directory? dir
|
|
|
|
|
2022-04-29 20:43:06 +00:00
|
|
|
files(dir).map do |f|
|
|
|
|
uri + f
|
|
|
|
end
|
2021-10-26 14:33:15 +00:00
|
|
|
end
|
2022-04-29 21:04:43 +00:00
|
|
|
end.flatten(3).compact
|
2022-04-23 21:56:34 +00:00
|
|
|
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
|