Commit a3b3777de331abe42194a3f873f1d312ae600885

Thomas de Grivel 2022-01-17T14:39:34

disk usage per repo, per org, per user

diff --git a/lib/kmxgit/organisation_manager/organisation.ex b/lib/kmxgit/organisation_manager/organisation.ex
index 180a0d2..6828a89 100644
--- a/lib/kmxgit/organisation_manager/organisation.ex
+++ b/lib/kmxgit/organisation_manager/organisation.ex
@@ -37,4 +37,10 @@ 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/user_manager/user.ex b/lib/kmxgit/user_manager/user.ex
index 41af245..e998e76 100644
--- a/lib/kmxgit/user_manager/user.ex
+++ b/lib/kmxgit/user_manager/user.ex
@@ -269,4 +269,10 @@ 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/repository_controller.ex b/lib/kmxgit_web/controllers/repository_controller.ex
index 025c355..896c52b 100644
--- a/lib/kmxgit_web/controllers/repository_controller.ex
+++ b/lib/kmxgit_web/controllers/repository_controller.ex
@@ -406,16 +406,16 @@ defmodule KmxgitWeb.RepositoryController do
   end
   defp show_op(conn, :tree, %{tree: tree, git: git, org: org, path: path, repo: repo, user: user}) do
     conn
-    |> assign(:tree, tree)
-    |> assign(:tree_url, tree && Routes.repository_path(conn, :show, Repository.owner_slug(repo), Repository.splat(repo, ["_tree", tree] ++ (if path, do: String.split(path, "/"), else: []))))
     |> assign_current_organisation(org)
     |> assign(:current_repository, repo)
+    |> assign(:disk_usage, Repository.disk_usage(repo))
     |> assign(:git, git)
     |> assign(:repo, repo)
-    |> assign(:repo_size, Repository.disk_usage(repo))
     |> assign(:members, Repository.members(repo))
     |> assign(:owner, org || user)
     |> assign(:path, path)
+    |> assign(:tree, tree)
+    |> assign(:tree_url, tree && Routes.repository_path(conn, :show, Repository.owner_slug(repo), Repository.splat(repo, ["_tree", tree] ++ (if path, do: String.split(path, "/"), else: []))))
     |> render("show.html")
   end
   defp show_op(conn, _, _) do
diff --git a/lib/kmxgit_web/controllers/slug_controller.ex b/lib/kmxgit_web/controllers/slug_controller.ex
index 36dce96..b622e34 100644
--- a/lib/kmxgit_web/controllers/slug_controller.ex
+++ b/lib/kmxgit_web/controllers/slug_controller.ex
@@ -1,6 +1,7 @@
 defmodule KmxgitWeb.SlugController do
   use KmxgitWeb, :controller
 
+  alias Kmxgit.OrganisationManager.Organisation
   alias Kmxgit.RepositoryManager
   alias Kmxgit.SlugManager
   alias Kmxgit.UserManager.User
@@ -19,6 +20,7 @@ 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(:repos, repos)
         |> assign(:page_title, gettext("User %{login}", login: user.slug.slug))
         |> assign(:user, user)
@@ -29,6 +31,7 @@ defmodule KmxgitWeb.SlugController do
         if org do
           conn
           |> assign(:current_organisation, org)
+          |> assign(:disk_usage, Organisation.disk_usage(org))
           |> assign(:org, org)
           |> assign(:page_title, org.name || org.slug.slug)
           |> put_view(OrganisationView)
diff --git a/lib/kmxgit_web/templates/organisation/show.html.heex b/lib/kmxgit_web/templates/organisation/show.html.heex
index 9a1aa47..28fcbd9 100644
--- a/lib/kmxgit_web/templates/organisation/show.html.heex
+++ b/lib/kmxgit_web/templates/organisation/show.html.heex
@@ -70,6 +70,10 @@
             </ul>
           </td>
         </tr>
+        <tr>
+          <th><%= gettext("Disk usage") %></th>
+          <td><%= FileSize.new(@disk_usage, :mb) |> FileSize.to_string() %></td>
+        </tr>
       </table>
     </div>
   </div>
diff --git a/lib/kmxgit_web/templates/repository/show_properties.html.heex b/lib/kmxgit_web/templates/repository/show_properties.html.heex
index 0bbe98e..baff4be 100644
--- a/lib/kmxgit_web/templates/repository/show_properties.html.heex
+++ b/lib/kmxgit_web/templates/repository/show_properties.html.heex
@@ -66,6 +66,6 @@
   <% end %>
   <tr>
     <th><%= gettext("Disk usage") %></th>
-    <td><%= FileSize.new(@repo_size, :mb) |> FileSize.to_string() %></td>
+    <td><%= FileSize.new(@disk_usage, :mb) |> FileSize.to_string() %></td>
   </tr>
 </table>
diff --git a/lib/kmxgit_web/templates/user/show.html.heex b/lib/kmxgit_web/templates/user/show.html.heex
index ab65bb5..b0e4cd7 100644
--- a/lib/kmxgit_web/templates/user/show.html.heex
+++ b/lib/kmxgit_web/templates/user/show.html.heex
@@ -82,6 +82,10 @@
             </pre>
           </td>
         </tr>
+        <tr>
+          <th><%= gettext("Disk usage") %></th>
+          <td><%= FileSize.new(@disk_usage, :mb) |> FileSize.to_string() %></td>
+</tr>
       </table>
     </div>
   </div>