diff --git a/assets/css/app.scss b/assets/css/app.scss index a7dc6767d..1c6f09d77 100644 --- a/assets/css/app.scss +++ b/assets/css/app.scss @@ -256,7 +256,7 @@ html body { .display-box.metrics { display: flex; - flex-wrap: row; + flex-wrap: wrap; gap: 4px; } diff --git a/lib/nerves_hub/devices/metrics.ex b/lib/nerves_hub/devices/metrics.ex index 38066ffe3..da3d395d5 100644 --- a/lib/nerves_hub/devices/metrics.ex +++ b/lib/nerves_hub/devices/metrics.ex @@ -143,6 +143,20 @@ defmodule NervesHub.Devices.Metrics do |> Map.put(:timestamp, get_latest_timestamp_for_device(device_id)) end + def get_latest_custom_metrics(device_id) do + default_metrics = Enum.map(@default_metric_types, &Atom.to_string/1) + + DeviceMetric + |> select([dm], dm.key) + |> where(device_id: ^device_id) + |> where([dm], dm.key not in ^default_metrics) + |> distinct(true) + |> Repo.all() + |> Enum.reduce(%{}, fn type, acc -> + Map.put(acc, type, get_latest_value(device_id, type)) + end) + end + defp get_value_or_nil(%DeviceMetric{value: value}), do: value defp get_value_or_nil(_), do: nil diff --git a/lib/nerves_hub_web/live/devices/show.ex b/lib/nerves_hub_web/live/devices/show.ex index 0de357e79..8b4e5b208 100644 --- a/lib/nerves_hub_web/live/devices/show.ex +++ b/lib/nerves_hub_web/live/devices/show.ex @@ -35,6 +35,8 @@ defmodule NervesHubWeb.Live.Devices.Show do |> assign(:update_information, Devices.resolve_update(device)) |> assign(:firmwares, Firmwares.get_firmware_for_device(device)) |> assign(:latest_metrics, Devices.Metrics.get_latest_metric_set_for_device(device.id)) + |> assign(:latest_custom_metrics, Devices.Metrics.get_latest_custom_metrics(device.id)) + |> assign_metadata() |> schedule_health_check_timer() |> assign(:fwup_progress, nil) |> audit_log_assigns(1) @@ -85,8 +87,15 @@ defmodule NervesHubWeb.Live.Devices.Show do end end - def handle_info(%Broadcast{event: "health_check_report"}, socket) do - {:noreply, assign(socket, health: Devices.get_latest_health(socket.assigns.device.id))} + def handle_info( + %Broadcast{event: "health_check_report"}, + %{assigns: %{device: device}} = socket + ) do + socket + |> assign(:latest_metrics, Devices.Metrics.get_latest_metric_set_for_device(device.id)) + |> assign(:latest_custom_metrics, Devices.Metrics.get_latest_custom_metrics(device.id)) + |> assign_metadata() + |> noreply end def handle_info(:check_health_interval, socket) do @@ -286,6 +295,24 @@ defmodule NervesHubWeb.Live.Devices.Show do end end + defp assign_metadata(%{assigns: %{device: device}} = socket) do + health = Devices.get_latest_health(device.id) + + metadata = + if health, do: health.data["metadata"] || %{}, else: %{} + + socket + |> assign(:metadata, Map.drop(metadata, standard_keys(device))) + end + + defp standard_keys(%{firmware_metadata: nil}), do: [] + + defp standard_keys(%{firmware_metadata: firmware_metadata}), + do: + firmware_metadata + |> Map.keys() + |> Enum.map(&to_string/1) + defp schedule_health_check_timer(socket) do if connected?(socket) and device_health_check_enabled?() do timer_ref = Process.send_after(self(), :check_health_interval, 500) diff --git a/lib/nerves_hub_web/live/devices/show.html.heex b/lib/nerves_hub_web/live/devices/show.html.heex index 4665802f9..f7b21fb27 100644 --- a/lib/nerves_hub_web/live/devices/show.html.heex +++ b/lib/nerves_hub_web/live/devices/show.html.heex @@ -69,6 +69,16 @@ + + <%= if !Enum.empty?(@metadata) do %> +

Metadata

+
+
+
<%= key |> String.replace("_", " ") |> String.capitalize() %>
+

<%= value %>

+
+
+ <% end %>
@@ -99,15 +109,15 @@
- <%= if (!Enum.any?(Map.values(@latest_metrics))) do %> + <%= if (!Enum.any?(Map.values(@latest_metrics)) or !Enum.any?(Map.values(@latest_custom_metrics))) do %>
No health information have been received for this device.
<% end %>
<%= if (Enum.any?(Map.values(@latest_metrics))) do %> -
-
+
+
Load avg
<%= @latest_metrics.load_1min %> | <%= @latest_metrics.load_5min %> | <%= @latest_metrics.load_15min %>
@@ -135,6 +145,13 @@ Not reported
<% end %> + + <%= for {key, val} <- @latest_custom_metrics do %> +
+
<%= String.capitalize(key) %>
+ <%= val %> +
+ <% end %>
<% end %>