analytics/lib/workers/check_usage.ex

142 lines
4.0 KiB
Elixir
Raw Normal View History

defmodule Plausible.Workers.CheckUsage do
use Plausible.Repo
use Oban.Worker, queue: :check_usage
Refactor enterprise plan upgrade and change-plan actions (#3397) * rename enterprise?/1 function * change link text to Upgrade when subscription deleted * extract paddle_button and paddle_script components * create a new upgrade-to-enterprise-plan page * extract upgrade_link component * rename function * link to enterprise plan upgrade page from settings ...if the user has an enterprise plan configured * fetch enterprise plan price on the new page * add change_enterprise_plan functionality on the new page * render existing change_enterprise_plan_contact_us.html ...when subscribed to latest configured enterprise plan * rename vars and extract resumable? fn * remove dead billing route * small test refactor: extract convenience fn * add tests for... ...restricting paused and past_due subscription access to the new enterprise plan page. 1. redirect to /settings from the controller action 2. hiding the change-plan link from the user settings * implement redirect to /settings * hide the enterprise upgrade/change-plan link * add tests for a deleted enterprise subscription * plug in the new controller action and delete dead code * optimize for dark mode * fix compile warning * credo fix * display N/A instead of crash when price nil * change subscription.status type to Ecto.Enum Also, create a new `Subscription.Status` module that exposes macros to return the used atom values (prevent typos at compiletime). * fix bug (@conn not available anymore) * use Routes.billing_path where applicable * add a status() type * silence credo * refactor suggestion from review Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Remove the __using__ macro from Subscription.Status ... instead be explicit about requires and aliases and also order the use, import, require, and alias clauses according to https://github.com/christopheradams/elixir_style_guide#module-attribute-ordering * drop the virtual Enteprise 'price_per_interval' field * apply review suggestion to make the code more DRY * use dot syntax to fetch current user in new controller actions * fix formatting --------- Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>
2023-10-10 20:35:17 +03:00
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
2021-03-29 14:53:34 +03:00
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,
2021-10-20 17:49:11 +03:00
left_join: ep in Plausible.Billing.EnterprisePlan,
on: ep.user_id == u.id,
where: is_nil(u.grace_period),
Refactor enterprise plan upgrade and change-plan actions (#3397) * rename enterprise?/1 function * change link text to Upgrade when subscription deleted * extract paddle_button and paddle_script components * create a new upgrade-to-enterprise-plan page * extract upgrade_link component * rename function * link to enterprise plan upgrade page from settings ...if the user has an enterprise plan configured * fetch enterprise plan price on the new page * add change_enterprise_plan functionality on the new page * render existing change_enterprise_plan_contact_us.html ...when subscribed to latest configured enterprise plan * rename vars and extract resumable? fn * remove dead billing route * small test refactor: extract convenience fn * add tests for... ...restricting paused and past_due subscription access to the new enterprise plan page. 1. redirect to /settings from the controller action 2. hiding the change-plan link from the user settings * implement redirect to /settings * hide the enterprise upgrade/change-plan link * add tests for a deleted enterprise subscription * plug in the new controller action and delete dead code * optimize for dark mode * fix compile warning * credo fix * display N/A instead of crash when price nil * change subscription.status type to Ecto.Enum Also, create a new `Subscription.Status` module that exposes macros to return the used atom values (prevent typos at compiletime). * fix bug (@conn not available anymore) * use Routes.billing_path where applicable * add a status() type * silence credo * refactor suggestion from review Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Remove the __using__ macro from Subscription.Status ... instead be explicit about requires and aliases and also order the use, import, require, and alias clauses according to https://github.com/christopheradams/elixir_style_guide#module-attribute-ordering * drop the virtual Enteprise 'price_per_interval' field * apply review suggestion to make the code more DRY * use dot syntax to fetch current user in new controller actions * fix formatting --------- Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>
2023-10-10 20:35:17 +03:00
where: s.status == ^Subscription.Status.active(),
2021-03-01 17:51:57 +03:00
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),
2021-10-20 17:49:11 +03:00
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
2021-05-11 11:30:47 +03:00
def check_enterprise_subscriber(subscriber, quota_mod) do
pageview_usage = check_pageview_usage(subscriber, quota_mod)
site_usage = check_site_usage_for_enterprise(subscriber)
2021-05-10 12:52:42 +03:00
case {pageview_usage, site_usage} do
{{:below_limit, _}, {:below_limit, _}} ->
nil
2021-10-20 17:49:11 +03:00
{{_, 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()
|> Repo.update()
end
end
2021-10-20 17:49:11 +03:00
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()
|> 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)
2021-11-29 13:04:02 +03:00
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 + Quota.pageview_allowance_margin()))
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