-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from 10 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
4c6f47b
Change comparison input to support modes
vinibrsl c240820
Add disabled option to comparison UI
vinibrsl 44e506e
Implement last year comparison mode
vinibrsl 9af66ed
Fix comparison input text color in dark mode
vinibrsl a6c634a
Fix Credo warnings
vinibrsl b174e80
Rename last_year to year_over_year comparison mode
vinibrsl 6004968
Move comparisons to its own module
vinibrsl 960354a
Apply comparisons to top stats
vinibrsl 6f7a7f6
Add some failing tests for comparisons
ukutaht e4b55ef
Limit year over year comparison relative to current day
vinibrsl 7f479be
Move shift_back to Comparison module
vinibrsl da1c092
Change shift_back calls to Comparison.compare
vinibrsl 3c3bd41
Ignore invalid comparison modes
vinibrsl 7839bde
Add more comparison test cases
vinibrsl 3f59376
Refactor previous period functions to use earliest/2
vinibrsl c2d480b
Fix compare function typespec
vinibrsl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
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() | ||
) :: {: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, now)} | ||
else | ||
{:error, :not_supported} | ||
end | ||
end | ||
|
||
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", now) do | ||
start_date = Date.add(source_query.date_range.first, -365) | ||
end_date = earliest(source_query.date_range.last, now) |> Date.add(-365) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
defmodule Plausible.Stats.ComparisonsTest do | ||
use Plausible.DataCase | ||
alias Plausible.Stats.{Query, Comparisons} | ||
|
||
describe "previous_period" do | ||
test "shifts back this month period" 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-27] | ||
assert comparison.date_range.last == ~D[2023-02-28] | ||
end | ||
end | ||
|
||
describe "year over year" 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, "year_over_year", now) | ||
|
||
assert comparison.date_range.first == ~D[2022-03-01] | ||
assert comparison.date_range.last == ~D[2022-03-02] | ||
end | ||
|
||
test "shifts back last month period" do | ||
site = build(:site) | ||
query = Query.from(site, %{"period" => "month", "date" => "2023-02-02"}) | ||
now = ~N[2023-03-02 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 this year period" do | ||
site = build(:site) | ||
query = Query.from(site, %{"period" => "year", "date" => "2023-03-01"}) | ||
now = ~N[2023-03-02 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-02] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously the comparison query property was a boolean, on or off. Now it supports multiple modes and it's a string, e.g.
last_year
,previous_period
.