Skip to content

Commit

Permalink
feat: show statistics in the dashboard front
Browse files Browse the repository at this point in the history
  • Loading branch information
danirod committed Jun 2, 2024
1 parent a6e9a73 commit af6ad2b
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="col text-center">
<p class="mb-0"><strong><%= label %></strong></p>
<p class="mb-0 lead"><%= value %></h5>
<p><small class="badge rounded-pill <%= pill_class %>">(<%= increment %>% to previous)</small></p>
</div>
25 changes: 25 additions & 0 deletions app/components/dashboard/dashboard_statistic_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Dashboard
class DashboardStatisticComponent < ViewComponent::Base
def initialize(label:, value:, increment:)
@label = label
@value = value
@increment = increment
end

private

attr_reader :label, :value, :increment

def pill_class
if increment > 0
'text-bg-success'
elsif increment < 0
'text-bg-warning'
else
'text-bg-secondary'
end
end
end
end
9 changes: 9 additions & 0 deletions app/controllers/dashboard/dashboard_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ def index
@topics = Topic.count
@users = User.count
@opinions = Opinion.count
@statistics = fetch_statistics
end

private

def fetch_statistics
Plausible::Integration.last_30_days(true)
rescue Net::HTTPError
nil
end
end
end
2 changes: 1 addition & 1 deletion app/services/plausible/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def client

def timeseries_params
yesterday = DateTime.current.yesterday
sixty_days_ago = yesterday - 60.days
sixty_days_ago = yesterday - 59.days # one less because otherwise it is one more

date = "#{sixty_days_ago.strftime('%F')},#{yesterday.strftime('%F')}"
{ site_id:, period: 'custom', date:, metrics: 'visits,pageviews,visit_duration' }
Expand Down
11 changes: 11 additions & 0 deletions app/services/plausible/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ def self.client
Plausible::Integration.new.client
end

def self.last_30_days(compare = false)
data = Rails.cache.fetch('plausible:last_30_days', expires_in: 2.hours) do
client.time_series
end
if compare
Plausible::MonthAnalyzer.new(data).month_compare
else
Plausible::MonthAnalyzer.new(data).month_forecast
end
end

def client
Plausible::Client.new(site_id, api_key)
end
Expand Down
41 changes: 41 additions & 0 deletions app/services/plausible/month_analyzer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

module Plausible
class MonthAnalyzer
def initialize(value)
@value = value
@this_month = @value[30..]
@previous_month = @value[0..29]
end

def month_compare
this_sum = month_forecast
prev_sum = reduce_month(previous_month)
%i[visits pageviews visit_duration].index_with { |k| fold_comparison(this_sum, prev_sum, k) }
end

def month_forecast
reduce_month(this_month)
end

private

attr_reader :value, :this_month, :previous_month

def fold_comparison(this, previous, key)
delta = (((this[key].to_f / previous[key]) - 1) * 100).round
{ value: this[key], previous: previous[key], delta: delta }
end

def reduce_month(month)
accum = month.reduce(Hash::new(0)) do |total, day|
duration = day["visit_duration"] * day["visits"]
{ pageviews: total[:pageviews] + day["pageviews"],
visit_duration: total[:visit_duration] + duration,
visits: total[:visits] + day["visits"] }
end
accum[:visit_duration] = (accum[:visit_duration].to_f / accum[:visits]).round
accum
end
end
end
16 changes: 16 additions & 0 deletions app/views/dashboard/dashboard/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@
</div>
</div>
<div class="col-sm-9 col-md-9 col-lg-10">

<div class="card mb-5">
<div class="card-header">
Últimos 30 días
</div>
<div class="card-body row">
<% if @statistics.present? %>
<%= render Dashboard::DashboardStatisticComponent.new(label: 'Sesiones', value: @statistics[:visits][:value], increment: @statistics[:visits][:delta]) %>
<%= render Dashboard::DashboardStatisticComponent.new(label: 'Páginas', value: @statistics[:pageviews][:value], increment: @statistics[:pageviews][:delta]) %>
<%= render Dashboard::DashboardStatisticComponent.new(label: 'Tiempo', value: running_time(@statistics[:visit_duration][:value]), increment: @statistics[:visit_duration][:delta]) %>
<% else %>
<p class="card-text">No se ha podido consultar el servicio de estadísticas externas.</p>
<% end %>
</div>
</div>

<div class="card mb-5">
<div class="card-header">
Acciones rápidas
Expand Down

0 comments on commit af6ad2b

Please sign in to comment.