Skip to content

Commit

Permalink
- Add logging for live pa connectivity test
Browse files Browse the repository at this point in the history
- Refactor logging to be cleaner and more easily extendable
- Make sure unit tests actually run on mix test
  • Loading branch information
PaulJKim committed Jan 3, 2025
1 parent 2e3cfc0 commit 911fc72
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 85 deletions.
28 changes: 16 additions & 12 deletions lib/monitoring/uptime.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defmodule Monitoring.Uptime do
require Logger

@spec monitor_node_uptime(list(map()), integer()) :: :ok
def monitor_node_uptime(nodes, timestamp) do
@spec monitor_nodes(list(map()), integer()) :: :ok
def monitor_nodes(nodes, timestamp) do
{:ok, date_time} = DateTime.from_unix(timestamp, :second)

Enum.each(nodes, fn node ->
Expand All @@ -23,7 +23,7 @@ defmodule Monitoring.Uptime do
get_device_status_fields(node)

"unknown" ->
Logger.warn("unknown_node: ", inspect(node))
Logger.warn("unknown_node: #{inspect(node)}")
[]
end

Expand All @@ -43,6 +43,7 @@ defmodule Monitoring.Uptime do
"Status" => status
}) do
[
node_type: "live_pa_connectivity",
station_code: station_code,
ip_address: ip_address,
status: String.replace(status, " ", "_")
Expand All @@ -64,6 +65,7 @@ defmodule Monitoring.Uptime do
|> Enum.map(&(String.trim_leading(&1) |> String.split() |> List.first()))

[
node_type: "headend_server",
ip_address: ip_address,
uptime_days: days,
uptime_hours: hours,
Expand All @@ -83,31 +85,31 @@ defmodule Monitoring.Uptime do
"status" => status
}) do
[
application: String.replace(sw_component, " ", "_"),
node_type: String.replace(sw_component, " ", "_"),
is_online: is_online,
status: status
]
end

defp get_device_status_fields(%{"description" => description, "is_online" => is_online} = node) do
device_type = get_device_type(node)
node_type = get_node_type(node)

[line, station, device_id, maybe_sign_zone] = String.split(description, ":")

log_fields = [
node_type: device_type,
node_type: node_type,
line: line,
station: String.replace(station, " ", "_"),
device_id: device_id,
is_online: is_online
]

if device_type == "sign",
if node_type == "sign",
do: log_fields ++ [sign_zone: maybe_sign_zone],
else: log_fields
end

defp get_device_type(%{"node_type" => node_type}) do
defp get_node_type(%{"node_type" => node_type}) do
case node_type do
"SGN" ->
"sign"
Expand Down Expand Up @@ -143,9 +145,11 @@ defmodule Monitoring.Uptime do
DateTime.to_string(date_time)
]

Enum.reduce(fields, base, fn {k, v}, acc ->
acc ++ [" #{Atom.to_string(k)}=", v]
end)
|> Logger.info()
formatted_fields =
Enum.flat_map(fields, fn {k, v} ->
[" #{Atom.to_string(k)}=", v]
end)

Logger.info(base ++ formatted_fields)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule RealtimeSignsWeb.MonitoringController do
%{"time" => timestamp, "data" => %{"nodes" => nodes}} = _params
) do
Logger.info("Received uptime statuses from ARINC: Node count=#{Enum.count(nodes)}")
Uptime.monitor_node_uptime(nodes, timestamp)
Uptime.monitor_nodes(nodes, timestamp)
send_resp(conn, 200, "")
end

Expand Down
72 changes: 0 additions & 72 deletions test/monitoring/uptime_test.ex

This file was deleted.

114 changes: 114 additions & 0 deletions test/monitoring/uptime_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
defmodule Monitoring.UptimeTest do
use ExUnit.Case, async: true
import ExUnit.CaptureLog

test "Processes SCU uptime data" do
nodes = [
%{
"description" => "line:station:UNITTESTSCU001:SCU",
"node_type" => "PSS",
"is_online" => "true"
}
]

log =
capture_log([level: :info], fn ->
Monitoring.Uptime.monitor_nodes(nodes, 1_234_567_890)
end)

assert log =~
"node_type=scu line=line station=station device_id=UNITTESTSCU001 is_online=true"
end

test "Processes sign uptime data" do
nodes = [
%{
"description" => "line:station:UNITTESTSIGN001:C",
"node_type" => "SGN",
"is_online" => "true"
}
]

log =
capture_log([level: :info], fn ->
Monitoring.Uptime.monitor_nodes(nodes, 1_234_567_890)
end)

assert log =~
"node_type=sign line=line station=station device_id=UNITTESTSIGN001 is_online=true sign_zone=C"
end

test "Test unspecified node type" do
nodes = [
%{
"description" => "line:station:UNITTESTSIGN001:C",
"is_online" => "true"
}
]

log =
capture_log([level: :warn], fn ->
Monitoring.Uptime.monitor_nodes(nodes, 1_234_567_890)
end)

assert log =~ "unknown_node"
end

test "Processes live PA connectivity" do
nodes = [
%{
"Location ID" => "TEST",
"IP Address" => "0.0.0.0",
"Status" => "Timed out"
}
]

log =
capture_log([level: :info], fn ->
Monitoring.Uptime.monitor_nodes(nodes, 1_234_567_890)
end)

assert log =~
"node_type=live_pa_connectivity station_code=TEST ip_address=0.0.0.0 status=Timed_out"
end

test "Processes live PA application status" do
nodes = [
%{
"is_online" => "true",
"status" => "RUNNING",
"sw_component" => "Live PA"
}
]

log =
capture_log([level: :info], fn ->
Monitoring.Uptime.monitor_nodes(nodes, 1_234_567_890)
end)

assert log =~ "node_type=Live_PA is_online=true status=RUNNING"
end

test "Processes headend server stats" do
nodes = [
%{
"Available_D_Space" => "29.78",
"Available_C_Space" => "29.78",
"AvailableMemory" => "71.27",
"Total_D_Space" => "60",
"TotalMemory" => "127.69",
"Total_C_Space" => "60",
"Uptime" => "864 days, 21 hours, 55 minutes",
"IPAddress" => "0.0.0.0"
}
]

log =
capture_log([level: :info], fn ->
Monitoring.Uptime.monitor_nodes(nodes, 1_234_567_890)
end)

assert log =~
"node_type=headend_server ip_address=0.0.0.0 uptime_days=864 uptime_hours=21 uptime_minutes=55 total_memory=127.69 total_c_space=60 total_d_space=60 available_memory=71.27 available_c_space=29.78 available_d_space=29.78"
end
end

0 comments on commit 911fc72

Please sign in to comment.