Commit b14387811e73cd30af893c58321e31bd1c008efe

Thomas de Grivel 2022-01-26T15:47:26

[admin] dashboard: stats

diff --git a/lib/kmxgit/git_manager.ex b/lib/kmxgit/git_manager.ex
index b94d4c8..8dfa9ea 100644
--- a/lib/kmxgit/git_manager.ex
+++ b/lib/kmxgit/git_manager.ex
@@ -287,7 +287,7 @@ defmodule Kmxgit.GitManager do
     end
   end
 
-  defp du_ks(path) do
+  def du_ks(path) do
     {out, status} = System.cmd("du", ["-ks", path], stderr_to_stdout: true)
     case status do
       0 ->
diff --git a/lib/kmxgit_web/controllers/admin/dashboard_controller.ex b/lib/kmxgit_web/controllers/admin/dashboard_controller.ex
index ee54c50..b79c4ea 100644
--- a/lib/kmxgit_web/controllers/admin/dashboard_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/dashboard_controller.ex
@@ -1,9 +1,18 @@
 defmodule KmxgitWeb.Admin.DashboardController do
   use KmxgitWeb, :controller
 
+  alias Kmxgit.GitManager
+  alias Kmxgit.OrganisationManager
+  alias Kmxgit.RepositoryManager
+  alias Kmxgit.UserManager
+
   def index(conn, _params) do
     conn
+    |> assign(:disk_usage, GitManager.du_ks("priv/git"))
+    |> assign(:org_count, OrganisationManager.count_organisations())
     |> assign(:page_title, gettext "Dashboard")
+    |> assign(:repo_count, RepositoryManager.count_repositories())
+    |> assign(:user_count, UserManager.count_users())
     |> render("index.html")
   end
 end
diff --git a/lib/kmxgit_web/controllers/page_controller.ex b/lib/kmxgit_web/controllers/page_controller.ex
index ca7dec7..3f216cd 100644
--- a/lib/kmxgit_web/controllers/page_controller.ex
+++ b/lib/kmxgit_web/controllers/page_controller.ex
@@ -3,6 +3,7 @@ defmodule KmxgitWeb.PageController do
 
   require Logger
 
+  alias Kmxgit.GitManager
   alias Kmxgit.OrganisationManager
   alias Kmxgit.Repo
   alias Kmxgit.RepositoryManager
@@ -27,24 +28,12 @@ defmodule KmxgitWeb.PageController do
     |> render(:doc_git_install)
   end
 
-  def du_ks(path) do
-    {out, status} = System.cmd("du", ["-ks", path], stderr_to_stdout: true)
-    case status do
-      0 ->
-        {du, _} = Integer.parse(out)
-        du
-      _ ->
-        Logger.error(out)
-        0
-    end
-  end
-
   def index(conn, _params) do
     if ! UserManager.admin_user_present? do
       redirect(conn, to: Routes.page_path(conn, :new_admin))
     else
       conn
-      |> assign(:disk_usage, du_ks("/home/git"))
+      |> assign(:disk_usage, GitManager.du_ks("priv/git"))
       |> assign(:git_ssh_url, Application.get_env(:kmxgit, :git_ssh_url))
       |> assign(:org_count, OrganisationManager.count_organisations())
       |> assign(:repo_count, RepositoryManager.count_repositories())
diff --git a/lib/kmxgit_web/templates/admin/dashboard/index.html.heex b/lib/kmxgit_web/templates/admin/dashboard/index.html.heex
index 018617b..35bb5d1 100644
--- a/lib/kmxgit_web/templates/admin/dashboard/index.html.heex
+++ b/lib/kmxgit_web/templates/admin/dashboard/index.html.heex
@@ -2,4 +2,29 @@
 
   <h1>Admin dashboard</h1>
 
+  <h2><%= link gettext("Stats"), to: "#stats", id: "stats" %></h2>
+
+  <div class="row">
+    <div class="col col-12 col-md-5">
+      <table class="table">
+        <tr>
+          <th><%= gettext "Disk usage" %></th>
+          <td><%= disk_usage(@disk_usage) %></td>
+        </tr>
+        <tr>
+          <th><%= gettext "Users" %></th>
+          <td><%= @user_count %></td>
+        </tr>
+        <tr>
+          <th><%= gettext "Organisations" %></th>
+          <td><%= @org_count %></td>
+        </tr>
+        <tr>
+          <th><%= gettext "Repositories" %></th>
+          <td><%= @repo_count %></td>
+        </tr>
+      </table>
+    </div>
+  </div>
+
 </div>