towerops/lib/towerops_web/helpers/status_helpers.ex
Graham McIntire d1403c8069 Fix failing tests and clean up code
- Fix doctests for Accounts, Agents.Stats, Snmp to match actual behavior
- Fix dynamic_extra_test vendor post-processing tests to seed sensor data
- Fix activity_controller_test to seed devices for feed data
- Fix session_manager_test to create browser session for test
- Fix topology_test link creation for connection test
- Fix device_monitor/driver_worker tests for unique job constraints
- Fix accounts_test expired_tokens assertion (magic link token is expired)
- Fix happy_path_test and show_events_test to seed monitor data
- Fix admin user_live_test user.name -> user.email (no name field)
- Fix schema_test to seed activity data
- Fix mobile_qr_live_test to match actual template text
- Fix SnmpKit.MIB doctests and tests for enriched return values
- Fix onboarding_live, mobile_controller, mib_test weak assertions
- Remove dead code and fix credo warnings
2026-06-16 14:54:34 -05:00

55 lines
1.4 KiB
Elixir

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
"""
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.
"""
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