diff --git a/lib/music_library/records.ex b/lib/music_library/records.ex index 2d5f0334..662681af 100644 --- a/lib/music_library/records.ex +++ b/lib/music_library/records.ex @@ -39,6 +39,15 @@ defmodule MusicLibrary.Records do Repo.aggregate(Record, :count) end + def count_records_by_format do + q = + from r in Record, + group_by: r.format, + select: {r.format, count(r.id)} + + Repo.all(q) + end + def search_records_count(query) 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 7aacb5c5..1a9523c0 100644 --- a/lib/music_library_web/controllers/stats_controller.ex +++ b/lib/music_library_web/controllers/stats_controller.ex @@ -4,8 +4,15 @@ defmodule MusicLibraryWeb.StatsController do alias MusicLibrary.Records def index(conn, _params) do - records_count = Records.count_records() + records_count_by_format = Records.count_records_by_format() + records_count = Enum.reduce(records_count_by_format, 0, fn {_, count}, acc -> acc + count end) record = Records.get_latest_record!() - render(conn, :index, records_count: records_count, record: record, nav_section: :dashboard) + + render(conn, :index, + records_count_by_format: records_count_by_format, + records_count: records_count, + record: record, + nav_section: :dashboard + ) end end diff --git a/lib/music_library_web/controllers/stats_html.ex b/lib/music_library_web/controllers/stats_html.ex index e7e4fe94..7bc60a5e 100644 --- a/lib/music_library_web/controllers/stats_html.ex +++ b/lib/music_library_web/controllers/stats_html.ex @@ -2,4 +2,11 @@ defmodule MusicLibraryWeb.StatsHTML do use MusicLibraryWeb, :html embed_templates "stats_html/*" + + defp format_records_count(records_count_by_format) do + Enum.map(records_count_by_format, fn {format, count} -> + Integer.to_string(count) <> " " <> Atom.to_string(format) + end) + |> Enum.join(", ") + 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 ed097715..9e7a581f 100644 --- a/lib/music_library_web/controllers/stats_html/index.html.heex +++ b/lib/music_library_web/controllers/stats_html/index.html.heex @@ -24,6 +24,9 @@

<%= @records_count %>

+

+ <%= format_records_count(@records_count_by_format) %> +