|
|
|
|
@ -44,6 +44,15 @@ defmodule Plausible.Stats.Comparisons do
|
|
|
|
|
* `:to` - a ISO-8601 date string used when mode is `"custom"`. Must be
|
|
|
|
|
after `from`.
|
|
|
|
|
|
|
|
|
|
* `:match_day_of_week?` - determines whether the comparison query should be
|
|
|
|
|
adjusted to match the day of the week of the source query. When this option
|
|
|
|
|
is set to true, the comparison query is shifted to start on the same day of
|
|
|
|
|
the week as the source query, rather than on the exact same date. For
|
|
|
|
|
example, if the source query starts on Sunday, January 1st, 2023 and the
|
|
|
|
|
`year_over_year` comparison query is configured to `match_day_of_week?`,
|
|
|
|
|
it will be shifted to start on Sunday, January 2nd, 2022 instead of
|
|
|
|
|
January 1st. Defaults to false.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
def compare(%Plausible.Site{} = site, %Stats.Query{} = source_query, mode, opts \\ []) do
|
|
|
|
|
if valid_mode?(source_query, mode) do
|
|
|
|
|
@ -61,7 +70,13 @@ defmodule Plausible.Stats.Comparisons do
|
|
|
|
|
end_date = earliest(source_query.date_range.last, now) |> Date.add(-365)
|
|
|
|
|
|
|
|
|
|
range = Date.range(start_date, end_date)
|
|
|
|
|
{:ok, %Stats.Query{source_query | date_range: range}}
|
|
|
|
|
|
|
|
|
|
comparison_query =
|
|
|
|
|
source_query
|
|
|
|
|
|> Map.put(:date_range, range)
|
|
|
|
|
|> maybe_match_day_of_week(source_query, opts)
|
|
|
|
|
|
|
|
|
|
{:ok, comparison_query}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp do_compare(source_query, "previous_period", opts) do
|
|
|
|
|
@ -74,7 +89,13 @@ defmodule Plausible.Stats.Comparisons do
|
|
|
|
|
new_last = Date.add(last, diff_in_days)
|
|
|
|
|
|
|
|
|
|
range = Date.range(new_first, new_last)
|
|
|
|
|
{:ok, %Stats.Query{source_query | date_range: range}}
|
|
|
|
|
|
|
|
|
|
comparison_query =
|
|
|
|
|
source_query
|
|
|
|
|
|> Map.put(:date_range, range)
|
|
|
|
|
|> maybe_match_day_of_week(source_query, opts)
|
|
|
|
|
|
|
|
|
|
{:ok, comparison_query}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp do_compare(source_query, "custom", opts) do
|
|
|
|
|
@ -91,6 +112,52 @@ defmodule Plausible.Stats.Comparisons do
|
|
|
|
|
if Date.compare(a, b) in [:eq, :lt], do: a, else: b
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp maybe_match_day_of_week(comparison_query, source_query, opts) do
|
|
|
|
|
if Keyword.get(opts, :match_day_of_week?, false) do
|
|
|
|
|
day_to_match = Date.day_of_week(source_query.date_range.first)
|
|
|
|
|
|
|
|
|
|
new_first =
|
|
|
|
|
shift_to_nearest(
|
|
|
|
|
day_to_match,
|
|
|
|
|
comparison_query.date_range.first,
|
|
|
|
|
source_query.date_range.first
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
days_shifted = Date.diff(new_first, comparison_query.date_range.first)
|
|
|
|
|
new_last = Date.add(comparison_query.date_range.last, days_shifted)
|
|
|
|
|
|
|
|
|
|
new_range = Date.range(new_first, new_last)
|
|
|
|
|
%Stats.Query{comparison_query | date_range: new_range}
|
|
|
|
|
else
|
|
|
|
|
comparison_query
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp shift_to_nearest(day_of_week, date, reject) do
|
|
|
|
|
if Date.day_of_week(date) == day_of_week do
|
|
|
|
|
date
|
|
|
|
|
else
|
|
|
|
|
[next_occurring(day_of_week, date), previous_occurring(day_of_week, date)]
|
|
|
|
|
|> Enum.sort_by(&Date.diff(date, &1))
|
|
|
|
|
|> Enum.reject(&(&1 == reject))
|
|
|
|
|
|> List.first()
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp next_occurring(day_of_week, date) do
|
|
|
|
|
days_to_add = day_of_week - Date.day_of_week(date)
|
|
|
|
|
days_to_add = if days_to_add > 0, do: days_to_add, else: days_to_add + 7
|
|
|
|
|
|
|
|
|
|
Date.add(date, days_to_add)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp previous_occurring(day_of_week, date) do
|
|
|
|
|
days_to_subtract = Date.day_of_week(date) - day_of_week
|
|
|
|
|
days_to_subtract = if days_to_subtract > 0, do: days_to_subtract, else: days_to_subtract + 7
|
|
|
|
|
|
|
|
|
|
Date.add(date, -days_to_subtract)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@spec valid_mode?(Stats.Query.t(), mode()) :: boolean()
|
|
|
|
|
@doc """
|
|
|
|
|
Returns whether the source query and the selected mode support comparisons.
|
|
|
|
|
|