towerops/lib/towerops/monitoring/check.ex

33 lines
1 KiB
Elixir

defmodule Towerops.Monitoring.Check do
@moduledoc """
Monitoring check schema for device reachability status.
Records the result of each monitoring check (success/failure) with latency
and tracks which agent performed the check for intelligent routing.
"""
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "monitoring_checks" do
field :status, Ecto.Enum, values: [:success, :failure]
field :response_time_ms, :float
field :checked_at, :utc_datetime
belongs_to :device, Towerops.Devices.Device
belongs_to :agent_token, Towerops.Agents.AgentToken
timestamps(type: :utc_datetime, updated_at: false)
end
@doc false
def changeset(check, attrs) do
check
|> cast(attrs, [:device_id, :agent_token_id, :status, :response_time_ms, :checked_at])
|> validate_required([:device_id, :status, :checked_at])
|> foreign_key_constraint(:device_id, match: :suffix)
|> foreign_key_constraint(:agent_token_id, match: :suffix)
end
end