fix mistaken poller ids

This commit is contained in:
Graham McIntire 2026-02-05 13:57:15 -06:00
parent 472482c4f5
commit cb0cc66768
No known key found for this signature in database
3 changed files with 18 additions and 21 deletions

View file

@ -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 """

View file

@ -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}")

View file

@ -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