obtener estadísticas de cualquier link

This commit is contained in:
f 2021-10-09 17:27:45 -03:00
parent 5ca8e3c923
commit 6c485e5e18
6 changed files with 73 additions and 5 deletions

View file

@ -25,6 +25,7 @@ class StatsController < ApplicationController
hostnames hostnames
last_stat last_stat
chart_options chart_options
normalized_urls
end end
# Genera un gráfico de visitas por dominio asociado a este sitio # 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) render json: Rollup.series(resource, **options)
end 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 private
def last_stat def last_stat
@ -72,6 +89,28 @@ class StatsController < ApplicationController
@hostnames ||= [@site.hostname, @site.alternative_hostnames].flatten @hostnames ||= [@site.hostname, @site.alternative_hostnames].flatten
end 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. # Opciones por defecto para los gráficos.
# #
# La invitación a volver dentro de X tiempo es para dar un estimado de # La invitación a volver dentro de X tiempo es para dar un estimado de

View file

@ -20,4 +20,8 @@ class SiteStatPolicy
def resources? def resources?
index? index?
end end
def uris?
index?
end
end end

View file

@ -10,13 +10,29 @@
.mb-3 .mb-3
- Stat::INTERVALS.each do |interval| - 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 .mb-3
%h2= t('.host.title', count: @hostnames.size) %h2= t('.host.title', count: @hostnames.size)
%p.lead= t('.host.description') %p.lead= t('.host.description')
= line_chart site_stats_host_path(@chart_params), **@chart_options = 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| - Stat::RESOURCES.each do |resource|
.mb-3 .mb-3
%h2= t(".resources.#{resource}.title") %h2= t(".resources.#{resource}.title")

View file

@ -266,6 +266,12 @@ en:
one: 'Site visits' one: 'Site visits'
other: 'Visits by site name' other: 'Visits by site name'
description: 'Counts visited pages on your site, grouped by domain names in use.' 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: resources:
builds: builds:
title: 'Site publication' title: 'Site publication'

View file

@ -271,6 +271,12 @@ es:
one: 'Visitas del sitio' one: 'Visitas del sitio'
other: 'Visitas por nombre 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.' 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: resources:
builds: builds:
title: 'Publicaciones del sitio' title: 'Publicaciones del sitio'
@ -605,7 +611,3 @@ es:
edit: 'Editando' edit: 'Editando'
usuaries: usuaries:
index: 'Usuaries' index: 'Usuaries'
day: 'Día'
week: 'Semana'
month: 'Mes'
year: 'Año'

View file

@ -76,6 +76,7 @@ Rails.application.routes.draw do
resources :stats, only: [:index] resources :stats, only: [:index]
get :'stats/host', to: 'stats#host' get :'stats/host', to: 'stats#host'
get :'stats/uris', to: 'stats#uris'
get :'stats/resources', to: 'stats#resources' get :'stats/resources', to: 'stats#resources'
end end
end end