Skip to content

Commit b92fad3

Browse files
authored
Handle comparison of today and 24h against a date range (#6213)
* Handle comparison of today and 24h against a date range * Remove unnecessary clauses from `DateTimeRange` * Add tests for mixing dates and datetimes in `DateTimeRange.new!`
1 parent efe996d commit b92fad3

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

lib/plausible/stats/comparisons.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,16 @@ defmodule Plausible.Stats.Comparisons do
166166
DateTimeRange.new!(comparison_start, comparison_end)
167167
end
168168

169+
defp get_comparison_datetime_range(
170+
%Query{
171+
input_date_range: input_range,
172+
include: %{compare: {:date_range, from_date, to_date}}
173+
} = source_query
174+
)
175+
when input_range in [:"24h", :day] do
176+
DateTimeRange.new!(from_date, to_date, source_query.timezone)
177+
end
178+
169179
defp get_comparison_date_range(%Query{include: %{compare: :year_over_year}} = source_query) do
170180
source_date_range = Query.date_range(source_query, trim_trailing: true)
171181

test/plausible/stats/comparisons_test.exs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,40 @@ defmodule Plausible.Stats.ComparisonsTest do
496496
assert comparison_query.utc_time_range.last == ~U[2023-03-14 18:30:00Z]
497497
end
498498

499+
test "custom time zone works with 24h comparison and a custom date range" do
500+
site = insert(:site, timezone: "US/Eastern")
501+
502+
query =
503+
QueryBuilder.build!(site,
504+
metrics: [:visitors],
505+
input_date_range: :"24h",
506+
include: [compare: {:date_range, ~D[2023-02-15], ~D[2023-02-18]}],
507+
now: ~U[2023-03-15 18:30:00Z]
508+
)
509+
510+
comparison_query = Comparisons.get_comparison_query(query)
511+
512+
assert comparison_query.utc_time_range.first == ~U[2023-02-15 05:00:00Z]
513+
assert comparison_query.utc_time_range.last == ~U[2023-02-19 04:59:59Z]
514+
end
515+
516+
test "custom time zone works with day/today comparison and a custom date range" do
517+
site = insert(:site, timezone: "US/Eastern")
518+
519+
query =
520+
QueryBuilder.build!(site,
521+
metrics: [:visitors],
522+
input_date_range: :day,
523+
include: [compare: {:date_range, ~D[2023-02-15], ~D[2023-02-18]}],
524+
now: ~U[2023-03-15 18:30:00Z]
525+
)
526+
527+
comparison_query = Comparisons.get_comparison_query(query)
528+
529+
assert comparison_query.utc_time_range.first == ~U[2023-02-15 05:00:00Z]
530+
assert comparison_query.utc_time_range.last == ~U[2023-02-19 04:59:59Z]
531+
end
532+
499533
test "shifts back 24h period to match day of week when mode is previous_period with match_day_of_week",
500534
%{site: site} do
501535
query =
@@ -516,5 +550,39 @@ defmodule Plausible.Stats.ComparisonsTest do
516550
assert comparison_query.utc_time_range.first == ~U[2023-03-07 18:30:00Z]
517551
assert comparison_query.utc_time_range.last == ~U[2023-03-08 18:30:00Z]
518552
end
553+
554+
test "handles custom date range comparison against 24h period", %{site: site} do
555+
query =
556+
QueryBuilder.build!(site,
557+
metrics: [:visitors],
558+
input_date_range: :"24h",
559+
include: [compare: {:date_range, ~D[2023-02-15], ~D[2023-02-18]}],
560+
561+
# Wednesday
562+
now: ~U[2023-03-15 18:30:00Z]
563+
)
564+
565+
comparison_query = Comparisons.get_comparison_query(query)
566+
567+
assert comparison_query.utc_time_range.first == ~U[2023-02-15 00:00:00Z]
568+
assert comparison_query.utc_time_range.last == ~U[2023-02-18 23:59:59Z]
569+
end
570+
571+
test "handles custom date range comparison day (today) period", %{site: site} do
572+
query =
573+
QueryBuilder.build!(site,
574+
metrics: [:visitors],
575+
input_date_range: :day,
576+
include: [compare: {:date_range, ~D[2023-02-15], ~D[2023-02-18]}],
577+
578+
# Wednesday
579+
now: ~U[2023-03-15 18:30:00Z]
580+
)
581+
582+
comparison_query = Comparisons.get_comparison_query(query)
583+
584+
assert comparison_query.utc_time_range.first == ~U[2023-02-15 00:00:00Z]
585+
assert comparison_query.utc_time_range.last == ~U[2023-02-18 23:59:59Z]
586+
end
519587
end
520588
end

test/plausible/stats/datetime_range_test.exs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,26 @@ defmodule Plausible.Stats.DateTimeRangeTest do
4646
assert range.last == DateTime.new!(last_date, ~T[23:59:59], "UTC")
4747
end
4848

49+
test "creates a range from a mix of date and datetime" do
50+
first = DateTime.new!(~D[2023-02-15], ~T[12:30:00], "UTC")
51+
last = ~D[2023-02-18]
52+
53+
range = DateTimeRange.new!(first, last, "UTC")
54+
55+
assert range.first == first
56+
assert range.last == DateTime.new!(last, ~T[23:59:59], "UTC")
57+
end
58+
59+
test "creates a range from a mix of datetime and date" do
60+
first = ~D[2023-02-15]
61+
last = DateTime.new!(~D[2023-02-18], ~T[12:30:00], "UTC")
62+
63+
range = DateTimeRange.new!(first, last, "UTC")
64+
65+
assert range.first == DateTime.new!(first, ~T[00:00:00], "UTC")
66+
assert range.last == last
67+
end
68+
4969
test "handles timezone gaps (spring forward)" do
5070
# https://stackoverflow.com/questions/18489927/a-day-without-midnight
5171
range = DateTimeRange.new!(~D[2020-03-29], ~D[2020-03-29], "Asia/Beirut")

0 commit comments

Comments
 (0)