diff --git a/lib/error_tracker/web/live/dashboard.ex b/lib/error_tracker/web/live/dashboard.ex index d0b06be..0bff44d 100644 --- a/lib/error_tracker/web/live/dashboard.ex +++ b/lib/error_tracker/web/live/dashboard.ex @@ -64,14 +64,27 @@ defmodule ErrorTracker.Web.Live.Dashboard do total_errors = Repo.aggregate(query, :count) - errors_query = - query - |> order_by(desc: :last_occurrence_at) - |> offset((^page - 1) * @per_page) - |> limit(@per_page) + errors = + Repo.all( + from query, + order_by: [desc: :last_occurrence_at], + offset: (^page - 1) * @per_page, + limit: @per_page + ) + + error_ids = Enum.map(errors, & &1.id) + + occurrences = + errors + |> Ecto.assoc(:occurrences) + |> where([o], o.error_id in ^error_ids) + |> group_by([o], o.error_id) + |> select([o], {o.error_id, count(o.id)}) + |> Repo.all() assign(socket, - errors: Repo.all(errors_query), + errors: errors, + occurrences: Map.new(occurrences), total_pages: (total_errors / @per_page) |> Float.ceil() |> trunc ) end diff --git a/lib/error_tracker/web/live/dashboard.html.heex b/lib/error_tracker/web/live/dashboard.html.heex index 35d24f6..057f72a 100644 --- a/lib/error_tracker/web/live/dashboard.html.heex +++ b/lib/error_tracker/web/live/dashboard.html.heex @@ -41,7 +41,7 @@ Error - Last occurrence + Occurrences Status @@ -62,7 +62,8 @@

- <%= format_datetime(error.last_occurrence_at) %> +

Last: <%= format_datetime(error.last_occurrence_at) %>

+

Total: <%= @occurrences[error.id] %>

<.badge :if={error.status == :resolved} color={:green}>Resolved diff --git a/lib/error_tracker/web/live/show.ex b/lib/error_tracker/web/live/show.ex index 564b6a1..29244c4 100644 --- a/lib/error_tracker/web/live/show.ex +++ b/lib/error_tracker/web/live/show.ex @@ -104,7 +104,14 @@ defmodule ErrorTracker.Web.Live.Show do |> List.flatten() |> Enum.reverse() - assign(socket, :occurrences, occurrences) + total_occurrences = + socket.assigns.error + |> Ecto.assoc(:occurrences) + |> Repo.aggregate(:count) + + socket + |> assign(:occurrences, occurrences) + |> assign(:total_occurrences, total_occurrences) end defp related_occurrences(query, num_results) do diff --git a/lib/error_tracker/web/live/show.html.heex b/lib/error_tracker/web/live/show.html.heex index 1b06e5b..0cfd6f0 100644 --- a/lib/error_tracker/web/live/show.html.heex +++ b/lib/error_tracker/web/live/show.html.heex @@ -63,7 +63,7 @@
- <.section title="Occurrence"> + <.section title={"Occurrence (#{@total_occurrences} total)"}>