From 82729085d2fd236d063a0bd76297532849577305 Mon Sep 17 00:00:00 2001 From: Kieran Eglin Date: Fri, 13 Dec 2024 09:23:16 -0800 Subject: [PATCH] Added size to sources table --- .../custom_components/text_components.ex | 21 +++++++++++++++++++ .../controllers/pages/page_html.ex | 18 ---------------- .../pages/page_html/home.html.heex | 2 +- .../sources/source_live/index_table_live.ex | 18 ++++++++-------- .../source_live/index_table_live.html.heex | 9 ++------ 5 files changed, 33 insertions(+), 35 deletions(-) diff --git a/lib/pinchflat_web/components/custom_components/text_components.ex b/lib/pinchflat_web/components/custom_components/text_components.ex index d2bc8f6..2145bae 100644 --- a/lib/pinchflat_web/components/custom_components/text_components.ex +++ b/lib/pinchflat_web/components/custom_components/text_components.ex @@ -2,6 +2,7 @@ defmodule PinchflatWeb.CustomComponents.TextComponents do @moduledoc false use Phoenix.Component + alias Pinchflat.Utils.NumberUtils alias PinchflatWeb.CoreComponents @doc """ @@ -125,4 +126,24 @@ defmodule PinchflatWeb.CustomComponents.TextComponents do {@word}{if @count == 1, do: "", else: @suffix} """ end + + @doc """ + Renders a human-readable byte size + """ + + attr :byte_size, :integer, required: true + + def readable_filesize(assigns) do + {num, suffix} = NumberUtils.human_byte_size(assigns.byte_size, precision: 2) + + assigns = + Map.merge(assigns, %{ + num: num, + suffix: suffix + }) + + ~H""" + <.localized_number number={@num} /> {@suffix} + """ + end end diff --git a/lib/pinchflat_web/controllers/pages/page_html.ex b/lib/pinchflat_web/controllers/pages/page_html.ex index f4f2665..16f7731 100644 --- a/lib/pinchflat_web/controllers/pages/page_html.ex +++ b/lib/pinchflat_web/controllers/pages/page_html.ex @@ -1,23 +1,5 @@ defmodule PinchflatWeb.Pages.PageHTML do use PinchflatWeb, :html - alias Pinchflat.Utils.NumberUtils - embed_templates "page_html/*" - - attr :media_filesize, :integer, required: true - - def readable_media_filesize(assigns) do - {num, suffix} = NumberUtils.human_byte_size(assigns.media_filesize, precision: 2) - - assigns = - Map.merge(assigns, %{ - num: num, - suffix: suffix - }) - - ~H""" - <.localized_number number={@num} /> {@suffix} - """ - end end diff --git a/lib/pinchflat_web/controllers/pages/page_html/home.html.heex b/lib/pinchflat_web/controllers/pages/page_html/home.html.heex index bd74446..1d7b50c 100644 --- a/lib/pinchflat_web/controllers/pages/page_html/home.html.heex +++ b/lib/pinchflat_web/controllers/pages/page_html/home.html.heex @@ -33,7 +33,7 @@ Library Size

- <.readable_media_filesize media_filesize={@media_item_size} /> + <.readable_filesize byte_size={@media_item_size} />

diff --git a/lib/pinchflat_web/controllers/sources/source_live/index_table_live.ex b/lib/pinchflat_web/controllers/sources/source_live/index_table_live.ex index ad636e3..5ada1b0 100644 --- a/lib/pinchflat_web/controllers/sources/source_live/index_table_live.ex +++ b/lib/pinchflat_web/controllers/sources/source_live/index_table_live.ex @@ -51,13 +51,12 @@ defmodule PinchflatWeb.Sources.SourceLive.IndexTableLive do |> then(&{:noreply, &1}) end - defp sort_attr(:pending_count), do: dynamic([s, mp, dl, pe], field(pe, :pending_count)) - defp sort_attr(:downloaded_count), do: dynamic([s, mp, dl], field(dl, :downloaded_count)) - defp sort_attr(:media_profile_name), do: dynamic([s, mp], field(mp, :name)) - defp sort_attr(:custom_name), do: dynamic([s], field(s, :custom_name)) - defp sort_attr(:enabled), do: dynamic([s], field(s, :enabled)) - defp sort_attr(:retention_period_days), do: dynamic([s], field(s, :retention_period_days)) - defp sort_attr(_), do: sort_attr(:custom_name) + defp sort_attr(:pending_count), do: dynamic([s, mp, dl, pe], pe.pending_count) + defp sort_attr(:downloaded_count), do: dynamic([s, mp, dl], dl.downloaded_count) + defp sort_attr(:media_size_bytes), do: dynamic([s, mp, dl], dl.media_size_bytes) + defp sort_attr(:media_profile_name), do: dynamic([s, mp], mp.name) + defp sort_attr(:custom_name), do: dynamic([s], s.custom_name) + defp sort_attr(:enabled), do: dynamic([s], s.enabled) defp set_sources(%{assigns: assigns} = socket) do sources = @@ -74,7 +73,7 @@ defmodule PinchflatWeb.Sources.SourceLive.IndexTableLive do downloaded_subquery = from( m in MediaItem, - select: %{downloaded_count: count(m.id), source_id: m.source_id}, + select: %{downloaded_count: count(m.id), source_id: m.source_id, media_size_bytes: sum(m.media_size_bytes)}, where: ^MediaQuery.downloaded(), group_by: m.source_id ) @@ -102,7 +101,8 @@ defmodule PinchflatWeb.Sources.SourceLive.IndexTableLive do select: map(s, ^Source.__schema__(:fields)), select_merge: %{ downloaded_count: coalesce(d.downloaded_count, 0), - pending_count: coalesce(p.pending_count, 0) + pending_count: coalesce(p.pending_count, 0), + media_size_bytes: coalesce(d.media_size_bytes, 0) } end end diff --git a/lib/pinchflat_web/controllers/sources/source_live/index_table_live.html.heex b/lib/pinchflat_web/controllers/sources/source_live/index_table_live.html.heex index 254b5fa..cf2ce18 100644 --- a/lib/pinchflat_web/controllers/sources/source_live/index_table_live.html.heex +++ b/lib/pinchflat_web/controllers/sources/source_live/index_table_live.html.heex @@ -15,13 +15,8 @@ <.localized_number number={source.downloaded_count} /> - <:col :let={source} label="Retention" sort_key="retention_period_days"> - <%= if source.retention_period_days && source.retention_period_days > 0 do %> - <.localized_number number={source.retention_period_days} /> - <.pluralize count={source.retention_period_days} word="day" /> - <% else %> - - <% end %> + <:col :let={source} label="Size" sort_key="media_size_bytes"> + <.readable_filesize byte_size={source.media_size_bytes} /> <:col :let={source} label="Media Profile" sort_key="media_profile_name"> <.subtle_link href={~p"/media_profiles/#{source.media_profile_id}"}>