Commit f91b358b2f915c7343a8e6d2834957ae315a7ed1

Thomas de Grivel 2022-01-26T20:34:40

[admin] disk usage in index

diff --git a/lib/kmxgit/git_manager.ex b/lib/kmxgit/git_manager.ex
index 8dfa9ea..6d1f4d6 100644
--- a/lib/kmxgit/git_manager.ex
+++ b/lib/kmxgit/git_manager.ex
@@ -295,7 +295,7 @@ defmodule Kmxgit.GitManager do
         k
       x ->
         IO.inspect(x)
-        -1
+        0
     end
   end
 
diff --git a/lib/kmxgit/organisation_manager.ex b/lib/kmxgit/organisation_manager.ex
index 9dbdca4..bd15b90 100644
--- a/lib/kmxgit/organisation_manager.ex
+++ b/lib/kmxgit/organisation_manager.ex
@@ -16,6 +16,14 @@ defmodule Kmxgit.OrganisationManager do
       order_by: s.slug
   end
 
+  def put_disk_usage(org = %Organisation{}) do
+    %Organisation{org | disk_usage: Organisation.disk_usage(org)}
+  end
+  def put_disk_usage(orgs) when is_list(orgs) do
+    orgs
+    |> Enum.map(&put_disk_usage/1)
+  end
+
   def count_organisations do
     Repo.one from org in Organisation, select: count()
   end
diff --git a/lib/kmxgit/organisation_manager/organisation.ex b/lib/kmxgit/organisation_manager/organisation.ex
index 87525be..53d9321 100644
--- a/lib/kmxgit/organisation_manager/organisation.ex
+++ b/lib/kmxgit/organisation_manager/organisation.ex
@@ -10,6 +10,7 @@ defmodule Kmxgit.OrganisationManager.Organisation do
 
   schema "organisations" do
     field :description, :string
+    field :disk_usage, :integer, virtual: true, default: 0
     field :name, :string
     has_many :owned_repositories, Repository
     many_to_many :users, User, join_through: "users_organisations", on_replace: :delete, on_delete: :delete_all
diff --git a/lib/kmxgit/repository_manager.ex b/lib/kmxgit/repository_manager.ex
index 618d6ad..63bde13 100644
--- a/lib/kmxgit/repository_manager.ex
+++ b/lib/kmxgit/repository_manager.ex
@@ -18,8 +18,16 @@ defmodule Kmxgit.RepositoryManager do
     |> Enum.sort_by(&Repository.full_slug/1)
   end
 
+  def put_disk_usage(repo = %Repository{}) do
+    %Repository{repo | disk_usage: Repository.disk_usage(repo)}
+  end
+  def put_disk_usage(repos) when is_list(repos) do
+    repos
+    |> Enum.map(&put_disk_usage/1)
+  end
+
   def count_repositories do
-    Repo.one from org in Repository, select: count()
+    Repo.one from repo in Repository, select: count()
   end
 
   def list_contributor_repositories(user) do
diff --git a/lib/kmxgit/repository_manager/repository.ex b/lib/kmxgit/repository_manager/repository.ex
index 2fd0271..bd58f79 100644
--- a/lib/kmxgit/repository_manager/repository.ex
+++ b/lib/kmxgit/repository_manager/repository.ex
@@ -11,6 +11,7 @@ defmodule Kmxgit.RepositoryManager.Repository do
   schema "repositories" do
     field :deploy_keys, :string
     field :description, :string
+    field :disk_usage, :integer, virtual: true, default: 0
     field :fork_to, :string, virtual: true
     belongs_to :forked_from, __MODULE__
     belongs_to :organisation, Organisation, on_replace: :nilify
diff --git a/lib/kmxgit/user_manager.ex b/lib/kmxgit/user_manager.ex
index 59666f6..1787026 100644
--- a/lib/kmxgit/user_manager.ex
+++ b/lib/kmxgit/user_manager.ex
@@ -15,6 +15,14 @@ defmodule Kmxgit.UserManager do
                 :slug]
   end
 
+  def put_disk_usage(user = %User{}) do
+    %User{user | disk_usage: User.disk_usage(user)}
+  end
+  def put_disk_usage(users) when is_list(users) do
+    users
+    |> Enum.map(&put_disk_usage/1)
+  end
+
   def count_users do
     Repo.one from user in User, select: count()
   end
diff --git a/lib/kmxgit/user_manager/user.ex b/lib/kmxgit/user_manager/user.ex
index bc2cd4c..ed53e1d 100644
--- a/lib/kmxgit/user_manager/user.ex
+++ b/lib/kmxgit/user_manager/user.ex
@@ -12,6 +12,7 @@ defmodule Kmxgit.UserManager.User do
     field :confirmed_at, :utc_datetime
     field :deploy_only, :boolean, null: false, default: false
     field :description, :string, null: true
+    field :disk_usage, :integer, virtual: true, default: 0
     field :email, :string
     field :hashed_password, :string, redact: true
     field :is_admin, :boolean, null: false, default: false
diff --git a/lib/kmxgit_web/controllers/admin/organisation_controller.ex b/lib/kmxgit_web/controllers/admin/organisation_controller.ex
index 8cda078..4482a60 100644
--- a/lib/kmxgit_web/controllers/admin/organisation_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/organisation_controller.ex
@@ -8,6 +8,7 @@ defmodule KmxgitWeb.Admin.OrganisationController do
 
   def index(conn, _params) do
     orgs = OrganisationManager.list_organisations
+    |> OrganisationManager.put_disk_usage()
     conn
     |> assign(:orgs, orgs)
     |> render("index.html")
diff --git a/lib/kmxgit_web/controllers/admin/repository_controller.ex b/lib/kmxgit_web/controllers/admin/repository_controller.ex
index b0b927a..df419ea 100644
--- a/lib/kmxgit_web/controllers/admin/repository_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/repository_controller.ex
@@ -10,6 +10,7 @@ defmodule KmxgitWeb.Admin.RepositoryController do
 
   def index(conn, _params) do
     repos = RepositoryManager.list_repositories
+    |> RepositoryManager.put_disk_usage()
     conn
     |> assign(:repos, repos)
     |> render("index.html")
diff --git a/lib/kmxgit_web/controllers/admin/user_controller.ex b/lib/kmxgit_web/controllers/admin/user_controller.ex
index 4aea7ae..733126e 100644
--- a/lib/kmxgit_web/controllers/admin/user_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/user_controller.ex
@@ -9,6 +9,7 @@ defmodule KmxgitWeb.Admin.UserController do
 
   def index(conn, _params) do
     users = UserManager.list_users
+    |> UserManager.put_disk_usage()
     conn
     |> assign(:page_title, gettext("Users"))
     |> assign(:users, users)
diff --git a/lib/kmxgit_web/templates/admin/organisation/index.html.heex b/lib/kmxgit_web/templates/admin/organisation/index.html.heex
index 092578f..ce77433 100644
--- a/lib/kmxgit_web/templates/admin/organisation/index.html.heex
+++ b/lib/kmxgit_web/templates/admin/organisation/index.html.heex
@@ -13,15 +13,17 @@
         <th><%= gettext "Id" %></th>
         <th><%= gettext "Name" %></th>
         <th><%= gettext "Slug" %></th>
+        <th><%= gettext "Disk usage" %></th>
         <th><%= gettext "Actions" %></th>
       </tr>
     </thead>
     <tbody>
-      <%= Enum.map @orgs, fn org -> %>
+      <%= for org <- @orgs do %>
         <tr>
           <td><%= link org.id, to: Routes.admin_organisation_path(@conn, :show, org) %></td>
           <td><%= link org.name, to: Routes.admin_organisation_path(@conn, :show, org) %></td>
           <td><%= link org.slug.slug, to: Routes.admin_organisation_path(@conn, :show, org) %></td>
+          <td><%= disk_usage(org.disk_usage) %></td>
           <td>
             <%= link gettext("Show"), to: Routes.slug_path(@conn, :show, org.slug.slug), class: "btn btn-sm btn-primary" %>
           </td>
diff --git a/lib/kmxgit_web/templates/admin/repository/index.html.heex b/lib/kmxgit_web/templates/admin/repository/index.html.heex
index 034932b..ae9231c 100644
--- a/lib/kmxgit_web/templates/admin/repository/index.html.heex
+++ b/lib/kmxgit_web/templates/admin/repository/index.html.heex
@@ -6,6 +6,7 @@
       <th><%= gettext "Id" %></th>
       <th><%= gettext "Owner" %></th>
       <th><%= gettext "Slug" %></th>
+      <th><%= gettext "Disk usage" %></th>
       <th><%= gettext "Actions" %></th>
     </tr>
     <%= Enum.map @repos, fn(repo) -> %>
@@ -20,6 +21,7 @@
           <% end %>
         </td>
         <td><%= link Repository.full_slug(repo), to: Routes.admin_repository_path(@conn, :show, repo) %></td>
+        <td><%= disk_usage(repo.disk_usage) %></td>
         <td>
           <%= link gettext("Show"), to: Routes.repository_path(@conn, :show, Repository.owner_slug(repo), Repository.splat(repo)), class: "btn btn-sm btn-primary" %>
         </td>
diff --git a/lib/kmxgit_web/templates/admin/user/index.html.heex b/lib/kmxgit_web/templates/admin/user/index.html.heex
index 2b1d9d3..5f9089b 100644
--- a/lib/kmxgit_web/templates/admin/user/index.html.heex
+++ b/lib/kmxgit_web/templates/admin/user/index.html.heex
@@ -14,6 +14,7 @@
       <th><%= gettext "Name" %></th>
       <th><%= gettext "Email" %></th>
       <th><%= gettext "Login" %></th>
+      <th><%= gettext "Disk usage" %></th>
       <th><%= gettext "2FA" %></th>
       <th><%= gettext "Admin" %></th>
       <th><%= gettext "Deploy" %></th>
@@ -25,6 +26,7 @@
         <td><%= link user.name, to: Routes.admin_user_path(@conn, :show, user) %></td>
         <td><%= link user.email, to: "mailto:#{user.email}" %></td>
         <td><%= link User.login(user), to: Routes.admin_user_path(@conn, :show, user) %></td>
+        <td><%= disk_usage(user.disk_usage) %></td>
         <td><%= if user.totp_last != 0, do: "TOTP", else: "none" %></td>
         <td><%= user.is_admin %></td>
         <td><%= user.deploy_only %></td>