From cb0cc66768165c41ee5a016078008f6ca4b3cdf3 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Thu, 5 Feb 2026 13:57:15 -0600 Subject: [PATCH] fix mistaken poller ids --- lib/towerops/devices.ex | 7 +++-- lib/towerops_web/live/graph_live/show.ex | 4 +-- .../workers/device_poller_worker_test.exs | 28 ++++++++----------- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/lib/towerops/devices.ex b/lib/towerops/devices.ex index a016d942..c44c12c9 100644 --- a/lib/towerops/devices.ex +++ b/lib/towerops/devices.ex @@ -173,9 +173,10 @@ defmodule Towerops.Devices do """ def resolve_agent_token_id(device) do # Ensure organization, site (if present), and agent_assignments are preloaded + # Note: site: :organization is needed for fallback agent token resolution device = if device.site_id do - Repo.preload(device, [:organization, :site, agent_assignments: []]) + Repo.preload(device, [:organization, [site: :organization], agent_assignments: []]) else Repo.preload(device, [:organization, agent_assignments: []]) end @@ -216,7 +217,7 @@ defmodule Towerops.Devices do |> Repo.get(id) |> case do nil -> nil - device -> Repo.preload(device, [:organization, :site]) + device -> Repo.preload(device, [:organization, site: :organization]) end end @@ -226,7 +227,7 @@ defmodule Towerops.Devices do def get_device!(id) do DeviceSchema |> Repo.get!(id) - |> Repo.preload([:organization, :site]) + |> Repo.preload([:organization, site: :organization]) end @doc """ diff --git a/lib/towerops_web/live/graph_live/show.ex b/lib/towerops_web/live/graph_live/show.ex index 719f6f49..80c272ea 100644 --- a/lib/towerops_web/live/graph_live/show.ex +++ b/lib/towerops_web/live/graph_live/show.ex @@ -686,8 +686,8 @@ defmodule ToweropsWeb.GraphLive.Show do now = DateTime.utc_now() timestamp_ms = DateTime.to_unix(now, :millisecond) - # Check if device has agent assignment - has_agent = socket.assigns[:has_agent_assignment] || false + # Check if device has agent assignment (fresh check each poll to avoid stale data) + has_agent = Agents.get_device_assignment(device.id) != nil Logger.info("Live poll for device #{device.name}: has_agent=#{has_agent}") diff --git a/test/towerops/workers/device_poller_worker_test.exs b/test/towerops/workers/device_poller_worker_test.exs index a106af46..e3d9d2e5 100644 --- a/test/towerops/workers/device_poller_worker_test.exs +++ b/test/towerops/workers/device_poller_worker_test.exs @@ -177,9 +177,9 @@ defmodule Towerops.Workers.DevicePollerWorkerTest do {:ok, processor} = Repo.insert(%Towerops.Snmp.Processor{ snmp_device_id: snmp_device.id, - processor_index: 1, + processor_index: "1", processor_type: "hr_processor", - processor_descr: "CPU 1" + description: "CPU 1" }) # 5. Mock SNMP response @@ -207,22 +207,18 @@ defmodule Towerops.Workers.DevicePollerWorkerTest do "1.3.6.1.2.1.2.2.1.20.2" -> {:ok, 0} "1.3.6.1.2.1.2.2.1.13.2" -> {:ok, 0} "1.3.6.1.2.1.2.2.1.19.2" -> {:ok, 0} + # Interface change detection OIDs (for interface 2) + # Speed + "1.3.6.1.2.1.2.2.1.5.2" -> {:ok, 100_000_000} + # Physical address (MAC) + "1.3.6.1.2.1.2.2.1.6.2" -> {:ok, <<0, 17, 34, 51, 68, 85>>} + # Admin status (down=2) + "1.3.6.1.2.1.2.2.1.7.2" -> {:ok, 2} + # Oper status (up=1) + "1.3.6.1.2.1.2.2.1.8.2" -> {:ok, 1} _ -> {:error, :no_such_object} end end) - |> stub(:get_multiple, fn _target, oids, _opts -> - # 1.3.6.1.2.1.2.2.1.5.2 (speed) - # 1.3.6.1.2.1.2.2.1.6.2 (phys) - # 1.3.6.1.2.1.2.2.1.7.2 (admin) - # 1.3.6.1.2.1.2.2.1.8.2 (oper) - if Enum.any?(oids, &String.contains?(&1, ".2")) do - # Change admin status to down (2) - # 1=up - {:ok, [100_000_000, <<0, 17, 34, 51, 68, 85>>, 2, 1]} - else - {:error, :timeout} - end - end) # Allow other calls (like interfaces, neighbors etc) to return empty/ok |> stub(:walk, fn _target, _oid, _opts -> {:ok, []} end) @@ -258,7 +254,7 @@ defmodule Towerops.Workers.DevicePollerWorkerTest do assert length(readings) >= 1 reading = List.last(readings) assert reading.processor_id == processor.id - assert reading.usage_percent == 25 + assert reading.load_percent == 25.0 end test "handles SNMP errors gracefully", %{site: site} do