mirror of
https://github.com/plausible/analytics.git
synced 2024-08-17 05:40:24 +03:00
Reset grace period if usage drops within the last cycle (#4212)
So that `LockSites` job unlocks the site, encountering no grace period and no upgrade needed.
This commit is contained in:
parent
062519a9fa
commit
4603aa7a51
@ -43,7 +43,6 @@ defmodule Plausible.Workers.CheckUsage do
|
||||
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.
|
||||
@ -55,18 +54,87 @@ defmodule Plausible.Workers.CheckUsage do
|
||||
)
|
||||
|
||||
for subscriber <- active_subscribers do
|
||||
if subscriber.enterprise_plan do
|
||||
check_enterprise_subscriber(subscriber, quota_mod)
|
||||
else
|
||||
check_regular_subscriber(subscriber, quota_mod)
|
||||
case {subscriber.grace_period, subscriber.enterprise_plan} do
|
||||
{nil, nil} ->
|
||||
check_regular_subscriber(subscriber, quota_mod)
|
||||
|
||||
{nil, _} ->
|
||||
check_enterprise_subscriber(subscriber, quota_mod)
|
||||
|
||||
{_, nil} ->
|
||||
maybe_remove_grace_period(subscriber, quota_mod)
|
||||
|
||||
_ ->
|
||||
:skip
|
||||
end
|
||||
end
|
||||
|
||||
:ok
|
||||
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
|
||||
|
||||
@spec last_usage_cycle_below_limit?(Quota.monthly_pageview_usage(), non_neg_integer()) ::
|
||||
boolean()
|
||||
|
||||
def last_usage_cycle_below_limit?(usage, limit) when is_integer(limit) do
|
||||
limit = ceil(limit * (1 + Quota.pageview_allowance_margin()))
|
||||
|
||||
Quota.below_limit?(usage.last_cycle.total, limit)
|
||||
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
|
||||
|
||||
def maybe_remove_grace_period(subscriber, quota_mod) do
|
||||
case check_pageview_usage_last_cycle(subscriber, quota_mod) do
|
||||
{:below_limit, _} ->
|
||||
subscriber
|
||||
|> Plausible.Auth.GracePeriod.remove_changeset()
|
||||
|> Repo.update()
|
||||
|
||||
_ ->
|
||||
:skip
|
||||
end
|
||||
end
|
||||
|
||||
defp check_regular_subscriber(subscriber, quota_mod) do
|
||||
case check_pageview_usage_two_cycles(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
|
||||
|
||||
def check_enterprise_subscriber(subscriber, quota_mod) do
|
||||
pageview_usage = check_pageview_usage(subscriber, quota_mod)
|
||||
pageview_usage = check_pageview_usage_two_cycles(subscriber, quota_mod)
|
||||
site_usage = check_site_usage_for_enterprise(subscriber)
|
||||
|
||||
case {pageview_usage, site_usage} do
|
||||
@ -88,25 +156,7 @@ defmodule Plausible.Workers.CheckUsage do
|
||||
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()
|
||||
|> Repo.update()
|
||||
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
defp check_pageview_usage(subscriber, quota_mod) do
|
||||
defp check_pageview_usage_two_cycles(subscriber, quota_mod) do
|
||||
usage = quota_mod.monthly_pageview_usage(subscriber)
|
||||
limit = Quota.monthly_pageview_limit(subscriber)
|
||||
|
||||
@ -117,25 +167,14 @@ defmodule Plausible.Workers.CheckUsage do
|
||||
end
|
||||
end
|
||||
|
||||
@spec exceeds_last_two_usage_cycles?(Quota.monthly_pageview_usage(), non_neg_integer()) ::
|
||||
boolean()
|
||||
defp check_pageview_usage_last_cycle(subscriber, quota_mod) do
|
||||
usage = quota_mod.monthly_pageview_usage(subscriber)
|
||||
limit = Quota.monthly_pageview_limit(subscriber)
|
||||
|
||||
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}}
|
||||
if last_usage_cycle_below_limit?(usage, limit) do
|
||||
{:below_limit, usage}
|
||||
else
|
||||
{:over_limit, {usage, limit}}
|
||||
{:over_limit, usage}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -184,7 +184,68 @@ defmodule Plausible.Workers.CheckUsageTest do
|
||||
assert html_body =~ "We recommend you upgrade to the 100k/mo plan"
|
||||
end
|
||||
|
||||
test "clears grace period when plan is applicable again", %{user: user} do
|
||||
quota_stub =
|
||||
Plausible.Billing.Quota
|
||||
|> stub(:monthly_pageview_usage, fn _user ->
|
||||
%{
|
||||
penultimate_cycle: %{date_range: @date_range, total: 11_000},
|
||||
last_cycle: %{date_range: @date_range, total: 11_000}
|
||||
}
|
||||
end)
|
||||
|
||||
insert(:subscription,
|
||||
user: user,
|
||||
paddle_plan_id: @paddle_id_10k,
|
||||
last_bill_date: Timex.shift(Timex.today(), days: -1)
|
||||
)
|
||||
|
||||
CheckUsage.perform(nil, quota_stub)
|
||||
assert user |> Repo.reload() |> Plausible.Auth.GracePeriod.active?()
|
||||
|
||||
quota_stub =
|
||||
Plausible.Billing.Quota
|
||||
|> stub(:monthly_pageview_usage, fn _user ->
|
||||
%{
|
||||
penultimate_cycle: %{date_range: @date_range, total: 11_000},
|
||||
last_cycle: %{date_range: @date_range, total: 9_000}
|
||||
}
|
||||
end)
|
||||
|
||||
CheckUsage.perform(nil, quota_stub)
|
||||
refute user |> Repo.reload() |> Plausible.Auth.GracePeriod.active?()
|
||||
end
|
||||
|
||||
describe "enterprise customers" do
|
||||
test "skips checking enterprise users who already have a grace period", %{user: user} do
|
||||
%{grace_period: existing_grace_period} =
|
||||
user
|
||||
|> Plausible.Auth.GracePeriod.start_manual_lock_changeset()
|
||||
|> Repo.update!()
|
||||
|
||||
quota_stub =
|
||||
Plausible.Billing.Quota
|
||||
|> stub(:monthly_pageview_usage, fn _user ->
|
||||
%{
|
||||
penultimate_cycle: %{date_range: @date_range, total: 1_100_000},
|
||||
last_cycle: %{date_range: @date_range, total: 1_100_000}
|
||||
}
|
||||
end)
|
||||
|
||||
enterprise_plan = insert(:enterprise_plan, user: user, monthly_pageview_limit: 1_000_000)
|
||||
|
||||
insert(:subscription,
|
||||
user: user,
|
||||
paddle_plan_id: enterprise_plan.paddle_plan_id,
|
||||
last_bill_date: Timex.shift(Timex.today(), days: -1)
|
||||
)
|
||||
|
||||
CheckUsage.perform(nil, quota_stub)
|
||||
|
||||
assert_no_emails_delivered()
|
||||
assert Repo.reload(user).grace_period.id == existing_grace_period.id
|
||||
end
|
||||
|
||||
test "checks billable pageview usage for enterprise customer, sends usage information to enterprise@plausible.io",
|
||||
%{
|
||||
user: user
|
||||
|
Loading…
Reference in New Issue
Block a user