diff --git a/lib/towerops/agents.ex b/lib/towerops/agents.ex index ec824c7a..b641b4bb 100644 --- a/lib/towerops/agents.ex +++ b/lib/towerops/agents.ex @@ -195,9 +195,32 @@ defmodule Towerops.Agents do _ -> [last_seen_at: now, last_ip: ip_address] end - AgentToken - |> where([t], t.id == ^agent_token_id) - |> Repo.update_all(set: updates) + result = + AgentToken + |> where([t], t.id == ^agent_token_id) + |> Repo.update_all(set: updates) + + # Auto-dismiss agent offline insights when agent comes back online + dismiss_agent_offline_insights(agent_token_id) + + result + end + + defp dismiss_agent_offline_insights(agent_token_id) do + alias Towerops.Preseem.Insight + + now = DateTime.truncate(DateTime.utc_now(), :second) + + {count, _} = + Insight + |> where(agent_token_id: ^agent_token_id, type: "agent_offline", status: "active") + |> Repo.update_all(set: [status: "dismissed", dismissed_at: now]) + + if count > 0 do + Logger.info("Auto-dismissed #{count} agent offline insight(s) for agent #{agent_token_id}") + end + + :ok end @doc """