Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement "Year over Year" comparison mode #2704

Merged
merged 16 commits into from
Mar 7, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions lib/plausible/stats/comparisons.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,44 @@ defmodule Plausible.Stats.Comparisons do
@modes ~w(previous_period year_over_year)
@disallowed_periods ~w(realtime all)

@spec compare(Plausible.Site.t(), Stats.Query.t(), String.t()) ::
{:ok, Stats.Query.t()} | {:error, :not_supported}
def compare(%Plausible.Site{} = site, %Stats.Query{} = source_query, mode) do
@type mode() :: String.t() | nil

@spec compare(
Plausible.Site.t(),
Stats.Query.t(),
mode(),
NaiveDateTime.t()
) :: {:ok, Stats.Query.t()} | {:error, :not_supported}
def compare(
%Plausible.Site{} = site,
%Stats.Query{} = source_query,
mode,
now \\ NaiveDateTime.utc_now()
) do
if valid_mode?(source_query, mode) do
{:ok, do_compare(site, source_query, mode)}
{:ok, do_compare(site, source_query, mode, now)}
else
{:error, :not_supported}
end
end

defp do_compare(site, source_query, "previous_period") do
defp do_compare(site, source_query, "previous_period", _now) do
Stats.Query.shift_back(source_query, site)
end

defp do_compare(_site, source_query, "year_over_year") do
defp do_compare(_site, source_query, "year_over_year", now) do
start_date = Date.add(source_query.date_range.first, -365)
end_date = Date.add(source_query.date_range.last, -365)
range = Date.range(start_date, end_date)
end_date = earliest(source_query.date_range.last, now) |> Date.add(-365)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elegant 👌


range = Date.range(start_date, end_date)
%Stats.Query{source_query | date_range: range}
end

@spec valid_mode?(Stats.Query.t(), String.t()) :: boolean()
defp earliest(a, b) do
if Date.compare(a, b) in [:eq, :lt], do: a, else: b
end

@spec valid_mode?(Stats.Query.t(), mode()) :: boolean()
@doc """
Returns whether the source query and the selected mode support comparisons.

Expand Down