From 2661c9f60df323d79ff6c5f5b9aea4e6b039825a Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Mon, 14 Oct 2024 22:33:05 +0100 Subject: [PATCH] Display type stats --- .../controllers/stats_controller.ex | 5 +++++ .../controllers/stats_html/index.html.heex | 17 +++++++++++++++++ .../controllers/stats_controller_test.exs | 14 +++++++++++--- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/music_library_web/controllers/stats_controller.ex b/lib/music_library_web/controllers/stats_controller.ex index d5777be1..4bf02f7e 100644 --- a/lib/music_library_web/controllers/stats_controller.ex +++ b/lib/music_library_web/controllers/stats_controller.ex @@ -8,11 +8,16 @@ defmodule MusicLibraryWeb.StatsController do Records.count_records_by_format() |> Enum.sort_by(fn {_format, count} -> count end, :desc) + records_count_by_type = + Records.count_records_by_type() + |> Enum.sort_by(fn {_type, count} -> count end, :desc) + records_count = Enum.reduce(records_count_by_format, 0, fn {_, count}, acc -> acc + count end) latest_record = Records.get_latest_record!() render(conn, :index, records_count_by_format: records_count_by_format, + records_count_by_type: records_count_by_type, records_count: records_count, latest_record: latest_record, nav_section: :stats 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 35e71df5..0058aad8 100644 --- a/lib/music_library_web/controllers/stats_html/index.html.heex +++ b/lib/music_library_web/controllers/stats_html/index.html.heex @@ -54,3 +54,20 @@ + +
+
+
+
+ <%= Record.type_long_label(type) %> +
+
+ +
+
+
+
diff --git a/test/music_library_web/controllers/stats_controller_test.exs b/test/music_library_web/controllers/stats_controller_test.exs index ae2b9160..8ed93a35 100644 --- a/test/music_library_web/controllers/stats_controller_test.exs +++ b/test/music_library_web/controllers/stats_controller_test.exs @@ -19,18 +19,26 @@ defmodule MusicLibraryWeb.StatsControllerTest do describe "GET /" do setup [:create_records] - test "it shows the record count (total and by format)", %{conn: conn, records: records} do + test "it shows the record counts (total, format, and type)", %{conn: conn, records: records} do conn = get(conn, "/") - assert html_response(conn, 200) =~ records |> length() |> Integer.to_string() + response = html_response(conn, 200) + + assert response =~ records |> length() |> Integer.to_string() records |> Enum.frequencies_by(& &1.format) |> Enum.each(fn {format, count} -> - response = html_response(conn, 200) assert response =~ "\n#{count}\n" assert response =~ "\n#{Record.format_long_label(format)}\n" end) + + records + |> Enum.frequencies_by(& &1.type) + |> Enum.each(fn {type, count} -> + assert response =~ "\n#{count}\n" + assert response =~ "\n#{Record.type_long_label(type)}\n" + end) end test "it shows the latest record", %{conn: conn, records: records} do