defmodule ToweropsWeb.Helpers.StatusHelpers do @moduledoc """ Helpers for displaying organization status indicators. """ alias Towerops.Alerts alias Towerops.Organizations.Organization @doc """ Returns a status emoji based on the organization's active alerts. ## Status Logic - 🔴 Red circle: Critical alerts (device_down, agent_offline, or severity 1) - 🟡 Yellow circle: Warning alerts (severity 2 or other active alerts) - 🟢 Green circle: No active alerts ## Examples iex> status_emoji(%Organization{id: org_id}) "🔴" """ def status_emoji(nil), do: "🟢" def status_emoji(%Organization{id: organization_id}) do case get_organization_status(organization_id) do :critical -> "🔴" :warning -> "🟡" :healthy -> "🟢" end end @doc """ Returns a page title with status emoji prepended. ## Examples iex> title_with_status("Dashboard", %Organization{}) "🔴 Dashboard" """ def title_with_status(title, organization) do emoji = status_emoji(organization) "#{emoji} #{title}" end defp get_organization_status(organization_id) do alerts = Alerts.list_organization_active_alerts(organization_id) cond do has_critical_alerts?(alerts) -> :critical Enum.any?(alerts) -> :warning true -> :healthy end end defp has_critical_alerts?(alerts) do Enum.any?(alerts, fn alert -> # Device down or agent offline are always critical # Severity 1 is critical alert.alert_type in ["device_down", "agent_offline"] or alert.severity == 1 end) end end