Commit 59f79269ef07710b6554b1dd10855db5b547e8d3

Thomas de Grivel 2022-01-18T10:32:54

better disk usage

diff --git a/lib/kmxgit/organisation_manager/organisation.ex b/lib/kmxgit/organisation_manager/organisation.ex
index 6828a89..180a0d2 100644
--- a/lib/kmxgit/organisation_manager/organisation.ex
+++ b/lib/kmxgit/organisation_manager/organisation.ex
@@ -37,10 +37,4 @@ defmodule Kmxgit.OrganisationManager.Organisation do
     org.owned_repositories
     |> Enum.sort_by(&Repository.full_slug/1)
   end
-
-  def disk_usage(org) do
-    org.owned_repositories
-    |> Enum.map(&Repository.disk_usage/1)
-    |> Enum.sum()
-  end
 end
diff --git a/lib/kmxgit/repository_manager/repository.ex b/lib/kmxgit/repository_manager/repository.ex
index b3aade9..d83d44b 100644
--- a/lib/kmxgit/repository_manager/repository.ex
+++ b/lib/kmxgit/repository_manager/repository.ex
@@ -205,8 +205,17 @@ defmodule Kmxgit.RepositoryManager.Repository do
     |> Enum.join("\n")
   end
 
-  def disk_usage(repo) do
-    {:ok, {du, _}} = GitManager.disk_usage(full_slug(repo))
-    du
+  def disk_usage(repo = %__MODULE__{}) do
+    case GitManager.disk_usage(full_slug(repo)) do
+      {:ok, {du, _}} -> du
+      x ->
+        IO.inspect(x)
+        0
+    end
+  end
+  def disk_usage(repos) when is_list(repos) do
+    repos
+    |> Enum.map(&disk_usage/1)
+    |> Enum.sum()
   end
 end
diff --git a/lib/kmxgit/user_manager/user.ex b/lib/kmxgit/user_manager/user.ex
index e998e76..41af245 100644
--- a/lib/kmxgit/user_manager/user.ex
+++ b/lib/kmxgit/user_manager/user.ex
@@ -269,10 +269,4 @@ defmodule Kmxgit.UserManager.User do
       |> add_error(:totp_last, "invalid token")
     end
   end
-
-  def disk_usage(user) do
-    user.owned_repositories
-    |> Enum.map(&Repository.disk_usage/1)
-    |> Enum.sum()
-  end
 end
diff --git a/lib/kmxgit_web/controllers/page_controller.ex b/lib/kmxgit_web/controllers/page_controller.ex
index aa21bd7..3d85258 100644
--- a/lib/kmxgit_web/controllers/page_controller.ex
+++ b/lib/kmxgit_web/controllers/page_controller.ex
@@ -1,6 +1,8 @@
 defmodule KmxgitWeb.PageController do
   use KmxgitWeb, :controller
 
+  require Logger
+
   alias Kmxgit.Repo
   alias Kmxgit.RepositoryManager
   alias Kmxgit.RepositoryManager.Repository
@@ -19,11 +21,24 @@ defmodule KmxgitWeb.PageController do
     |> resp(200, a)
   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
+      x ->
+        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"))
       |> render(:index)
     end
   end
diff --git a/lib/kmxgit_web/controllers/slug_controller.ex b/lib/kmxgit_web/controllers/slug_controller.ex
index b622e34..5a7fefe 100644
--- a/lib/kmxgit_web/controllers/slug_controller.ex
+++ b/lib/kmxgit_web/controllers/slug_controller.ex
@@ -3,6 +3,7 @@ defmodule KmxgitWeb.SlugController do
 
   alias Kmxgit.OrganisationManager.Organisation
   alias Kmxgit.RepositoryManager
+  alias Kmxgit.RepositoryManager.Repository
   alias Kmxgit.SlugManager
   alias Kmxgit.UserManager.User
   alias KmxgitWeb.ErrorView
@@ -20,7 +21,8 @@ defmodule KmxgitWeb.SlugController do
         contributor_repos = RepositoryManager.list_contributor_repositories(user)
         repos = owned_repos ++ contributor_repos
         conn
-        |> assign(:disk_usage, User.disk_usage(user))
+        |> assign(:disk_usage, Repository.disk_usage(owned_repos))
+        |> assign(:disk_usage_all, Repository.disk_usage(repos))
         |> assign(:repos, repos)
         |> assign(:page_title, gettext("User %{login}", login: user.slug.slug))
         |> assign(:user, user)
@@ -31,7 +33,7 @@ defmodule KmxgitWeb.SlugController do
         if org do
           conn
           |> assign(:current_organisation, org)
-          |> assign(:disk_usage, Organisation.disk_usage(org))
+          |> assign(:disk_usage, Repository.disk_usage(org.owned_repositories))
           |> assign(:org, org)
           |> assign(:page_title, org.name || org.slug.slug)
           |> put_view(OrganisationView)
diff --git a/lib/kmxgit_web/templates/page/index.html.heex b/lib/kmxgit_web/templates/page/index.html.heex
index 18f946c..facd597 100644
--- a/lib/kmxgit_web/templates/page/index.html.heex
+++ b/lib/kmxgit_web/templates/page/index.html.heex
@@ -10,9 +10,18 @@
     .
   </p>
   <p>
-    <%= gettext "You can register and create public and private Git repositories." %>
+    <%= gettext "Anyone can register and create public and private Git repositories." %>
   </p>
 
   <h3><%= gettext "SSH keys fingerprint" %></h3>
   <pre><%= render("ssh_keys_fingerprint.txt") %></pre>
+
+  <h3><%= gettext("Stats") %></h3>
+
+  <table class="table">
+    <tr>
+      <th><%= gettext "Disk usage" %></th>
+      <td><%= disk_usage(@disk_usage) %></td>
+    </tr>
+  </table>
 </div>
diff --git a/lib/kmxgit_web/templates/user/show.html.heex b/lib/kmxgit_web/templates/user/show.html.heex
index 7fd1350..6fabf5a 100644
--- a/lib/kmxgit_web/templates/user/show.html.heex
+++ b/lib/kmxgit_web/templates/user/show.html.heex
@@ -83,9 +83,13 @@
           </td>
         </tr>
         <tr>
-          <th><%= gettext("Disk usage") %></th>
+          <th><%= gettext "Disk usage" %></th>
           <td><%= disk_usage(@disk_usage) %></td>
-</tr>
+        </tr>
+        <tr>
+          <th><%= gettext "Accessible" %></th>
+          <td><%= disk_usage(@disk_usage_all) %></td>
+        </tr>
       </table>
     </div>
   </div>