Commit c3be0b9aa15393dedb5aa52ffddd9890d33dc04e

Thomas de Grivel 2022-01-18T11:18:15

[front page] stats: count users, orgs, repos.

diff --git a/lib/kmxgit/organisation_manager.ex b/lib/kmxgit/organisation_manager.ex
index 0a0954b..7435a98 100644
--- a/lib/kmxgit/organisation_manager.ex
+++ b/lib/kmxgit/organisation_manager.ex
@@ -15,6 +15,10 @@ defmodule Kmxgit.OrganisationManager do
       order_by: s.slug
   end
 
+  def count_organisations do
+    Repo.one from org in Organisation, select: count()
+  end
+
   def get_organisation(id) do
     Repo.one from organisation in Organisation,
       where: [id: ^id],
diff --git a/lib/kmxgit/repository_manager.ex b/lib/kmxgit/repository_manager.ex
index a015cfe..618d6ad 100644
--- a/lib/kmxgit/repository_manager.ex
+++ b/lib/kmxgit/repository_manager.ex
@@ -18,6 +18,10 @@ defmodule Kmxgit.RepositoryManager do
     |> Enum.sort_by(&Repository.full_slug/1)
   end
 
+  def count_repositories do
+    Repo.one from org in Repository, select: count()
+  end
+
   def list_contributor_repositories(user) do
     list_repositories()
     |> Enum.filter(fn repo ->
diff --git a/lib/kmxgit/user_manager.ex b/lib/kmxgit/user_manager.ex
index 75e2990..fe3c1af 100644
--- a/lib/kmxgit/user_manager.ex
+++ b/lib/kmxgit/user_manager.ex
@@ -13,6 +13,10 @@ defmodule Kmxgit.UserManager do
     Repo.all from user in User, preload: :slug
   end
 
+  def count_users do
+    Repo.one from user in User, select: count()
+  end
+
   def get_user(id) do
     Repo.one from user in User,
       where: [id: ^id],
diff --git a/lib/kmxgit_web/controllers/page_controller.ex b/lib/kmxgit_web/controllers/page_controller.ex
index a4057b5..fd18f3d 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.OrganisationManager
   alias Kmxgit.Repo
   alias Kmxgit.RepositoryManager
   alias Kmxgit.RepositoryManager.Repository
@@ -39,6 +40,9 @@ defmodule KmxgitWeb.PageController do
     else
       conn
       |> assign(:disk_usage, du_ks("/home/git"))
+      |> assign(:org_count, OrganisationManager.count_organisations())
+      |> assign(:repo_count, RepositoryManager.count_repositories())
+      |> assign(:user_count, UserManager.count_users())
       |> render(:index)
     end
   end
diff --git a/lib/kmxgit_web/templates/page/index.html.heex b/lib/kmxgit_web/templates/page/index.html.heex
index 14a0ccc..af6ca69 100644
--- a/lib/kmxgit_web/templates/page/index.html.heex
+++ b/lib/kmxgit_web/templates/page/index.html.heex
@@ -25,6 +25,18 @@
           <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>