From 40b67a6399dab5eebaa0434edad74fbc747187bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wala?= Date: Thu, 11 Jul 2024 12:04:06 +0200 Subject: [PATCH] Add per-second stats, UX improvements (#5) * Add per second stats, minor improvements * Refactor the code to be more concise * Fix crash on removing currently inspected PeerConnection * Remove unnecessary `case` statement * Apply requested changes * Fix invalid value in one of the stats --- lib/ex_webrtc_dashboard.ex | 379 ++++++++++++++++++++----------------- mix.exs | 2 +- mix.lock | 18 +- 3 files changed, 220 insertions(+), 179 deletions(-) diff --git a/lib/ex_webrtc_dashboard.ex b/lib/ex_webrtc_dashboard.ex index c681c10..d2c7433 100644 --- a/lib/ex_webrtc_dashboard.ex +++ b/lib/ex_webrtc_dashboard.ex @@ -13,7 +13,7 @@ defmodule ExWebRTCDashboard do @impl true def menu_link(_session, _caps) do - {:ok, "Elixir WebRTC"} + {:ok, "WebRTC"} end @impl true @@ -69,7 +69,7 @@ defmodule ExWebRTCDashboard do def render(assigns) do ~H""" <%= if @pc_pids == %{} do %> - Waiting for peer connections to be spawned... + Waiting for PeerConnections to be spawned... <% else %> <.live_nav_bar id="navbar" page={@page}> <:item :for={{pc_str, {_pc, pc_stats}} <- @pc_pids} name={pc_str} method="redirect"> @@ -95,23 +95,24 @@ defmodule ExWebRTCDashboard do Process.send_after(self(), :update_stats, 1_000) socket = update_pc_pids(socket) pc_pids = socket.assigns.pc_pids + pc_str = socket.assigns.current_pc_str cond do - socket.assigns.current_pc_str == nil and pc_pids != %{} -> + pc_pids != %{} and (pc_str == nil or not Map.has_key?(pc_pids, pc_str)) -> nav = List.first(Map.keys(socket.assigns.pc_pids)) to = live_dashboard_path(socket, socket.assigns.page, nav: nav) {:noreply, push_navigate(socket, to: to)} - socket.assigns.current_pc_str == nil -> + pc_str == nil -> {:noreply, socket} true -> pc = socket.assigns.current_pc - pc_str = socket.assigns.current_pc_str case fetch_stats(pc) do {:ok, stats} -> - update_plots(stats) + {^pc, old_stats} = Map.fetch!(pc_pids, pc_str) + update_plots(stats, old_stats) pc_pids = put_in(socket.assigns.pc_pids, [pc_str], {pc, stats}) socket = assign(socket, pc_pids: pc_pids) {:noreply, socket} @@ -153,7 +154,7 @@ defmodule ExWebRTCDashboard do