Added library size to homepage (#264)

This commit is contained in:
Kieran 2024-05-27 10:43:50 -07:00 committed by GitHub
parent 3a5c06fe64
commit b2e16d50cd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 75 additions and 5 deletions

View file

@ -13,4 +13,27 @@ defmodule Pinchflat.Utils.NumberUtils do
|> max(minimum)
|> min(maximum)
end
@doc """
Converts a number to a human readable byte size. Can take a precision
option to specify the number of decimal places to round to.
Returns {integer(), String.t()}
"""
def human_byte_size(number, opts \\ [])
def human_byte_size(nil, opts), do: human_byte_size(0, opts)
def human_byte_size(number, opts) do
precision = Keyword.get(opts, :precision, 2)
suffixes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
base = 1024
Enum.reduce_while(suffixes, {number / 1.0, "B"}, fn suffix, {value, _} ->
if value < base do
{:halt, {Float.round(value, precision), suffix}}
else
{:cont, {value / base, suffix}}
end
end)
end
end

View file

@ -20,14 +20,14 @@ defmodule PinchflatWeb.Pages.PageController do
end
defp render_home_page(conn) do
downloaded_media_items = where(MediaQuery.new(), ^MediaQuery.downloaded())
conn
|> render(:home,
media_profile_count: Repo.aggregate(MediaProfile, :count, :id),
source_count: Repo.aggregate(Source, :count, :id),
media_item_count:
MediaQuery.new()
|> where(^MediaQuery.downloaded())
|> Repo.aggregate(:count, :id)
media_item_size: Repo.aggregate(downloaded_media_items, :sum, :media_size_bytes),
media_item_count: Repo.aggregate(downloaded_media_items, :count, :id)
)
end

View file

@ -1,5 +1,13 @@
defmodule PinchflatWeb.Pages.PageHTML do
use PinchflatWeb, :html
alias Pinchflat.Utils.NumberUtils
embed_templates "page_html/*"
def readable_media_filesize(media_filesize) do
{num, suffix} = NumberUtils.human_byte_size(media_filesize, precision: 1)
"#{Float.round(num)} #{suffix}"
end
end

View file

@ -1,4 +1,4 @@
<div class="grid grid-cols-1 gap-4 md:grid-cols-3">
<div class="grid grid-cols-1 gap-4 md:grid-cols-4">
<div class="rounded-sm border px-7.5 py-6 shadow-default border-strokedark bg-boxdark">
<a href={~p"/media_profiles"} class="mt-4 flex flex-col items-center justify-center">
<span class="text-md font-medium">Media Profile(s)</span>
@ -23,6 +23,14 @@
</h4>
</span>
</div>
<div class="rounded-sm border px-7.5 py-6 shadow-default border-strokedark bg-boxdark">
<span class="mt-4 flex flex-col items-center justify-center">
<span class="text-md font-medium">Library Size</span>
<h4 class="text-title-md font-bold text-white">
<%= readable_media_filesize(@media_item_size) %>
</h4>
</span>
</div>
</div>
<div class="rounded-sm border shadow-default border-strokedark bg-boxdark mt-4 p-5">

View file

@ -16,4 +16,35 @@ defmodule Pinchflat.Utils.NumberUtilsTest do
assert NumberUtils.clamp(2, 1, 3) == 2
end
end
describe "human_byte_size/1" do
test "converts byte size to human readable format" do
assert NumberUtils.human_byte_size(1024) == {1, "KB"}
assert NumberUtils.human_byte_size(1024 * 1024) == {1, "MB"}
assert NumberUtils.human_byte_size(1024 * 1024 * 1024) == {1, "GB"}
assert NumberUtils.human_byte_size(1024 * 1024 * 1024 * 1024) == {1, "TB"}
assert NumberUtils.human_byte_size(1024 * 1024 * 1024 * 1024 * 1024) == {1, "PB"}
assert NumberUtils.human_byte_size(1024 * 1024 * 1024 * 1024 * 1024 * 1024) == {1, "EB"}
assert NumberUtils.human_byte_size(1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024) == {1, "ZB"}
assert NumberUtils.human_byte_size(1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024) == {1, "YB"}
end
test "returns the number when it is less than 1024" do
assert NumberUtils.human_byte_size(512) == {512, "B"}
end
test "optionally takes a precision" do
assert NumberUtils.human_byte_size(1234 * 1024, precision: 0) == {1, "MB"}
assert NumberUtils.human_byte_size(1234 * 1024, precision: 1) == {1.2, "MB"}
assert NumberUtils.human_byte_size(1234 * 1024, precision: 2) == {1.21, "MB"}
end
test "handles 0's well" do
assert NumberUtils.human_byte_size(0) == {0, "B"}
end
test "handles nil well" do
assert NumberUtils.human_byte_size(nil) == {0, "B"}
end
end
end