diff --git a/lib/music_library/listening_stats.ex b/lib/music_library/listening_stats.ex index d84afe3f..d0047c6f 100644 --- a/lib/music_library/listening_stats.ex +++ b/lib/music_library/listening_stats.ex @@ -26,6 +26,14 @@ defmodule MusicLibrary.ListeningStats do Repo.aggregate(Track, :count, :scrobbled_at_uts) end + @spec artist_play_count(String.t()) :: non_neg_integer() + def artist_play_count(artist_musicbrainz_id) do + from(t in Track, + where: fragment("json_extract(?, '$.musicbrainz_id')", t.artist) == ^artist_musicbrainz_id + ) + |> Repo.aggregate(:count) + end + @spec recent_activity(String.t(), non_neg_integer()) :: map() def recent_activity(timezone, limit \\ 100) do # When we get recent tracks, we need to: diff --git a/lib/music_library_web/live/artist_live/show.ex b/lib/music_library_web/live/artist_live/show.ex index 4fa8d3cb..d6028877 100644 --- a/lib/music_library_web/live/artist_live/show.ex +++ b/lib/music_library_web/live/artist_live/show.ex @@ -4,7 +4,7 @@ defmodule MusicLibraryWeb.ArtistLive.Show do import MusicLibraryWeb.RecordComponents, only: [record_grid: 1, country_label: 1, artist_image: 1] - alias MusicLibrary.{Artists, Chats, Records} + alias MusicLibrary.{Artists, Chats, ListeningStats, Records} alias MusicLibrary.Artists.ArtistInfo alias MusicLibraryWeb.ArtistLive.Biography alias MusicLibraryWeb.ErrorMessages @@ -210,24 +210,7 @@ defmodule MusicLibraryWeb.ArtistLive.Show do
- <.async_result :let={lastfm_artist_info} assign={@lastfm_artist_info}> - <:loading> - {gettext("Loading play count")} - <.loading /> - - <:failed :let={_failure}> -
- <.icon - name="hero-exclamation-triangle" - class="-mt-1 mr-1 ml-2 size-5" - aria-hidden="true" - data-slot="icon" - /> - {gettext("Error loading play count")} -
- - <.play_count play_count={lastfm_artist_info.play_count} /> - + <.play_count play_count={@play_count} />
@@ -664,6 +647,7 @@ defmodule MusicLibraryWeb.ArtistLive.Show do |> assign(:current_section, :artists) |> assign(:artist, artist) |> assign(:artist_info, artist_info) + |> assign(:play_count, ListeningStats.artist_play_count(musicbrainz_id)) |> assign(:chat_count, Chats.count_chats(:artist, musicbrainz_id)) |> assign(:biography, Biography.build(artist_info)) |> assign(:external_links, ArtistInfo.external_links(artist_info)) diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index cff60a37..c242992a 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -299,11 +299,6 @@ msgstr "" msgid "Loading biography" msgstr "" -#: lib/music_library_web/live/artist_live/show.ex -#, elixir-autogen, elixir-format -msgid "Loading play count" -msgstr "" - #: lib/music_library_web/live/collection_live/show.ex #: lib/music_library_web/live/wishlist_live/show.ex #, elixir-autogen, elixir-format @@ -333,11 +328,6 @@ msgstr "" msgid "Error loading biography" msgstr "" -#: lib/music_library_web/live/artist_live/show.ex -#, elixir-autogen, elixir-format -msgid "Error loading play count" -msgstr "" - #: lib/music_library_web/live/collection_live/index.ex #: lib/music_library_web/live/record_set_live/index.ex #: lib/music_library_web/live/scrobble_rules_live/index.ex diff --git a/priv/gettext/en/LC_MESSAGES/default.po b/priv/gettext/en/LC_MESSAGES/default.po index 6c4cd050..bea005f1 100644 --- a/priv/gettext/en/LC_MESSAGES/default.po +++ b/priv/gettext/en/LC_MESSAGES/default.po @@ -299,11 +299,6 @@ msgstr "" msgid "Loading biography" msgstr "" -#: lib/music_library_web/live/artist_live/show.ex -#, elixir-autogen, elixir-format -msgid "Loading play count" -msgstr "" - #: lib/music_library_web/live/collection_live/show.ex #: lib/music_library_web/live/wishlist_live/show.ex #, elixir-autogen, elixir-format @@ -333,11 +328,6 @@ msgstr "" msgid "Error loading biography" msgstr "" -#: lib/music_library_web/live/artist_live/show.ex -#, elixir-autogen, elixir-format -msgid "Error loading play count" -msgstr "" - #: lib/music_library_web/live/collection_live/index.ex #: lib/music_library_web/live/record_set_live/index.ex #: lib/music_library_web/live/scrobble_rules_live/index.ex diff --git a/test/music_library_web/live/artist_live/show_test.exs b/test/music_library_web/live/artist_live/show_test.exs index 41f35297..7024051a 100644 --- a/test/music_library_web/live/artist_live/show_test.exs +++ b/test/music_library_web/live/artist_live/show_test.exs @@ -43,7 +43,7 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do conn |> visit(~p"/artists/#{artist_musicbrainz_id}") |> unwrap(&render_async/1) - |> assert_has("span", "123") + |> assert_has("span", "No scrobbles") |> assert_has("dt", "Biography") end @@ -64,9 +64,8 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do conn |> visit(~p"/artists/#{artist_musicbrainz_id}") |> unwrap(&render_async/1) - |> refute_has("span", "123") + |> assert_has("span", "No scrobbles") |> refute_has("summary", "Biography") - |> assert_has("div", "Error loading play count") |> assert_has("div", "Error loading biography") end