New admin route for displaying usage (#3577)
* add a new crm usage route for admins * add a test for admin route authorization * add full_build_only tagpull/3540/head^2 r2023.12.06-271
parent
7212d336ae
commit
4566e6b530
@ -0,0 +1,73 @@
|
||||
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.subscription),
|
||||
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
|
||||
"""
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Usage - user:#{user.id}</title>
|
||||
<style>
|
||||
ul, li {margin-top: 10px;}
|
||||
body {padding-top: 10px;}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<ul>
|
||||
<li>Sites: <b>#{usage.sites}</b> / #{limits.sites}</li>
|
||||
<li>Team members: <b>#{usage.team_members}</b> / #{limits.team_members}</li>
|
||||
<li>Features: #{features_usage(usage.features)}</li>
|
||||
<li>Monthly pageviews: #{monthly_pageviews_usage(usage.monthly_pageviews, limits.monthly_pageviews)}</li>
|
||||
</ul>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
"""
|
||||
end
|
||||
|
||||
defp features_usage(features_module_list) do
|
||||
list_items =
|
||||
features_module_list
|
||||
|> Enum.map_join(fn f_mod -> "<li>#{f_mod.display_name()}</li>" end)
|
||||
|
||||
"<ul>#{list_items}</ul>"
|
||||
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} ->
|
||||
"<li>#{cycle} (#{PlausibleWeb.TextHelpers.format_date_range(usage.date_range)}): <b>#{usage.total}</b> / #{limit}</li>"
|
||||
end)
|
||||
|
||||
"<ul>#{Enum.join(list_items)}</ul>"
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,13 @@
|
||||
defmodule PlausibleWeb.AdminControllerTest do
|
||||
use PlausibleWeb.ConnCase
|
||||
|
||||
describe "GET /crm/auth/user/:user_id/usage" do
|
||||
setup [:create_user, :log_in]
|
||||
|
||||
@tag :full_build_only
|
||||
test "returns 403 if the logged in user is not a super admin", %{conn: conn} do
|
||||
conn = get(conn, "/crm/auth/user/1/usage")
|
||||
assert response(conn, 403) == "Not allowed"
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue