Implement "Year over Year" comparison mode (#2704)
This pull request adds support for multiple comparison modes, changes the comparison checkbox to a combobox, and implements the year over year comparison mode. The feature is still behind a feature flag. Co-authored-by: Uku Taht <uku.taht@gmail.com>pull/2730/head r2023.03.07-70
parent
874d664521
commit
8b0f0cabc2
@ -0,0 +1,70 @@
|
||||
defmodule Plausible.Stats.Comparisons do
|
||||
@moduledoc """
|
||||
This module provides functions for comparing query periods.
|
||||
|
||||
It allows you to compare a given period with a previous period or with the
|
||||
same period from the previous year. For example, you can compare this month's
|
||||
main graph with last month or with the same month from last year.
|
||||
"""
|
||||
|
||||
alias Plausible.Stats
|
||||
|
||||
@modes ~w(previous_period year_over_year)
|
||||
@disallowed_periods ~w(realtime all)
|
||||
|
||||
@type mode() :: String.t() | nil
|
||||
|
||||
@spec compare(
|
||||
Plausible.Site.t(),
|
||||
Stats.Query.t(),
|
||||
mode(),
|
||||
NaiveDateTime.t() | nil
|
||||
) :: {:ok, Stats.Query.t()} | {:error, :not_supported}
|
||||
def compare(
|
||||
%Plausible.Site{} = site,
|
||||
%Stats.Query{} = source_query,
|
||||
mode,
|
||||
now \\ nil
|
||||
) do
|
||||
if valid_mode?(source_query, mode) do
|
||||
now = now || Timex.now(site.timezone)
|
||||
{:ok, do_compare(source_query, mode, now)}
|
||||
else
|
||||
{:error, :not_supported}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_compare(source_query, "year_over_year", now) do
|
||||
start_date = Date.add(source_query.date_range.first, -365)
|
||||
end_date = earliest(source_query.date_range.last, now) |> Date.add(-365)
|
||||
|
||||
range = Date.range(start_date, end_date)
|
||||
%Stats.Query{source_query | date_range: range}
|
||||
end
|
||||
|
||||
defp do_compare(source_query, "previous_period", now) do
|
||||
last = earliest(source_query.date_range.last, now)
|
||||
diff_in_days = Date.diff(source_query.date_range.first, last) - 1
|
||||
|
||||
new_first = Date.add(source_query.date_range.first, diff_in_days)
|
||||
new_last = Date.add(last, diff_in_days)
|
||||
|
||||
range = Date.range(new_first, new_last)
|
||||
%Stats.Query{source_query | date_range: range}
|
||||
end
|
||||
|
||||
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.
|
||||
|
||||
For example, the realtime view doesn't support comparisons. Additionally, only
|
||||
#{inspect(@modes)} are supported.
|
||||
"""
|
||||
def valid_mode?(%Stats.Query{period: period}, mode) do
|
||||
mode in @modes && period not in @disallowed_periods
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,131 @@
|
||||
defmodule Plausible.Stats.ComparisonsTest do
|
||||
use Plausible.DataCase
|
||||
alias Plausible.Stats.{Query, Comparisons}
|
||||
|
||||
describe "this month" do
|
||||
test "shifts back this month period" do
|
||||
site = build(:site)
|
||||
query = Query.from(site, %{"period" => "month", "date" => "2023-03-02"})
|
||||
now = ~N[2023-03-02 14:00:00]
|
||||
|
||||
{:ok, comparison} = Comparisons.compare(site, query, "previous_period", now)
|
||||
|
||||
assert comparison.date_range.first == ~D[2023-02-27]
|
||||
assert comparison.date_range.last == ~D[2023-02-28]
|
||||
end
|
||||
|
||||
test "shifts back this month period when it's the first day of the month" do
|
||||
site = build(:site)
|
||||
query = Query.from(site, %{"period" => "month", "date" => "2023-03-01"})
|
||||
now = ~N[2023-03-01 14:00:00]
|
||||
|
||||
{:ok, comparison} = Comparisons.compare(site, query, "previous_period", now)
|
||||
|
||||
assert comparison.date_range.first == ~D[2023-02-28]
|
||||
assert comparison.date_range.last == ~D[2023-02-28]
|
||||
end
|
||||
end
|
||||
|
||||
describe "previous month" do
|
||||
test "shifts back using the same number of days when previous_period" do
|
||||
site = build(:site)
|
||||
query = Query.from(site, %{"period" => "month", "date" => "2023-02-01"})
|
||||
now = ~N[2023-03-01 14:00:00]
|
||||
|
||||
{:ok, comparison} = Comparisons.compare(site, query, "previous_period", now)
|
||||
|
||||
assert comparison.date_range.first == ~D[2023-01-04]
|
||||
assert comparison.date_range.last == ~D[2023-01-31]
|
||||
end
|
||||
|
||||
test "shifts back the full month when year_over_year" do
|
||||
site = build(:site)
|
||||
query = Query.from(site, %{"period" => "month", "date" => "2023-02-01"})
|
||||
now = ~N[2023-03-01 14:00:00]
|
||||
|
||||
{:ok, comparison} = Comparisons.compare(site, query, "year_over_year", now)
|
||||
|
||||
assert comparison.date_range.first == ~D[2022-02-01]
|
||||
assert comparison.date_range.last == ~D[2022-02-28]
|
||||
end
|
||||
|
||||
test "shifts back whole month plus one day when year_over_year and a leap year" do
|
||||
site = build(:site)
|
||||
query = Query.from(site, %{"period" => "month", "date" => "2020-02-01"})
|
||||
now = ~N[2023-03-01 14:00:00]
|
||||
|
||||
{:ok, comparison} = Comparisons.compare(site, query, "year_over_year", now)
|
||||
|
||||
assert comparison.date_range.first == ~D[2019-02-01]
|
||||
assert comparison.date_range.last == ~D[2019-03-01]
|
||||
end
|
||||
end
|
||||
|
||||
describe "year to date" do
|
||||
test "shifts back by the same number of days when previous_period" do
|
||||
site = build(:site)
|
||||
query = Query.from(site, %{"period" => "year", "date" => "2023-03-01"})
|
||||
now = ~N[2023-03-01 14:00:00]
|
||||
|
||||
{:ok, comparison} = Comparisons.compare(site, query, "previous_period", now)
|
||||
|
||||
assert comparison.date_range.first == ~D[2022-11-02]
|
||||
assert comparison.date_range.last == ~D[2022-12-31]
|
||||
end
|
||||
|
||||
test "shifts back by the same number of days when year_over_year" do
|
||||
site = build(:site)
|
||||
query = Query.from(site, %{"period" => "year", "date" => "2023-03-01"})
|
||||
now = ~N[2023-03-01 14:00:00]
|
||||
|
||||
{:ok, comparison} = Comparisons.compare(site, query, "year_over_year", now)
|
||||
|
||||
assert comparison.date_range.first == ~D[2022-01-01]
|
||||
assert comparison.date_range.last == ~D[2022-03-01]
|
||||
end
|
||||
end
|
||||
|
||||
describe "previous year" do
|
||||
test "shifts back a whole year when year_over_year" do
|
||||
site = build(:site)
|
||||
query = Query.from(site, %{"period" => "year", "date" => "2022-03-02"})
|
||||
|
||||
{:ok, comparison} = Comparisons.compare(site, query, "year_over_year")
|
||||
|
||||
assert comparison.date_range.first == ~D[2021-01-01]
|
||||
assert comparison.date_range.last == ~D[2021-12-31]
|
||||
end
|
||||
|
||||
test "shifts back a whole year when previous_period" do
|
||||
site = build(:site)
|
||||
query = Query.from(site, %{"period" => "year", "date" => "2022-03-02"})
|
||||
|
||||
{:ok, comparison} = Comparisons.compare(site, query, "previous_period")
|
||||
|
||||
assert comparison.date_range.first == ~D[2021-01-01]
|
||||
assert comparison.date_range.last == ~D[2021-12-31]
|
||||
end
|
||||
end
|
||||
|
||||
describe "custom" do
|
||||
test "shifts back by the same number of days when previous_period" do
|
||||
site = build(:site)
|
||||
query = Query.from(site, %{"period" => "custom", "date" => "2023-01-01,2023-01-07"})
|
||||
|
||||
{:ok, comparison} = Comparisons.compare(site, query, "previous_period")
|
||||
|
||||
assert comparison.date_range.first == ~D[2022-12-25]
|
||||
assert comparison.date_range.last == ~D[2022-12-31]
|
||||
end
|
||||
|
||||
test "shifts back to last year when year_over_year" do
|
||||
site = build(:site)
|
||||
query = Query.from(site, %{"period" => "custom", "date" => "2023-01-01,2023-01-07"})
|
||||
|
||||
{:ok, comparison} = Comparisons.compare(site, query, "year_over_year")
|
||||
|
||||
assert comparison.date_range.first == ~D[2022-01-01]
|
||||
assert comparison.date_range.last == ~D[2022-01-07]
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue