From d9d2179ac6ffea17dc75eca39afa123b5a629e41 Mon Sep 17 00:00:00 2001 From: Elin Olsson Date: Tue, 5 Nov 2024 17:22:22 +0100 Subject: [PATCH] Use DeviceConnection status for map markers --- lib/nerves_hub/devices.ex | 14 ++++++++------ lib/nerves_hub/devices/connections.ex | 2 +- lib/nerves_hub_web/live/dashboard/index.ex | 5 +++-- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/nerves_hub/devices.ex b/lib/nerves_hub/devices.ex index ff6200f38..5e169d313 100644 --- a/lib/nerves_hub/devices.ex +++ b/lib/nerves_hub/devices.ex @@ -104,18 +104,20 @@ defmodule NervesHub.Devices do def get_minimal_device_location_by_org_id_and_product_id(org_id, product_id) do Device - |> select([d], %{ + |> where(org_id: ^org_id) + |> where(product_id: ^product_id) + |> where([d], not is_nil(fragment("?->'location'->'latitude'", d.connection_metadata))) + |> where([d], not is_nil(fragment("?->'location'->'longitude'", d.connection_metadata))) + |> join(:left, [d], dc in subquery(Connections.latest_row_query()), on: dc.device_id == d.id) + |> where([d, dc], dc.rn == 1) + |> select([d, dc], %{ id: d.id, identifier: d.identifier, - connection_status: d.connection_status, + connection_status: dc.status, latitude: fragment("?->'location'->'latitude'", d.connection_metadata), longitude: fragment("?->'location'->'longitude'", d.connection_metadata), firmware_uuid: fragment("?->'uuid'", d.firmware_metadata) }) - |> where(org_id: ^org_id) - |> where(product_id: ^product_id) - |> where([d], not is_nil(fragment("?->'location'->'latitude'", d.connection_metadata))) - |> where([d], not is_nil(fragment("?->'location'->'longitude'", d.connection_metadata))) |> Repo.exclude_deleted() |> Repo.all() end diff --git a/lib/nerves_hub/devices/connections.ex b/lib/nerves_hub/devices/connections.ex index 151ee7eaf..a83b8439f 100644 --- a/lib/nerves_hub/devices/connections.ex +++ b/lib/nerves_hub/devices/connections.ex @@ -98,7 +98,7 @@ defmodule NervesHub.Devices.Connections do |> select([lr, d], d.id) end - defp latest_row_query() do + def latest_row_query() do DeviceConnection |> select([dc], %{ device_id: dc.device_id, diff --git a/lib/nerves_hub_web/live/dashboard/index.ex b/lib/nerves_hub_web/live/dashboard/index.ex index b80ce7cce..c373a4c96 100644 --- a/lib/nerves_hub_web/live/dashboard/index.ex +++ b/lib/nerves_hub_web/live/dashboard/index.ex @@ -85,7 +85,8 @@ defmodule NervesHubWeb.Live.Dashboard.Index do duration = t - socket.assigns.time if duration >= @delay do - devices = Devices.get_minimal_device_location_by_org_id_and_product_id(org.id, product.id) + devices = + Devices.get_minimal_device_location_by_org_id_and_product_id(org.id, product.id) latest_firmwares = Deployments.get_deployments_by_product(product.id) @@ -144,7 +145,7 @@ defmodule NervesHubWeb.Live.Dashboard.Index do markers end - defp get_connection_status([%{status: :connected}]), do: "connected" + defp get_connection_status(:connected), do: "connected" defp get_connection_status(_), do: "offline" defp refresh_after(socket, delay) do