Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
aerosol committed Jul 2, 2024
1 parent 44816c6 commit f51943a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/workers/traffic_change_notifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Plausible.Workers.TrafficChangeNotifier do

@impl Oban.Worker
def perform(_job, clickhouse \\ Plausible.Stats.Clickhouse) do
today = Date.utc_today()
notifications =
Repo.all(
from sn in TrafficChangeNotification,
Expand All @@ -18,6 +19,12 @@ defmodule Plausible.Workers.TrafficChangeNotifier do
join: s in Plausible.Site,
on: sn.site_id == s.id,
where: not s.locked,
join: sm in Plausible.Site.Membership,
on: sm.site_id == s.id,
where: sm.role == :owner,
join: u in Plausible.Auth.User,
on: u.id == sm.user_id,
where: is_nil(u.accept_traffic_until) or u.accept_traffic_until > ^today,
preload: [site: s]
)

Expand Down
45 changes: 44 additions & 1 deletion test/workers/traffic_change_notifier_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,50 @@ defmodule Plausible.Workers.TrafficChangeNotifierTest do
alias Plausible.Workers.TrafficChangeNotifier

describe "drops" do

test "does not notify anyone if we've stopped accepting traffic for the owner" do
site = insert(:site, memberships: [
build(:site_membership,
user: build(:user, accept_traffic_until: Date.utc_today()),
role: :owner)])

insert(:drop_notification,
site: site,
threshold: 10,
recipients: ["[email protected]", "[email protected]"]
)

clickhouse_stub =
stub(Plausible.Stats.Clickhouse, :current_visitors_12h, fn _site -> 1 end)

TrafficChangeNotifier.perform(nil, clickhouse_stub)

assert_no_emails_delivered()
end

test "does notify if threshold reached and we're accepting traffic" do
site = insert(:site, memberships: [
build(:site_membership,
user: build(:user, accept_traffic_until: Date.utc_today() |> Date.add(+1)),
role: :owner)])

insert(:drop_notification,
site: site,
threshold: 10,
recipients: ["[email protected]"]
)

clickhouse_stub =
stub(Plausible.Stats.Clickhouse, :current_visitors_12h, fn _site -> 1 end)

TrafficChangeNotifier.perform(nil, clickhouse_stub)

assert_email_delivered_with(
subject: "Traffic Drop on #{site.domain}",
to: [nil: "[email protected]"]
)
end

test "does not notify anyone if current visitors does not drop below notification threshold" do
site = insert(:site)

Expand All @@ -24,7 +68,6 @@ defmodule Plausible.Workers.TrafficChangeNotifierTest do

test "notifies all recipients when traffic drops under configured threshold" do
site = insert(:site)

insert(:drop_notification,
site: site,
threshold: 10,
Expand Down

0 comments on commit f51943a

Please sign in to comment.