Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing difference comparing previous period (#39)
* preparing for showing changes comparing prior period. * getting data from previous period * let the start of the period be beginning of the day * Cleaned up types So we are not doing `.to_s` and `.to_i` after each other. no need anymore.
- Loading branch information
Showing
16 changed files
with
159 additions
and
37 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -1,17 +1,55 @@ | ||
module DomainMetrics | ||
def unique_query(domain) : String | ||
def unique_query(domain) : Int64 | ||
metrics(domain).unique_query | ||
end | ||
|
||
def total_query(domain) : String | ||
def total_query(domain) : Int64 | ||
metrics(domain).total_query | ||
end | ||
|
||
def bounce_query(domain) : String | ||
def bounce_query(domain) : Int64 | ||
metrics(domain).bounce_query | ||
end | ||
|
||
def metrics(domain) : Metrics | ||
@metrics ||= Metrics.new(domain, period) | ||
end | ||
|
||
def unique_query_previous(domain) : Int64 | ||
previous_metric(domain).unique_query | ||
end | ||
|
||
def total_query_previous(domain) : Int64 | ||
previous_metric(domain).total_query | ||
end | ||
|
||
def bounce_query_previous(domain) : Int64 | ||
previous_metric(domain).bounce_query | ||
end | ||
|
||
def previous_metric(domain) : MetricsNew | ||
start_date, end_date = previous_period | ||
@previous_metrics ||= MetricsNew.new(domain, start_date, end_date) | ||
end | ||
|
||
private def previous_period | ||
case period | ||
when "14d" | ||
start_date = 15.days.ago.at_end_of_day | ||
return [start_date - 14.days, start_date] | ||
when "30d" | ||
start_date = 31.days.ago.at_end_of_day | ||
return [start_date - 30.days, start_date] | ||
return "30 days" | ||
when "60d" | ||
start_date = 61.days.ago.at_end_of_day | ||
return [start_date - 60.days, start_date] | ||
when "90d" | ||
start_date = 91.days.ago.at_end_of_day | ||
return [start_date - 90.days, start_date] | ||
else | ||
start_date = 8.days.ago.at_end_of_day | ||
return [start_date - 7.days, start_date] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
class DifferenceComponent < BaseComponent | ||
needs now : Int64 | ||
needs before : Int64 | ||
needs reverse : Bool = false | ||
|
||
def render | ||
div class: "flex items-center ml-4" do | ||
if reverse? | ||
if up? | ||
img src: "/assets/svg/arrow-up-red.svg", class: "text-red-800 strong text-xs h-4 w-4 block mr-1" | ||
span class: "text-red-800 strong text-xs" do | ||
raw count_percentage + "%" | ||
end | ||
else | ||
img src: "/assets/svg/arrow-down-green.svg", class: "text-green-800 strong text-xs h-4 w-4 block mr-1" | ||
span class: "text-green-800 strong text-xs" do | ||
raw count_percentage + "%" | ||
end | ||
end | ||
else | ||
if up? | ||
img src: "/assets/svg/arrow-up-green.svg", class: "text-green-800 strong text-xs h-4 w-4 block mr-1" | ||
span class: "text-green-800 strong text-xs" do | ||
raw count_percentage + "%" | ||
end | ||
else | ||
img src: "/assets/svg/arrow-down-red.svg", class: "text-red-800 strong text-xs h-4 w-4 block mr-1" | ||
span class: "text-red-800 strong text-xs" do | ||
raw count_percentage + "%" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
private def up? | ||
now > before | ||
end | ||
|
||
private def count_percentage : String | ||
custom_now = now == 0 ? 0.1 : now | ||
custom_before = before == 0 ? 0.1 : before | ||
if up? | ||
increase = custom_now - custom_before | ||
((increase.to_f / custom_before)*100).format(decimal_places: 0, delimiter: "") | ||
else | ||
decrease = custom_before - custom_now | ||
((decrease.to_f / custom_now)*100).format(decimal_places: 0, delimiter: "") | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,44 @@ | ||
class TotalRowComponent < BaseComponent | ||
needs total_unique : String | ||
needs total_sum : String | ||
needs total_bounce : String | ||
needs total_unique : Int64 | ||
needs total_sum : Int64 | ||
needs total_bounce : Int64 | ||
needs total_unique_previous : Int64 | ||
needs total_previous : Int64 | ||
needs total_bounce_previous : Int64 | ||
|
||
def render | ||
div class: "big_letters" do | ||
div class: "max-w-6xl mx-auto py-3 px-2 sm:px-0 grid grid-flow-col gap-6" do | ||
div class: "p-3" do | ||
para @total_unique.to_s, class: "text-3xl strong" | ||
para "Unique Visitors", class: "text-sm uppercase" | ||
div class: "text-4xl strong flex items-center" do | ||
para normalize_number(@total_unique), class: "text-4xl strong" | ||
mount DifferenceComponent.new(now: @total_unique, before: @total_unique_previous) | ||
end | ||
para "Unique", class: "text-sm uppercase font-bold" | ||
end | ||
div class: "p-3" do | ||
para @total_sum.to_s, class: "text-3xl strong" | ||
para "Total Pageviews", class: "text-sm strong uppercase" | ||
div class: "text-4xl strong flex items-center" do | ||
para normalize_number(@total_sum), class: "text-4xl strong" | ||
mount DifferenceComponent.new(now: @total_sum, before: @total_previous) | ||
end | ||
para "Pageviews", class: "text-sm font-bold uppercase" | ||
end | ||
div class: "p-3" do | ||
para "#{@total_bounce.to_s}%", class: "text-3xl strong" | ||
para "Bounce rate", class: "text-sm strong uppercase" | ||
div class: "text-4xl strong flex items-center" do | ||
para "#{@total_bounce.to_s}%", class: "text-4xl strong" | ||
mount DifferenceComponent.new(now: @total_bounce, before: @total_bounce_previous, reverse: true) | ||
end | ||
para "Bounce", class: "text-sm font-bold uppercase" | ||
end | ||
end | ||
end | ||
end | ||
|
||
private def normalize_number(number : Int64) : String | ||
if number >= 1000 | ||
number.humanize(precision: 2, significant: false) | ||
else | ||
number.to_s | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
class TotalSerializer < BaseSerializer | ||
def initialize(@views : String, @bounce : String, @unique : String) | ||
def initialize(@views : Int64, @bounce : Int64, @unique : Int64) | ||
end | ||
|
||
def render | ||
{page_views: @views, bounce: @bounce, unique: @unique} | ||
{page_views: @views.to_s, bounce: @bounce.to_s, unique: @unique.to_s} | ||
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