Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Refactor `Imported.check_dates` (->`clamp_dates`) for better felxibility (#3983)

Adrian Gruntkowskib95106572472
diff --git a/lib/plausible/imported.ex b/lib/plausible/imported.ex
index 82cdc9a8eb..b21aac504d 100644
--- a/lib/plausible/imported.ex
+++ b/lib/plausible/imported.ex
@@ -158,16 +158,22 @@ defmodule Plausible.Imported do
:ok
end
- @spec check_dates(Site.t(), Date.t() | nil, Date.t() | nil) ::
- {:ok, Date.t(), Date.t()} | {:error, :no_data | :no_time_window}
- def check_dates(_site, nil, _end_date), do: {:error, :no_data}
+ @spec clamp_dates(Site.t(), Date.t(), Date.t()) ::
+ {:ok, Date.t(), Date.t()} | {:error, :no_time_window}
+ def clamp_dates(site, start_date, end_date) do
+ cutoff_date = get_cutoff_date(site)
+ occupied_ranges = get_occupied_date_ranges(site)
- def check_dates(site, start_date, end_date) do
- cutoff_date = Plausible.Sites.native_stats_start_date(site) || Timex.today(site.timezone)
+ clamp_dates(occupied_ranges, cutoff_date, start_date, end_date)
+ end
+
+ @spec clamp_dates([Date.Range.t()], Date.t(), Date.t(), Date.t()) ::
+ {:ok, Date.t(), Date.t()} | {:error, :no_time_window}
+ def clamp_dates(occupied_ranges, cutoff_date, start_date, end_date) do
end_date = Enum.min([end_date, cutoff_date], Date)
with true <- Date.diff(end_date, start_date) >= 2,
- [_ | _] = free_ranges <- find_free_ranges(start_date, end_date, site) do
+ [_ | _] = free_ranges <- find_free_ranges(start_date, end_date, occupied_ranges) do
longest = Enum.max_by(free_ranges, &Date.diff(&1.last, &1.first))
{:ok, longest.first, longest.last}
else
@@ -175,13 +181,20 @@ defmodule Plausible.Imported do
end
end
- defp find_free_ranges(start_date, end_date, site) do
- occupied_ranges =
- site
- |> Imported.list_all_imports(Imported.SiteImport.completed())
- |> Enum.reject(&(Date.diff(&1.end_date, &1.start_date) < 2))
- |> Enum.map(&Date.range(&1.start_date, &1.end_date))
+ @spec get_occupied_date_ranges(Site.t()) :: [Date.Range.t()]
+ def get_occupied_date_ranges(site) do
+ site
+ |> Imported.list_all_imports(Imported.SiteImport.completed())
+ |> Enum.reject(&(Date.diff(&1.end_date, &1.start_date) < 2))
+ |> Enum.map(&Date.range(&1.start_date, &1.end_date))
+ end
+
+ @spec get_cutoff_date(Site.t()) :: Date.t()
+ def get_cutoff_date(site) do
+ Plausible.Sites.native_stats_start_date(site) || Timex.today(site.timezone)
+ end
+ defp find_free_ranges(start_date, end_date, occupied_ranges) do
Date.range(start_date, end_date)
|> free_ranges(start_date, occupied_ranges, [])
end
diff --git a/lib/plausible_web/controllers/google_analytics_controller.ex b/lib/plausible_web/controllers/google_analytics_controller.ex
index ade4752358..e98c63242c 100644
--- a/lib/plausible_web/controllers/google_analytics_controller.ex
+++ b/lib/plausible_web/controllers/google_analytics_controller.ex
@@ -139,7 +139,8 @@ defmodule PlausibleWeb.GoogleAnalyticsController do
with {:ok, api_start_date} <-
Google.API.get_analytics_start_date(access_token, property_or_view),
{:ok, api_end_date} <- Google.API.get_analytics_end_date(access_token, property_or_view),
- {:ok, start_date, end_date} <- Imported.check_dates(site, api_start_date, api_end_date) do
+ :ok <- ensure_dates(api_start_date, api_end_date),
+ {:ok, start_date, end_date} <- Imported.clamp_dates(site, api_start_date, api_end_date) do
action =
if Timex.before?(api_start_date, @universal_analytics_new_user_metric_date) do
:user_metric_notice
@@ -299,7 +300,7 @@ defmodule PlausibleWeb.GoogleAnalyticsController do
legacy: legacy == "true"
]
- with {:ok, start_date, end_date} <- Imported.check_dates(site, start_date, end_date),
+ with {:ok, start_date, end_date} <- Imported.clamp_dates(site, start_date, end_date),
import_opts = [{:start_date, start_date}, {:end_date, end_date} | import_opts],
{:ok, _} <- schedule_job(site, current_user, property_or_view, import_opts) do
conn
@@ -324,6 +325,9 @@ defmodule PlausibleWeb.GoogleAnalyticsController do
end
end
+ defp ensure_dates(%Date{}, %Date{}), do: :ok
+ defp ensure_dates(_, _), do: {:error, :no_data}
+
defp schedule_job(site, current_user, property_or_view, opts) do
if Google.API.property?(property_or_view) do
opts = Keyword.put(opts, :property, property_or_view)
diff --git a/test/plausible/imported_test.exs b/test/plausible/imported_test.exs
index df81f5a416..c0d994f25e 100644
--- a/test/plausible/imported_test.exs
+++ b/test/plausible/imported_test.exs
@@ -119,7 +119,7 @@ defmodule Plausible.ImportedTest do
end
end
- describe "check_dates/3" do
+ describe "clamp_dates/3" do
test "crops dates from both ends when overlapping with existing import and native stats" do
site = insert(:site)
@@ -139,7 +139,7 @@ defmodule Plausible.ImportedTest do
)
assert {:ok, ~D[2021-05-12], ~D[2023-10-25]} =
- Imported.check_dates(site, ~D[2021-04-11], ~D[2024-01-12])
+ Imported.clamp_dates(site, ~D[2021-04-11], ~D[2024-01-12])
end
test "picks longest continuous range when containing existing import" do
@@ -161,27 +161,27 @@ defmodule Plausible.ImportedTest do
)
assert {:ok, ~D[2021-05-12], ~D[2023-10-25]} =
- Imported.check_dates(site, ~D[2019-03-21], ~D[2024-01-12])
+ Imported.clamp_dates(site, ~D[2019-03-21], ~D[2024-01-12])
end
test "does not alter the dates when there are no imports and no native stats" do
site = insert(:site)
assert {:ok, ~D[2021-05-12], ~D[2024-01-12]} =
- Imported.check_dates(site, ~D[2021-05-12], ~D[2024-01-12])
+ Imported.clamp_dates(site, ~D[2021-05-12], ~D[2024-01-12])
end
test "ignores input date range difference smaller than 2 days" do
site = insert(:site)
assert {:error, :no_time_window} =
- Imported.check_dates(site, ~D[2024-01-12], ~D[2024-01-12])
+ Imported.clamp_dates(site, ~D[2024-01-12], ~D[2024-01-12])
assert {:error, :no_time_window} =
- Imported.check_dates(site, ~D[2024-01-12], ~D[2024-01-13])
+ Imported.clamp_dates(site, ~D[2024-01-12], ~D[2024-01-13])
assert {:ok, ~D[2024-01-12], ~D[2024-01-14]} =
- Imported.check_dates(site, ~D[2024-01-12], ~D[2024-01-14])
+ Imported.clamp_dates(site, ~D[2024-01-12], ~D[2024-01-14])
end
test "ignores imports with date range difference smaller than 2 days" do
@@ -199,7 +199,7 @@ defmodule Plausible.ImportedTest do
)
assert {:ok, ~D[2021-04-22], ~D[2024-03-14]} =
- Imported.check_dates(site, ~D[2021-04-22], ~D[2024-03-14])
+ Imported.clamp_dates(site, ~D[2021-04-22], ~D[2024-03-14])
end
test "returns no time window when input range starts after native stats start date" do
@@ -210,7 +210,7 @@ defmodule Plausible.ImportedTest do
])
assert {:error, :no_time_window} =
- Imported.check_dates(site, ~D[2023-10-28], ~D[2024-01-13])
+ Imported.clamp_dates(site, ~D[2023-10-28], ~D[2024-01-13])
end
test "returns no time window when input range starts less than 2 days before native stats start date" do
@@ -221,7 +221,7 @@ defmodule Plausible.ImportedTest do
])
assert {:error, :no_time_window} =
- Imported.check_dates(site, ~D[2023-10-24], ~D[2024-01-13])
+ Imported.clamp_dates(site, ~D[2023-10-24], ~D[2024-01-13])
end
test "crops time range at native stats start date when effective range is 2 days or longer" do
@@ -232,13 +232,7 @@ defmodule Plausible.ImportedTest do
])
assert {:ok, ~D[2023-10-23], ~D[2023-10-25]} =
- Imported.check_dates(site, ~D[2023-10-23], ~D[2024-01-13])
- end
-
- test "returns no data error when start date missing" do
- site = insert(:site)
-
- assert {:error, :no_data} = Imported.check_dates(site, nil, nil)
+ Imported.clamp_dates(site, ~D[2023-10-23], ~D[2024-01-13])
end
test "returns no time window error when date range overlaps with existing import and stats completely" do
@@ -260,7 +254,7 @@ defmodule Plausible.ImportedTest do
)
assert {:error, :no_time_window} =
- Imported.check_dates(site, ~D[2021-04-11], ~D[2024-01-12])
+ Imported.clamp_dates(site, ~D[2021-04-11], ~D[2024-01-12])
end
end
end