defmodule PlausibleWeb.AdminController do
use PlausibleWeb, :controller
alias Plausible.Billing.Quota
def usage(conn, params) do
user =
params["user_id"]
|> String.to_integer()
|> Plausible.Users.with_subscription()
usage = Quota.usage(user, with_features: true)
limits = %{
monthly_pageviews: Quota.monthly_pageview_limit(user),
sites: Quota.site_limit(user),
team_members: Quota.team_member_limit(user)
}
html_response = usage_and_limits_html(user, usage, limits)
conn
|> put_resp_content_type("text/html")
|> send_resp(200, html_response)
end
defp usage_and_limits_html(user, usage, limits) do
"""
Usage - user:#{user.id}
- Sites: #{usage.sites} / #{limits.sites}
- Team members: #{usage.team_members} / #{limits.team_members}
- Features: #{features_usage(usage.features)}
- Monthly pageviews: #{monthly_pageviews_usage(usage.monthly_pageviews, limits.monthly_pageviews)}
"""
end
defp features_usage(features_module_list) do
list_items =
features_module_list
|> Enum.map_join(fn f_mod -> "#{f_mod.display_name()}" end)
""
end
defp monthly_pageviews_usage(usage, limit) do
list_items =
usage
|> Enum.sort_by(fn {_cycle, usage} -> usage.date_range.first end, :desc)
|> Enum.map(fn {cycle, usage} ->
"#{cycle} (#{PlausibleWeb.TextHelpers.format_date_range(usage.date_range)}): #{usage.total} / #{limit}"
end)
""
end
end