ver cantidad de visitas

This commit is contained in:
f 2021-10-08 16:31:02 -03:00
parent 930f88903e
commit a7ae7f8e8d
7 changed files with 94 additions and 17 deletions

View file

@ -5,14 +5,68 @@ class StatsController < ApplicationController
include Pundit include Pundit
before_action :authenticate_usuarie! before_action :authenticate_usuarie!
INTERVALS = %i[hour day week month].freeze
# XXX: Permitir a Chart.js inyectar su propio CSS
content_security_policy only: :index do |policy|
policy.style_src :self, :unsafe_inline
policy.script_src :self, :unsafe_inline
end
def index def index
@site = find_site @site = find_site
authorize SiteStat.new(@site) authorize SiteStat.new(@site)
# Solo queremos el promedio de tiempo de compilación, no de @chart_params = { interval: interval }
# instalación de dependencias. hostnames
stats = @site.build_stats.jekyll end
@build_avg = stats.average(:seconds).to_f.round(2)
@build_max = stats.maximum(:seconds).to_f.round(2) # Genera un gráfico de visitas por dominio asociado a este sitio
def host
@site = find_site
authorize SiteStat.new(@site)
@stats = Rollup.where_dimensions(host: hostnames).multi_series('host', interval: interval).tap do |series|
series.each do |serie|
serie[:name] = serie.dig(:dimensions, 'host')
serie[:data].transform_values! do |value|
value * nodes
end
end
end
render json: @stats
end
private
# TODO: Eliminar cuando mergeemos referer-origin
def hostnames
@hostnames ||= [@site.hostname, @site.alternative_hostnames].flatten
end
# Obtiene y valida los intervalos
#
# @return [Symbol]
def interval
@interval ||= begin
i = params[:interval].to_sym
INTERVALS.include?(i) ? i : :day
end
end
# Obtiene la cantidad de nodos de Sutty, para poder calcular la
# cantidad de visitas.
#
# Como repartimos las visitas por nodo rotando las IPs en el
# nameserver y los resolvedores de DNS eligen un nameserver
# aleatoriamente, la cantidad de visitas se reparte
# equitativamente.
#
# XXX: Remover cuando podamos centralizar los AccessLog
#
# @return [Integer]
def nodes
@nodes ||= ENV.fetch('NODES', 1).to_i
end end
end end

View file

@ -18,6 +18,7 @@ import 'etc'
import Rails from '@rails/ujs' import Rails from '@rails/ujs'
import Turbolinks from 'turbolinks' import Turbolinks from 'turbolinks'
import * as ActiveStorage from '@rails/activestorage' import * as ActiveStorage from '@rails/activestorage'
import 'chartkick/chart.js'
Rails.start() Rails.start()
Turbolinks.start() Turbolinks.start()

View file

@ -12,4 +12,8 @@ class SiteStatPolicy
def index? def index?
site_stat.site.usuarie? usuarie site_stat.site.usuarie? usuarie
end end
def host?
index?
end
end end

View file

@ -1,17 +1,12 @@
= render 'layouts/breadcrumb',
crumbs: [link_to(t('sites.index.title'), sites_path),
link_to(@site.name, site_path(@site)), t('.title')]
.row .row
.col .col
%h1= t('.title') %h1= t('.title')
%p.lead= t('.help') %p.lead= t('.help')
%table.table.table-condensed .mb-3
%tbody - StatsController::INTERVALS.each do |interval|
%tr = link_to t(".#{interval}"), site_stats_path(interval: interval), class: 'btn'
%td= t('.build.average')
%td= distance_of_time_in_words_if_more_than_a_minute @build_avg .mb-3
%tr %h2= t('.host', count: @hostnames.size)
%td= t('.build.maximum') = line_chart site_stats_host_path(@chart_params), locale: 'es'
%td= distance_of_time_in_words_if_more_than_a_minute @build_max

View file

@ -255,6 +255,15 @@ en:
build: build:
average: 'Average building time' average: 'Average building time'
maximum: 'Maximum building time' maximum: 'Maximum building time'
hour: 'Hourly'
day: 'Daily'
week: 'Weekly'
month: 'Monthly'
year: 'Yearly'
host:
zero: 'Site visits'
one: 'Site visits'
other: 'Visits by site name'
sites: sites:
donations: donations:
url: 'https://donaciones.sutty.nl/en/' url: 'https://donaciones.sutty.nl/en/'

View file

@ -260,6 +260,15 @@ es:
build: build:
average: 'Tiempo promedio de generación' average: 'Tiempo promedio de generación'
maximum: 'Tiempo máximo de generación' maximum: 'Tiempo máximo de generación'
hour: 'Por hora'
day: 'Diarias'
week: 'Semanales'
month: 'Mensuales'
year: 'Anuales'
host:
zero: 'Visitas del sitio'
one: 'Visitas del sitio'
other: 'Visitas por nombre del sitio'
sites: sites:
donations: donations:
url: 'https://donaciones.sutty.nl/' url: 'https://donaciones.sutty.nl/'
@ -584,3 +593,7 @@ es:
edit: 'Editando' edit: 'Editando'
usuaries: usuaries:
index: 'Usuaries' index: 'Usuaries'
day: 'Día'
week: 'Semana'
month: 'Mes'
year: 'Año'

View file

@ -75,5 +75,6 @@ Rails.application.routes.draw do
post 'reorder_posts', to: 'sites#reorder_posts' post 'reorder_posts', to: 'sites#reorder_posts'
resources :stats, only: [:index] resources :stats, only: [:index]
get :'stats/host', to: 'stats#host'
end end
end end