File size: 1,304 Bytes
8da2481 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | defmodule Plausible.Stats.Util do
@moduledoc """
Utilities for modifying stat results
"""
@doc """
This function adds the `visitors` metric into the list of
given metrics if it's not already there and if it is needed
for any of the other metrics to be calculated.
"""
def maybe_add_visitors_metric(metrics) do
needed? =
Enum.any?(
[:percentage, :conversion_rate, :group_conversion_rate, :time_on_page],
&(&1 in metrics)
)
if needed? and :visitors not in metrics do
metrics ++ [:visitors]
else
metrics
end
end
def shortname(_query, metric) when is_atom(metric), do: metric
def shortname(_query, "time:" <> _), do: :time
def shortname(query, dimension) do
index = Enum.find_index(query.dimensions, &(&1 == dimension))
:"dim#{index}"
end
def percentage(x, y) when is_integer(x) and x > 0 and is_integer(y) and y > 0 do
result =
x
|> Decimal.div(y)
|> Decimal.mult(100)
|> Decimal.round(2)
|> Decimal.to_string()
case result do
<<compact::binary-size(1), ".00">> -> compact
<<compact::binary-size(2), ".00">> -> compact
<<compact::binary-size(3), ".00">> -> compact
decimal -> decimal
end
end
def percentage(_x, _y) do
"0"
end
end
|