-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show statistics in the dashboard front
- Loading branch information
Showing
7 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
app/components/dashboard/dashboard_statistic_component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters