5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-29 15:06:22 +00:00

Merge branch 'blazer' into panel.sutty.nl

This commit is contained in:
f 2022-04-30 20:45:21 -03:00
commit 034a9d4aee
3 changed files with 43 additions and 18 deletions

View file

@ -34,7 +34,14 @@ class StatsController < ApplicationController
def index
breadcrumb I18n.t('stats.index.title'), ''
@chart_params = { interval: interval }
params.with_defaults! default_url_options
@chart_params = {
interval: interval,
period_start: params[:period_start],
period_end: params[:period_end]
}
hostnames
last_stat
chart_options
@ -46,17 +53,17 @@ class StatsController < ApplicationController
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)
.transform_values do |v|
v * nodes
end
.sort_by do |_, v|
v
end.reverse.to_h
rollup_scope.where(interval: interval, name: "host|#{column}")
.where_dimensions(host: hostnames)
.group("dimensions->>'#{column}'")
.sum(:value)
.transform_values(&:to_i)
.transform_values do |v|
v * nodes
end
.sort_by do |_, v|
v
end.reverse.to_h
end
end
end

View file

@ -11,8 +11,6 @@
# Los hostnames de un sitio van a poder obtenerse a partir de
# Site#hostnames con la garantía de que son únicos.
class UriCollectionJob < PeriodicJob
include RecursiveRollup
# Ignoramos imágenes porque suelen ser demasiadas y no aportan a las
# estadísticas.
IMAGES = %w[.png .jpg .jpeg .gif .webp .jfif].freeze
@ -76,13 +74,29 @@ class UriCollectionJob < PeriodicJob
# @param :dimensions [Hash]
# @param :filter [Hash]
# @return [nil]
def rollup(name:, dimensions:, filter: nil)
def rollup(name:, dimensions:, interval: starting_interval, filter: nil)
AccessLog.where(**(filter || dimensions))
.where('created_at >= ?', beginning_of_interval)
.completed_requests
.non_robots
.group(*dimensions.keys)
.rollup(name, interval: starting_interval, update: true)
.rollup(name, interval: interval, update: true)
end
# Generar rollups con el resto de la información
#
# @param :name [String]
# @param :dimensions [Hash]
# @param :filter [Hash]
# @return [nil]
def reduce_rollup(name:, dimensions:, filter: nil)
Stat::INTERVALS.reduce do |_previous, current|
rollup(name: name, dimensions: dimensions, filter: filter, interval: current)
current
end
nil
end
def stat_name

View file

@ -9,9 +9,13 @@
%time{ datetime: @last_stat.updated_at }
#{time_ago_in_words @last_stat.updated_at}.
.mb-5
%form.mb-5.form-inline{ method: 'get' }
- Stat::INTERVALS.each do |interval|
= link_to t(".#{interval}"), site_stats_path(interval: interval, urls: params[:urls]), class: "btn #{'btn-primary active' if @interval == interval}"
= link_to t(".#{interval}"), site_stats_path(interval: interval, urls: params[:urls], period_start: params[:period_start].to_date.try(:"beginning_of_#{interval}").to_date, period_end: params[:period_end]), class: "mb-0 btn #{'btn-primary active' if @interval == interval}"
%input.form-control{ type: 'date', name: :period_start, value: params[:period_start] }
%input.form-control{ type: 'date', name: :period_end, value: params[:period_end] }
%button.btn.mb-0{ type: 'submit' }= t('.filter')
.mb-5
%h2= t('.host.title', count: @hostnames.size)