diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 35f7f108..84ca3028 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -39,6 +39,20 @@ class StatsController < ApplicationController last_stat chart_options normalized_urls + + expires_in = Time.now.try(:"end_of_#{Stat.default_interval}") - Time.now + @columns = {} + + Stat::COLUMNS.each do |column| + @columns[column] = + Rails.cache.fetch("stats/#{column}/#{site.id}", expires_in: expires_in) do + Rollup.where(interval: interval, name: "host|#{column}") + .where_dimensions(host: hostnames) + .group("dimensions->>'#{column}'") + .sum(:value) + .transform_values(&:to_i) + end + end end # Genera un gráfico de visitas por dominio asociado a este sitio diff --git a/app/jobs/uri_collection_job.rb b/app/jobs/uri_collection_job.rb index fe50929d..a7dd2a00 100644 --- a/app/jobs/uri_collection_job.rb +++ b/app/jobs/uri_collection_job.rb @@ -26,7 +26,7 @@ class UriCollectionJob < PeriodicJob # Recordar la última vez que se corrió la tarea stat = site.stats.create! name: STAT_NAME # Columnas a agrupar - columns = %i[http_referer geoip2_data_country_name].zip([nil]).to_h + columns = Stat::COLUMNS.zip([nil]).to_h # Recorremos todos los hostnames y uris posibles y luego agrupamos # recursivamente para no tener que recalcular, asumiendo que es más diff --git a/app/models/stat.rb b/app/models/stat.rb index 5f72ccd0..f1016026 100644 --- a/app/models/stat.rb +++ b/app/models/stat.rb @@ -5,6 +5,14 @@ class Stat < ApplicationRecord # XXX: Los intervalos van en orden de mayor especificidad a menor INTERVALS = %i[day month year].freeze RESOURCES = %i[builds space_used build_time].freeze + COLUMNS = %i[http_referer geoip2_data_country_name].freeze belongs_to :site + + # El intervalo por defecto + # + # @return [Symbol] + def self.default_interval + INTERVALS.first + end end diff --git a/app/views/stats/index.haml b/app/views/stats/index.haml index 02683781..be0c1426 100644 --- a/app/views/stats/index.haml +++ b/app/views/stats/index.haml @@ -32,6 +32,27 @@ - if @normalized_urls.present? = line_chart site_stats_uris_path(urls: @normalized_urls, **@chart_params), **@chart_options + .row.mb-5.row-cols-1.row-cols-md-2 + - @columns.each_pair do |column, values| + - next if values.blank? + .col.mb-5 + %h2= t(".columns.#{column}.title") + %p.lead= t(".columns.#{column}.description") + + %table.table + %colgroup + %col + %col + %thead + %tr.sticky-top.background-white + %th{ scope: 'col' }= t(".columns.#{column}.column") + %th{ scope: 'col' }= t('.columns.visits') + %tfoot + %tbody + - values.each_pair do |col, val| + %tr + %th{ scope: 'row', style: 'word-break: break-all' }= col.blank? ? t('.columns.empty') : col + %td= val .mb-5 %h2= t('.resources.title') %p.lead= t('.resources.description') diff --git a/config/locales/en.yml b/config/locales/en.yml index 732eb4bd..76760958 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -284,6 +284,17 @@ en: build_time: title: 'Publication time' description: 'Average time your site takes to build.' + columns: + visits: "Visits" + empty: "(nothing)" + http_referer: + title: "Referers" + description: "Visits by origin" + column: "Referer" + geoip2_data_country_name: + title: "Countries" + description: "Visits by country" + column: "Country" sites: donations: url: 'https://donaciones.sutty.nl/en/' diff --git a/config/locales/es.yml b/config/locales/es.yml index e47c1fb5..82bfd253 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -289,6 +289,17 @@ es: build_time: title: 'Tiempo de publicación' description: 'Tiempo promedio que toma en publicarse tu sitio.' + columns: + visits: "Visitas" + empty: "(nada)" + http_referer: + title: "Referencias" + description: "Orígenes de las visitas" + column: "Referencia" + geoip2_data_country_name: + title: "Países" + description: "Cantidad de visitas por país" + column: "País" sites: donations: url: 'https://donaciones.sutty.nl/'