42 lines
1.3 KiB
Elixir
42 lines
1.3 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])
|
|
|> round_response_time()
|
|
|> foreign_key_constraint(:device_id, match: :suffix)
|
|
|> foreign_key_constraint(:agent_token_id, match: :suffix)
|
|
end
|
|
|
|
defp round_response_time(changeset) do
|
|
case get_change(changeset, :response_time_ms) do
|
|
nil -> changeset
|
|
ms when is_float(ms) -> put_change(changeset, :response_time_ms, Float.round(ms, 1))
|
|
_ -> changeset
|
|
end
|
|
end
|
|
end
|