Deployment and lifecycle management for Nix
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

server/agent: presence updates the ui

+53 -9
+1 -1
lib/sower_web/agent_channel.ex
··· 82 82 Logger.debug(msg: "Tracking agent presence", agent_sid: agent.sid) 83 83 84 84 {:ok, _} = 85 - Presence.track(self(), "agents", socket.assigns.agent.sid, %{ 85 + Presence.track(self(), "agent:presence", socket.assigns.agent.sid, %{ 86 86 online_at: DateTime.utc_now() 87 87 }) 88 88
+22 -3
lib/sower_web/live/agent_live/index.ex
··· 1 1 defmodule SowerWeb.AgentLive.Index do 2 2 use SowerWeb, :live_view 3 3 4 + alias Phoenix.Socket.Broadcast 4 5 alias Sower.Orchestration 5 6 alias Sower.Orchestration.Agent 7 + alias SowerWeb.Presence 6 8 7 9 @impl true 8 10 def mount(_params, _session, socket) do 9 - {:ok, stream(socket, :agents, Orchestration.list_agents())} 11 + if connected?(socket) do 12 + Phoenix.PubSub.subscribe(Sower.PubSub, "agent:presence") 13 + end 14 + 15 + {:ok, 16 + stream(socket, :agents, Orchestration.list_agents()) 17 + |> assign(:agent_presence, Presence.list("agent:presence"))} 10 18 end 11 19 12 20 @impl true ··· 14 22 {:noreply, apply_action(socket, socket.assigns.live_action, params)} 15 23 end 16 24 17 - defp apply_action(socket, :edit, %{"id" => id}) do 25 + defp apply_action(socket, :edit, %{"sid" => sid}) do 18 26 socket 19 27 |> assign(:page_title, "Edit Agent") 20 - |> assign(:agent, Orchestration.get_agent!(id)) 28 + |> assign(:agent, Orchestration.get_agent_sid!(sid)) 21 29 end 22 30 23 31 defp apply_action(socket, :new, _params) do ··· 35 43 @impl true 36 44 def handle_info({SowerWeb.AgentLive.FormComponent, {:saved, agent}}, socket) do 37 45 {:noreply, stream_insert(socket, :agents, agent)} 46 + end 47 + 48 + @impl true 49 + def handle_info(%Broadcast{topic: "agent:presence", event: "presence_diff"}, socket) do 50 + # update the presence list, then touch the stream to force a table refresh 51 + socket = 52 + socket 53 + |> assign(:agent_presence, Presence.list("agent:presence")) 54 + |> stream(:agents, Orchestration.list_agents()) 55 + 56 + {:noreply, socket} 38 57 end 39 58 40 59 @impl true
+1
lib/sower_web/live/agent_live/index.html.heex
··· 12 12 rows={@streams.agents} 13 13 row_click={fn {_id, agent} -> JS.navigate(~p"/agents/#{agent}") end} 14 14 > 15 + <:col :let={{_id, agent}} label="Online">{agent.sid in Map.keys(@agent_presence)}</:col> 15 16 <:col :let={{_id, agent}} label="Name">{agent.name}</:col> 16 17 <:col :let={{_id, agent}} label="sid">{agent.sid}</:col> 17 18 <:col :let={{_id, agent}} label="Local sid">{agent.local_sid}</:col>
+28 -5
lib/sower_web/live/agent_live/show.ex
··· 1 1 defmodule SowerWeb.AgentLive.Show do 2 2 use SowerWeb, :live_view 3 3 4 + alias Phoenix.Socket.Broadcast 4 5 alias Sower.Orchestration 6 + alias SowerWeb.Presence 5 7 6 8 @impl true 7 9 def mount(_params, _session, socket) do 8 - {:ok, socket} 10 + if connected?(socket) do 11 + Phoenix.PubSub.subscribe(Sower.PubSub, "agent:presence") 12 + end 13 + 14 + {:ok, add_online_status(socket)} 9 15 end 10 16 11 17 @impl true 12 18 def handle_params(%{"sid" => sid}, _, socket) do 13 - {:noreply, 14 - socket 15 - |> assign(:page_title, page_title(socket.assigns.live_action)) 16 - |> assign(:agent, Orchestration.get_agent_sid!(sid))} 19 + socket = 20 + socket 21 + |> assign(:page_title, page_title(socket.assigns.live_action)) 22 + |> assign(:agent, Orchestration.get_agent_sid!(sid)) 23 + |> add_online_status() 24 + 25 + {:noreply, socket} 26 + end 27 + 28 + @impl true 29 + def handle_info(%Broadcast{topic: "agent:presence", event: "presence_diff"}, socket) do 30 + {:noreply, add_online_status(socket)} 31 + end 32 + 33 + defp add_online_status(%{assigns: %{agent: agent}} = socket) do 34 + online_agents = Presence.list("agent:presence") |> Map.keys() 35 + assign(socket, :online, agent.sid in online_agents) 36 + end 37 + 38 + defp add_online_status(socket) do 39 + assign(socket, :online, false) 17 40 end 18 41 19 42 defp page_title(:show), do: "Show Agent"
+1
lib/sower_web/live/agent_live/show.html.heex
··· 9 9 </.header> 10 10 11 11 <.list> 12 + <:item title="Online">{@online}</:item> 12 13 <:item title="Name">{@agent.name}</:item> 13 14 <:item title="sid" readonly: tru>{@agent.sid}</:item> 14 15 <:item title="Local sid">{@agent.local_sid}</:item>