2019-09-02 14:29:19 +03:00
|
|
|
defmodule PlausibleWeb.StatsView do
|
|
|
|
use PlausibleWeb, :view
|
|
|
|
|
2020-05-26 16:09:34 +03:00
|
|
|
def base_domain do
|
|
|
|
PlausibleWeb.Endpoint.host()
|
|
|
|
end
|
|
|
|
|
|
|
|
def plausible_url do
|
2020-10-05 15:01:54 +03:00
|
|
|
PlausibleWeb.Endpoint.url()
|
2020-05-26 16:09:34 +03:00
|
|
|
end
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
def large_number_format(n) do
|
|
|
|
cond do
|
|
|
|
n >= 1_000 && n < 1_000_000 ->
|
|
|
|
thousands = trunc(n / 100) / 10
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
if thousands == trunc(thousands) || n >= 100_000 do
|
|
|
|
"#{trunc(thousands)}k"
|
|
|
|
else
|
|
|
|
"#{thousands}k"
|
|
|
|
end
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2021-03-16 13:03:50 +03:00
|
|
|
n >= 1_000_000 && n < 1_000_000_000 ->
|
2019-09-02 14:29:19 +03:00
|
|
|
millions = trunc(n / 100_000) / 10
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2021-03-24 12:50:15 +03:00
|
|
|
if millions == trunc(millions) || n > 100_000_000 do
|
2021-03-24 13:08:05 +03:00
|
|
|
"#{trunc(millions)}M"
|
2019-09-02 14:29:19 +03:00
|
|
|
else
|
2021-03-24 13:08:05 +03:00
|
|
|
"#{millions}M"
|
|
|
|
end
|
|
|
|
|
|
|
|
n >= 1_000_000_000 && n < 1_000_000_000_000 ->
|
|
|
|
billions = trunc(n / 100_000_000) / 10
|
|
|
|
|
|
|
|
if billions == trunc(billions) || n > 100_000_000_000 do
|
|
|
|
"#{trunc(billions)}B"
|
|
|
|
else
|
|
|
|
"#{billions}B"
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
2020-06-08 10:35:13 +03:00
|
|
|
|
|
|
|
true ->
|
|
|
|
Integer.to_string(n)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def bar(count, all, color \\ :blue) do
|
|
|
|
~E"""
|
2020-03-06 12:11:38 +03:00
|
|
|
<div class="bg-<%= color %>-100" style="width: <%= bar_width(count, all) %>%; height: 30px"></div>
|
2019-09-02 14:29:19 +03:00
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
2022-09-01 11:22:04 +03:00
|
|
|
def stats_container_class(conn) do
|
|
|
|
cond do
|
|
|
|
conn.assigns[:embedded] && conn.assigns[:width] == "manual" -> ""
|
|
|
|
conn.assigns[:embedded] -> "max-width-screen-lg mx-auto"
|
|
|
|
!conn.assigns[:embedded] -> "container"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
defp bar_width(count, all) do
|
2020-06-08 10:35:13 +03:00
|
|
|
max =
|
|
|
|
Enum.max_by(all, fn
|
|
|
|
{_, count} -> count
|
|
|
|
{_, count, _} -> count
|
|
|
|
end)
|
|
|
|
|> elem(1)
|
2019-11-06 07:41:06 +03:00
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
count / max * 100
|
|
|
|
end
|
2022-09-28 10:42:15 +03:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns a readable stats URL.
|
|
|
|
|
|
|
|
Native Phoenix router functions percent-encode all diacritics, resulting in
|
|
|
|
ugly URLs, e.g. `https://plausible.io/café.com` transforms into
|
|
|
|
`https://plausible.io/caf%C3%A9.com`.
|
|
|
|
|
|
|
|
This function encodes only the slash (`/`) character from the site's domain.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> PlausibleWeb.StatsView.pretty_stats_url(%Plausible.Site{domain: "user.gittea.io/repo"})
|
|
|
|
"http://localhost:8000/user.gittea.io%2Frepo"
|
|
|
|
|
|
|
|
iex> PlausibleWeb.StatsView.pretty_stats_url(%Plausible.Site{domain: "anakin.test"})
|
|
|
|
"http://localhost:8000/anakin.test"
|
|
|
|
|
|
|
|
iex> PlausibleWeb.StatsView.pretty_stats_url(%Plausible.Site{domain: "café.test"})
|
|
|
|
"http://localhost:8000/café.test"
|
|
|
|
|
|
|
|
"""
|
|
|
|
def pretty_stats_url(%Plausible.Site{domain: domain}) when is_binary(domain) do
|
|
|
|
pretty_domain = String.replace(domain, "/", "%2F")
|
|
|
|
"#{plausible_url()}/#{pretty_domain}"
|
|
|
|
end
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|