View Source Plausible.Billing.Quota (Plausible v0.0.1)

This module provides functions to work with plans usage and limits.

Summary

Functions

Returns a list of features the user can use. Trial users have the ability to use all features during their trial.

Returns whether the usage is below the limit or not. Returns false if usage is equal to the limit.

Enterprise plans are always allowed to add more sites (even when over limit) to avoid service disruption. Their usage is checked in a background job instead (see check_usage.ex).

Ensures that the given user (or the usage map) is within the limits of the given plan.

Given a user, this function returns the features used across all the sites this user owns + StatsAPI if the user has a configured Stats API key.

Queries the ClickHouse database for the monthly pageview usage. If the given user's subscription is active, past_due, or a deleted (but not yet expired), a map with the following structure is returned

Returns the number of sites the given user owns.

Returns the total count of team members associated with the user's sites.

Returns whether the usage is within the limit or not. Returns true if usage is equal to the limit.

Types

@type limit() :: :site_limit | :pageview_limit | :team_member_limit
Link to this type

monthly_pageview_usage()

View Source
@type monthly_pageview_usage() :: %{required(period()) => usage_cycle()}
@type over_limits_error() :: {:over_plan_limits, [limit()]}
@type period() :: :last_30_days | :current_cycle | :last_cycle | :penultimate_cycle
@type usage_cycle() :: %{
  date_range: Date.Range.t(),
  pageviews: non_neg_integer(),
  custom_events: non_neg_integer(),
  total: non_neg_integer()
}

Functions

Link to this function

allowed_features_for(user)

View Source

Returns a list of features the user can use. Trial users have the ability to use all features during their trial.

Link to this function

below_limit?(usage, limit)

View Source
@spec below_limit?(non_neg_integer(), non_neg_integer() | :unlimited) :: boolean()

Returns whether the usage is below the limit or not. Returns false if usage is equal to the limit.

Link to this function

ensure_can_add_new_site(user)

View Source

Enterprise plans are always allowed to add more sites (even when over limit) to avoid service disruption. Their usage is checked in a background job instead (see check_usage.ex).

Link to this function

ensure_within_plan_limits(user_or_usage, plan, opts \\ [])

View Source
@spec ensure_within_plan_limits(
  Plausible.Auth.User.t() | map(),
  struct() | atom() | nil,
  Keyword.t()
) ::
  :ok | {:error, over_limits_error()}

Ensures that the given user (or the usage map) is within the limits of the given plan.

An opts argument can be passed with ignore_pageview_limit: true which bypasses the pageview limit check and returns :ok as long as the other limits are not exceeded.

@spec features_usage(Plausible.Auth.User.t() | Plausible.Site.t()) :: [atom()]

Given a user, this function returns the features used across all the sites this user owns + StatsAPI if the user has a configured Stats API key.

Given a site, returns the features used by the site.

Link to this function

monthly_pageview_limit(user)

View Source
@spec monthly_pageview_limit(
  Plausible.Auth.User.t()
  | Plausible.Billing.Subscription.t()
) ::
  non_neg_integer() | :unlimited
Link to this function

monthly_pageview_usage(user, site_ids \\ nil)

View Source
@spec monthly_pageview_usage(Plausible.Auth.User.t(), list() | nil) ::
  monthly_pageview_usage()

Queries the ClickHouse database for the monthly pageview usage. If the given user's subscription is active, past_due, or a deleted (but not yet expired), a map with the following structure is returned:

%{
  current_cycle: usage_cycle(),
  last_cycle: usage_cycle(),
  penultimate_cycle: usage_cycle()
}

In all other cases of the subscription status (or a free_10k subscription which does not have a last_bill_date defined) - the following structure is returned:

%{last_30_days: usage_cycle()}

Given only a user as input, the usage is queried from across all the sites that the user owns. Alternatively, given an optional argument of site_ids, the usage from across all those sites is queried instead.

Link to this function

pageview_allowance_margin()

View Source
@spec site_limit(Plausible.Auth.User.t()) :: non_neg_integer() | :unlimited
@spec site_usage(Plausible.Auth.User.t()) :: non_neg_integer()

Returns the number of sites the given user owns.

@spec team_member_limit(Plausible.Auth.User.t()) :: non_neg_integer()
@spec team_member_usage(Plausible.Auth.User.t()) :: integer()

Returns the total count of team members associated with the user's sites.

  • The given user (i.e. the owner) is not counted as a team member.

  • Pending invitations are counted as team members even before accepted.

  • Users are counted uniquely - i.e. even if an account is associated with many sites owned by the given user, they still count as one team member.

Link to this function

usage_cycle(user, cycle, owned_site_ids \\ nil, today \\ Timex.today())

View Source
@spec usage_cycle(Plausible.Auth.User.t(), period(), list() | nil, Date.t()) ::
  usage_cycle()
Link to this function

within_limit?(usage, limit)

View Source
@spec within_limit?(non_neg_integer(), non_neg_integer() | :unlimited) :: boolean()

Returns whether the usage is within the limit or not. Returns true if usage is equal to the limit.