- Added Stats module to agent LiveView index - Load organization-wide health statistics on mount - Display agent health, equipment counts, and assignment breakdown - Refresh stats after agent creation/revocation - All 10 agent LiveView tests passing
194 lines
6.4 KiB
Elixir
194 lines
6.4 KiB
Elixir
defmodule ToweropsWeb.AgentLive.Index do
|
|
@moduledoc false
|
|
use ToweropsWeb, :live_view
|
|
|
|
alias Towerops.Agents
|
|
alias Towerops.Agents.Stats
|
|
|
|
@impl true
|
|
def mount(_params, _session, socket) do
|
|
organization = socket.assigns.current_organization
|
|
agent_tokens = Agents.list_organization_agent_tokens(organization.id)
|
|
|
|
# Get equipment counts for each agent (directly assigned)
|
|
equipment_counts =
|
|
Map.new(agent_tokens, fn token ->
|
|
{token.id, Agents.count_assigned_equipment(token.id)}
|
|
end)
|
|
|
|
# Get organization-wide agent health statistics
|
|
agent_health_stats = Stats.get_organization_agent_health(organization.id)
|
|
assignment_breakdown = Stats.get_equipment_assignment_breakdown(organization.id)
|
|
offline_agents = Stats.get_offline_agents(organization.id)
|
|
|
|
{:ok,
|
|
socket
|
|
|> assign(:page_title, "Remote Agents")
|
|
|> assign(:agent_tokens, agent_tokens)
|
|
|> assign(:equipment_counts, equipment_counts)
|
|
|> assign(:agent_health_stats, agent_health_stats)
|
|
|> assign(:assignment_breakdown, assignment_breakdown)
|
|
|> assign(:offline_agents, offline_agents)
|
|
|> assign(:new_token, nil)
|
|
|> assign(:show_token_modal, false)
|
|
|> assign(:agent_form, to_form(%{"name" => ""}))}
|
|
end
|
|
|
|
@impl true
|
|
def handle_params(params, _url, socket) do
|
|
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("create_agent", params, socket) do
|
|
name =
|
|
case params do
|
|
%{"agent_form" => %{"name" => n}} -> n
|
|
%{"name" => n} -> n
|
|
end
|
|
|
|
organization = socket.assigns.current_organization
|
|
|
|
case Agents.create_agent_token(organization.id, name) do
|
|
{:ok, agent_token, token} ->
|
|
agent_tokens = Agents.list_organization_agent_tokens(organization.id)
|
|
|
|
# Refresh equipment counts
|
|
equipment_counts =
|
|
Map.new(agent_tokens, fn t ->
|
|
{t.id, Agents.count_assigned_equipment(t.id)}
|
|
end)
|
|
|
|
# Refresh health statistics
|
|
agent_health_stats = Stats.get_organization_agent_health(organization.id)
|
|
assignment_breakdown = Stats.get_equipment_assignment_breakdown(organization.id)
|
|
offline_agents = Stats.get_offline_agents(organization.id)
|
|
|
|
{:noreply,
|
|
socket
|
|
|> assign(:agent_tokens, agent_tokens)
|
|
|> assign(:equipment_counts, equipment_counts)
|
|
|> assign(:agent_health_stats, agent_health_stats)
|
|
|> assign(:assignment_breakdown, assignment_breakdown)
|
|
|> assign(:offline_agents, offline_agents)
|
|
|> assign(:new_token, %{agent_token: agent_token, token: token})
|
|
|> assign(:show_token_modal, true)
|
|
|> assign(:agent_form, to_form(%{"name" => ""}))
|
|
|> put_flash(:info, "Agent created successfully")}
|
|
|
|
{:error, _changeset} ->
|
|
{:noreply, put_flash(socket, :error, "Failed to create agent")}
|
|
end
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("close_token_modal", _params, socket) do
|
|
{:noreply,
|
|
socket
|
|
|> assign(:show_token_modal, false)
|
|
|> assign(:new_token, nil)}
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("show_setup", %{"id" => id}, socket) do
|
|
agent_token = Agents.get_agent_token!(id)
|
|
|
|
{:noreply,
|
|
socket
|
|
|> assign(:new_token, %{agent_token: agent_token, token: agent_token.token})
|
|
|> assign(:show_token_modal, true)}
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("regenerate_token", %{"id" => id}, socket) do
|
|
agent_token = Agents.get_agent_token!(id)
|
|
organization = socket.assigns.current_organization
|
|
|
|
# Revoke the old token and create a new one
|
|
case Agents.revoke_agent_token(id) do
|
|
{:ok, _} ->
|
|
case Agents.create_agent_token(organization.id, agent_token.name) do
|
|
{:ok, new_agent_token, token} ->
|
|
agent_tokens = Agents.list_organization_agent_tokens(organization.id)
|
|
|
|
{:noreply,
|
|
socket
|
|
|> assign(:agent_tokens, agent_tokens)
|
|
|> assign(:new_token, %{agent_token: new_agent_token, token: token})
|
|
|> assign(:show_token_modal, true)
|
|
|> put_flash(:info, "New token generated successfully")}
|
|
|
|
{:error, _changeset} ->
|
|
{:noreply, put_flash(socket, :error, "Failed to generate new token")}
|
|
end
|
|
|
|
{:error, _} ->
|
|
{:noreply, put_flash(socket, :error, "Failed to revoke old token")}
|
|
end
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("revoke_agent", %{"id" => id}, socket) do
|
|
case Agents.revoke_agent_token(id) do
|
|
{:ok, _} ->
|
|
organization = socket.assigns.current_organization
|
|
agent_tokens = Agents.list_organization_agent_tokens(organization.id)
|
|
|
|
# Refresh health statistics
|
|
agent_health_stats = Stats.get_organization_agent_health(organization.id)
|
|
offline_agents = Stats.get_offline_agents(organization.id)
|
|
|
|
{:noreply,
|
|
socket
|
|
|> assign(:agent_tokens, agent_tokens)
|
|
|> assign(:agent_health_stats, agent_health_stats)
|
|
|> assign(:offline_agents, offline_agents)
|
|
|> put_flash(:info, "Agent revoked successfully")}
|
|
|
|
{:error, _} ->
|
|
{:noreply, put_flash(socket, :error, "Failed to revoke agent")}
|
|
end
|
|
end
|
|
|
|
defp apply_action(socket, :index, _params) do
|
|
socket
|
|
end
|
|
|
|
defp agent_status(agent_token) do
|
|
case agent_token.last_seen_at do
|
|
nil ->
|
|
{:never, "Never connected"}
|
|
|
|
last_seen ->
|
|
seconds_ago = DateTime.diff(DateTime.utc_now(), last_seen, :second)
|
|
|
|
cond do
|
|
seconds_ago < 120 -> {:online, "Online"}
|
|
seconds_ago < 300 -> {:warning, "Warning"}
|
|
true -> {:offline, "Offline"}
|
|
end
|
|
end
|
|
end
|
|
|
|
defp status_badge_class(status) do
|
|
case status do
|
|
:online -> "bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-300"
|
|
:warning -> "bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-300"
|
|
:offline -> "bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-300"
|
|
:never -> "bg-zinc-100 text-zinc-800 dark:bg-zinc-800 dark:text-zinc-300"
|
|
end
|
|
end
|
|
|
|
defp format_last_seen(nil), do: "Never"
|
|
|
|
defp format_last_seen(datetime) do
|
|
seconds_ago = DateTime.diff(DateTime.utc_now(), datetime, :second)
|
|
|
|
cond do
|
|
seconds_ago < 60 -> "#{seconds_ago}s ago"
|
|
seconds_ago < 3600 -> "#{div(seconds_ago, 60)}m ago"
|
|
seconds_ago < 86_400 -> "#{div(seconds_ago, 3600)}h ago"
|
|
true -> "#{div(seconds_ago, 86_400)}d ago"
|
|
end
|
|
end
|
|
end
|