1import { A, useParams } from "@solidjs/router";
2import RotateCcw from "lucide-solid/icons/rotate-ccw";
3import Server from "lucide-solid/icons/server";
4import { createSignal, Match, onCleanup, Show, Switch } from "solid-js";
5import { queries } from "../api.ts";
6import { AppIcon } from "../components/AppIcon.tsx";
7import { ListPage } from "../components/ListPage.tsx";
8import { lastGood, Loaded } from "../components/Loaded.tsx";
9import { RangePicker } from "../components/Metric.tsx";
10import { StatusLabel } from "../components/Status.tsx";
11import { Strip } from "../components/Strip.tsx";
12import { TabBar } from "../components/TabBar.tsx";
13import { bytes, cores, datetime, duration, plural } from "../format.ts";
14import { AppMetrics, Logs } from "./Service.tsx";
15
16/** Remounts per app, so switching never shows the last one's logs. */
17export function Vps() {
18 const params = useParams<{ name: string }>();
19 return <Show when={params.name} keyed><VpsPage /></Show>;
20}
21
22function VpsPage() {
23 const params = useParams<{ name: string; tab?: string }>();
24 const [services, { refetch }] = queries.vps.use();
25 const timer = setInterval(refetch, 15_000);
26 onCleanup(() => clearInterval(timer));
27 const loaded = lastGood(services);
28 const [range, setRange] = createSignal(3600);
29 return (
30 <Loaded data={services} what="VPS services" retry={refetch} skeleton={<div class="page"><div class="skeleton" style={{ height: "112px" }} /></div>}>
31 {() => (
32 <Show when={loaded()?.find((service) => service.name === params.name)} fallback={
33 <div class="page empty">The VPS hasn't reported {params.name} in the last week.</div>
34 }>
35 {(data) => {
36 const tabHref = (tab: string) => `/vps/${data().name}${tab ? "/" + tab : ""}`;
37 let tools!: HTMLDivElement;
38 return (
39 <ListPage id="service" class="service-page" flush head={
40 <div class="page-head">
41 <span class="service-icon"><AppIcon id={data().id} name={data().name} icon={null} /></span>
42 <h1>{data().name}</h1>
43 <div class="stats">
44 <Show when={data().health === "healthy" && data().startedAt} fallback={
45 <span class="stat" role="status"><StatusLabel health={data().health} /></span>
46 }>
47 {(t) => (
48 <span class="stat" role="status" tabIndex={0} data-tip={`since ${datetime(t())}`}>
49 <StatusLabel health="healthy">up {duration(Date.now() / 1000 - t())}</StatusLabel>
50 </span>
51 )}
52 </Show>
53 <span class="stat"><Server size={13} aria-hidden="true" /><b>usw-server-1</b></span>
54 <Show when={data().restarts}>
55 {(restarts) => <span class="stat"><RotateCcw size={13} aria-hidden="true" />{plural(restarts(), "restart")}</span>}
56 </Show>
57 </div>
58 </div>
59 } summary={
60 <Show when={data().cpu !== null}>
61 <div class="service-strips">
62 <Strip title="cpu" metric="service.cpu" service={data().id} range={range()} format={cores} height={64}
63 tabs={<RangePicker value={range()} onChange={setRange} />}
64 now={() => ({ value: cores(data().cpu ?? 0) })} />
65 <Strip title="memory" metric="service.memory" service={data().id} range={range()} format={bytes}
66 height={64 + 24} last now={() => ({ value: bytes(data().memory ?? 0) })} />
67 </div>
68 </Show>
69 }>
70 <div ref={tools} class="tab-row">
71 <TabBar label="View">
72 <A href={tabHref("")} end>logs</A>
73 <Show when={data().applicationMetrics.length}><A href={tabHref("metrics")} end>metrics</A></Show>
74 </TabBar>
75 </div>
76 <Switch>
77 <Match when={!params.tab}><Logs service={{ id: data().id, containers: [], logs: null }} tools={tools} /></Match>
78 <Match when={params.tab === "metrics" && data().applicationMetrics.length}>
79 <AppMetrics service={data()} range={range()} />
80 </Match>
81 </Switch>
82 </ListPage>
83 );
84 }}
85 </Show>
86 )}
87 </Loaded>
88 );
89}
90