analytics/lib/workers/check_usage.ex
Marko Saric 0b00762591
Changes to the emails as discussed (#3540)
* Update over_limit.html.eex

* Update dashboard_locked.html.eex

* Update dashboard_locked.html.eex

* Update over_limit.html.eex

* Update dashboard_locked.html.eex

* fix tests

* stop querying owned_site_ids three times

... when querying for billing cycles. Adds an optional `owned_site_ids`
argument to the `usage_cycle` function.

* add penultimate billing cycle info to emails

This commit also refactors some code and adds unit tests to email templates

* use delimit_integer instead of large_number_format

... to display usage with exact numbers such as 1,099,999 instead of 1M

* add penultimate cycle date ranges and linebreaks

---------

Co-authored-by: RobertJoonas <56999674+RobertJoonas@users.noreply.github.com>
Co-authored-by: Robert Joonas <robertjoonas16@gmail.com>
2023-12-06 12:02:22 +00:00

142 lines
4.1 KiB
Elixir

defmodule Plausible.Workers.CheckUsage do
use Plausible.Repo
use Oban.Worker, queue: :check_usage
require Plausible.Billing.Subscription.Status
alias Plausible.Billing.{Subscription, Quota}
alias Plausible.Auth.User
defmacro yesterday() do
quote do
fragment("now() - INTERVAL '1 day'")
end
end
defmacro last_day_of_month(day) do
quote do
fragment(
"(date_trunc('month', ?::date) + interval '1 month' - interval '1 day')::date",
unquote(day)
)
end
end
defmacro day_of_month(date) do
quote do
fragment("EXTRACT(day from ?::date)", unquote(date))
end
end
defmacro least(left, right) do
quote do
fragment("least(?, ?)", unquote(left), unquote(right))
end
end
@impl Oban.Worker
def perform(_job, quota_mod \\ Quota, today \\ Timex.today()) do
yesterday = today |> Timex.shift(days: -1)
active_subscribers =
Repo.all(
from(u in User,
join: s in Plausible.Billing.Subscription,
on: s.user_id == u.id,
left_join: ep in Plausible.Billing.EnterprisePlan,
on: ep.user_id == u.id,
where: is_nil(u.grace_period),
where: s.status == ^Subscription.Status.active(),
where: not is_nil(s.last_bill_date),
# Accounts for situations like last_bill_date==2021-01-31 AND today==2021-03-01. Since February never reaches the 31st day, the account is checked on 2021-03-01.
where:
least(day_of_month(s.last_bill_date), day_of_month(last_day_of_month(^yesterday))) ==
day_of_month(^yesterday),
preload: [subscription: s, enterprise_plan: ep]
)
)
for subscriber <- active_subscribers do
if subscriber.enterprise_plan do
check_enterprise_subscriber(subscriber, quota_mod)
else
check_regular_subscriber(subscriber, quota_mod)
end
end
:ok
end
def check_enterprise_subscriber(subscriber, quota_mod) do
pageview_usage = check_pageview_usage(subscriber, quota_mod)
site_usage = check_site_usage_for_enterprise(subscriber)
case {pageview_usage, site_usage} do
{{:below_limit, _}, {:below_limit, _}} ->
nil
{{_, pageview_usage}, {_, {site_usage, site_allowance}}} ->
PlausibleWeb.Email.enterprise_over_limit_internal_email(
subscriber,
pageview_usage,
site_usage,
site_allowance
)
|> Plausible.Mailer.send()
subscriber
|> Plausible.Auth.GracePeriod.start_manual_lock_changeset(pageview_usage.last_cycle.total)
|> Repo.update()
end
end
defp check_regular_subscriber(subscriber, quota_mod) do
case check_pageview_usage(subscriber, quota_mod) do
{:over_limit, pageview_usage} ->
suggested_plan =
Plausible.Billing.Plans.suggest(subscriber, pageview_usage.last_cycle.total)
PlausibleWeb.Email.over_limit_email(subscriber, pageview_usage, suggested_plan)
|> Plausible.Mailer.send()
subscriber
|> Plausible.Auth.GracePeriod.start_changeset(pageview_usage.last_cycle.total)
|> Repo.update()
_ ->
nil
end
end
defp check_pageview_usage(subscriber, quota_mod) do
usage = quota_mod.monthly_pageview_usage(subscriber)
limit = Quota.monthly_pageview_limit(subscriber.subscription)
if exceeds_last_two_usage_cycles?(usage, limit) do
{:over_limit, usage}
else
{:below_limit, usage}
end
end
@spec exceeds_last_two_usage_cycles?(Quota.monthly_pageview_usage(), non_neg_integer()) ::
boolean()
def exceeds_last_two_usage_cycles?(usage, limit) when is_integer(limit) do
limit = ceil(limit * 1.1)
Enum.all?([usage.last_cycle, usage.penultimate_cycle], fn usage ->
not Quota.below_limit?(usage.total, limit)
end)
end
defp check_site_usage_for_enterprise(subscriber) do
limit = subscriber.enterprise_plan.site_limit
usage = Quota.site_usage(subscriber)
if Quota.below_limit?(usage, limit) do
{:below_limit, {usage, limit}}
else
{:over_limit, {usage, limit}}
end
end
end