From 83fa2e99a52b48ae53c8b399b50bcc418abda446 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Sun, 6 Apr 2025 08:36:04 +0100 Subject: [PATCH] Amend counts to only operate on collection records --- lib/music_library/collection.ex | 36 +++++++++++++++++-- lib/music_library/records.ex | 36 ------------------- .../live/stats_live/index.ex | 4 +-- 3 files changed, 35 insertions(+), 41 deletions(-) diff --git a/lib/music_library/collection.ex b/lib/music_library/collection.ex index 6b6dd3bc..95e3cc64 100644 --- a/lib/music_library/collection.ex +++ b/lib/music_library/collection.ex @@ -71,12 +71,42 @@ defmodule MusicLibrary.Collection do Repo.all(q) end - def count_records_by_genre do + @doc """ + Returns a list of tuples containing artist names and their record count, + ordered by count in descending order. + + q = + from r in fragment("records, json_each(records.release_ids)"), + where: fragment("records.format = ?", ^format) and r.value == ^release_id, + select: %{ + record_id: fragment("records.id"), + purchased_at: fragment("records.purchased_at") + } + """ + def count_records_by_artist(limit \\ 30) do + q = + from r in fragment("records, json_each(records.artists)"), + where: fragment("records.purchased_at IS NOT NULL"), + group_by: fragment("json_extract(?, '$.name')", r.value), + order_by: [desc: fragment("count(1)")], + select: {fragment("json_extract(?, '$.name')", r.value), fragment("count(1)")}, + limit: ^limit + + Repo.all(q) + end + + @doc """ + Returns a list of tuples containing genre names and their record count, + ordered by count in descending order. + """ + def count_records_by_genre(limit \\ 30) do q = from r in fragment("records, json_each(records.genres)"), + where: fragment("records.purchased_at IS NOT NULL"), group_by: r.value, - order_by: [desc: count(r.value)], - select: %{genre: r.value, count: count(r.value)} + order_by: [desc: fragment("count(1)")], + select: {r.value, fragment("count(1)")}, + limit: ^limit Repo.all(q) end diff --git a/lib/music_library/records.ex b/lib/music_library/records.ex index b804924c..02745507 100644 --- a/lib/music_library/records.ex +++ b/lib/music_library/records.ex @@ -267,40 +267,4 @@ defmodule MusicLibrary.Records do def change_record(%Record{} = record, attrs \\ %{}) do Record.changeset(record, attrs) end - - @doc """ - Returns a list of tuples containing artist names and their record count, - ordered by count in descending order. - """ - def count_records_by_artist do - q = - from r in fragment(""" - select json_extract(artist.value, '$.name') as name, count(1) as count - from records, json_each(records.artists) artist - group by name - order by count desc - limit 30 - """), - select: {fragment("name"), fragment("count")} - - Repo.all(q) - end - - @doc """ - Returns a list of tuples containing genre names and their record count, - ordered by count in descending order. - """ - def count_records_by_genre do - q = - from r in fragment(""" - select genre.value as name, count(1) as count - from records, json_each(records.genres) genre - group by name - order by count desc - limit 30 - """), - select: {fragment("name"), fragment("count")} - - Repo.all(q) - end end diff --git a/lib/music_library_web/live/stats_live/index.ex b/lib/music_library_web/live/stats_live/index.ex index d3b72030..e448a5ad 100644 --- a/lib/music_library_web/live/stats_live/index.ex +++ b/lib/music_library_web/live/stats_live/index.ex @@ -30,8 +30,8 @@ defmodule MusicLibraryWeb.StatsLive.Index do latest_record: latest_record, page_title: gettext("Stats"), nav_section: :stats, - records_by_artist: Records.count_records_by_artist(), - records_by_genre: Records.count_records_by_genre() + records_by_artist: Collection.count_records_by_artist(), + records_by_genre: Collection.count_records_by_genre() )} end