From af1f893db5f09f69fa437f2741f3a8b8a666c450 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Wed, 5 Jun 2024 12:23:25 -0400 Subject: [PATCH] Update individual clients in the live view --- lib/chronoscope/application.ex | 2 +- lib/chronoscope/nts/client.ex | 2 +- lib/chronoscope/{monitor.ex => view_activator.ex} | 2 +- lib/chronoscope_web/live/index_live.ex | 15 ++++++++++----- lib/chronoscope_web/live/index_live.html.heex | 12 ++++++------ 5 files changed, 19 insertions(+), 14 deletions(-) rename lib/chronoscope/{monitor.ex => view_activator.ex} (95%) diff --git a/lib/chronoscope/application.ex b/lib/chronoscope/application.ex index f297711..1f35d3d 100644 --- a/lib/chronoscope/application.ex +++ b/lib/chronoscope/application.ex @@ -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 diff --git a/lib/chronoscope/nts/client.ex b/lib/chronoscope/nts/client.ex index 2500e6e..b39fec9 100644 --- a/lib/chronoscope/nts/client.ex +++ b/lib/chronoscope/nts/client.ex @@ -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 diff --git a/lib/chronoscope/monitor.ex b/lib/chronoscope/view_activator.ex similarity index 95% rename from lib/chronoscope/monitor.ex rename to lib/chronoscope/view_activator.ex index 9ed2392..54b7fb5 100644 --- a/lib/chronoscope/monitor.ex +++ b/lib/chronoscope/view_activator.ex @@ -1,4 +1,4 @@ -defmodule Chronoscope.Monitor do +defmodule Chronoscope.ViewActivator do use GenServer require Logger diff --git a/lib/chronoscope_web/live/index_live.ex b/lib/chronoscope_web/live/index_live.ex index b385b06..1b72ddb 100644 --- a/lib/chronoscope_web/live/index_live.ex +++ b/lib/chronoscope_web/live/index_live.ex @@ -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 diff --git a/lib/chronoscope_web/live/index_live.html.heex b/lib/chronoscope_web/live/index_live.html.heex index 9ca85be..de4f785 100644 --- a/lib/chronoscope_web/live/index_live.html.heex +++ b/lib/chronoscope_web/live/index_live.html.heex @@ -32,12 +32,12 @@ - - <% {status, response} = server.key_establishment_response %> + + <% {status, response} = client.key_establishment_response %> <%= if (status == :ok) do %> - <%= server.server.host %>:<%= server.server.port %> + <%= client.server.host %>:<%= client.server.port %> <%= 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)%> - <%= server.last_key_establishment |> Calendar.strftime("%Y-%m-%d %H:%M:%SZ") %> + <%= client.last_key_establishment |> Calendar.strftime("%Y-%m-%d %H:%M:%SZ") %> <% else %> - <%= server.server.host %>:<%= server.server.port %> + <%= client.server.host %>:<%= client.server.port %> @@ -91,7 +91,7 @@ - <%= - server.last_key_establishment |> Calendar.strftime("%Y-%m-%d %H:%M:%SZ") + client.last_key_establishment |> Calendar.strftime("%Y-%m-%d %H:%M:%SZ") %> <% end %>