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 f51943a commit b0c7646
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
13 changes: 7 additions & 6 deletions lib/workers/traffic_change_notifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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 @@ -19,12 +20,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,
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
28 changes: 19 additions & 9 deletions test/workers/traffic_change_notifier_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ 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)])
site =
insert(:site,
memberships: [
build(:site_membership,
user: build(:user, accept_traffic_until: Date.utc_today()),
role: :owner
)
]
)

insert(:drop_notification,
site: site,
Expand All @@ -27,10 +31,15 @@ defmodule Plausible.Workers.TrafficChangeNotifierTest do
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)])
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,
Expand Down Expand Up @@ -68,6 +77,7 @@ 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 b0c7646

Please sign in to comment.