From 7674c94ace3f0581ae4e825ac2147a5386e0ed1f Mon Sep 17 00:00:00 2001 From: RobertJoonas <56999674+RobertJoonas@users.noreply.github.com> Date: Wed, 18 Oct 2023 11:29:13 +0300 Subject: [PATCH] Refactor: Add 'goals' feature to JSON plan files (#3435) * fix text color in dark mode * rename a function * use aliases in quota.ex * rename a function * make Goals a similar feature to others but with a free option * rename a function * mix format --- lib/plausible/billing/feature.ex | 47 +++++++++------ lib/plausible/billing/plans.ex | 24 ++++---- lib/plausible/billing/quota.ex | 47 ++++++++------- lib/plausible_web/components/billing.ex | 2 +- lib/plausible_web/live/choose_plan.ex | 5 +- lib/plausible_web/live/goal_settings/form.ex | 2 +- .../templates/auth/user_settings.html.heex | 2 +- .../templates/site/settings_funnels.html.heex | 2 +- .../templates/site/settings_props.html.heex | 2 +- priv/plans_v1.json | 20 +++---- priv/plans_v2.json | 20 +++---- priv/plans_v3.json | 16 ++--- priv/plans_v4.json | 32 +++++----- priv/sandbox_plans.json | 32 +++++----- priv/unlisted_plans_v1.json | 2 +- priv/unlisted_plans_v2.json | 2 +- test/plausible/billing/quota_test.exs | 60 ++++++++++--------- .../plausible_web/components/billing_test.exs | 16 ++--- 18 files changed, 178 insertions(+), 155 deletions(-) diff --git a/lib/plausible/billing/feature.ex b/lib/plausible/billing/feature.ex index 3fe5677ed..3af3a001b 100644 --- a/lib/plausible/billing/feature.ex +++ b/lib/plausible/billing/feature.ex @@ -9,18 +9,25 @@ defmodule Plausible.Billing.Feature do When defining new features, the following options are expected by the `__using__` macro: + * `:name` - an atom representing the feature name in the plan JSON + file (see also Plausible.Billing.Plan). + * `:display_name` - human-readable display name of the feature * `:toggle_field` - the field in the %Plausible.Site{} schema that toggles the feature. If `nil` or not set, toggle/2 silently returns `:ok` - * `:extra_feature` - an atom representing the feature name in the plan JSON - file (see also Plausible.Billing.Plan). If `nil` or not set, - `check_availability/1` silently returns `:ok` + * `:free` - if set to `true`, makes the `check_availability/1` function + always return `:ok` (no matter the user's subscription status) Functions defined by `__using__` can be overridden if needed. """ + @doc """ + Returns the atom representing the feature name in the plan JSON file. + """ + @callback name() :: atom() + @doc """ Returns the human-readable display name of the feature. """ @@ -32,7 +39,7 @@ defmodule Plausible.Billing.Feature do @callback toggle_field() :: atom() @doc """ - Toggles the feature on and off for a site. Returns + Toggles the feature on and off for a site. Returns `{:error, :upgrade_required}` when toggling a feature the site owner does not have access to. """ @@ -51,9 +58,10 @@ defmodule Plausible.Billing.Feature do :ok | {:error, :upgrade_required} | {:error, :not_implemented} @features [ - Plausible.Billing.Feature.Funnels, Plausible.Billing.Feature.Goals, + Plausible.Billing.Feature.StatsAPI, Plausible.Billing.Feature.Props, + Plausible.Billing.Feature.Funnels, Plausible.Billing.Feature.RevenueGoals ] @@ -70,6 +78,9 @@ defmodule Plausible.Billing.Feature do @behaviour Plausible.Billing.Feature alias Plausible.Billing.Quota + @impl true + def name, do: Keyword.get(unquote(opts), :name) + @impl true def display_name, do: Keyword.get(unquote(opts), :display_name) @@ -89,12 +100,10 @@ defmodule Plausible.Billing.Feature do @impl true def check_availability(%Plausible.Auth.User{} = user) do - extra_feature = Keyword.get(unquote(opts), :extra_feature) - cond do - is_nil(extra_feature) -> :ok not FunWithFlags.enabled?(:business_tier, for: user) -> :ok - extra_feature in Quota.extra_features_limit(user) -> :ok + Keyword.get(unquote(opts), :free) -> :ok + __MODULE__ in Quota.allowed_features_for(user) -> :ok true -> {:error, :upgrade_required} end end @@ -122,36 +131,38 @@ end defmodule Plausible.Billing.Feature.Funnels do @moduledoc false use Plausible.Billing.Feature, + name: :funnels, display_name: "Funnels", - toggle_field: :funnels_enabled, - extra_feature: :funnels + toggle_field: :funnels_enabled end defmodule Plausible.Billing.Feature.RevenueGoals do @moduledoc false use Plausible.Billing.Feature, - display_name: "Revenue Goals", - extra_feature: :revenue_goals + name: :revenue_goals, + display_name: "Revenue Goals" end defmodule Plausible.Billing.Feature.Goals do @moduledoc false use Plausible.Billing.Feature, + name: :goals, display_name: "Goals", - toggle_field: :conversions_enabled + toggle_field: :conversions_enabled, + free: true end defmodule Plausible.Billing.Feature.Props do @moduledoc false use Plausible.Billing.Feature, + name: :props, display_name: "Custom Properties", - toggle_field: :props_enabled, - extra_feature: :props + toggle_field: :props_enabled end defmodule Plausible.Billing.Feature.StatsAPI do @moduledoc false use Plausible.Billing.Feature, - display_name: "Stats API", - extra_feature: :stats_api + name: :stats_api, + display_name: "Stats API" end diff --git a/lib/plausible/billing/plans.ex b/lib/plausible/billing/plans.ex index 0b9cfe592..2566fe772 100644 --- a/lib/plausible/billing/plans.ex +++ b/lib/plausible/billing/plans.ex @@ -2,7 +2,7 @@ defmodule Plausible.Billing.Plan do @moduledoc false @derive Jason.Encoder - @enforce_keys ~w(kind site_limit monthly_pageview_limit team_member_limit extra_features volume monthly_product_id yearly_product_id)a + @enforce_keys ~w(kind site_limit monthly_pageview_limit team_member_limit features volume monthly_product_id yearly_product_id)a defstruct @enforce_keys ++ [:monthly_cost, :yearly_cost] @type t() :: @@ -16,7 +16,7 @@ defmodule Plausible.Billing.Plan do yearly_cost: Money.t() | nil, monthly_product_id: String.t() | nil, yearly_product_id: String.t() | nil, - extra_features: [atom()] + features: [atom()] } | :enterprise @@ -31,8 +31,6 @@ defmodule Plausible.Billing.Plans do alias Plausible.Billing.{Subscription, Plan, EnterprisePlan} alias Plausible.Auth.User - @available_features ["props", "revenue_goals", "funnels", "stats_api"] - for f <- [ :plans_v1, :plans_v2, @@ -56,20 +54,26 @@ defmodule Plausible.Billing.Plans do _any -> raise ArgumentError, "Failed to parse team member limit from plan JSON files" end - extra_features = - Enum.map(raw.extra_features, fn feature -> - if feature in @available_features, - do: String.to_atom(feature), - else: raise(ArgumentError, "Failed to parse extra features from plan JSON files") + features = + Plausible.Billing.Feature.list() + |> Enum.filter(fn module -> + to_string(module.name()) in raw.features end) + if length(features) != length(raw.features), + do: + raise( + ArgumentError, + "Unrecognized feature(s) in #{inspect(raw.features)} (#{f}.json)" + ) + volume = PlausibleWeb.StatsView.large_number_format(raw.monthly_pageview_limit) raw |> Map.put(:volume, volume) |> Map.put(:kind, String.to_atom(raw.kind)) |> Map.put(:team_member_limit, team_member_limit) - |> Map.put(:extra_features, extra_features) + |> Map.put(:features, features) |> Plan.new() end) diff --git a/lib/plausible/billing/quota.ex b/lib/plausible/billing/quota.ex index 491d550bb..89595c596 100644 --- a/lib/plausible/billing/quota.ex +++ b/lib/plausible/billing/quota.ex @@ -4,7 +4,9 @@ defmodule Plausible.Billing.Quota do """ import Ecto.Query - alias Plausible.Billing.Plans + alias Plausible.Billing + alias Plausible.Billing.{Plan, Plans, Subscription, EnterprisePlan, Feature} + alias Plausible.Billing.Feature.{Goals, RevenueGoals, Funnels, Props} @limit_sites_since ~D[2021-05-05] @spec site_limit(Plausible.Auth.User.t()) :: non_neg_integer() | :unlimited @@ -28,8 +30,8 @@ defmodule Plausible.Billing.Quota do user = Plausible.Users.with_subscription(user) case Plans.get_subscription_plan(user.subscription) do - %Plausible.Billing.EnterprisePlan{} -> :unlimited - %Plausible.Billing.Plan{site_limit: site_limit} -> site_limit + %EnterprisePlan{} -> :unlimited + %Plan{site_limit: site_limit} -> site_limit :free_10k -> @site_limit_for_free_10k nil -> @site_limit_for_trials end @@ -46,17 +48,17 @@ defmodule Plausible.Billing.Quota do @monthly_pageview_limit_for_free_10k 10_000 @monthly_pageview_limit_for_trials :unlimited - @spec monthly_pageview_limit(Plausible.Billing.Subscription.t()) :: + @spec monthly_pageview_limit(Subscription.t()) :: non_neg_integer() | :unlimited @doc """ Returns the limit of pageviews for a subscription. """ def monthly_pageview_limit(subscription) do case Plans.get_subscription_plan(subscription) do - %Plausible.Billing.EnterprisePlan{monthly_pageview_limit: limit} -> + %EnterprisePlan{monthly_pageview_limit: limit} -> limit - %Plausible.Billing.Plan{monthly_pageview_limit: limit} -> + %Plan{monthly_pageview_limit: limit} -> limit :free_10k -> @@ -80,7 +82,7 @@ defmodule Plausible.Billing.Quota do """ def monthly_pageview_usage(user) do user - |> Plausible.Billing.usage_breakdown() + |> Billing.usage_breakdown() |> Tuple.sum() end @@ -93,8 +95,8 @@ defmodule Plausible.Billing.Quota do user = Plausible.Users.with_subscription(user) case Plans.get_subscription_plan(user.subscription) do - %Plausible.Billing.EnterprisePlan{} -> :unlimited - %Plausible.Billing.Plan{team_member_limit: limit} -> limit + %EnterprisePlan{} -> :unlimited + %Plan{team_member_limit: limit} -> limit :free_10k -> :unlimited nil -> @team_member_limit_for_trials end @@ -129,11 +131,13 @@ defmodule Plausible.Billing.Quota do Plausible.Repo.one(query) end - @spec extra_features_usage(Plausible.Auth.User.t()) :: [atom()] + @spec features_usage(Plausible.Auth.User.t()) :: [atom()] @doc """ - Returns a list of extra features the given user's sites uses. + Returns a list of features the given user is using. At the + current stage, the only features that we need to know the + usage for are `Props`, `Funnels`, and `RevenueGoals` """ - def extra_features_usage(user) do + def features_usage(user) do props_usage_query = from s in Plausible.Site, inner_join: os in subquery(owned_sites_query(user)), @@ -152,9 +156,9 @@ defmodule Plausible.Billing.Quota do where: not is_nil(g.currency) queries = [ - props: props_usage_query, - funnels: funnels_usage_query, - revenue_goals: revenue_goals_usage + {Props, props_usage_query}, + {Funnels, funnels_usage_query}, + {RevenueGoals, revenue_goals_usage} ] Enum.reduce(queries, [], fn {feature, query}, acc -> @@ -162,19 +166,18 @@ defmodule Plausible.Billing.Quota do end) end - @all_features [:props, :revenue_goals, :funnels, :stats_api] @doc """ - Returns a list of extra features the user can use. Trial users have the + Returns a list of features the user can use. Trial users have the ability to use all features during their trial. """ - def extra_features_limit(user) do + def allowed_features_for(user) do user = Plausible.Users.with_subscription(user) case Plans.get_subscription_plan(user.subscription) do - %Plausible.Billing.EnterprisePlan{} -> @all_features - %Plausible.Billing.Plan{extra_features: extra_features} -> extra_features - :free_10k -> [] - nil -> @all_features + %EnterprisePlan{} -> Feature.list() + %Plan{features: features} -> features + :free_10k -> [Goals] + nil -> Feature.list() end end diff --git a/lib/plausible_web/components/billing.ex b/lib/plausible_web/components/billing.ex index 9f039bb0b..1125d56b7 100644 --- a/lib/plausible_web/components/billing.ex +++ b/lib/plausible_web/components/billing.ex @@ -15,7 +15,7 @@ defmodule PlausibleWeb.Components.Billing do attr(:rest, :global) # credo:disable-for-next-line Credo.Check.Refactor.CyclomaticComplexity - def extra_feature_notice(assigns) do + def premium_feature_notice(assigns) do private_preview? = not FunWithFlags.enabled?(:business_tier, for: assigns.current_user) display_upgrade_link? = assigns.current_user.id == assigns.billable_user.id has_access? = assigns.feature_mod.check_availability(assigns.billable_user) == :ok diff --git a/lib/plausible_web/live/choose_plan.ex b/lib/plausible_web/live/choose_plan.ex index e1a2dd7b3..b24887e31 100644 --- a/lib/plausible_web/live/choose_plan.ex +++ b/lib/plausible_web/live/choose_plan.ex @@ -381,7 +381,10 @@ defmodule PlausibleWeb.Live.ChoosePlan do

<.contact_button class="" /> -