Live-ticking relative timestamps on agents page

This commit is contained in:
Graham McIntire 2026-02-10 10:24:21 -06:00
parent 481fc6d4a2
commit 5c59777baa
No known key found for this signature in database
3 changed files with 10 additions and 2 deletions

View file

@ -777,6 +777,7 @@ defmodule ToweropsWeb.CoreComponents do
attr :timezone, :string, default: "UTC", doc: "User's timezone for full timestamp"
attr :class, :string, default: "", doc: "Additional CSS classes"
attr :format, :string, default: "relative", doc: "Display format: 'relative' or 'absolute'"
attr :now, :any, default: nil, doc: "Current time for live-ticking relative timestamps"
def timestamp(assigns) do
assigns =

View file

@ -21,6 +21,7 @@ defmodule ToweropsWeb.AgentLive.Index do
# Subscribe to agent health updates for real-time status changes
if connected?(socket) do
_ = Phoenix.PubSub.subscribe(Towerops.PubSub, "agents:health")
:timer.send_interval(1000, :tick)
end
# If superadmin (including when impersonating), also load cloud pollers and global default
@ -70,6 +71,7 @@ defmodule ToweropsWeb.AgentLive.Index do
|> assign(:agent_image, agent_image)
|> assign(:new_token, nil)
|> assign(:show_token_modal, false)
|> assign(:now, DateTime.utc_now())
|> assign(:agent_form, to_form(%{"name" => "", "is_cloud_poller" => false}))}
end
@ -363,6 +365,11 @@ defmodule ToweropsWeb.AgentLive.Index do
|> put_flash(:info, message)}
end
@impl true
def handle_info(:tick, socket) do
{:noreply, assign(socket, :now, DateTime.utc_now())}
end
# Handle real-time agent status updates
# Only process events for this organization's agents, or cloud pollers if superuser
@impl true

View file

@ -112,7 +112,7 @@
<:col :let={{_id, agent}} label="Last Seen">
<div class="text-sm text-gray-900 dark:text-white">
<.timestamp datetime={agent.last_seen_at} timezone={@timezone} />
<.timestamp datetime={agent.last_seen_at} timezone={@timezone} now={@now} />
</div>
<%= if agent.last_seen_at do %>
<div class="text-xs text-gray-500 dark:text-gray-400 mt-0.5">
@ -223,7 +223,7 @@
<:col :let={{_id, agent}} label="Last Seen">
<div class="text-sm text-gray-900 dark:text-white">
<.timestamp datetime={agent.last_seen_at} timezone={@timezone} />
<.timestamp datetime={agent.last_seen_at} timezone={@timezone} now={@now} />
</div>
<%= if agent.last_seen_at do %>
<div class="text-xs text-gray-500 dark:text-gray-400 mt-0.5">