diff --git a/lib/kmxgit/git.ex b/lib/kmxgit/git.ex
index 24092c7..9bca9f0 100644
--- a/lib/kmxgit/git.ex
+++ b/lib/kmxgit/git.ex
@@ -10,6 +10,52 @@ defmodule Kmxgit.Git do
"#{@git_root}/#{repo}.git"
end
+ def du_ks(path) do
+ {out, status} = System.cmd("du", ["-ks", path], stderr_to_stdout: true)
+ case status do
+ 0 ->
+ {k, _} = Integer.parse(out)
+ k
+ x ->
+ IO.inspect(x)
+ 0
+ end
+ end
+
+ def du_ks_(path) do
+ if File.dir?(path) do
+ case File.ls(path) do
+ {:ok, files} ->
+ Enum.reduce(files, 0, fn file, acc ->
+ if file != "." && file != ".." do
+ du_ks("#{path}/#{file}") + acc
+ else
+ acc
+ end
+ end)
+ {:error, err} ->
+ IO.inspect(err)
+ 0
+ end
+ else
+ case File.lstat(path, time: :posix) do
+ {:ok, stat} -> stat.size / 1024
+ {:error, err} ->
+ IO.inspect(err)
+ 0
+ end
+ end
+ end
+
+ def dir_disk_usage(dir) do
+ du_ks("#{@git_root}/#{dir}")
+ end
+
+ def disk_usage(repo) do
+ git_dir(repo)
+ |> du_ks()
+ end
+
# NIFs
def branches(repo) do
diff --git a/lib/kmxgit/git_manager.ex b/lib/kmxgit/git_manager.ex
index 2f0fc23..54cce2f 100644
--- a/lib/kmxgit/git_manager.ex
+++ b/lib/kmxgit/git_manager.ex
@@ -299,6 +299,7 @@ defmodule Kmxgit.GitManager do
end
end
+ @deprecated "use Kmxgit.Git instead"
def du_ks(path) do
{out, status} = System.cmd("du", ["-ks", path], stderr_to_stdout: true)
case status do
@@ -311,10 +312,12 @@ defmodule Kmxgit.GitManager do
end
end
+ @deprecated "use Kmxgit.Git instead"
def dir_disk_usage(dir) do
du_ks("#{@git_root}/#{dir}")
end
+ @deprecated "use Kmxgit.Git instead"
def disk_usage(repo) do
git_dir(repo)
|> du_ks()
diff --git a/lib/kmxgit/organisation_manager/organisation.ex b/lib/kmxgit/organisation_manager/organisation.ex
index 90f30a1..b966b54 100644
--- a/lib/kmxgit/organisation_manager/organisation.ex
+++ b/lib/kmxgit/organisation_manager/organisation.ex
@@ -3,7 +3,7 @@ defmodule Kmxgit.OrganisationManager.Organisation do
use Ecto.Schema
import Ecto.Changeset
- alias Kmxgit.GitManager
+ alias Kmxgit.Git
alias Kmxgit.RepositoryManager.Repository
alias Kmxgit.SlugManager.Slug
alias Kmxgit.UserManager.User
@@ -46,7 +46,7 @@ defmodule Kmxgit.OrganisationManager.Organisation do
def disk_usage(org) do
if org do
- GitManager.dir_disk_usage(org.slug.slug)
+ Git.dir_disk_usage(org.slug.slug)
else
0
end
diff --git a/lib/kmxgit/repository_manager/repository.ex b/lib/kmxgit/repository_manager/repository.ex
index 8df055b..87fcb51 100644
--- a/lib/kmxgit/repository_manager/repository.ex
+++ b/lib/kmxgit/repository_manager/repository.ex
@@ -3,7 +3,7 @@ defmodule Kmxgit.RepositoryManager.Repository do
use Ecto.Schema
import Ecto.Changeset
- alias Kmxgit.GitManager
+ alias Kmxgit.Git
alias Kmxgit.RepositoryManager
alias Kmxgit.OrganisationManager.Organisation
alias Kmxgit.UserManager.User
@@ -211,7 +211,7 @@ defmodule Kmxgit.RepositoryManager.Repository do
end
def disk_usage(repo = %__MODULE__{}) do
- GitManager.disk_usage(full_slug(repo))
+ Git.disk_usage(full_slug(repo))
end
def disk_usage(repos) when is_list(repos) do
repos
diff --git a/lib/kmxgit/user_manager/user.ex b/lib/kmxgit/user_manager/user.ex
index 78910f5..5a6a4fd 100644
--- a/lib/kmxgit/user_manager/user.ex
+++ b/lib/kmxgit/user_manager/user.ex
@@ -2,7 +2,7 @@ defmodule Kmxgit.UserManager.User do
use Ecto.Schema
import Ecto.Changeset
- alias Kmxgit.GitManager
+ alias Kmxgit.Git
alias Kmxgit.OrganisationManager.Organisation
alias Kmxgit.RepositoryManager.Repository
alias Kmxgit.SlugManager.Slug
@@ -273,6 +273,6 @@ defmodule Kmxgit.UserManager.User do
end
def disk_usage(user) do
- GitManager.dir_disk_usage(login(user))
+ Git.dir_disk_usage(login(user))
end
end
diff --git a/lib/kmxgit_web.ex b/lib/kmxgit_web.ex
index 5c56545..46c5b7d 100644
--- a/lib/kmxgit_web.ex
+++ b/lib/kmxgit_web.ex
@@ -58,9 +58,12 @@ defmodule KmxgitWeb do
alias Kmxgit.UserManager.User
def disk_usage(size) do
+ units = {:kb, :mb, :gb, :tb}
+ index = min(3, trunc(:math.log2(abs(size)) / 10))
+ unit = elem(units, index)
FileSize.new(size, :kb)
- |> FileSize.convert(:mb)
- |> FileSize.to_string()
+ |> FileSize.convert(unit)
+ |> FileSize.format()
end
def recaptcha_site_key do
diff --git a/lib/kmxgit_web/controllers/admin/dashboard_controller.ex b/lib/kmxgit_web/controllers/admin/dashboard_controller.ex
index 56f2e63..206d1d1 100644
--- a/lib/kmxgit_web/controllers/admin/dashboard_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/dashboard_controller.ex
@@ -1,14 +1,14 @@
defmodule KmxgitWeb.Admin.DashboardController do
use KmxgitWeb, :controller
- alias Kmxgit.GitManager
+ alias Kmxgit.Git
alias Kmxgit.OrganisationManager
alias Kmxgit.RepositoryManager
alias Kmxgit.UserManager
def index(conn, _params) do
conn
- |> assign(:disk_usage, GitManager.du_ks("priv/git/"))
+ |> assign(:disk_usage, Git.du_ks("priv/git/"))
|> assign(:org_count, OrganisationManager.count_organisations())
|> assign(:page_title, gettext "Dashboard")
|> assign(:repo_count, RepositoryManager.count_repositories())
diff --git a/lib/kmxgit_web/controllers/page_controller.ex b/lib/kmxgit_web/controllers/page_controller.ex
index b81d90f..8322694 100644
--- a/lib/kmxgit_web/controllers/page_controller.ex
+++ b/lib/kmxgit_web/controllers/page_controller.ex
@@ -3,7 +3,7 @@ defmodule KmxgitWeb.PageController do
require Logger
- alias Kmxgit.GitManager
+ alias Kmxgit.Git
alias Kmxgit.OrganisationManager
alias Kmxgit.RepositoryManager
alias Kmxgit.RepositoryManager.Repository
@@ -34,7 +34,7 @@ defmodule KmxgitWeb.PageController do
else
conn
|> assign(:discord, Application.get_env(:kmxgit, :discord))
- |> assign(:disk_usage, GitManager.du_ks("priv/git/"))
+ |> assign(:disk_usage, Git.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())