Skip to content

Commit

Permalink
Reintroduce missing custom metrics and metadata to device page. (#1578)
Browse files Browse the repository at this point in the history
* Reintroduce missing custom metrics and metadata to device page.
  • Loading branch information
elinol authored Oct 3, 2024
1 parent c61a588 commit 711e985
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
2 changes: 1 addition & 1 deletion assets/css/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ html body {

.display-box.metrics {
display: flex;
flex-wrap: row;
flex-wrap: wrap;
gap: 4px;
}

Expand Down
14 changes: 14 additions & 0 deletions lib/nerves_hub/devices/metrics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 29 additions & 2 deletions lib/nerves_hub_web/live/devices/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 20 additions & 3 deletions lib/nerves_hub_web/live/devices/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@
</div>

<DeviceLocation.render location={@device.connection_metadata["location"]} />

<%= if !Enum.empty?(@metadata) do %>
<h3 class="mb-2">Metadata</h3>
<div class="display-box metrics">
<div :for={{key, value} <- @metadata} class="display-box-item">
<div :if={value != ""} class="help-text"><%= key |> String.replace("_", " ") |> String.capitalize() %></div>
<p><%= value %></p>
</div>
</div>
<% end %>
</div>

<div>
Expand Down Expand Up @@ -99,15 +109,15 @@
</button>
</div>

<%= if (!Enum.any?(Map.values(@latest_metrics))) do %>
<%= if (!Enum.any?(Map.values(@latest_metrics)) or !Enum.any?(Map.values(@latest_custom_metrics))) do %>
<div class="display-box">
No health information have been received for this device.
</div>
<% end %>
<div>
<%= if (Enum.any?(Map.values(@latest_metrics))) do %>
<div :if={@latest_metrics.load_1min} class="device-health">
<div class="callout">
<div class="device-health">
<div :if={@latest_metrics.load_1min} class="callout">
<div class="help-text label">Load avg</div>
<%= @latest_metrics.load_1min %> | <%= @latest_metrics.load_5min %> | <%= @latest_metrics.load_15min %>
</div>
Expand Down Expand Up @@ -135,6 +145,13 @@
Not reported
</div>
<% end %>

<%= for {key, val} <- @latest_custom_metrics do %>
<div class="callout">
<div class="help-text label"><%= String.capitalize(key) %></div>
<%= val %>
</div>
<% end %>
</div>
<% end %>

Expand Down

0 comments on commit 711e985

Please sign in to comment.