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]},
# Start to serve requests, typically the last entry
ChronoscopeWeb.Endpoint,
Chronoscope.Monitor
Chronoscope.ViewActivator
]
# 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
state
|> 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
state
end

View File

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

View File

@ -8,15 +8,20 @@ defmodule ChronoscopeWeb.IndexLive do
def mount(_params, _session, socket) do
ChronoscopeWeb.Endpoint.subscribe(@topic)
{:ok, assign(socket, %{servers: server_list()})}
{:ok, assign(socket, %{clients: client_list()})}
end
def handle_info(%{topic: @topic}, socket) do
{:noreply, assign(socket, %{servers: server_list()})}
def handle_info(%{topic: @topic, event: "key-exchange", payload: client}, socket) do
{:noreply, update(socket, :clients, &update_client(&1, client))}
end
defp server_list() do
Chronoscope.Monitor
defp update_client(client_list, client) do
# 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)
|> NTS.list_clients()
end

View File

@ -32,12 +32,12 @@
</tr>
</thead>
<tbody>
<tr :for={server <- @servers} class="hover:bg-zinc-100 dark:hover:bg-zinc-700">
<% {status, response} = server.key_establishment_response %>
<tr :for={client <- @clients} class="hover:bg-zinc-100 dark:hover:bg-zinc-700">
<% {status, response} = client.key_establishment_response %>
<%= if (status == :ok) do %>
<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 class="py-2 px-6 whitespace-nowrap">
<%= 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)%>
</td>
<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>
<% else %>
<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 class="py-2 px-6 whitespace-nowrap">
<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">
<%=
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>
<% end %>