feat(server): GET /api/hosts/:name returns latest fast/medium/slow samples

This commit is contained in:
Carsten 2026-04-21 22:30:35 +02:00
parent f09a77996b
commit 30b507ba6b
3 changed files with 71 additions and 4 deletions

View file

@ -0,0 +1,30 @@
defmodule ServerWeb.HostController do
use ServerWeb, :controller
alias Server.{Metrics, Repo, Schema.Host}
def show(conn, %{"name" => name}) do
case Repo.get_by(Host, name: name) do
nil ->
conn
|> put_status(:not_found)
|> json(%{error: "host_not_found"})
%Host{} = host ->
samples =
for interval <- ~w(fast medium slow),
sample = Metrics.latest_sample(host.id, interval),
into: %{} do
{interval, %{collected_at: sample.collected_at, payload: sample.payload}}
end
json(conn, %{
name: host.name,
status: host.status,
agent_version: host.agent_version,
last_seen_at: host.last_seen_at,
samples: samples
})
end
end
end