feat(server): channel persists fast/medium/slow samples to metrics table

This commit is contained in:
Carsten 2026-04-21 22:28:36 +02:00
parent 687fc17082
commit 751e035579
2 changed files with 76 additions and 15 deletions

View file

@ -48,8 +48,8 @@ defmodule ServerWeb.HostChannelTest do
end
end
describe "metric:fast event" do
setup %{token: token} do
describe "metric events persist to DB" do
setup %{token: token, host: host} do
{:ok, socket} = connect(AgentSocket, %{})
{:ok, _reply, joined} =
@ -58,17 +58,63 @@ defmodule ServerWeb.HostChannelTest do
"agent_version" => "0.1.0"
})
%{socket: joined}
%{socket: joined, host: host}
end
test "accepts metric payload and replies :ok", %{socket: socket} do
test "metric:fast is stored with interval=fast", %{socket: socket, host: host} do
ts = "2026-04-21T12:00:00.123456Z"
ref =
push(socket, "metric:fast", %{
"collected_at" => "2026-04-21T12:00:00Z",
"collected_at" => ts,
"data" => %{"cpu_percent" => 12.3, "load1" => 0.2}
})
assert_reply ref, :ok
sample = Server.Metrics.latest_sample(host.id, "fast")
assert sample != nil
assert sample.payload == %{"cpu_percent" => 12.3, "load1" => 0.2}
{:ok, expected, _} = DateTime.from_iso8601(ts)
assert DateTime.compare(sample.collected_at, expected) == :eq
end
test "metric:medium is stored with interval=medium", %{socket: socket, host: host} do
ref =
push(socket, "metric:medium", %{
"collected_at" => "2026-04-21T12:05:00Z",
"data" => %{"vms_detail" => []}
})
assert_reply ref, :ok
sample = Server.Metrics.latest_sample(host.id, "medium")
assert sample != nil
assert sample.payload == %{"vms_detail" => []}
end
test "metric:slow is stored with interval=slow", %{socket: socket, host: host} do
ref =
push(socket, "metric:slow", %{
"collected_at" => "2026-04-21T12:30:00Z",
"data" => %{"system_info" => %{"pveversion" => "8.3.0"}}
})
assert_reply ref, :ok
sample = Server.Metrics.latest_sample(host.id, "slow")
assert sample != nil
assert sample.payload == %{"system_info" => %{"pveversion" => "8.3.0"}}
end
test "replies :error when collected_at is missing", %{socket: socket} do
ref = push(socket, "metric:fast", %{"data" => %{}})
assert_reply ref, :error, %{reason: "missing_collected_at"}
end
test "replies :error when data is missing", %{socket: socket} do
ref = push(socket, "metric:fast", %{"collected_at" => "2026-04-21T12:00:00Z"})
assert_reply ref, :error, %{reason: "missing_data"}
end
end