analytics/lib/workers/check_usage.ex
Adrian Gruntkowski 17b12ddaeb
Implement basics of Teams (#4658)
* Extend schemas with new fields and relationships for teams

* Implement listing sites and sites with invitations with teams

* Implement creating invitations with teams

* Implement accepting invites with teams

* Add `Teams.SiteTransfer` schema

* Implement creating ownership transfers

* Implement accepting site transfer between teams

* Make results shapes from `Teams.Memberships` role functions more consistent

* Remove :team relation from ApiKey schema

* Pass and provision team on subscription creation

* Pass and provision team on enterprise plan creation

* Implement creating site for a team

* Keep team in sync during legacy ownership transfer and invitations

* Resolve conflict in `Teams.get_or_create` without transaction

* Abstract `GracePeriod` manipulation behind `Plausible.Users`

* Put `User.start_trial` behind `Plausible.Users` API

* Sync team fields on user update, if team exists

* Sync cleaning invitations, updating and removing members

* Transfer invitations too

* Implement backfill script

* Allow separate pg repo for backfill script

* Rollback purposefully at the end

* Update backfill script with parallel processing

* Use `IS DISTINCT FROM` when comparing nullable fields

* Handle no teams to backfill case gracefully when reporting

* Parallelize guest memberships backfill

* Remove transaction wrapping and query timeouts

* Make team sync check more granular and fix formatting

* Wrap single team backfill in a transatction for consistent restarts

* Make invitation and site transfer backfills preserve invitation ID

* Update migration repo config for easier dev access

* Backfill teams for users with subscriptions without sites

* Log timestamps

* Put teams sync behind a compile-time flag

* Keep timestamps in sync and fix subscriptions backfill

* Fix formatting

* Make credo happy

* Don't `use Plausible.Migration` to avoid dialyzer complaining

None of the tooling from there is used anywhere and `@repo` can
be defined directly in the migration script.

* Drop SSL workarounds in the backfill script

---------

Co-authored-by: Adam Rutkowski <hq@mtod.org>
2024-10-21 07:35:23 +00:00

173 lines
4.8 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, usage_mod \\ Quota.Usage, today \\ Date.utc_today()) do
yesterday = today |> Date.shift(day: -1)
last_subscription_query =
from(s in Subscription,
order_by: [desc: s.inserted_at],
where: s.user_id == parent_as(:user).id,
limit: 1
)
active_subscribers =
Repo.all(
from(u in User,
as: :user,
inner_join: s in Plausible.Billing.Subscription,
on: s.user_id == u.id,
inner_lateral_join: ls in subquery(last_subscription_query),
on: ls.id == s.id,
left_join: ep in Plausible.Billing.EnterprisePlan,
on: ep.user_id == u.id,
where:
s.status in [
^Subscription.Status.active(),
^Subscription.Status.past_due(),
^Subscription.Status.deleted()
],
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: s.next_bill_date >= ^today,
where:
least(day_of_month(s.last_bill_date), day_of_month(last_day_of_month(^yesterday))) ==
day_of_month(^yesterday),
order_by: u.id,
preload: [subscription: s, enterprise_plan: ep]
)
)
for subscriber <- active_subscribers do
case {subscriber.grace_period, subscriber.enterprise_plan} do
{nil, nil} ->
check_regular_subscriber(subscriber, usage_mod)
{nil, _} ->
check_enterprise_subscriber(subscriber, usage_mod)
{_, nil} ->
maybe_remove_grace_period(subscriber, usage_mod)
_ ->
:skip
end
end
:ok
end
defp check_site_usage_for_enterprise(subscriber) do
limit = subscriber.enterprise_plan.site_limit
usage = Quota.Usage.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, usage_mod) do
case check_pageview_usage_last_cycle(subscriber, usage_mod) do
{:below_limit, _} ->
Plausible.Users.remove_grace_period(subscriber)
:ok
_ ->
:skip
end
end
defp check_regular_subscriber(subscriber, usage_mod) do
case check_pageview_usage_two_cycles(subscriber, usage_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()
Plausible.Users.start_grace_period(subscriber)
_ ->
nil
end
end
def check_enterprise_subscriber(subscriber, usage_mod) do
pageview_usage = check_pageview_usage_two_cycles(subscriber, usage_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()
Plausible.Users.start_manual_lock_grace_period(subscriber)
end
end
defp check_pageview_usage_two_cycles(subscriber, usage_mod) do
usage = usage_mod.monthly_pageview_usage(subscriber)
limit = Quota.Limits.monthly_pageview_limit(subscriber)
if Quota.exceeds_last_two_usage_cycles?(usage, limit) do
{:over_limit, usage}
else
{:below_limit, usage}
end
end
defp check_pageview_usage_last_cycle(subscriber, usage_mod) do
usage = usage_mod.monthly_pageview_usage(subscriber)
limit = Quota.Limits.monthly_pageview_limit(subscriber)
if :last_cycle in Quota.exceeded_cycles(usage, limit) do
{:over_limit, usage}
else
{:below_limit, usage}
end
end
end