From 6c485e5e18abf29d3b3cea0de584503417006c6e Mon Sep 17 00:00:00 2001 From: f Date: Sat, 9 Oct 2021 17:27:45 -0300 Subject: [PATCH] =?UTF-8?q?obtener=20estad=C3=ADsticas=20de=20cualquier=20?= =?UTF-8?q?link?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/stats_controller.rb | 39 +++++++++++++++++++++++++++++ app/policies/site_stat_policy.rb | 4 +++ app/views/stats/index.haml | 18 ++++++++++++- config/locales/en.yml | 6 +++++ config/locales/es.yml | 10 +++++--- config/routes.rb | 1 + 6 files changed, 73 insertions(+), 5 deletions(-) diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 1dc468bb..ca834ebd 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -25,6 +25,7 @@ class StatsController < ApplicationController hostnames last_stat chart_options + normalized_urls end # Genera un gráfico de visitas por dominio asociado a este sitio @@ -56,6 +57,22 @@ class StatsController < ApplicationController render json: Rollup.series(resource, **options) end + def uris + return unless stale? [last_stat, hostnames, interval, normalized_urls] + + options = { host: hostnames, uri: normalized_paths } + stats = Rollup.where_dimensions(**options).multi_series('host|uri', interval: interval).tap do |series| + series.each do |serie| + serie[:name] = serie.dig(:dimensions).slice('host', 'uri').values.join.sub('/index.html', '/') + serie[:data].transform_values! do |value| + value * nodes + end + end + end + + render json: stats + end + private def last_stat @@ -72,6 +89,28 @@ class StatsController < ApplicationController @hostnames ||= [@site.hostname, @site.alternative_hostnames].flatten end + # Normalizar las URLs + # + # @return [Array] + def normalized_urls + @normalized_urls ||= params.permit(:urls).try(:[], + :urls)&.split("\n")&.map(&:strip)&.select(&:present?)&.select do |uri| + uri.start_with? 'https://' + end&.map do |u| + # XXX: Eliminar + # @see {https://0xacab.org/sutty/containers/nginx/-/merge_requests/1} + next u unless u.end_with? '/' + + "#{u}index.html" + end&.uniq || [] + end + + def normalized_paths + @normalized_paths ||= normalized_urls.map do |u| + "/#{u.split('/', 4).last}" + end + end + # Opciones por defecto para los gráficos. # # La invitación a volver dentro de X tiempo es para dar un estimado de diff --git a/app/policies/site_stat_policy.rb b/app/policies/site_stat_policy.rb index 6c98c775..cb62b507 100644 --- a/app/policies/site_stat_policy.rb +++ b/app/policies/site_stat_policy.rb @@ -20,4 +20,8 @@ class SiteStatPolicy def resources? index? end + + def uris? + index? + end end diff --git a/app/views/stats/index.haml b/app/views/stats/index.haml index 992200a4..1ce61a98 100644 --- a/app/views/stats/index.haml +++ b/app/views/stats/index.haml @@ -10,13 +10,29 @@ .mb-3 - Stat::INTERVALS.each do |interval| - = link_to t(".#{interval}"), site_stats_path(interval: interval), class: "btn #{'btn-primary active' if params[:interval].to_sym == interval}" + = link_to t(".#{interval}"), site_stats_path(interval: interval, urls: params[:urls]), class: "btn #{'btn-primary active' if params[:interval].to_sym == interval}" .mb-3 %h2= t('.host.title', count: @hostnames.size) %p.lead= t('.host.description') = line_chart site_stats_host_path(@chart_params), **@chart_options + .mb-3 + - original_urls = params[:urls]&.split("\n")&.map(&:strip) + - rows = original_urls.size.zero? ? 3 : original_urls.size + %h2= t('.urls.title') + %p.lead= t('.urls.description') + %form + %input{ type: 'hidden', name: 'interval', value: @interval } + .form-group + %label{ for: 'urls' }= t('.urls.label') + %textarea#urls.form-control{ name: 'urls', autocomplete: 'on', required: true, rows: rows, aria_describedby: 'help-urls' }= params[:urls] + %small#help-urls.feedback.form-text.text-muted= t('.urls.help') + .form-group + %button.btn{ type: 'submit' }= t('.urls.submit') + - if @normalized_urls.present? + = line_chart site_stats_uris_path(urls: params[:urls], **@chart_params), **@chart_options + - Stat::RESOURCES.each do |resource| .mb-3 %h2= t(".resources.#{resource}.title") diff --git a/config/locales/en.yml b/config/locales/en.yml index 08313a03..af864e7b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -266,6 +266,12 @@ en: one: 'Site visits' other: 'Visits by site name' description: 'Counts visited pages on your site, grouped by domain names in use.' + urls: + title: 'Visits by URL' + description: 'Counts visits or downloads on any URL.' + label: 'URLs ("links")' + help: 'Copy and paste a single URL per line' + submit: 'Get stats' resources: builds: title: 'Site publication' diff --git a/config/locales/es.yml b/config/locales/es.yml index b1161287..7d0fe2bb 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -271,6 +271,12 @@ es: one: 'Visitas del sitio' other: 'Visitas por nombre del sitio' description: 'Cuenta la cantidad de páginas visitadas en tu sitio, dividida por los nombres de dominio en uso.' + urls: + title: 'Visitas por dirección' + description: 'Cantidad de visitas o descargas por dirección.' + label: 'Direcciones web ("links", vínculos)' + help: 'Copia y pega una dirección por línea.' + submit: 'Obtener estadísticas' resources: builds: title: 'Publicaciones del sitio' @@ -605,7 +611,3 @@ es: edit: 'Editando' usuaries: index: 'Usuaries' - day: 'Día' - week: 'Semana' - month: 'Mes' - year: 'Año' diff --git a/config/routes.rb b/config/routes.rb index b59266fb..1b0f9e0b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -76,6 +76,7 @@ Rails.application.routes.draw do resources :stats, only: [:index] get :'stats/host', to: 'stats#host' + get :'stats/uris', to: 'stats#uris' get :'stats/resources', to: 'stats#resources' end end