Skip to content

Commit

Permalink
Use DeviceConnection status for map markers
Browse files Browse the repository at this point in the history
  • Loading branch information
elinol committed Nov 5, 2024
1 parent 6ec2a08 commit d9d2179
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
14 changes: 8 additions & 6 deletions lib/nerves_hub/devices.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/nerves_hub/devices/connections.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions lib/nerves_hub_web/live/dashboard/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d9d2179

Please sign in to comment.