Added library size to homepage (#264)
This commit is contained in:
@@ -13,4 +13,27 @@ defmodule Pinchflat.Utils.NumberUtils do
|
|||||||
|> max(minimum)
|
|> max(minimum)
|
||||||
|> min(maximum)
|
|> min(maximum)
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
@@ -20,14 +20,14 @@ defmodule PinchflatWeb.Pages.PageController do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp render_home_page(conn) do
|
defp render_home_page(conn) do
|
||||||
|
downloaded_media_items = where(MediaQuery.new(), ^MediaQuery.downloaded())
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> render(:home,
|
|> render(:home,
|
||||||
media_profile_count: Repo.aggregate(MediaProfile, :count, :id),
|
media_profile_count: Repo.aggregate(MediaProfile, :count, :id),
|
||||||
source_count: Repo.aggregate(Source, :count, :id),
|
source_count: Repo.aggregate(Source, :count, :id),
|
||||||
media_item_count:
|
media_item_size: Repo.aggregate(downloaded_media_items, :sum, :media_size_bytes),
|
||||||
MediaQuery.new()
|
media_item_count: Repo.aggregate(downloaded_media_items, :count, :id)
|
||||||
|> where(^MediaQuery.downloaded())
|
|
||||||
|> Repo.aggregate(:count, :id)
|
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,13 @@
|
|||||||
defmodule PinchflatWeb.Pages.PageHTML do
|
defmodule PinchflatWeb.Pages.PageHTML do
|
||||||
use PinchflatWeb, :html
|
use PinchflatWeb, :html
|
||||||
|
|
||||||
|
alias Pinchflat.Utils.NumberUtils
|
||||||
|
|
||||||
embed_templates "page_html/*"
|
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
|
end
|
||||||
|
|||||||
@@ -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">
|
<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">
|
<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>
|
<span class="text-md font-medium">Media Profile(s)</span>
|
||||||
@@ -23,6 +23,14 @@
|
|||||||
</h4>
|
</h4>
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</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>
|
||||||
|
|
||||||
<div class="rounded-sm border shadow-default border-strokedark bg-boxdark mt-4 p-5">
|
<div class="rounded-sm border shadow-default border-strokedark bg-boxdark mt-4 p-5">
|
||||||
|
|||||||
@@ -16,4 +16,35 @@ defmodule Pinchflat.Utils.NumberUtilsTest do
|
|||||||
assert NumberUtils.clamp(2, 1, 3) == 2
|
assert NumberUtils.clamp(2, 1, 3) == 2
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user