From f47cbc77f5b113b1ccdc0d29ffb48448192e5a3f Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Fri, 29 Aug 2025 12:20:46 +0300 Subject: [PATCH] Add "On this day" section to stats --- lib/music_library/collection.ex | 13 +++ .../components/stats_components.ex | 88 ++++++++++++++++++- .../live/stats_live/index.ex | 2 + .../live/stats_live/index.html.heex | 11 ++- priv/gettext/default.pot | 8 ++ priv/gettext/en/LC_MESSAGES/default.po | 8 ++ 6 files changed, 128 insertions(+), 2 deletions(-) diff --git a/lib/music_library/collection.ex b/lib/music_library/collection.ex index 2c68ecad..50073284 100644 --- a/lib/music_library/collection.ex +++ b/lib/music_library/collection.ex @@ -40,6 +40,19 @@ defmodule MusicLibrary.Collection do Repo.all(q) end + def get_records_on_this_day(date \\ Date.utc_today()) do + month_day = Calendar.strftime(date, "%m-%d") + + q = + from r in Record, + where: not is_nil(r.purchased_at), + where: fragment("strftime('%m-%d', ?) = ?", r.release_date, ^month_day), + order_by: [desc: r.release_date], + select: ^Records.essential_fields() + + Repo.all(q) + end + def get_latest_record! do q = from r in Record, diff --git a/lib/music_library_web/components/stats_components.ex b/lib/music_library_web/components/stats_components.ex index 972125c9..b091454b 100644 --- a/lib/music_library_web/components/stats_components.ex +++ b/lib/music_library_web/components/stats_components.ex @@ -1,7 +1,11 @@ defmodule MusicLibraryWeb.StatsComponents do use MusicLibraryWeb, :live_component - attr :record, MusicLibrary.Records.Record, required: true + import MusicLibraryWeb.RecordComponents, only: [format_label: 1, type_label: 1, artist_links: 1] + + alias MusicLibrary.Records + + attr :record, Records.Record, required: true attr :title, :string, required: true attr :class, :string @@ -79,6 +83,88 @@ defmodule MusicLibraryWeb.StatsComponents do """ end + attr :record_show_path, :any, required: true + attr :records, :list, required: true + attr :current_date, Date, required: false, default: nil + + def records_on_this_day(assigns) do + ~H""" + + """ + end + def tracked_record?(tracked_releases, release_id) do Enum.find_value(tracked_releases, fn tracked_release -> if tracked_release.release_id == release_id, do: tracked_release.record_id diff --git a/lib/music_library_web/live/stats_live/index.ex b/lib/music_library_web/live/stats_live/index.ex index 3399b099..33631c0a 100644 --- a/lib/music_library_web/live/stats_live/index.ex +++ b/lib/music_library_web/live/stats_live/index.ex @@ -13,6 +13,7 @@ defmodule MusicLibraryWeb.StatsLive.Index do recent_tracks = LastFm.get_scrobbled_tracks() records_by_artists = Collection.count_records_by_artist(limit: 20) records_by_genre = Collection.count_records_by_genre(limit: 20) + records_on_this_day = Collection.get_records_on_this_day() if connected?(socket) do LastFm.subscribe_to_feed() @@ -27,6 +28,7 @@ defmodule MusicLibraryWeb.StatsLive.Index do |> stream_configure(:recent_albums, dom_id: fn album -> "album-#{album.scrobbled_at_uts}" end ) + |> stream(:records_on_this_day, records_on_this_day, reset: true) |> assign_counts() |> assign_scrobble_activity(recent_tracks) |> 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 9959d2b0..b129d7d3 100644 --- a/lib/music_library_web/live/stats_live/index.html.heex +++ b/lib/music_library_web/live/stats_live/index.html.heex @@ -487,7 +487,16 @@ -
+
+
+

+ {gettext("On This day")} +

+ <.records_on_this_day + records={@streams.records_on_this_day} + record_show_path={fn record -> ~p"/collection/#{record}" end} + /> +

{gettext("Top %{n} Artists", %{n: length(@records_by_artist)})} diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index 6527f20b..0cf3f82c 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -143,6 +143,7 @@ msgid "Purchased at" msgstr "" #: lib/music_library_web/components/record_components.ex +#: lib/music_library_web/components/stats_components.ex #: lib/music_library_web/live/collection_live/show.html.heex #, elixir-autogen, elixir-format msgid "Purchased on" @@ -434,6 +435,7 @@ msgid "ID" msgstr "" #: lib/music_library_web/components/record_components.ex +#: lib/music_library_web/components/stats_components.ex #: lib/music_library_web/live/wishlist_live/show.html.heex #, elixir-autogen, elixir-format msgid "Unreleased" @@ -905,6 +907,7 @@ msgid "Last year" msgstr "" #: lib/music_library_web/components/record_components.ex +#: lib/music_library_web/components/stats_components.ex #, elixir-autogen, elixir-format msgid "Wishlisted on" msgstr "" @@ -1295,3 +1298,8 @@ msgstr "" #, elixir-autogen, elixir-format msgid "Rotate colors" msgstr "" + +#: lib/music_library_web/live/stats_live/index.html.heex +#, elixir-autogen, elixir-format +msgid "On This day" +msgstr "" diff --git a/priv/gettext/en/LC_MESSAGES/default.po b/priv/gettext/en/LC_MESSAGES/default.po index 78bb1d17..214a09dd 100644 --- a/priv/gettext/en/LC_MESSAGES/default.po +++ b/priv/gettext/en/LC_MESSAGES/default.po @@ -143,6 +143,7 @@ msgid "Purchased at" msgstr "" #: lib/music_library_web/components/record_components.ex +#: lib/music_library_web/components/stats_components.ex #: lib/music_library_web/live/collection_live/show.html.heex #, elixir-autogen, elixir-format msgid "Purchased on" @@ -434,6 +435,7 @@ msgid "ID" msgstr "" #: lib/music_library_web/components/record_components.ex +#: lib/music_library_web/components/stats_components.ex #: lib/music_library_web/live/wishlist_live/show.html.heex #, elixir-autogen, elixir-format msgid "Unreleased" @@ -905,6 +907,7 @@ msgid "Last year" msgstr "" #: lib/music_library_web/components/record_components.ex +#: lib/music_library_web/components/stats_components.ex #, elixir-autogen, elixir-format, fuzzy msgid "Wishlisted on" msgstr "" @@ -1295,3 +1298,8 @@ msgstr "" #, elixir-autogen, elixir-format msgid "Rotate colors" msgstr "" + +#: lib/music_library_web/live/stats_live/index.html.heex +#, elixir-autogen, elixir-format +msgid "On This day" +msgstr ""