Update individual clients in the live view

This commit is contained in:
Mike Cifelli 2024-06-05 12:23:25 -04:00
parent 1efc641985
commit af1f893db5
Signed by: mike
GPG Key ID: 6B08C6BE47D08E4C
5 changed files with 19 additions and 14 deletions

View File

@ -23,7 +23,7 @@ defmodule Chronoscope.Application do
{Registry, [keys: :unique, name: Chronoscope.Gemini.Registry]}, {Registry, [keys: :unique, name: Chronoscope.Gemini.Registry]},
# Start to serve requests, typically the last entry # Start to serve requests, typically the last entry
ChronoscopeWeb.Endpoint, ChronoscopeWeb.Endpoint,
Chronoscope.Monitor Chronoscope.ViewActivator
] ]
# See https://hexdocs.pm/elixir/Supervisor.html # See https://hexdocs.pm/elixir/Supervisor.html

View File

@ -77,7 +77,7 @@ defmodule Chronoscope.NTS.Client do
if interval_surpassed?(now, state.last_key_establishment) do if interval_surpassed?(now, state.last_key_establishment) do
state state
|> Map.merge(current_data(state, now)) |> Map.merge(current_data(state, now))
|> tap(fn _ -> ChronoscopeWeb.Endpoint.broadcast(@topic, "", "") end) |> tap(&ChronoscopeWeb.Endpoint.broadcast(@topic, "key-exchange", Map.delete(&1, :timer)))
else else
state state
end end

View File

@ -1,4 +1,4 @@
defmodule Chronoscope.Monitor do defmodule Chronoscope.ViewActivator do
use GenServer use GenServer
require Logger require Logger

View File

@ -8,15 +8,20 @@ defmodule ChronoscopeWeb.IndexLive do
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
ChronoscopeWeb.Endpoint.subscribe(@topic) ChronoscopeWeb.Endpoint.subscribe(@topic)
{:ok, assign(socket, %{servers: server_list()})} {:ok, assign(socket, %{clients: client_list()})}
end end
def handle_info(%{topic: @topic}, socket) do def handle_info(%{topic: @topic, event: "key-exchange", payload: client}, socket) do
{:noreply, assign(socket, %{servers: server_list()})} {:noreply, update(socket, :clients, &update_client(&1, client))}
end end
defp server_list() do defp update_client(client_list, client) do
Chronoscope.Monitor # todo - use a map instead of list for fast lookups?
Enum.map(client_list, &if(client.server == &1.server, do: client, else: &1))
end
defp client_list() do
Chronoscope.ViewActivator
|> GenServer.call(:get_nts_servers) |> GenServer.call(:get_nts_servers)
|> NTS.list_clients() |> NTS.list_clients()
end end

View File

@ -32,12 +32,12 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr :for={server <- @servers} class="hover:bg-zinc-100 dark:hover:bg-zinc-700"> <tr :for={client <- @clients} class="hover:bg-zinc-100 dark:hover:bg-zinc-700">
<% {status, response} = server.key_establishment_response %> <% {status, response} = client.key_establishment_response %>
<%= if (status == :ok) do %> <%= if (status == :ok) do %>
<td class="py-2 px-6 whitespace-nowrap"> <td class="py-2 px-6 whitespace-nowrap">
<%= server.server.host %><span :if={server.server.port != 4460}>:<%= server.server.port %></span> <%= client.server.host %><span :if={client.server.port != 4460}>:<%= client.server.port %></span>
</td> </td>
<td class="py-2 px-6 whitespace-nowrap"> <td class="py-2 px-6 whitespace-nowrap">
<%= status %> <%= status %>
@ -68,12 +68,12 @@
<%= response.cert_expiration |> DateTime.from_iso8601 |> then(fn {:ok, dt, _} -> Calendar.strftime(dt, "%Y-%m-%d %H:%M:%SZ") end)%> <%= response.cert_expiration |> DateTime.from_iso8601 |> then(fn {:ok, dt, _} -> Calendar.strftime(dt, "%Y-%m-%d %H:%M:%SZ") end)%>
</td> </td>
<td class="py-2 px-6 whitespace-nowrap"> <td class="py-2 px-6 whitespace-nowrap">
<%= server.last_key_establishment |> Calendar.strftime("%Y-%m-%d %H:%M:%SZ") %> <%= client.last_key_establishment |> Calendar.strftime("%Y-%m-%d %H:%M:%SZ") %>
</td> </td>
<% else %> <% else %>
<td class="py-2 px-6 whitespace-nowrap"> <td class="py-2 px-6 whitespace-nowrap">
<%= server.server.host %><span :if={server.server.port != 4460}>:<%= server.server.port %></span> <%= client.server.host %><span :if={client.server.port != 4460}>:<%= client.server.port %></span>
</td> </td>
<td class="py-2 px-6 whitespace-nowrap"> <td class="py-2 px-6 whitespace-nowrap">
<span class="group relative"> <span class="group relative">
@ -91,7 +91,7 @@
<td class="py-2 px-6 whitespace-nowrap"> - </td> <td class="py-2 px-6 whitespace-nowrap"> - </td>
<td class="py-2 px-6 whitespace-nowrap"> <td class="py-2 px-6 whitespace-nowrap">
<%= <%=
server.last_key_establishment |> Calendar.strftime("%Y-%m-%d %H:%M:%SZ") client.last_key_establishment |> Calendar.strftime("%Y-%m-%d %H:%M:%SZ")
%> %>
</td> </td>
<% end %> <% end %>