2019-08-02 00:20:42 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Estadísticas del sitio
|
|
|
|
class StatsController < ApplicationController
|
|
|
|
include Pundit
|
|
|
|
before_action :authenticate_usuarie!
|
2021-10-08 21:33:31 +00:00
|
|
|
before_action :authorize_stats
|
2019-08-02 00:20:42 +00:00
|
|
|
|
2021-10-08 19:31:02 +00:00
|
|
|
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
|
|
|
|
|
2019-08-02 00:20:42 +00:00
|
|
|
def index
|
2021-10-08 19:31:02 +00:00
|
|
|
@chart_params = { interval: interval }
|
|
|
|
hostnames
|
2021-10-08 21:24:19 +00:00
|
|
|
last_stat
|
2021-10-08 19:31:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Genera un gráfico de visitas por dominio asociado a este sitio
|
|
|
|
def host
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
2021-10-08 21:24:19 +00:00
|
|
|
def last_stat
|
|
|
|
@last_stat ||= Stat.last
|
|
|
|
end
|
|
|
|
|
2021-10-08 21:33:31 +00:00
|
|
|
def authorize_stats
|
|
|
|
@site = find_site
|
|
|
|
authorize SiteStat.new(@site)
|
|
|
|
end
|
|
|
|
|
2021-10-08 19:31:02 +00:00
|
|
|
# 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
|
2019-08-02 00:20:42 +00:00
|
|
|
end
|
|
|
|
end
|