Add basic spike notifications
Co-authored-by: Jerod Santo <jerod@changelog.com>pull/453/head
parent
838e95421c
commit
54f70717c4
@ -0,0 +1,30 @@
|
||||
defmodule Plausible.Site.SpikeNotification do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
schema "spike_notifications" do
|
||||
field :recipients, {:array, :string}
|
||||
field :threshold, :integer
|
||||
field :last_sent, :naive_datetime
|
||||
belongs_to :site, Plausible.Site
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
def changeset(settings, attrs \\ %{}) do
|
||||
settings
|
||||
|> cast(attrs, [:site_id, :recipients])
|
||||
|> validate_required([:site_id, :recipients])
|
||||
|> unique_constraint(:site)
|
||||
end
|
||||
|
||||
def add_recipient(report, recipient) do
|
||||
report
|
||||
|> change(recipients: report.recipients ++ [recipient])
|
||||
end
|
||||
|
||||
def remove_recipient(report, recipient) do
|
||||
report
|
||||
|> change(recipients: List.delete(report.recipients, recipient))
|
||||
end
|
||||
end
|
||||
@ -0,0 +1 @@
|
||||
Yours site has <%= @current_visitors %> visitors. Nice!
|
||||
@ -0,0 +1,39 @@
|
||||
defmodule Plausible.Workers.SpikeNotifier do
|
||||
use Plausible.Repo
|
||||
alias Plausible.Stats.Query
|
||||
use Oban.Worker, queue: :spike_notifications
|
||||
@at_most_every "12 hours"
|
||||
|
||||
@impl Oban.Worker
|
||||
def perform(_args, _job, clickhouse \\ Plausible.Stats.Clickhouse) do
|
||||
notifications = Repo.all(
|
||||
from sn in Plausible.Site.SpikeNotification,
|
||||
where: is_nil(sn.last_sent),
|
||||
or_where: sn.last_sent < fragment("now() - INTERVAL ?", @at_most_every)
|
||||
)
|
||||
|
||||
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)
|
||||
end
|
||||
end
|
||||
|
||||
def notify(notification, current_visitors) do
|
||||
if current_visitors >= notification.threshold do
|
||||
for recipient <- notification.recipients do
|
||||
send_notification(recipient, notification.site, current_visitors)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp send_notification(recipient, site, current_visitors) do
|
||||
template = PlausibleWeb.Email.spike_notification(recipient, site, current_visitors)
|
||||
try do
|
||||
Plausible.Mailer.send_email(template)
|
||||
rescue
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,14 @@
|
||||
defmodule Plausible.Repo.Migrations.AddSpikeNotifications do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
create table(:spike_notifications) do
|
||||
add :site_id, references(:sites), null: false
|
||||
add :threshold, :integer, null: false
|
||||
add :last_sent, :naive_datetime
|
||||
add :recipients, {:array, :citext}, null: false, default: []
|
||||
|
||||
timestamps()
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,44 @@
|
||||
defmodule Plausible.Workers.SpikeNotifierTest do
|
||||
use Plausible.DataCase
|
||||
use Bamboo.Test
|
||||
import Double
|
||||
alias Plausible.Workers.SpikeNotifier
|
||||
|
||||
test "does not notify anyone if current visitors does not exceed notification threshold" do
|
||||
site = insert(:site)
|
||||
insert(:spike_notification, site: site, threshold: 10, recipients: ["jerod@example.com", "uku@example.com"])
|
||||
|
||||
clickhouse_stub = stub(Plausible.Stats.Clickhouse, :current_visitors, fn _site, _query -> 5 end)
|
||||
SpikeNotifier.perform(nil, nil, clickhouse_stub)
|
||||
|
||||
assert_no_emails_delivered()
|
||||
end
|
||||
|
||||
test "notifies all recipients when traffic is higher than configured threshold" do
|
||||
site = insert(:site)
|
||||
insert(:spike_notification, site: site, threshold: 10, recipients: ["jerod@example.com", "uku@example.com"])
|
||||
|
||||
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: "jerod@example.com"]
|
||||
)
|
||||
|
||||
assert_email_delivered_with(
|
||||
subject: "Traffic spike on #{site.domain}",
|
||||
to: [nil: "uku@example.com"]
|
||||
)
|
||||
end
|
||||
|
||||
test "does not notify anyone if a notification already went out in the last 12 hours" do
|
||||
site = insert(:site)
|
||||
insert(:spike_notification, site: site, threshold: 10, recipients: ["jerod@example.com", "uku@example.com"], last_sent: Timex.now())
|
||||
|
||||
clickhouse_stub = stub(Plausible.Stats.Clickhouse, :current_visitors, fn _site, _query -> 10 end)
|
||||
SpikeNotifier.perform(nil, nil, clickhouse_stub)
|
||||
|
||||
assert_no_emails_delivered()
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue