From 4c57f47bcac484eb72095c6e4f948dda8a0371fb Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Fri, 1 Nov 2024 19:04:40 +0000 Subject: [PATCH] Move sorting to db query --- lib/music_library/records.ex | 2 ++ lib/music_library_web/live/stats_live/index.ex | 8 ++------ priv/gettext/default.pot | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/music_library/records.ex b/lib/music_library/records.ex index 1886159e..e79cb68b 100644 --- a/lib/music_library/records.ex +++ b/lib/music_library/records.ex @@ -67,6 +67,7 @@ defmodule MusicLibrary.Records do from r in Record, where: not is_nil(r.purchased_at), group_by: r.format, + order_by: [desc: count(r.id)], select: {r.format, count(r.id)} Repo.all(q) @@ -77,6 +78,7 @@ defmodule MusicLibrary.Records do from r in Record, where: not is_nil(r.purchased_at), group_by: r.type, + order_by: [desc: count(r.id)], select: {r.type, count(r.id)} Repo.all(q) diff --git a/lib/music_library_web/live/stats_live/index.ex b/lib/music_library_web/live/stats_live/index.ex index a1f86a33..2ab98ece 100644 --- a/lib/music_library_web/live/stats_live/index.ex +++ b/lib/music_library_web/live/stats_live/index.ex @@ -7,13 +7,9 @@ defmodule MusicLibraryWeb.StatsLive.Index do alias Records.Record def mount(_params, _session, socket) do - collection_count_by_format = - Records.count_records_by_format() - |> Enum.sort_by(fn {_format, count} -> count end, :desc) + collection_count_by_format = Records.count_records_by_format() - collection_count_by_type = - Records.count_records_by_type() - |> Enum.sort_by(fn {_type, count} -> count end, :desc) + collection_count_by_type = Records.count_records_by_type() collection_count = Enum.reduce(collection_count_by_format, 0, fn {_, count}, acc -> acc + count end) diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index 2118af3c..060e1a6d 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -269,7 +269,7 @@ msgid "Something went wrong!" msgstr "" #: lib/music_library_web/components/layouts/app.html.heex:7 -#: lib/music_library_web/live/stats_live/index.ex:28 +#: lib/music_library_web/live/stats_live/index.ex:24 #, elixir-autogen, elixir-format msgid "Stats" msgstr ""