Compute artist play count from db

This commit is contained in:
Claudio Ortolina
2026-03-24 15:07:50 +00:00
parent d5be6e6182
commit c9283d7f28
5 changed files with 13 additions and 42 deletions
+8
View File
@@ -26,6 +26,14 @@ defmodule MusicLibrary.ListeningStats do
Repo.aggregate(Track, :count, :scrobbled_at_uts) Repo.aggregate(Track, :count, :scrobbled_at_uts)
end 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() @spec recent_activity(String.t(), non_neg_integer()) :: map()
def recent_activity(timezone, limit \\ 100) do def recent_activity(timezone, limit \\ 100) do
# When we get recent tracks, we need to: # When we get recent tracks, we need to:
+3 -19
View File
@@ -4,7 +4,7 @@ defmodule MusicLibraryWeb.ArtistLive.Show do
import MusicLibraryWeb.RecordComponents, import MusicLibraryWeb.RecordComponents,
only: [record_grid: 1, country_label: 1, artist_image: 1] 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 MusicLibrary.Artists.ArtistInfo
alias MusicLibraryWeb.ArtistLive.Biography alias MusicLibraryWeb.ArtistLive.Biography
alias MusicLibraryWeb.ErrorMessages alias MusicLibraryWeb.ErrorMessages
@@ -210,24 +210,7 @@ defmodule MusicLibraryWeb.ArtistLive.Show do
</div> </div>
<div class="mt-4 flex items-center justify-between"> <div class="mt-4 flex items-center justify-between">
<.async_result :let={lastfm_artist_info} assign={@lastfm_artist_info}> <.play_count play_count={@play_count} />
<:loading>
<span class="sr-only">{gettext("Loading play count")}</span>
<.loading />
</:loading>
<:failed :let={_failure}>
<div class="mt-4 text-sm/5 text-zinc-500 dark:text-zinc-400">
<.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")}
</div>
</:failed>
<.play_count play_count={lastfm_artist_info.play_count} />
</.async_result>
</div> </div>
</header> </header>
@@ -664,6 +647,7 @@ defmodule MusicLibraryWeb.ArtistLive.Show do
|> assign(:current_section, :artists) |> assign(:current_section, :artists)
|> assign(:artist, artist) |> assign(:artist, artist)
|> assign(:artist_info, artist_info) |> assign(:artist_info, artist_info)
|> assign(:play_count, ListeningStats.artist_play_count(musicbrainz_id))
|> assign(:chat_count, Chats.count_chats(:artist, musicbrainz_id)) |> assign(:chat_count, Chats.count_chats(:artist, musicbrainz_id))
|> assign(:biography, Biography.build(artist_info)) |> assign(:biography, Biography.build(artist_info))
|> assign(:external_links, ArtistInfo.external_links(artist_info)) |> assign(:external_links, ArtistInfo.external_links(artist_info))
-10
View File
@@ -299,11 +299,6 @@ msgstr ""
msgid "Loading biography" msgid "Loading biography"
msgstr "" 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/collection_live/show.ex
#: lib/music_library_web/live/wishlist_live/show.ex #: lib/music_library_web/live/wishlist_live/show.ex
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
@@ -333,11 +328,6 @@ msgstr ""
msgid "Error loading biography" msgid "Error loading biography"
msgstr "" 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/collection_live/index.ex
#: lib/music_library_web/live/record_set_live/index.ex #: lib/music_library_web/live/record_set_live/index.ex
#: lib/music_library_web/live/scrobble_rules_live/index.ex #: lib/music_library_web/live/scrobble_rules_live/index.ex
-10
View File
@@ -299,11 +299,6 @@ msgstr ""
msgid "Loading biography" msgid "Loading biography"
msgstr "" 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/collection_live/show.ex
#: lib/music_library_web/live/wishlist_live/show.ex #: lib/music_library_web/live/wishlist_live/show.ex
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
@@ -333,11 +328,6 @@ msgstr ""
msgid "Error loading biography" msgid "Error loading biography"
msgstr "" 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/collection_live/index.ex
#: lib/music_library_web/live/record_set_live/index.ex #: lib/music_library_web/live/record_set_live/index.ex
#: lib/music_library_web/live/scrobble_rules_live/index.ex #: lib/music_library_web/live/scrobble_rules_live/index.ex
@@ -43,7 +43,7 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
conn conn
|> visit(~p"/artists/#{artist_musicbrainz_id}") |> visit(~p"/artists/#{artist_musicbrainz_id}")
|> unwrap(&render_async/1) |> unwrap(&render_async/1)
|> assert_has("span", "123") |> assert_has("span", "No scrobbles")
|> assert_has("dt", "Biography") |> assert_has("dt", "Biography")
end end
@@ -64,9 +64,8 @@ defmodule MusicLibraryWeb.ArtistLive.ShowTest do
conn conn
|> visit(~p"/artists/#{artist_musicbrainz_id}") |> visit(~p"/artists/#{artist_musicbrainz_id}")
|> unwrap(&render_async/1) |> unwrap(&render_async/1)
|> refute_has("span", "123") |> assert_has("span", "No scrobbles")
|> refute_has("summary", "Biography") |> refute_has("summary", "Biography")
|> assert_has("div", "Error loading play count")
|> assert_has("div", "Error loading biography") |> assert_has("div", "Error loading biography")
end end