From b163fe5a47a9e578b26f1ded4efb1884c36557ec Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?=
Date: Mon, 15 Jul 2024 20:22:37 +0200
Subject: [PATCH] Display some stats for applications
Show the number of oauth code, token and client credential associated
with an application
---
lib/teiserver/o_auth/queries/code_query.ex | 5 +++-
.../admin/o_auth_application_controller.ex | 20 +++++++++++-----
.../admin/o_auth_application/index.html.heex | 8 +++++--
.../admin/o_auth_application/show.html.heex | 24 +++++++++++++++++--
4 files changed, 46 insertions(+), 11 deletions(-)
diff --git a/lib/teiserver/o_auth/queries/code_query.ex b/lib/teiserver/o_auth/queries/code_query.ex
index 67d52cf7c..aa3b68d17 100644
--- a/lib/teiserver/o_auth/queries/code_query.ex
+++ b/lib/teiserver/o_auth/queries/code_query.ex
@@ -1,5 +1,6 @@
defmodule Teiserver.OAuth.CodeQueries do
use TeiserverWeb, :queries
+ alias Teiserver.OAuth.Application
alias Teiserver.OAuth.Code
@doc """
@@ -34,7 +35,9 @@ defmodule Teiserver.OAuth.CodeQueries do
where: code.expires_at > ^as_at
end
- @spec count_per_apps([Application.id()], DateTime.t() | nil) :: %{Application.id() => non_neg_integer()}
+ @spec count_per_apps([Application.id()], DateTime.t() | nil) :: %{
+ Application.id() => non_neg_integer()
+ }
def count_per_apps(app_ids, as_at \\ nil) do
query =
base_query()
diff --git a/lib/teiserver_web/controllers/admin/o_auth_application_controller.ex b/lib/teiserver_web/controllers/admin/o_auth_application_controller.ex
index c4229d120..bc3425b39 100644
--- a/lib/teiserver_web/controllers/admin/o_auth_application_controller.ex
+++ b/lib/teiserver_web/controllers/admin/o_auth_application_controller.ex
@@ -18,10 +18,12 @@ defmodule TeiserverWeb.Admin.OAuthApplicationController do
@spec index(Plug.Conn.t(), map()) :: Plug.Conn.t()
def index(conn, _params) do
applications = ApplicationQueries.list_applications()
+ stats = ApplicationQueries.get_stats(Enum.map(applications, fn app -> app.id end))
conn
|> assign(:page_title, "BAR - oauth apps")
- |> render("index.html", applications: applications)
+ |> render("index.html", app_and_stats: Enum.zip(applications, stats)
+ )
end
@spec new(Plug.Conn.t(), map()) :: Plug.Conn.t()
@@ -73,9 +75,7 @@ defmodule TeiserverWeb.Admin.OAuthApplicationController do
def show(conn, assigns) do
case ApplicationQueries.get_application_by_id(Map.get(assigns, "id")) do
%Application{} = app ->
- conn
- |> assign(:page_title, "BAR - oauth app #{app.name}")
- |> render("show.html", app: app)
+ render_show(conn, app)
nil ->
conn
@@ -111,14 +111,14 @@ defmodule TeiserverWeb.Admin.OAuthApplicationController do
{:ok, app} ->
conn
|> put_flash(:info, "Application updated")
- |> render(:show, app: app)
+ |> render_show(app)
{:error, changeset} ->
changeset = fill_email_error(changeset)
conn
|> put_status(400)
- |> render(:edit, app: app, changeset: changeset)
+ |> render("edit.html", app: app, changeset: changeset)
end
nil ->
@@ -151,6 +151,14 @@ defmodule TeiserverWeb.Admin.OAuthApplicationController do
end
end
+ defp render_show(conn, app) do
+ [stats] = ApplicationQueries.get_stats(app.id)
+
+ conn
+ |> assign(:page_title, "BAR - oauth app #{app.name}")
+ |> render("show.html", app: app, stats: stats)
+ end
+
# split the comma separated fields and map emails to users
defp form_to_app(params) do
user_id = Map.get(Account.get_user_by_email(params["owner_email"]) || %{}, :id)
diff --git a/lib/teiserver_web/templates/admin/o_auth_application/index.html.heex b/lib/teiserver_web/templates/admin/o_auth_application/index.html.heex
index bfc93f16f..71f6a67a4 100644
--- a/lib/teiserver_web/templates/admin/o_auth_application/index.html.heex
+++ b/lib/teiserver_web/templates/admin/o_auth_application/index.html.heex
@@ -7,7 +7,7 @@
- <%= if length(@applications) > 0 do %>
+ <%= if length(@app_and_stats) > 0 do %>
- <%!-- TODO: add some application stats, like the number of authorization code, --%>
- <%!-- bearer token, refresh token and client id/secret associated with this app --%>
+
+ Stats
+
+
+
+
+
+ Authorization code |
+ <%= @stats.code_count %> |
+
+
+ Authentication token |
+ <%= @stats.token_count %> |
+
+
+ Client credentials |
+ <%= @stats.credential_count %> |
+
+
+
+
+