Skip to content

Commit

Permalink
Follow-up to Solve noisy warnings about a negative range was inferred…
Browse files Browse the repository at this point in the history
… for Date.range/2 (#4816)

* Clamp dates on both directions

We could still get warnings about negative date ranges when the date
being queried is in the future.

This could happen if the users local time is in the future for some
reason or they manually edit the url.

* Update cond
  • Loading branch information
macobo authored Nov 18, 2024
1 parent d187e59 commit 07a3436
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/plausible/stats/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,19 @@ defmodule Plausible.Stats.Query do

Date.range(
date_range.first,
earliest(date_range.last, today)
clamp(today, date_range)
)
else
date_range
end
end

defp earliest(a, b) do
if Date.compare(a, b) in [:eq, :lt], do: a, else: b
defp clamp(date, date_range) do
cond do
date in date_range -> date
Date.before?(date, date_range.first) -> date_range.first
Date.after?(date, date_range.last) -> date_range.last
end
end

def set(query, keywords) do
Expand Down
7 changes: 7 additions & 0 deletions test/plausible/stats/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,13 @@ defmodule Plausible.Stats.QueryTest do
~U[2024-05-07 07:00:00Z],
trim_trailing: true
) == Date.range(~D[2024-05-05], ~D[2024-05-06])

assert date_range(
{~U[2024-05-05 12:00:00Z], ~U[2024-05-08 11:59:59Z]},
"Etc/GMT+12",
~U[2024-05-03 07:00:00Z],
trim_trailing: true
) == Date.range(~D[2024-05-05], ~D[2024-05-05])
end
end

Expand Down

0 comments on commit 07a3436

Please sign in to comment.