diff --git a/lib/towerops/monitoring.ex b/lib/towerops/monitoring.ex index 03764787..328bac51 100644 --- a/lib/towerops/monitoring.ex +++ b/lib/towerops/monitoring.ex @@ -4,8 +4,10 @@ defmodule Towerops.Monitoring do """ import Ecto.Query - alias Towerops.Repo + + alias Ecto.Adapters.SQL alias Towerops.Monitoring.Check + alias Towerops.Repo @doc """ Creates a monitoring check. @@ -20,32 +22,21 @@ defmodule Towerops.Monitoring do Returns the list of checks for an equipment. """ def list_equipment_checks(equipment_id, limit \\ 100) do - from(c in Check, - where: c.equipment_id == ^equipment_id, - order_by: [desc: c.checked_at], - limit: ^limit - ) - |> Repo.all() + Repo.all(from(c in Check, where: c.equipment_id == ^equipment_id, order_by: [desc: c.checked_at], limit: ^limit)) end @doc """ Returns the latest check for an equipment. """ def get_latest_check(equipment_id) do - from(c in Check, - where: c.equipment_id == ^equipment_id, - order_by: [desc: c.checked_at], - limit: 1 - ) - |> Repo.one() + Repo.one(from(c in Check, where: c.equipment_id == ^equipment_id, order_by: [desc: c.checked_at], limit: 1)) end @doc """ Deletes old monitoring checks older than the given date. """ def delete_old_checks(older_than_date) do - from(c in Check, where: c.checked_at < ^older_than_date) - |> Repo.delete_all() + Repo.delete_all(from(c in Check, where: c.checked_at < ^older_than_date)) end @doc """ @@ -70,7 +61,7 @@ defmodule Towerops.Monitoring do ORDER BY bucket ASC """ - Ecto.Adapters.SQL.query!( + SQL.query!( Repo, query, [equipment_id, start_time, end_time] @@ -99,7 +90,7 @@ defmodule Towerops.Monitoring do ORDER BY bucket ASC """ - Ecto.Adapters.SQL.query!( + SQL.query!( Repo, query, [equipment_id, start_time, end_time] @@ -110,7 +101,7 @@ defmodule Towerops.Monitoring do Gets uptime percentage for equipment over the last N days. """ def get_uptime_percentage(equipment_id, days_ago \\ 30) do - start_time = DateTime.utc_now() |> DateTime.add(-days_ago * 24 * 60 * 60, :second) + start_time = DateTime.add(DateTime.utc_now(), -days_ago * 24 * 60 * 60, :second) query = """ SELECT @@ -120,7 +111,7 @@ defmodule Towerops.Monitoring do AND bucket >= $2 """ - case Ecto.Adapters.SQL.query!(Repo, query, [equipment_id, start_time]) do + case SQL.query!(Repo, query, [equipment_id, start_time]) do %{rows: [[uptime]]} when not is_nil(uptime) -> Float.round(uptime, 2) diff --git a/lib/towerops/monitoring/check.ex b/lib/towerops/monitoring/check.ex index 04dbfc44..8bdc9e20 100644 --- a/lib/towerops/monitoring/check.ex +++ b/lib/towerops/monitoring/check.ex @@ -1,5 +1,7 @@ defmodule Towerops.Monitoring.Check do + @moduledoc false use Ecto.Schema + import Ecto.Changeset @primary_key {:id, :binary_id, autogenerate: true} diff --git a/lib/towerops/monitoring/ping.ex b/lib/towerops/monitoring/ping.ex index 63e624f5..a41bcade 100644 --- a/lib/towerops/monitoring/ping.ex +++ b/lib/towerops/monitoring/ping.ex @@ -42,17 +42,15 @@ defmodule Towerops.Monitoring.Ping do end defp run_system_ping(ip_address, args) do - try do - case System.cmd("ping", args ++ [ip_address], stderr_to_stdout: true) do - {_output, 0} -> - :ok + case System.cmd("ping", args ++ [ip_address], stderr_to_stdout: true) do + {_output, 0} -> + :ok - {_output, _exit_code} -> - {:error, :timeout_or_unreachable} - end - rescue - e -> - {:error, {:exception, Exception.message(e)}} + {_output, _exit_code} -> + {:error, :timeout_or_unreachable} end + rescue + e -> + {:error, {:exception, Exception.message(e)}} end end diff --git a/lib/towerops/monitoring/supervisor.ex b/lib/towerops/monitoring/supervisor.ex index ed3293fe..a59fdd57 100644 --- a/lib/towerops/monitoring/supervisor.ex +++ b/lib/towerops/monitoring/supervisor.ex @@ -21,7 +21,7 @@ defmodule Towerops.Monitoring.Supervisor do # Start all monitors after supervisor is initialized (but not in test mode) # In test mode, the Repo uses Sandbox pool which we can detect - unless test_mode?() do + if !test_mode?() do Task.start(fn -> # Wait briefly for the supervisor tree to be fully initialized Process.sleep(100) @@ -58,8 +58,7 @@ defmodule Towerops.Monitoring.Supervisor do Starts monitors for all equipment that has monitoring enabled. """ def start_all_monitors do - Towerops.Equipment.list_monitored_equipment() - |> Enum.each(fn equipment -> + Enum.each(Towerops.Equipment.list_monitored_equipment(), fn equipment -> start_monitor(equipment.id) end) end diff --git a/priv/repo/migrations/20251221193939_create_monitoring_checks.exs b/priv/repo/migrations/20251221193939_create_monitoring_checks.exs index 5cc2bb5a..fc8a6381 100644 --- a/priv/repo/migrations/20251221193939_create_monitoring_checks.exs +++ b/priv/repo/migrations/20251221193939_create_monitoring_checks.exs @@ -5,8 +5,10 @@ defmodule Towerops.Repo.Migrations.CreateMonitoringChecks do # Create the table create table(:monitoring_checks, primary_key: false) do add :id, :binary_id, primary_key: true + add :equipment_id, references(:equipment, type: :binary_id, on_delete: :delete_all), null: false + add :status, :string, null: false add :response_time_ms, :integer add :checked_at, :utc_datetime, null: false