From a4670412df25b43968de01c36e44494ec98ba3f2 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Tue, 2 Sep 2025 08:17:42 +0300 Subject: [PATCH] Add total scrobble count to stats --- lib/music_library/scrobble_activity.ex | 4 ++++ lib/music_library_web/components/stats_components.ex | 9 ++++++++- lib/music_library_web/live/stats_live/index.ex | 3 +++ lib/music_library_web/live/stats_live/index.html.heex | 5 +++-- priv/gettext/default.pot | 5 +++++ priv/gettext/en/LC_MESSAGES/default.po | 5 +++++ 6 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/music_library/scrobble_activity.ex b/lib/music_library/scrobble_activity.ex index 54eb478e..a0276a6d 100644 --- a/lib/music_library/scrobble_activity.ex +++ b/lib/music_library/scrobble_activity.ex @@ -107,6 +107,10 @@ defmodule MusicLibrary.ScrobbleActivity do defp main_artist_name([]), do: nil defp main_artist_name([artist | _rest]), do: artist.name + def scrobble_count do + Repo.aggregate(Track, :count, :scrobbled_at_uts) + end + def from_recent_tracks(recent_tracks, timezone) do all_artist_pairs = Artists.get_all_artist_pairs() recent_release_ids = recent_release_ids(recent_tracks) diff --git a/lib/music_library_web/components/stats_components.ex b/lib/music_library_web/components/stats_components.ex index b2df0981..5dbbf280 100644 --- a/lib/music_library_web/components/stats_components.ex +++ b/lib/music_library_web/components/stats_components.ex @@ -48,7 +48,7 @@ defmodule MusicLibraryWeb.StatsComponents do attr :title, :string, required: true attr :count, :integer, required: true - attr :path, :string, required: true + attr :path, :string, required: false, default: nil def counter(assigns) do ~H""" @@ -60,11 +60,18 @@ defmodule MusicLibraryWeb.StatsComponents do
<.link + :if={@path} navigate={@path} class="block text-2xl sm:text-3xl font-semibold text-center text-zinc-900 hover:text-zinc-500 dark:text-zinc-300 dark:hover:text-zinc-200" > {@count} +

+ {@count} +

""" diff --git a/lib/music_library_web/live/stats_live/index.ex b/lib/music_library_web/live/stats_live/index.ex index 425ca979..bb18a038 100644 --- a/lib/music_library_web/live/stats_live/index.ex +++ b/lib/music_library_web/live/stats_live/index.ex @@ -135,6 +135,8 @@ defmodule MusicLibraryWeb.StatsLive.Index do artist_ids: artist_ids } = ScrobbleActivity.from_recent_tracks(recent_tracks, socket.assigns.timezone) + scrobble_count = ScrobbleActivity.scrobble_count() + last_updated_uts = if track = List.first(localized_recent_tracks) do track.scrobbled_at_uts @@ -142,6 +144,7 @@ defmodule MusicLibraryWeb.StatsLive.Index do socket |> assign(:last_updated_uts, last_updated_uts) + |> assign(:scrobble_count, scrobble_count) |> stream(:recent_tracks, localized_recent_tracks, reset: true) |> stream(:recent_albums, recent_albums, reset: true) |> assign( diff --git a/lib/music_library_web/live/stats_live/index.html.heex b/lib/music_library_web/live/stats_live/index.html.heex index 7087a314..49c590c0 100644 --- a/lib/music_library_web/live/stats_live/index.html.heex +++ b/lib/music_library_web/live/stats_live/index.html.heex @@ -3,11 +3,11 @@ {gettext("Records")} <%!-- TODO: replace with OSS version --%> -
+
<.album_preview record={@latest_record} title={gettext("Latest purchase")} - class="col-span-2 sm:col-span-3" + class="col-span-3 sm:col-span-2" /> <.counter title={gettext("Total collection")} @@ -15,6 +15,7 @@ path={~p"/collection"} /> <.counter title={gettext("Total wishlist")} count={@wishlist_count} path={~p"/wishlist"} /> + <.counter title={gettext("Total scrobbles")} count={@scrobble_count} />
diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index a90d8dba..c12fa190 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -1308,3 +1308,8 @@ msgstr[1] "" #, elixir-autogen, elixir-format msgid "Search (Cmd/Ctrl+K)" msgstr "" + +#: lib/music_library_web/live/stats_live/index.html.heex +#, elixir-autogen, elixir-format +msgid "Total scrobbles" +msgstr "" diff --git a/priv/gettext/en/LC_MESSAGES/default.po b/priv/gettext/en/LC_MESSAGES/default.po index e3581309..f4460f4e 100644 --- a/priv/gettext/en/LC_MESSAGES/default.po +++ b/priv/gettext/en/LC_MESSAGES/default.po @@ -1308,3 +1308,8 @@ msgstr[1] "" #, elixir-autogen, elixir-format, fuzzy msgid "Search (Cmd/Ctrl+K)" msgstr "" + +#: lib/music_library_web/live/stats_live/index.html.heex +#, elixir-autogen, elixir-format +msgid "Total scrobbles" +msgstr ""