defmodule BenchmarkWeb.DashboardLive do use BenchmarkWeb, :live_view alias Benchmark.Alerts @impl true def mount(_params, _session, socket) do alerts = Alerts.list_alerts() {:ok, assign(socket, alerts: alerts, count: length(alerts), search: "", ws_ready: connected?(socket) )} end @impl true def handle_event("add_alert", _params, socket) do {:ok, alert} = Alerts.create_random_alert() alerts = [alert | socket.assigns.alerts] {:noreply, assign(socket, alerts: alerts, count: length(alerts))} end @impl true def handle_event("delete_alert", %{"id" => id_str}, socket) do id = String.to_integer(id_str) Alerts.delete_alert(id) alerts = Enum.reject(socket.assigns.alerts, &(&1.id == id)) {:noreply, assign(socket, alerts: alerts, count: length(alerts))} end @impl true def handle_event("search", %{"query" => query}, socket) do alerts = Alerts.search_alerts(query) {:noreply, assign(socket, alerts: alerts, search: query, count: length(alerts))} end defp type_class("CRITICAL"), do: "px-2 py-1 rounded text-xs font-semibold bg-red-100 text-red-700" defp type_class("WARNING"), do: "px-2 py-1 rounded text-xs font-semibold bg-yellow-100 text-yellow-700" defp type_class(_), do: "px-2 py-1 rounded text-xs font-semibold bg-blue-100 text-blue-700" defp format_time(dt) do dt |> DateTime.to_time() |> Time.to_string() |> String.slice(0, 8) end end