mirror of
https://github.com/plausible/analytics.git
synced 2024-09-11 18:07:33 +03:00
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 tag
This commit is contained in:
parent
7212d336ae
commit
4566e6b530
@ -30,6 +30,7 @@ defmodule Plausible.Auth.UserAdmin do
|
||||
trial_expiry_date: %{name: "Trial expiry", value: &format_date(&1.trial_expiry_date)},
|
||||
subscription_plan: %{value: &subscription_plan/1},
|
||||
subscription_status: %{value: &subscription_status/1},
|
||||
usage: %{value: &usage_link/1},
|
||||
grace_period: %{value: &grace_period_status/1}
|
||||
]
|
||||
end
|
||||
@ -43,10 +44,6 @@ defmodule Plausible.Auth.UserAdmin do
|
||||
lock: %{
|
||||
name: "Lock",
|
||||
action: fn _, user -> lock(user) end
|
||||
},
|
||||
calculate_usage: %{
|
||||
name: "Calculate usage",
|
||||
action: fn _, user -> calculate_usage(user) end
|
||||
}
|
||||
]
|
||||
end
|
||||
@ -70,42 +67,6 @@ defmodule Plausible.Auth.UserAdmin do
|
||||
end
|
||||
end
|
||||
|
||||
@separator String.duplicate("_", 200)
|
||||
|
||||
def calculate_usage(user) do
|
||||
user = Plausible.Users.with_subscription(user)
|
||||
|
||||
pageview_limit =
|
||||
case Plausible.Billing.Quota.monthly_pageview_limit(user.subscription) do
|
||||
:unlimited -> "unlimited"
|
||||
integer -> PlausibleWeb.StatsView.large_number_format(integer)
|
||||
end
|
||||
|
||||
pageview_usage =
|
||||
user
|
||||
|> Plausible.Billing.Quota.monthly_pageview_usage()
|
||||
|> Enum.map_join(" #{@separator} ", fn {cycle, usage} ->
|
||||
"#{cycle}: (#{PlausibleWeb.TextHelpers.format_date_range(usage.date_range)}): #{usage.total}"
|
||||
end)
|
||||
|
||||
site_limit = Plausible.Billing.Quota.site_limit(user)
|
||||
site_usage = Plausible.Billing.Quota.site_usage(user)
|
||||
|
||||
team_member_limit = Plausible.Billing.Quota.team_member_limit(user)
|
||||
team_member_usage = Plausible.Billing.Quota.team_member_usage(user)
|
||||
|
||||
msg = """
|
||||
TOTAL PAGEVIEWS (limit: #{pageview_limit})
|
||||
#{@separator}
|
||||
#{pageview_usage}
|
||||
#{@separator}
|
||||
SITES (#{site_usage} / #{site_limit}) #{@separator}
|
||||
TEAM MEMBERS (#{team_member_usage} / #{team_member_limit})
|
||||
"""
|
||||
|
||||
{:error, user, msg}
|
||||
end
|
||||
|
||||
defp grace_period_status(%{grace_period: grace_period}) do
|
||||
case grace_period do
|
||||
nil ->
|
||||
@ -156,6 +117,11 @@ defmodule Plausible.Auth.UserAdmin do
|
||||
end
|
||||
end
|
||||
|
||||
defp usage_link(user) do
|
||||
path = PlausibleWeb.Router.Helpers.admin_path(PlausibleWeb.Endpoint, :usage, user.id)
|
||||
{:safe, ~s(<a href="#{path}">Usage</a>)}
|
||||
end
|
||||
|
||||
defp format_date(nil), do: "--"
|
||||
|
||||
defp format_date(date) do
|
||||
|
73
lib/plausible_web/controllers/admin_controller.ex
Normal file
73
lib/plausible_web/controllers/admin_controller.ex
Normal file
@ -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
|
@ -72,6 +72,13 @@ defmodule PlausibleWeb.Router do
|
||||
pipe_through: [PlausibleWeb.Plugs.NoRobots, PlausibleWeb.CRMAuthPlug]
|
||||
end
|
||||
|
||||
on_full_build do
|
||||
scope "/crm", PlausibleWeb do
|
||||
pipe_through :flags
|
||||
get "/auth/user/:user_id/usage", AdminController, :usage
|
||||
end
|
||||
end
|
||||
|
||||
on_full_build do
|
||||
scope path: "/flags" do
|
||||
pipe_through :flags
|
||||
|
13
test/plausible_web/controllers/admin_controller_test.exs
Normal file
13
test/plausible_web/controllers/admin_controller_test.exs
Normal file
@ -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
Block a user