From 825a754976b9d2a64bac04794161e2b75612ea50 Mon Sep 17 00:00:00 2001 From: hq1 Date: Thu, 13 Apr 2023 13:52:54 +0200 Subject: [PATCH] Make ingest threshold configurable (#2845) * Make ingest threshold configurable * Credo --- config/.env.dev | 1 + config/.env.test | 1 + config/runtime.exs | 14 ++++++++++++++ lib/plausible/site.ex | 5 +++++ lib/plausible/site/domain.ex | 2 +- .../controllers/site_controller_test.exs | 6 +++++- 6 files changed, 27 insertions(+), 2 deletions(-) diff --git a/config/.env.dev b/config/.env.dev index 1f9cb25d33..a42c5a0f89 100644 --- a/config/.env.dev +++ b/config/.env.dev @@ -16,4 +16,5 @@ GOOGLE_CLIENT_ID=875387135161-l8tp53dpt7fdhdg9m1pc3vl42si95rh0.apps.googleuserco GOOGLE_CLIENT_SECRET=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 PROMEX_DISABLED=false +SITE_DEFAULT_INGEST_THRESHOLD=1000000 V2_MIGRATION_DONE=1 diff --git a/config/.env.test b/config/.env.test index cb773330e4..108ac07369 100644 --- a/config/.env.test +++ b/config/.env.test @@ -12,3 +12,4 @@ SITE_LIMIT=3 HCAPTCHA_SITEKEY=test HCAPTCHA_SECRET=scottiger IP_GEOLOCATION_DB=test/priv/GeoLite2-City-Test.mmdb +SITE_DEFAULT_INGEST_THRESHOLD=1000000 diff --git a/config/runtime.exs b/config/runtime.exs index 50804dd256..e9b86ac08d 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -580,3 +580,17 @@ config :plausible, Plausible.PromEx, drop_metrics_groups: [], grafana: :disabled, metrics_server: :disabled + +if not is_selfhost do + site_default_ingest_threshold = + case System.get_env("SITE_DEFAULT_INGEST_THRESHOLD") do + threshold when byte_size(threshold) > 0 -> + {value, ""} = Integer.parse(threshold) + value + + _ -> + nil + end + + config :plausible, Plausible.Site, default_ingest_threshold: site_default_ingest_threshold +end diff --git a/lib/plausible/site.ex b/lib/plausible/site.ex index 2f78d12916..75e6c4bad8 100644 --- a/lib/plausible/site.ex +++ b/lib/plausible/site.ex @@ -19,6 +19,7 @@ defmodule Plausible.Site do field :native_stats_start_at, :naive_datetime field :ingest_rate_limit_scale_seconds, :integer, default: 60 + # default is set via changeset/2 field :ingest_rate_limit_threshold, :integer field :domain_changed_from, :string @@ -61,6 +62,10 @@ defmodule Plausible.Site do name: "domain_change_disallowed", message: @domain_unique_error ) + |> put_change( + :ingest_rate_limit_threshold, + Application.get_env(:plausible, __MODULE__)[:default_ingest_threshold] + ) end def update_changeset(site, attrs \\ %{}, opts \\ []) do diff --git a/lib/plausible/site/domain.ex b/lib/plausible/site/domain.ex index 3e88aacfdf..6afe7b890b 100644 --- a/lib/plausible/site/domain.ex +++ b/lib/plausible/site/domain.ex @@ -47,7 +47,7 @@ defmodule Plausible.Site.Domain do changeset = Site.update_changeset(site, %{domain: new_domain}, opts) changeset = - if Enum.empty?(changeset.changes) and is_nil(changeset.errors[:domain]) do + if is_nil(changeset.errors[:domain]) and is_nil(changeset.changes[:domain]) do Ecto.Changeset.add_error( changeset, :domain, diff --git a/test/plausible_web/controllers/site_controller_test.exs b/test/plausible_web/controllers/site_controller_test.exs index 97d647993a..3b170be4f3 100644 --- a/test/plausible_web/controllers/site_controller_test.exs +++ b/test/plausible_web/controllers/site_controller_test.exs @@ -107,7 +107,11 @@ defmodule PlausibleWeb.SiteControllerTest do }) assert redirected_to(conn) == "/example.com/snippet" - assert Repo.get_by(Plausible.Site, domain: "example.com") + assert site = Repo.get_by(Plausible.Site, domain: "example.com") + assert site.domain == "example.com" + assert site.timezone == "Europe/London" + assert site.ingest_rate_limit_scale_seconds == 60 + assert site.ingest_rate_limit_threshold == 1_000_000 end test "fails to create the site if only http:// provided", %{conn: conn} do