From 8d38d0d2aedca550e5d8815f6d4b45c70138cfdc Mon Sep 17 00:00:00 2001 From: f Date: Wed, 20 Oct 2021 13:14:19 -0300 Subject: [PATCH] =?UTF-8?q?recolecci=C3=B3n=20de=20estad=C3=ADsticas=20con?= =?UTF-8?q?=20mejor=20performance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/jobs/stat_collection_job.rb | 42 ++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/app/jobs/stat_collection_job.rb b/app/jobs/stat_collection_job.rb index 6f8470b8..a49a1635 100644 --- a/app/jobs/stat_collection_job.rb +++ b/app/jobs/stat_collection_job.rb @@ -21,14 +21,20 @@ class StatCollectionJob < ApplicationJob COLUMNS = %i[uri].freeze def perform(once: false) - Stat::INTERVALS.each do |interval| - options = { interval: interval } + Site.find_each do |site| + hostnames = [site.hostname, site.alternative_hostnames].flatten + + # Usamos el primero porque luego podemos hacer un rollup recursivo + options = { interval: Stat::INTERVALS.first } # Visitas por hostname - AccessLog.completed_requests.non_robots.pages.group(:host).rollup('host', **options) + hostnames.each do |hostname| + AccessLog.where(host: hostname).completed_requests.non_robots.pages.group(:host).rollup('host', **options) - combined_columns(**options) - stats_by_site(**options) + combined_columns(hostname, **options) + end + + stats_by_site(site, **options) end # Registrar que se hicieron todas las recolecciones @@ -40,9 +46,15 @@ class StatCollectionJob < ApplicationJob private # Combinación de columnas - def combined_columns(**options) + def combined_columns(hostname, **options) + where = { host: hostname } + COLUMNS.each do |column| - AccessLog.completed_requests.non_robots.group(:host, column).rollup("host|#{column}", **options) + AccessLog.where(host: hostname).pluck(Arel.sql("distinct #{column}")).each do |value| + where[column] = value + + AccessLog.where(**where).completed_requests.non_robots.group(:host, column).rollup("host|#{column}", **options) + end end end @@ -50,17 +62,15 @@ class StatCollectionJob < ApplicationJob # # XXX: En realidad se agrupan por el deploy_id, que siempre será el # del DeployLocal. - def stats_by_site(**options) - Site.find_each do |site| - site.build_stats.jekyll.group(:deploy_id).rollup('builds', **options) + def stats_by_site(site, **options) + site.build_stats.jekyll.group(:deploy_id).rollup('builds', **options) - site.build_stats.jekyll.group(:deploy_id).rollup('space_used', **options) do |rollup| - rollup.average(:bytes) - end + site.build_stats.jekyll.group(:deploy_id).rollup('space_used', **options) do |rollup| + rollup.average(:bytes) + end - site.build_stats.jekyll.group(:deploy_id).rollup('build_time', **options) do |rollup| - rollup.average(:seconds) - end + site.build_stats.jekyll.group(:deploy_id).rollup('build_time', **options) do |rollup| + rollup.average(:seconds) end end end