From 0ae5448a2b3d90e0ffeefdbb7d58f2f798c60c0f Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Wed, 6 Nov 2019 12:41:06 +0800 Subject: [PATCH] Show percentages for most stats --- lib/plausible/stats/stats.ex | 31 +++++++++++++------ .../templates/stats/browsers.html.eex | 4 +-- .../templates/stats/browsers_preview.html.eex | 4 +-- .../templates/stats/countries.html.eex | 6 ++-- .../stats/countries_preview.html.eex | 4 +-- .../stats/operating_systems.html.eex | 4 +-- .../stats/operating_systems_preview.html.eex | 4 +-- .../stats/screen_sizes_preview.html.eex | 4 +-- lib/plausible_web/views/stats_view.ex | 6 +++- test/plausible/stats/stats_test.exs | 22 ++++++------- 10 files changed, 52 insertions(+), 37 deletions(-) diff --git a/lib/plausible/stats/stats.ex b/lib/plausible/stats/stats.ex index 6dc506077..2186cbb50 100644 --- a/lib/plausible/stats/stats.ex +++ b/lib/plausible/stats/stats.ex @@ -129,27 +129,36 @@ defmodule Plausible.Stats do @available_screen_sizes ["Desktop", "Laptop", "Tablet", "Mobile"] def top_screen_sizes(site, query) do - Repo.all(from e in base_query(site, query), + sizes = Repo.all(from e in base_query(site, query), select: {e.screen_size, count(e.screen_size)}, group_by: e.screen_size, where: e.new_visitor == true and not is_nil(e.screen_size) - ) |> Enum.sort(fn {screen_size1, _}, {screen_size2, _} -> + ) + |> Enum.sort(fn {screen_size1, _}, {screen_size2, _} -> index1 = Enum.find_index(@available_screen_sizes, fn s -> s == screen_size1 end) index2 = Enum.find_index(@available_screen_sizes, fn s -> s == screen_size2 end) index2 > index1 end) + |> add_percentages + end + + defp add_percentages(stat_list) do + total = Enum.reduce(stat_list, 0, fn {_, count}, total -> total + count end) + Enum.map(stat_list, fn {stat, count} -> {stat, count, round(count / total * 100)} end) end def countries(site, query, limit \\ 5) do - Repo.all(from e in base_query(site, query), + countries = Repo.all(from e in base_query(site, query), select: {e.country_code, count(e.country_code)}, group_by: e.country_code, where: e.new_visitor == true and not is_nil(e.country_code), - order_by: [desc: count(e.country_code)], - limit: ^limit - ) |> Enum.map(fn {country_code, count} -> + order_by: [desc: count(e.country_code)] + ) + |> Enum.map(fn {country_code, count} -> {Plausible.Stats.CountryName.from_iso3166(country_code), count} end) + |> add_percentages + |> Enum.take(limit) end def browsers(site, query, limit \\ 5) do @@ -157,9 +166,10 @@ defmodule Plausible.Stats do select: {e.browser, count(e.browser)}, group_by: e.browser, where: e.new_visitor == true and not is_nil(e.browser), - order_by: [desc: count(e.browser)], - limit: ^limit + order_by: [desc: count(e.browser)] ) + |> add_percentages + |> Enum.take(limit) end def operating_systems(site, query, limit \\ 5) do @@ -167,9 +177,10 @@ defmodule Plausible.Stats do select: {e.operating_system, count(e.operating_system)}, group_by: e.operating_system, where: e.new_visitor == true and not is_nil(e.operating_system), - order_by: [desc: count(e.operating_system)], - limit: ^limit + order_by: [desc: count(e.operating_system)] ) + |> add_percentages + |> Enum.take(limit) end def current_visitors(site) do diff --git a/lib/plausible_web/templates/stats/browsers.html.eex b/lib/plausible_web/templates/stats/browsers.html.eex index c998c3b8d..8051218be 100644 --- a/lib/plausible_web/templates/stats/browsers.html.eex +++ b/lib/plausible_web/templates/stats/browsers.html.eex @@ -6,10 +6,10 @@
- <%= for {browser, count} <- @browsers do %> + <%= for {browser, count, percentage} <- @browsers do %>
<%= browser %> - <%= large_number_format(count) %> + <%= percentage %>%
<%= bar(count, @browsers, :red) %> <% end %> diff --git a/lib/plausible_web/templates/stats/browsers_preview.html.eex b/lib/plausible_web/templates/stats/browsers_preview.html.eex index afd88614c..e0f61b633 100644 --- a/lib/plausible_web/templates/stats/browsers_preview.html.eex +++ b/lib/plausible_web/templates/stats/browsers_preview.html.eex @@ -4,10 +4,10 @@
- <%= for {key, count} <- @browsers do %> + <%= for {key, count, percentage} <- @browsers do %>
<%= key %> - <%= large_number_format(count) %> + <%= percentage %>%
<%= bar(count, @browsers, :red) %> <% end %> diff --git a/lib/plausible_web/templates/stats/countries.html.eex b/lib/plausible_web/templates/stats/countries.html.eex index 03a1356cd..ebaf7f907 100644 --- a/lib/plausible_web/templates/stats/countries.html.eex +++ b/lib/plausible_web/templates/stats/countries.html.eex @@ -6,10 +6,10 @@
- <%= for {country, count} <- @countries do %> + <%= for {country, count, percentage} <- @countries do %>
- <%= Plausible.Stats.CountryName.from_iso3166(country) %> - <%= large_number_format(count) %> + <%= country %> + <%= percentage %>%
<%= bar(count, @countries, :indigo) %> <% end %> diff --git a/lib/plausible_web/templates/stats/countries_preview.html.eex b/lib/plausible_web/templates/stats/countries_preview.html.eex index 80b430fc6..730b43bec 100644 --- a/lib/plausible_web/templates/stats/countries_preview.html.eex +++ b/lib/plausible_web/templates/stats/countries_preview.html.eex @@ -4,10 +4,10 @@
- <%= for {key, count} <- @top_countries do %> + <%= for {key, count, percentage} <- @top_countries do %>
<%= key %> - <%= large_number_format(count) %> + <%= percentage %>%
<%= bar(count, @top_countries, :indigo) %> <% end %> diff --git a/lib/plausible_web/templates/stats/operating_systems.html.eex b/lib/plausible_web/templates/stats/operating_systems.html.eex index 6e4a64e96..51b1c0b1e 100644 --- a/lib/plausible_web/templates/stats/operating_systems.html.eex +++ b/lib/plausible_web/templates/stats/operating_systems.html.eex @@ -6,10 +6,10 @@
- <%= for {os, count} <- @operating_systems do %> + <%= for {os, count, percentage} <- @operating_systems do %>
<%= os %> - <%= count %> + <%= percentage %>%
<%= bar(count, @operating_systems, :blue) %> <% end %> diff --git a/lib/plausible_web/templates/stats/operating_systems_preview.html.eex b/lib/plausible_web/templates/stats/operating_systems_preview.html.eex index 196cb1c3c..db2e75e9d 100644 --- a/lib/plausible_web/templates/stats/operating_systems_preview.html.eex +++ b/lib/plausible_web/templates/stats/operating_systems_preview.html.eex @@ -4,10 +4,10 @@
- <%= for {key, count} <- @operating_systems do %> + <%= for {key, count, percentage} <- @operating_systems do %>
<%= key %> - <%= large_number_format(count) %> + <%= percentage %>%
<%= bar(count, @operating_systems, :blue) %> <% end %> diff --git a/lib/plausible_web/templates/stats/screen_sizes_preview.html.eex b/lib/plausible_web/templates/stats/screen_sizes_preview.html.eex index 775e9662b..20f7a715b 100644 --- a/lib/plausible_web/templates/stats/screen_sizes_preview.html.eex +++ b/lib/plausible_web/templates/stats/screen_sizes_preview.html.eex @@ -3,13 +3,13 @@
by visitors
- <%= for {screen_size, count} <- @top_screen_sizes do %> + <%= for {screen_size, count, percentage} <- @top_screen_sizes do %>
<%= icon_for(screen_size) %> <%= screen_size %> - <%= large_number_format(count) %> + <%= percentage %>%
<%= bar(count, @top_screen_sizes, :teal) %> <% end %> diff --git a/lib/plausible_web/views/stats_view.ex b/lib/plausible_web/views/stats_view.ex index 36e89294d..4385a3bda 100644 --- a/lib/plausible_web/views/stats_view.ex +++ b/lib/plausible_web/views/stats_view.ex @@ -104,7 +104,11 @@ defmodule PlausibleWeb.StatsView do end defp bar_width(count, all) do - max = Enum.max_by(all, fn {_, count} -> count end) |> elem(1) + max = Enum.max_by(all, fn + {_, count} -> count + {_, count, _} -> count + end) |> elem(1) + count / max * 100 end diff --git a/test/plausible/stats/stats_test.exs b/test/plausible/stats/stats_test.exs index 306375c2c..2af11c42b 100644 --- a/test/plausible/stats/stats_test.exs +++ b/test/plausible/stats/stats_test.exs @@ -53,7 +53,7 @@ defmodule Plausible.StatsTest do end describe "top_screen_sizes" do - test "shows top screen sizes by new visitors" do + test "shows top screen sizes by new visitors and percentages" do site = insert(:site) insert(:pageview, hostname: site.domain, screen_size: "desktop", new_visitor: true, timestamp: ~N[2019-01-01 01:00:00]) insert(:pageview, hostname: site.domain, screen_size: "desktop", new_visitor: true, timestamp: ~N[2019-01-01 02:00:00]) @@ -61,14 +61,14 @@ defmodule Plausible.StatsTest do query = Stats.Query.from(site.timezone, %{"period" => "day", "date" => "2019-01-01"}) assert Stats.top_screen_sizes(site, query) == [ - {"mobile", 1}, - {"desktop", 2} + {"mobile", 1, 33}, + {"desktop", 2, 67} ] end end describe "countries" do - test "shows top countries by new visitors" do + test "shows top countries by new visitors and percentages" do site = insert(:site) insert(:pageview, hostname: site.domain, country_code: "EE", new_visitor: true, timestamp: ~N[2019-01-01 01:00:00]) insert(:pageview, hostname: site.domain, country_code: "EE", new_visitor: true, timestamp: ~N[2019-01-01 02:00:00]) @@ -76,8 +76,8 @@ defmodule Plausible.StatsTest do query = Stats.Query.from(site.timezone, %{"period" => "day", "date" => "2019-01-01"}) assert Stats.countries(site, query) == [ - {"Estonia", 2}, - {"United Kingdom", 1} + {"Estonia", 2, 67}, + {"United Kingdom", 1, 33} ] end end @@ -91,14 +91,14 @@ defmodule Plausible.StatsTest do query = Stats.Query.from(site.timezone, %{"period" => "day", "date" => "2019-01-01"}) assert Stats.browsers(site, query) == [ - {"Chrome", 2}, - {"Safari", 1} + {"Chrome", 2, 67}, + {"Safari", 1, 33} ] end end describe "operating_systems" do - test "shows top browsers by new visitors" do + test "shows top operating systems by new visitors" do site = insert(:site) insert(:pageview, hostname: site.domain, operating_system: "Mac", new_visitor: true, timestamp: ~N[2019-01-01 01:00:00]) insert(:pageview, hostname: site.domain, operating_system: "Windows", new_visitor: true, timestamp: ~N[2019-01-01 02:00:00]) @@ -106,8 +106,8 @@ defmodule Plausible.StatsTest do query = Stats.Query.from(site.timezone, %{"period" => "day", "date" => "2019-01-01"}) assert Stats.operating_systems(site, query) == [ - {"Windows", 2}, - {"Mac", 1} + {"Windows", 2, 67}, + {"Mac", 1, 33} ] end end