Skip to content

Commit 7f4f963

Browse files
committed
fix: do not crash on not loaded device attributes
The device attributes when not loaded were not `nil`, but rather an `Ash` internal representation of it. When trying to query for them the server crashed, making it impossible to read any device. This PR removes the code handling this case and makes use of ash internal cache system. Signed-off-by: Luca Zaninotto <luca.zaninotto@secomind.com>
1 parent ab037fd commit 7f4f963

1 file changed

Lines changed: 4 additions & 21 deletions

File tree

  • backend/lib/edgehog/geolocation/providers

backend/lib/edgehog/geolocation/providers/ipbase.ex

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
#
21
# This file is part of Edgehog.
32
#
4-
# Copyright 2021 - 2025 SECO Mind Srl
3+
# Copyright 2021-2026 SECO Mind Srl
54
#
65
# Licensed under the Apache License, Version 2.0 (the "License");
76
# you may not use this file except in compliance with the License.
@@ -16,7 +15,6 @@
1615
# limitations under the License.
1716
#
1817
# SPDX-License-Identifier: Apache-2.0
19-
#
2018

2119
defmodule Edgehog.Geolocation.Providers.IPBase do
2220
@moduledoc false
@@ -27,8 +25,6 @@ defmodule Edgehog.Geolocation.Providers.IPBase do
2725
alias Edgehog.EdgehogTeslaClient
2826
alias Edgehog.Geolocation.Position
2927

30-
@device_status_attributes [:last_connection, :last_disconnection, :last_seen_ip]
31-
3228
@impl Edgehog.Geolocation.GeolocationProvider
3329
def geolocate(%Device{} = device) do
3430
with {:ok, device_status} <- fetch_device_status(device) do
@@ -49,25 +45,12 @@ defmodule Edgehog.Geolocation.Providers.IPBase do
4945
end
5046

5147
defp fetch_device_status(%Device{} = device) do
52-
if uninitialized_device_status?(device) do
53-
with {:ok, device_data} <- Ash.load(device, :device_status),
54-
:ok <- validate_device_status_exists(device_data.device_status) do
55-
{:ok, device_data.device_status}
56-
end
57-
else
58-
{:ok,
59-
%{
60-
last_seen_ip: device.last_seen_ip,
61-
last_connection: device.last_connection,
62-
last_disconnection: device.last_disconnection
63-
}}
48+
with {:ok, device_data} <- Ash.load(device, :device_status),
49+
:ok <- validate_device_status_exists(device_data.device_status) do
50+
{:ok, device_data.device_status}
6451
end
6552
end
6653

67-
defp uninitialized_device_status?(device) do
68-
Enum.any?(@device_status_attributes, &(Map.get(device, &1) == nil))
69-
end
70-
7154
defp validate_device_status_exists(nil), do: {:error, :device_status_not_found}
7255
defp validate_device_status_exists(_), do: :ok
7356

0 commit comments

Comments
 (0)