Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Make sure a notification is not sent again within 12 hours
Uku Taht11c9bdc328b9
diff --git a/lib/plausible/site/spike_notification.ex b/lib/plausible/site/spike_notification.exindex 9cca56cddf..30a0a43716 100644--- a/lib/plausible/site/spike_notification.ex+++ b/lib/plausible/site/spike_notification.ex@@ -18,6 +18,11 @@ defmodule Plausible.Site.SpikeNotification do|> unique_constraint(:site)end+ def was_sent(schema) do+ schema+ |> change(last_sent: NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second))+ end+def add_recipient(report, recipient) doreport|> change(recipients: report.recipients ++ [recipient])diff --git a/lib/workers/spike_notifier.ex b/lib/workers/spike_notifier.exindex 1faa9060ef..0865232884 100644--- a/lib/workers/spike_notifier.ex+++ b/lib/workers/spike_notifier.ex@@ -1,19 +1,20 @@defmodule Plausible.Workers.SpikeNotifier douse Plausible.Repoalias Plausible.Stats.Query+ alias Plausible.Site.SpikeNotificationuse Oban.Worker, queue: :spike_notifications@at_most_every "12 hours"@impl Oban.Workerdef perform(_args, _job, clickhouse \\ Plausible.Stats.Clickhouse) donotifications = Repo.all(- from sn in Plausible.Site.SpikeNotification,+ from sn in SpikeNotification,where: is_nil(sn.last_sent),- or_where: sn.last_sent < fragment("now() - INTERVAL ?", @at_most_every)+ or_where: sn.last_sent < fragment("now() - INTERVAL ?", @at_most_every),+ preload: :site)for notification <- notifications do- notification = Repo.preload(notification, :site)query = Query.from(notification.site.timezone, %{"period" => "realtime"})current_visitors = clickhouse.current_visitors(notification.site, query)notify(notification, current_visitors)@@ -25,6 +26,9 @@ defmodule Plausible.Workers.SpikeNotifier dofor recipient <- notification.recipients dosend_notification(recipient, notification.site, current_visitors)end+ notification+ |> SpikeNotification.was_sent()+ |> Repo.updateendenddiff --git a/test/workers/spike_notifier_test.exs b/test/workers/spike_notifier_test.exsindex fb673cb62a..7ee78fb604 100644--- a/test/workers/spike_notifier_test.exs+++ b/test/workers/spike_notifier_test.exs@@ -34,9 +34,16 @@ defmodule Plausible.Workers.SpikeNotifierTest dotest "does not notify anyone if a notification already went out in the last 12 hours" dosite = insert(:site)- insert(:spike_notification, site: site, threshold: 10, recipients: ["[email protected]", "[email protected]"], last_sent: Timex.now())-+ insert(:spike_notification, site: site, threshold: 10, recipients: ["[email protected]"])clickhouse_stub = stub(Plausible.Stats.Clickhouse, :current_visitors, fn _site, _query -> 10 end)++ SpikeNotifier.perform(nil, nil, clickhouse_stub)++ assert_email_delivered_with(+ subject: "Traffic spike on #{site.domain}",+ to: [nil: "[email protected]"]+ )+SpikeNotifier.perform(nil, nil, clickhouse_stub)assert_no_emails_delivered()