towerops/lib/towerops/snmp/sensor_reading.ex
Graham McIntire dc7db8ce39
Fix all remaining worker test failures
- Add 'error' to valid sensor reading status values
  - SensorReading changeset was rejecting 'error' status
  - PollWorker creates error readings when SNMP fails
  - Fixed validation to include: ok, warning, critical, error

- Replace log capture tests with functional assertions
  - ExUnit.CaptureLog not reliably capturing worker logs
  - Changed to verify actual behavior instead of log messages
  - Tests now check created records and state changes

- Add debug assertions to sensor error test
  - Verify SNMP device exists and has sensors
  - Better error messages for test failures

All 1045 tests now passing (0 failures, 2 skipped)
2026-01-20 12:20:04 -06:00

44 lines
1.2 KiB
Elixir

defmodule Towerops.Snmp.SensorReading do
@moduledoc """
Time-series sensor reading schema.
Stores individual sensor readings (value, status, timestamp) collected
during SNMP polling cycles. Stored in TimescaleDB hypertable.
"""
use Ecto.Schema
import Ecto.Changeset
alias Towerops.Snmp.Sensor
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "snmp_sensor_readings" do
field :value, :float
field :status, :string
field :checked_at, :utc_datetime
belongs_to :sensor, Sensor
timestamps(type: :utc_datetime, updated_at: false)
end
@type t :: %__MODULE__{
id: Ecto.UUID.t(),
value: float() | nil,
status: String.t(),
checked_at: DateTime.t(),
sensor_id: Ecto.UUID.t(),
sensor: Ecto.Association.NotLoaded.t() | Sensor.t(),
inserted_at: DateTime.t()
}
@doc false
def changeset(reading, attrs) do
reading
|> cast(attrs, [:sensor_id, :value, :status, :checked_at])
|> validate_required([:sensor_id, :status, :checked_at])
|> validate_inclusion(:status, ["ok", "warning", "critical", "error"])
|> foreign_key_constraint(:sensor_id)
end
end