From 039ba95a600b04d4a1e6662b47c146a2150f3783 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Sun, 29 Sep 2024 17:21:48 +0100 Subject: [PATCH] Improve home page stats --- README.md | 16 +++++ lib/music_library/records.ex | 9 +++ .../controllers/stats_controller.ex | 3 +- .../controllers/stats_html/index.html.heex | 61 +++++++++++++++++-- 4 files changed, 82 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 6fad0014..5a559c4e 100644 --- a/README.md +++ b/README.md @@ -27,3 +27,19 @@ Ready to run in production? Please [check our deployment guides](https://hexdocs [Exqlite](https://github.com/elixir-sqlite/exqlite?tab=readme-ov-file) recommends using [ExSqlean](https://github.com/mindreframer/ex_sqlean), which in turn uses [Sqlean](https://github.com/mindreframer/sqlean), but the extension is NOT part of the ExSQlean set. Need to investigate with forking and PR. + +### Queries + +- To get the count of records per genre: + + ``` + select genre.value, count(genre.value) as c from records, json_each(records.genres) genre group by genre.value order by c desc; + ``` + +- To get the count of records per artist: + + ``` + select json_extract(artist.value, '$.name') AS name, count(1) as c from records, json_each(records.artists) artist group by name order by c desc; + ``` + + Note that this query would fail to disambiguate artists with the same name - can be fixed by using the artist `musicbrainz_id`. diff --git a/lib/music_library/records.ex b/lib/music_library/records.ex index 1c2b8873..92df6a26 100644 --- a/lib/music_library/records.ex +++ b/lib/music_library/records.ex @@ -45,6 +45,15 @@ defmodule MusicLibrary.Records do def get_record!(id), do: Repo.get!(Record, id) + def get_latest_record! do + q = + from r in Record, + order_by: [desc: r.inserted_at], + limit: 1 + + Repo.one!(q) + end + def get_image!(id) do q = from r in Record, diff --git a/lib/music_library_web/controllers/stats_controller.ex b/lib/music_library_web/controllers/stats_controller.ex index 4e766cef..d2cdd4f5 100644 --- a/lib/music_library_web/controllers/stats_controller.ex +++ b/lib/music_library_web/controllers/stats_controller.ex @@ -5,6 +5,7 @@ defmodule MusicLibraryWeb.StatsController do def index(conn, _params) do records_count = Records.count_records() - render(conn, :index, records_count: records_count) + record = Records.get_latest_record!() + render(conn, :index, records_count: records_count, record: record) end end diff --git a/lib/music_library_web/controllers/stats_html/index.html.heex b/lib/music_library_web/controllers/stats_html/index.html.heex index f8373a65..06718d97 100644 --- a/lib/music_library_web/controllers/stats_html/index.html.heex +++ b/lib/music_library_web/controllers/stats_html/index.html.heex @@ -1,11 +1,60 @@

Music Library

-
-
-
Total Records
-
-
- <%= @records_count %> + +
+
+
+
+ +
+

Total Records

+
+
+

<%= @records_count %>

+ +
+
+
+
+ {@record.title} +

Latest record

+
+
+

<%= @record.title %>

+