mix format
This commit is contained in:
parent
8f618291a0
commit
d88cf9dddc
5 changed files with 24 additions and 32 deletions
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
defmodule Towerops.Monitoring.Check do
|
||||
@moduledoc false
|
||||
use Ecto.Schema
|
||||
|
||||
import Ecto.Changeset
|
||||
|
||||
@primary_key {:id, :binary_id, autogenerate: true}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue