towerops/lib/towerops/snmp/sensor_reading.ex
2026-01-17 15:08:49 -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"])
|> foreign_key_constraint(:sensor_id)
end
end