diff --git a/lib/music_library/collection.ex b/lib/music_library/collection.ex index 0c8d91bd..213cfa02 100644 --- a/lib/music_library/collection.ex +++ b/lib/music_library/collection.ex @@ -71,7 +71,9 @@ defmodule MusicLibrary.Collection do Repo.all(q) end - def count_records_by_artist(limit \\ 30) do + def count_records_by_artist(opts \\ []) do + limit = Keyword.get(opts, :limit, 30) + q = from r in fragment("records, json_each(records.artists)"), where: fragment("records.purchased_at IS NOT NULL"), @@ -87,7 +89,9 @@ defmodule MusicLibrary.Collection do Repo.all(q) end - def count_records_by_genre(limit \\ 30) do + def count_records_by_genre(opts \\ []) do + limit = Keyword.get(opts, :limit, 30) + q = from r in fragment("records, json_each(records.genres)"), where: fragment("records.purchased_at IS NOT NULL"), diff --git a/lib/music_library_web/live/stats_live/index.ex b/lib/music_library_web/live/stats_live/index.ex index e448a5ad..2b446608 100644 --- a/lib/music_library_web/live/stats_live/index.ex +++ b/lib/music_library_web/live/stats_live/index.ex @@ -10,6 +10,8 @@ defmodule MusicLibraryWeb.StatsLive.Index do def mount(_params, _session, socket) do latest_record = Collection.get_latest_record!() recent_tracks = LastFm.get_scrobbled_tracks() + records_by_artists = Collection.count_records_by_artist(limit: 50) + records_by_genre = Collection.count_records_by_genre(limit: 50) if connected?(socket) do LastFm.subscribe_to_feed() @@ -30,8 +32,8 @@ defmodule MusicLibraryWeb.StatsLive.Index do latest_record: latest_record, page_title: gettext("Stats"), nav_section: :stats, - records_by_artist: Collection.count_records_by_artist(), - records_by_genre: Collection.count_records_by_genre() + records_by_artist: records_by_artists, + records_by_genre: records_by_genre )} end