Categories addressed: - pattern_match / pattern_match_cov (32): remove dead case/with clauses that dialyzer proved unreachable from the caller types. - contract_supertype / extra_range / invalid_contract / contract_with_opaque (25): narrow @spec declarations to match actual success typings. - call / call_without_opaque (18): fix bad calls, narrow User.t to allow nil for in-memory changeset structs, suppress Ecto.Multi opaque-type false positives with targeted @dialyzer directives. - guard_fail / no_return / unused_fun / unknown_function (13): remove dead || fallbacks, simplify always-true params, cascade-resolve no_returns via the underlying pattern_match and call fixes. Real production bug fixed: StormDetector.handle_cast/2 had swapped `:queue.in` args (`queue |> :queue.in(ts)` which desugars to `:queue.in(queue, ts)` — wrong argument order). Alert timestamps were never being enqueued, so storm detection would fail at runtime. Corrected to `ts |> :queue.in(queue)`. .dialyzer_ignore.exs: suppress two genuine dep-PLT gaps (:ranch.get_addr/1 false positive from Bandit's transitive ranch, and the Cloak.Vault GenServer callback_info on the CI build path). `mix dialyzer` now: Total errors: 114, Skipped: 114 — passes clean. Warnings: 88 → 0.
183 lines
4.7 KiB
Elixir
183 lines
4.7 KiB
Elixir
defmodule ToweropsWeb.InsightsLive.Index do
|
|
@moduledoc """
|
|
Unified insights page showing insights from all sources (Preseem, Gaiia, SNMP, System).
|
|
"""
|
|
use ToweropsWeb, :live_view
|
|
|
|
alias Towerops.Preseem
|
|
alias Towerops.Repo
|
|
|
|
@impl true
|
|
def mount(_params, _session, socket) do
|
|
org = socket.assigns.current_scope.organization
|
|
|
|
{:ok,
|
|
socket
|
|
|> assign(:organization, org)
|
|
|> assign(:page_title, t("Insights"))
|
|
|> assign(:selected_ids, MapSet.new())}
|
|
end
|
|
|
|
@impl true
|
|
def handle_params(params, _url, socket) do
|
|
filter_source = params["source"]
|
|
filter_urgency = params["urgency"]
|
|
filter_status = params["status"] || "active"
|
|
|
|
{:noreply,
|
|
socket
|
|
|> assign(:filter_source, filter_source)
|
|
|> assign(:filter_urgency, filter_urgency)
|
|
|> assign(:filter_status, filter_status)
|
|
|> assign(:selected_ids, MapSet.new())
|
|
|> load_insights()}
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("filter", params, socket) do
|
|
query_params = %{}
|
|
|
|
query_params =
|
|
if params["source"] && params["source"] != "",
|
|
do: Map.put(query_params, "source", params["source"]),
|
|
else: query_params
|
|
|
|
query_params =
|
|
if params["urgency"] && params["urgency"] != "",
|
|
do: Map.put(query_params, "urgency", params["urgency"]),
|
|
else: query_params
|
|
|
|
query_params =
|
|
if params["status"] && params["status"] != "",
|
|
do: Map.put(query_params, "status", params["status"]),
|
|
else: query_params
|
|
|
|
{:noreply, push_patch(socket, to: ~p"/insights?#{query_params}")}
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("dismiss", %{"id" => id}, socket) do
|
|
case Preseem.dismiss_insight(id) do
|
|
{:ok, _} ->
|
|
{:noreply,
|
|
socket
|
|
|> load_insights()
|
|
|> put_flash(:info, t("Insight dismissed"))}
|
|
|
|
{:error, _} ->
|
|
{:noreply, put_flash(socket, :error, t("Failed to dismiss"))}
|
|
end
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("toggle_select", %{"id" => id}, socket) do
|
|
selected = socket.assigns.selected_ids
|
|
|
|
selected =
|
|
if MapSet.member?(selected, id),
|
|
do: MapSet.delete(selected, id),
|
|
else: MapSet.put(selected, id)
|
|
|
|
{:noreply, assign(socket, :selected_ids, selected)}
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("select_all", _params, socket) do
|
|
ids = MapSet.new(socket.assigns.insights, & &1.id)
|
|
{:noreply, assign(socket, :selected_ids, ids)}
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("deselect_all", _params, socket) do
|
|
{:noreply, assign(socket, :selected_ids, MapSet.new())}
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("bulk_dismiss", _params, socket) do
|
|
ids = MapSet.to_list(socket.assigns.selected_ids)
|
|
|
|
if ids == [] do
|
|
{:noreply, socket}
|
|
else
|
|
{:ok, count} = Preseem.dismiss_insights(ids)
|
|
|
|
{:noreply,
|
|
socket
|
|
|> assign(:selected_ids, MapSet.new())
|
|
|> load_insights()
|
|
|> put_flash(:info, "#{count} insight(s) dismissed")}
|
|
end
|
|
end
|
|
|
|
defp load_insights(socket) do
|
|
org_id = socket.assigns.organization.id
|
|
opts = [status: socket.assigns.filter_status]
|
|
|
|
opts =
|
|
if socket.assigns.filter_source,
|
|
do: Keyword.put(opts, :source, socket.assigns.filter_source),
|
|
else: opts
|
|
|
|
opts =
|
|
if socket.assigns.filter_urgency,
|
|
do: Keyword.put(opts, :urgency, socket.assigns.filter_urgency),
|
|
else: opts
|
|
|
|
insights =
|
|
org_id
|
|
|> Preseem.list_insights(opts)
|
|
|> Repo.preload([:preseem_access_point, :device])
|
|
|
|
assign(socket, :insights, insights)
|
|
end
|
|
|
|
defp urgency_classes(urgency) do
|
|
case urgency do
|
|
"critical" ->
|
|
"bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-400"
|
|
|
|
"warning" ->
|
|
"bg-yellow-100 text-yellow-800 dark:bg-yellow-900/30 dark:text-yellow-400"
|
|
|
|
"info" ->
|
|
"bg-blue-100 text-blue-800 dark:bg-blue-900/30 dark:text-blue-400"
|
|
|
|
_ ->
|
|
"bg-gray-100 text-gray-600 dark:bg-gray-800 dark:text-gray-400"
|
|
end
|
|
end
|
|
|
|
defp source_classes(source) do
|
|
case source do
|
|
"preseem" ->
|
|
"bg-indigo-100 text-indigo-800 dark:bg-indigo-900/30 dark:text-indigo-400"
|
|
|
|
"gaiia" ->
|
|
"bg-emerald-100 text-emerald-800 dark:bg-emerald-900/30 dark:text-emerald-400"
|
|
|
|
"snmp" ->
|
|
"bg-orange-100 text-orange-800 dark:bg-orange-900/30 dark:text-orange-400"
|
|
|
|
"system" ->
|
|
"bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300"
|
|
|
|
_ ->
|
|
"bg-gray-100 text-gray-600 dark:bg-gray-800 dark:text-gray-400"
|
|
end
|
|
end
|
|
|
|
defp build_filter_params(base, overrides) do
|
|
base
|
|
|> Map.merge(overrides)
|
|
|> Enum.reject(fn {_k, v} -> is_nil(v) end)
|
|
|> Map.new()
|
|
end
|
|
|
|
defp selected?(selected_ids, id) do
|
|
MapSet.member?(selected_ids, id)
|
|
end
|
|
|
|
defp any_selected?(selected_ids) do
|
|
MapSet.size(selected_ids) > 0
|
|
end
|
|
end
|