defmodule AprsmeWeb.StatusLive.Index do @moduledoc """ LiveView for displaying real-time APRS-IS connection status """ use AprsmeWeb, :live_view # 5 seconds - reduced from 1 second to improve performance @refresh_interval 5_000 @impl true def mount(_params, _session, socket) do # Load cached status on mount for fast initial load aprs_status = get_cached_aprs_status() socket = assign(socket, page_title: "System Status", aprs_status: aprs_status, current_time: DateTime.utc_now(), health_score: calculate_health_score(aprs_status), loading: false ) if connected?(socket) do # Subscribe to status updates via PubSub Phoenix.PubSub.subscribe(Aprsme.PubSub, "aprs_status") # Schedule the first refresh with a slight delay Process.send_after(self(), :refresh_status, 500) end {:ok, socket} end @impl true def handle_info(:refresh_status, socket) do # Refresh status asynchronously self_pid = self() Task.start(fn -> status = get_aprs_status() send(self_pid, {:status_updated, status}) end) # Schedule next refresh schedule_refresh() {:noreply, assign(socket, loading: true)} end @impl true def handle_info({:status_updated, status}, socket) do socket = assign(socket, aprs_status: status, current_time: DateTime.utc_now(), health_score: calculate_health_score(status), loading: false ) {:noreply, socket} end @impl true def handle_info({:aprs_status_update, status}, socket) do # Handle real-time status updates via PubSub socket = assign(socket, aprs_status: status, current_time: DateTime.utc_now(), health_score: calculate_health_score(status) ) {:noreply, socket} end @impl true def render(assigns) do ~H"""
{get_health_description(@health_score, @aprs_status.connected)}
{gettext("Leader node")} {gettext("highlighted")}
{gettext( "Status is aggregated from all nodes in the cluster. Only the leader node maintains the APRS-IS connection." )}