defmodule ServerWeb.HostDetailLive do
use ServerWeb, :live_view
alias Server.{Metrics, Repo, Schema.Host}
@impl true
def mount(%{"name" => name}, _session, socket) do
case Repo.get_by(Host, name: name) do
nil ->
{:ok,
socket
|> put_flash(:error, "Host not found")
|> push_navigate(to: ~p"/")}
%Host{} = host ->
if connected?(socket),
do: Phoenix.PubSub.subscribe(Server.PubSub, "metrics:#{host.id}")
{:ok, socket |> assign(host: host, page_title: host.name) |> load_samples()}
end
end
@impl true
def handle_info({:metric_inserted, _host_id, _interval}, socket) do
{:noreply, load_samples(socket)}
end
defp load_samples(socket) do
host_id = socket.assigns.host.id
assign(socket,
fast: Metrics.latest_sample(host_id, "fast"),
medium: Metrics.latest_sample(host_id, "medium"),
slow: Metrics.latest_sample(host_id, "slow")
)
end
@impl true
def render(assigns) do
~H"""
| Dataset | Count | Oldest | Newest |
|---|---|---|---|
| {ds["name"]} | {ds["snapshot_count"]} | {unix_to_date(ds["oldest_snapshot_unix"])} | {unix_to_date(ds["newest_snapshot_unix"])} |
| Name | Type | Usage |
|---|---|---|
| {s["name"]} | {s["type"]} | {storage_usage(s)} |
| VMID | Name | Type | Status |
|---|---|---|---|
| {vm["vmid"]} | {vm["name"]} | {vm["type"]} | {vm["status"]} |