140 lines
5.5 KiB
Vue
140 lines
5.5 KiB
Vue
<script setup lang="ts">
|
|
import { Button } from "@/components/ui/button";
|
|
import { computed, onMounted, onUnmounted, ref } from "vue";
|
|
import { ChevronDown } from "@lucide/vue";
|
|
|
|
import { Badge } from "@/components/ui/badge";
|
|
import { DataTable, type DataTableColumn } from "@/components/ui/data-table";
|
|
import { Section } from "@/components/ui/section";
|
|
import { Timestamp } from "@/components/ui/timestamp";
|
|
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
|
|
import { api } from "@/lib/api";
|
|
|
|
interface AppliedRecord {
|
|
kind: string;
|
|
applied_version: number;
|
|
error?: string;
|
|
state: "current" | "failed" | "unknown";
|
|
}
|
|
|
|
interface Service {
|
|
service: string;
|
|
instance_id: string;
|
|
state: "up" | "degraded" | "down" | "unknown";
|
|
version: string;
|
|
install_type: string;
|
|
started_at: string;
|
|
reported_at: string;
|
|
detail?: string;
|
|
applied: AppliedRecord[];
|
|
checks?: { name: string; status: "ok" | "degraded"; detail?: string }[];
|
|
}
|
|
|
|
const services = ref<Service[]>([]);
|
|
const error = ref<string | null>(null);
|
|
let timer: ReturnType<typeof setInterval> | undefined;
|
|
|
|
const tone = { up: "ok", degraded: "warn", down: "danger", unknown: "idle" } as const;
|
|
|
|
const columns = (agent: boolean): DataTableColumn<Service>[] => [
|
|
{ id: "name", label: agent ? "Task" : "Service", fixed: true },
|
|
{ id: "version", label: "Version" },
|
|
{ id: "install", label: "Runs as", hidden: true },
|
|
{ id: "uptime", label: "Uptime" },
|
|
{ id: "seen", label: "Last seen", hidden: true },
|
|
{ id: "state", label: "State" },
|
|
];
|
|
|
|
// An agent reports for one task, so agents get their own table keyed by task.
|
|
const tables = computed(() => {
|
|
const agents = services.value.filter((s) => s.service === "archie-agent");
|
|
return [
|
|
{ title: "Services", key: "services", agent: false, rows: services.value.filter((s) => s.service !== "archie-agent") },
|
|
...(agents.length ? [{ title: "Agents", key: "agents", agent: true, rows: agents }] : []),
|
|
];
|
|
});
|
|
const rowKey = (service: Service) => `${service.service}/${service.instance_id}`;
|
|
|
|
function uptime(service: Service): string {
|
|
if (!service.started_at || service.state === "down") return "";
|
|
const seconds = Math.max(
|
|
0,
|
|
Math.round((Date.now() - Date.parse(service.started_at)) / 1000),
|
|
);
|
|
const days = Math.floor(seconds / 86400);
|
|
const hours = Math.floor((seconds % 86400) / 3600);
|
|
const minutes = Math.floor((seconds % 3600) / 60);
|
|
if (days) return `${days}d ${hours}h`;
|
|
if (hours) return `${hours}h ${minutes}m`;
|
|
return `${minutes}m`;
|
|
}
|
|
|
|
// A row names what is wrong with a service and nothing else: passing checks
|
|
// and applied versions that match are noise here.
|
|
function problems(service: Service): { key: string; text: string }[] {
|
|
return [
|
|
...(service.checks ?? [])
|
|
.filter((check) => check.status !== "ok")
|
|
.map((check) => ({
|
|
key: `check:${check.name}`,
|
|
text: [check.name, check.detail].filter(Boolean).join(": "),
|
|
})),
|
|
...service.applied
|
|
.filter((record) => record.error)
|
|
.map((record) => ({ key: `applied:${record.kind}`, text: `${record.kind}: ${record.error}` })),
|
|
];
|
|
}
|
|
|
|
const stateLabel = (service: Service, agent: boolean) => (agent && service.state === "down" ? "stale" : service.state);
|
|
const reasons = (service: Service) => {
|
|
const found = problems(service).map((problem) => problem.text);
|
|
return found.length || !service.detail ? found : [service.detail];
|
|
};
|
|
|
|
async function load(): Promise<void> {
|
|
try {
|
|
services.value = await api.services<Service[]>();
|
|
error.value = null;
|
|
} catch (err) {
|
|
error.value = String((err as Error).message || err);
|
|
}
|
|
}
|
|
|
|
onMounted(() => {
|
|
void load();
|
|
timer = setInterval(() => void load(), 15_000);
|
|
});
|
|
onUnmounted(() => clearInterval(timer));
|
|
</script>
|
|
|
|
<template>
|
|
<p v-if="error" class="mb-4 text-sm text-danger">{{ error }}</p>
|
|
<Section v-for="table in tables" v-else :key="table.key" :title="table.title">
|
|
<DataTable :columns="columns(table.agent)" :rows="table.rows" :row-key="rowKey" :storage-key="`status.${table.key}`">
|
|
<template #cell-name="{ row }">
|
|
<RouterLink v-if="table.agent" :to="`/tasks/${row.instance_id}`" class="hover:underline">#{{ row.instance_id }}</RouterLink>
|
|
<span v-else class="font-mono">{{ row.service }}</span>
|
|
</template>
|
|
<template #cell-version="{ row }"><span class="font-mono text-fg-muted">{{ row.version }}</span></template>
|
|
<template #cell-install="{ row }"><span class="text-fg-muted">{{ row.install_type }}</span></template>
|
|
<template #cell-uptime="{ row }"><span class="tabular-nums">{{ uptime(row) }}</span></template>
|
|
<template #cell-seen="{ row }"><Timestamp :at="row.reported_at" class="text-fg-muted" /></template>
|
|
<template #cell-state="{ row }">
|
|
<template v-if="row.state !== 'up'">
|
|
<Popover v-if="reasons(row).length">
|
|
<PopoverTrigger as-child>
|
|
<Button type="button" variant="ghost" class="h-auto rounded-sm p-0" :aria-label="`Why ${row.service} is ${row.state}`">
|
|
<Badge :variant="tone[row.state]">{{ stateLabel(row, table.agent) }}<ChevronDown data-icon="inline-end" /></Badge>
|
|
</Button>
|
|
</PopoverTrigger>
|
|
<PopoverContent align="start" class="w-96 max-w-[calc(100vw-2rem)] space-y-1 text-sm">
|
|
<p v-for="reason in reasons(row)" :key="reason" class="break-words">{{ reason }}</p>
|
|
</PopoverContent>
|
|
</Popover>
|
|
<Badge v-else :variant="tone[row.state]">{{ stateLabel(row, table.agent) }}</Badge>
|
|
</template>
|
|
</template>
|
|
</DataTable>
|
|
</Section>
|
|
</template>
|