Skip to content

Commit 9ad25b0

Browse files
committed
Refactor percentage helper and extract it to Plausible.Stats.Util
1 parent 348e70e commit 9ad25b0

3 files changed

Lines changed: 22 additions & 40 deletions

File tree

extra/lib/plausible/stats/funnel.ex

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ defmodule Plausible.Stats.Funnel do
1111

1212
import Ecto.Query
1313
import Plausible.Stats.SQL.Fragments
14+
import Plausible.Stats.Util, only: [percentage: 2]
1415

1516
alias Plausible.ClickhouseRepo
1617
alias Plausible.Stats.{Base, Query}
@@ -167,24 +168,4 @@ defmodule Plausible.Stats.Funnel do
167168
|> elem(2)
168169
|> Enum.reverse()
169170
end
170-
171-
defp percentage(x, y) when x in [0, nil] or y in [0, nil] do
172-
"0"
173-
end
174-
175-
defp percentage(x, y) do
176-
result =
177-
x
178-
|> Decimal.div(y)
179-
|> Decimal.mult(100)
180-
|> Decimal.round(2)
181-
|> Decimal.to_string()
182-
183-
case result do
184-
<<compact::binary-size(1), ".00">> -> compact
185-
<<compact::binary-size(2), ".00">> -> compact
186-
<<compact::binary-size(3), ".00">> -> compact
187-
decimal -> decimal
188-
end
189-
end
190171
end

lib/plausible/stats/exploration.ex

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ defmodule Plausible.Stats.Exploration do
1414

1515
import Ecto.Query
1616
import Plausible.Stats.SQL.Fragments
17+
import Plausible.Stats.Util, only: [percentage: 2]
1718

1819
alias Plausible.ClickhouseRepo
1920
alias Plausible.Stats.Base
@@ -240,24 +241,4 @@ defmodule Plausible.Stats.Exploration do
240241
|> Map.fetch!(:funnel)
241242
|> Enum.reverse()
242243
end
243-
244-
defp percentage(x, y) when x in [0, nil] or y in [0, nil] do
245-
"0"
246-
end
247-
248-
defp percentage(x, y) do
249-
result =
250-
x
251-
|> Decimal.div(y)
252-
|> Decimal.mult(100)
253-
|> Decimal.round(2)
254-
|> Decimal.to_string()
255-
256-
case result do
257-
<<compact::binary-size(1), ".00">> -> compact
258-
<<compact::binary-size(2), ".00">> -> compact
259-
<<compact::binary-size(3), ".00">> -> compact
260-
decimal -> decimal
261-
end
262-
end
263244
end

lib/plausible/stats/util.ex

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,24 @@ defmodule Plausible.Stats.Util do
2929
index = Enum.find_index(query.dimensions, &(&1 == dimension))
3030
:"dim#{index}"
3131
end
32+
33+
def percentage(x, y) when is_integer(x) and x > 0 and is_integer(y) and y > 0 do
34+
result =
35+
x
36+
|> Decimal.div(y)
37+
|> Decimal.mult(100)
38+
|> Decimal.round(2)
39+
|> Decimal.to_string()
40+
41+
case result do
42+
<<compact::binary-size(1), ".00">> -> compact
43+
<<compact::binary-size(2), ".00">> -> compact
44+
<<compact::binary-size(3), ".00">> -> compact
45+
decimal -> decimal
46+
end
47+
end
48+
49+
def percentage(_x, _y) do
50+
"0"
51+
end
3252
end

0 commit comments

Comments
 (0)