From 059df171bc8f898c9ea61a537c2987eaced8aefb Mon Sep 17 00:00:00 2001 From: Vini Brasil Date: Wed, 9 Aug 2023 14:33:59 -0300 Subject: [PATCH] Refactor Plausible.Billing.Plans module (#3249) This commit introduces a series of improvements on the Plans module including function renaming, documentation and readability. This is the groundwork for billing plans. There should be no actual changes with this commit, therefore no changes in tests either. --- lib/mix/tasks/analyze_plans.ex | 10 +- lib/plausible/billing/plans.ex | 207 +++++++++--------- lib/plausible/billing/site_locker.ex | 2 +- lib/plausible_web/email.ex | 2 +- .../templates/billing/_plan_option.html.eex | 10 +- .../templates/billing/change_plan.html.eex | 4 +- .../templates/billing/upgrade.html.eex | 4 +- .../templates/email/dashboard_locked.html.eex | 2 +- .../templates/email/over_limit.html.eex | 2 +- .../email/trial_upgrade_email.html.eex | 2 +- lib/workers/check_usage.ex | 2 +- lib/workers/notify_annual_renewal.ex | 2 +- priv/sandbox_plans.json | 16 ++ priv/unlisted_plans_v1.json | 9 + priv/unlisted_plans_v2.json | 9 + test/plausible/billing/plans_test.exs | 58 ++++- 16 files changed, 206 insertions(+), 135 deletions(-) create mode 100644 priv/sandbox_plans.json create mode 100644 priv/unlisted_plans_v1.json create mode 100644 priv/unlisted_plans_v2.json diff --git a/lib/mix/tasks/analyze_plans.ex b/lib/mix/tasks/analyze_plans.ex index 748709607..331a048c6 100644 --- a/lib/mix/tasks/analyze_plans.ex +++ b/lib/mix/tasks/analyze_plans.ex @@ -17,19 +17,19 @@ defmodule Mix.Tasks.AnalyzePlans do res = Enum.map(res, fn {plan_id, count} -> - plan = Plausible.Billing.Plans.for_product_id(plan_id) + plan = Plausible.Billing.Plans.find(plan_id) if plan do - is_monthly = plan_id == plan[:monthly_product_id] + is_monthly = plan_id == plan.monthly_product_id monthly_revenue = if is_monthly do - price(plan[:monthly_cost]) + price(plan.monthly_cost) else - price(plan[:yearly_cost]) / 12 + price(plan.yearly_cost) / 12 end - {PlausibleWeb.StatsView.large_number_format(plan[:limit]), monthly_revenue, count} + {PlausibleWeb.StatsView.large_number_format(plan.limit), monthly_revenue, count} end end) |> Enum.filter(& &1) diff --git a/lib/plausible/billing/plans.ex b/lib/plausible/billing/plans.ex index 7cc18c4ac..100f9702e 100644 --- a/lib/plausible/billing/plans.ex +++ b/lib/plausible/billing/plans.ex @@ -1,80 +1,98 @@ -defmodule Plausible.Billing.Plans do - use Plausible.Repo +defmodule Plausible.Billing.Plan do + @moduledoc false - @unlisted_plans_v1 [ - %{limit: 150_000_000, yearly_product_id: "648089", yearly_cost: "$4800"} - ] - - @unlisted_plans_v2 [ - %{limit: 10_000_000, monthly_product_id: "655350", monthly_cost: "$250"} - ] - - @sandbox_plans [ - %{ - limit: 10_000, - monthly_product_id: "19878", - yearly_product_id: "20127", - monthly_cost: "$6", - yearly_cost: "$60" - }, - %{ - limit: 100_000, - monthly_product_id: "20657", - yearly_product_id: "20658", - monthly_cost: "$12.34", - yearly_cost: "$120.34" - } - ] - - @type plan() :: - %{ + @derive Jason.Encoder + @enforce_keys ~w(limit volume monthly_cost yearly_cost monthly_product_id yearly_product_id)a + defstruct @enforce_keys + + @type t() :: + %__MODULE__{ limit: non_neg_integer(), - monthly_product_id: String.t(), - yearly_product_id: String.t(), - monthly_cost: String.t(), - yearly_cost: String.t() + volume: String.t(), + monthly_cost: String.t() | nil, + yearly_cost: String.t() | nil, + monthly_product_id: String.t() | nil, + yearly_product_id: String.t() | nil } | :enterprise +end + +defmodule Plausible.Billing.Plans do + use Plausible.Repo + + for f <- [ + :plans_v1, + :plans_v2, + :plans_v3, + :unlisted_plans_v1, + :unlisted_plans_v2, + :sandbox_plans + ] do + path = Application.app_dir(:plausible, ["priv", "#{f}.json"]) + + contents = + path + |> File.read!() + |> Jason.decode!(keys: :atoms!) + |> Enum.map(&Map.put(&1, :volume, PlausibleWeb.StatsView.large_number_format(&1.limit))) + |> Enum.map(&struct!(Plausible.Billing.Plan, &1)) + + Module.put_attribute(__MODULE__, f, contents) + Module.put_attribute(__MODULE__, :external_resource, path) + end + + @spec for_user(Plausible.Auth.User.t()) :: [Plausible.Billing.Plan.t()] + @doc """ + Returns a list of plans available for the user to choose. - def plans_for(user) do + As new versions of plans are introduced, users who were on old plans can + still choose from old plans. + """ + def for_user(user) do user = Plausible.Users.with_subscription(user) - sandbox_plans = plans_sandbox() - v1_plans = plans_v1() - v2_plans = plans_v2() - v3_plans = plans_v3() - - raw_plans = - cond do - contains?(v1_plans, user.subscription) -> - v1_plans - - contains?(v2_plans, user.subscription) -> - v2_plans - - contains?(v3_plans, user.subscription) -> - v3_plans - - contains?(sandbox_plans, user.subscription) -> - sandbox_plans - - true -> - cond do - Application.get_env(:plausible, :environment) == "dev" -> sandbox_plans - Timex.before?(user.inserted_at, ~D[2022-01-01]) -> v2_plans - true -> v3_plans - end - end - Enum.map(raw_plans, fn plan -> Map.put(plan, :volume, number_format(plan[:limit])) end) + cond do + find(user.subscription, @plans_v1) -> @plans_v1 + find(user.subscription, @plans_v2) -> @plans_v2 + find(user.subscription, @plans_v3) -> @plans_v3 + find(user.subscription, plans_sandbox()) -> plans_sandbox() + Application.get_env(:plausible, :environment) == "dev" -> plans_sandbox() + Timex.before?(user.inserted_at, ~D[2022-01-01]) -> @plans_v2 + true -> @plans_v3 + end end - def all_yearly_plan_ids do - Enum.map(all_plans(), fn plan -> plan[:yearly_product_id] end) + @spec yearly_product_ids() :: [String.t()] + @doc """ + List yearly plans product IDs. + """ + def yearly_product_ids do + for %{yearly_product_id: yearly_product_id} <- all(), + is_binary(yearly_product_id), + do: yearly_product_id end - def for_product_id(product_id) do - Enum.find(all_plans(), fn plan -> - product_id in [plan[:monthly_product_id], plan[:yearly_product_id]] + @spec find(String.t() | Plausible.Billing.Subscription.t(), [Plausible.Billing.Plan.t()]) :: + Plausible.Billing.Plan.t() | nil + @spec find(nil, any()) :: nil + @doc """ + Finds a plan by product ID. + + Returns nil when plan can't be found. + """ + def find(product_id_or_subscription, scope \\ all()) + + def find(nil, _scope) do + nil + end + + def find(%Plausible.Billing.Subscription{} = subscription, scope) do + find(subscription.paddle_plan_id, scope) + end + + def find(product_id, scope) do + Enum.find(scope, fn plan -> + product_id in [plan.monthly_product_id, plan.yearly_product_id] end) end @@ -82,14 +100,14 @@ defmodule Plausible.Billing.Plans do do: "N/A" def subscription_interval(subscription) do - case for_product_id(subscription.paddle_plan_id) do + case find(subscription.paddle_plan_id) do nil -> enterprise_plan = get_enterprise_plan(subscription) enterprise_plan && enterprise_plan.billing_interval plan -> - if subscription.paddle_plan_id == plan[:monthly_product_id] do + if subscription.paddle_plan_id == plan.monthly_product_id do "monthly" else "yearly" @@ -100,7 +118,7 @@ defmodule Plausible.Billing.Plans do def allowance(%Plausible.Billing.Subscription{paddle_plan_id: "free_10k"}), do: 10_000 def allowance(subscription) do - found = for_product_id(subscription.paddle_plan_id) + found = find(subscription.paddle_plan_id) if found do Map.fetch!(found, :limit) @@ -127,46 +145,29 @@ defmodule Plausible.Billing.Plans do end @enterprise_level_usage 10_000_000 - @spec suggested_plan(Plausible.Auth.User.t(), non_neg_integer()) :: plan() - def suggested_plan(user, usage_during_cycle) do + @spec suggest(Plausible.Auth.User.t(), non_neg_integer()) :: Plausible.Billing.Plan.t() + @doc """ + Returns the most appropriate plan for a user based on their usage during a + given cycle. + + If the usage during the cycle exceeds the enterprise-level threshold, or if + the user already belongs to an enterprise plan, it suggests the :enterprise + plan. + + Otherwise, it recommends the plan where the cycle usage falls just under the + plan's limit from the available options for the user. + """ + def suggest(user, usage_during_cycle) do cond do usage_during_cycle > @enterprise_level_usage -> :enterprise Plausible.Auth.enterprise?(user) -> :enterprise - true -> Enum.find(plans_for(user), fn plan -> usage_during_cycle < plan[:limit] end) + true -> Enum.find(for_user(user), &(usage_during_cycle < &1.limit)) end end - defp contains?(_plans, nil), do: false - - defp contains?(plans, subscription) do - Enum.any?(plans, fn plan -> - plan[:monthly_product_id] == subscription.paddle_plan_id || - plan[:yearly_product_id] == subscription.paddle_plan_id - end) - end - - defp number_format(num) do - PlausibleWeb.StatsView.large_number_format(num) - end - - defp all_plans() do - plans_v1() ++ - @unlisted_plans_v1 ++ plans_v2() ++ @unlisted_plans_v2 ++ plans_v3() ++ plans_sandbox() - end - - defp plans_v1() do - File.read!(Application.app_dir(:plausible) <> "/priv/plans_v1.json") - |> Jason.decode!(keys: :atoms) - end - - defp plans_v2() do - File.read!(Application.app_dir(:plausible) <> "/priv/plans_v2.json") - |> Jason.decode!(keys: :atoms) - end - - defp plans_v3() do - File.read!(Application.app_dir(:plausible) <> "/priv/plans_v3.json") - |> Jason.decode!(keys: :atoms) + defp all() do + @plans_v1 ++ + @unlisted_plans_v1 ++ @plans_v2 ++ @unlisted_plans_v2 ++ @plans_v3 ++ plans_sandbox() end defp plans_sandbox() do diff --git a/lib/plausible/billing/site_locker.ex b/lib/plausible/billing/site_locker.ex index def40b497..1cec2e836 100644 --- a/lib/plausible/billing/site_locker.ex +++ b/lib/plausible/billing/site_locker.ex @@ -42,7 +42,7 @@ defmodule Plausible.Billing.SiteLocker do defp send_grace_period_end_email(user) do {_, last_cycle} = Plausible.Billing.last_two_billing_cycles(user) {_, last_cycle_usage} = Plausible.Billing.last_two_billing_months_usage(user) - suggested_plan = Plausible.Billing.Plans.suggested_plan(user, last_cycle_usage) + suggested_plan = Plausible.Billing.Plans.suggest(user, last_cycle_usage) template = PlausibleWeb.Email.dashboard_locked( diff --git a/lib/plausible_web/email.ex b/lib/plausible_web/email.ex index fe5c5504e..0737cfea5 100644 --- a/lib/plausible_web/email.ex +++ b/lib/plausible_web/email.ex @@ -79,7 +79,7 @@ defmodule PlausibleWeb.Email do end def trial_upgrade_email(user, day, {pageviews, custom_events}) do - suggested_plan = Plausible.Billing.Plans.suggested_plan(user, pageviews + custom_events) + suggested_plan = Plausible.Billing.Plans.suggest(user, pageviews + custom_events) base_email() |> to(user) diff --git a/lib/plausible_web/templates/billing/_plan_option.html.eex b/lib/plausible_web/templates/billing/_plan_option.html.eex index 1c7cdf91c..942e165ae 100644 --- a/lib/plausible_web/templates/billing/_plan_option.html.eex +++ b/lib/plausible_web/templates/billing/_plan_option.html.eex @@ -1,7 +1,7 @@ - - <%= @plan[:volume] %> - - / mo - / yr + + <%= @plan.volume %> + + / mo + / yr diff --git a/lib/plausible_web/templates/billing/change_plan.html.eex b/lib/plausible_web/templates/billing/change_plan.html.eex index 044a02864..c5851b110 100644 --- a/lib/plausible_web/templates/billing/change_plan.html.eex +++ b/lib/plausible_web/templates/billing/change_plan.html.eex @@ -2,7 +2,7 @@