Skip to content

Commit

Permalink
Display some stats for applications
Browse files Browse the repository at this point in the history
Show the number of oauth code, token and client credential associated
with an application
  • Loading branch information
geekingfrog committed Jul 27, 2024
1 parent 8a1d4df commit b163fe5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
5 changes: 4 additions & 1 deletion lib/teiserver/o_auth/queries/code_query.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Teiserver.OAuth.CodeQueries do
use TeiserverWeb, :queries
alias Teiserver.OAuth.Application
alias Teiserver.OAuth.Code

@doc """
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@
</div>
</p>

<%= if length(@applications) > 0 do %>
<%= if length(@app_and_stats) > 0 do %>
<table class="table">
<thead>
<tr>
<th>id</th>
<th>client id (uid)</th>
<th>name</th>
<th>owner</th>
<th>code/token/creds</th>
<th>actions</th>
</tr>
</thead>
<tbody>
<%= for app <- @applications do %>
<%= for {app, stats} <- @app_and_stats do %>
<tr>
<td><%= app.id %></td>
<td><%= app.uid %></td>
<td><%= app.name %></td>
<td><%= app.owner.name %> (<%= app.owner.email %>)</td>
<td>
<%= stats.code_count %> / <%= stats.token_count %> / <%= stats.credential_count %>
</td>
<td>
<a href={~p"/teiserver/admin/oauth_application/#{app.id}"}>
<button type="button" class="btn btn-primary btn-sm">show</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,28 @@
</table>
</div>
</div>
<%!-- TODO: add some application stats, like the number of authorization code, --%>
<%!-- bearer token, refresh token and client id/secret associated with this app --%>

<h2>Stats</h2>
<div class="row">
<div class="col-6">
<table class="table">
<tbody>
<tr>
<td>Authorization code</td>
<td><%= @stats.code_count %></td>
</tr>
<tr>
<td>Authentication token</td>
<td><%= @stats.token_count %></td>
</tr>
<tr>
<td>Client credentials</td>
<td><%= @stats.credential_count %></td>
</tr>
</tbody>
</table>
</div>
</div>

<p>
<a href={~p"/teiserver/admin/oauth_application/#{@app.id}/edit"}>
Expand Down

0 comments on commit b163fe5

Please sign in to comment.