Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Add Docker Swarm frontend components

rcourtman233278a9d28a
diff --git a/frontend-modern/src/components/Docker/DockerClusterServicesTable.tsx b/frontend-modern/src/components/Docker/DockerClusterServicesTable.tsx
new file mode 100644
index 0000000000..5bb87a8cee
--- /dev/null
+++ b/frontend-modern/src/components/Docker/DockerClusterServicesTable.tsx
@@ -0,0 +1,426 @@
+import { Component, For, Show, createMemo, createSignal } from 'solid-js';
+import type { DockerHost } from '@/types/api';
+import { Card } from '@/components/shared/Card';
+import { EmptyState } from '@/components/shared/EmptyState';
+import { StatusDot } from '@/components/shared/StatusDot';
+import { formatRelativeTime } from '@/utils/format';
+import { usePersistentSignal } from '@/hooks/usePersistentSignal';
+import {
+ groupHostsByCluster,
+ aggregateClusterServices,
+ formatNodeDistribution,
+ getServiceHealthStatus,
+ type SwarmCluster,
+ type ClusterService,
+} from './swarmClusterHelpers';
+
+interface DockerClusterServicesTableProps {
+ hosts: DockerHost[];
+ searchTerm?: string;
+}
+
+type SortKey = 'name' | 'stack' | 'mode' | 'replicas' | 'nodes' | 'status';
+type SortDirection = 'asc' | 'desc';
+
+const SORT_KEYS: SortKey[] = ['name', 'stack', 'mode', 'replicas', 'nodes', 'status'];
+
+const SORT_DEFAULT_DIRECTION: Record<SortKey, SortDirection> = {
+ name: 'asc',
+ stack: 'asc',
+ mode: 'asc',
+ replicas: 'desc',
+ nodes: 'desc',
+ status: 'desc',
+};
+
+const STATUS_PRIORITY: Record<string, number> = {
+ critical: 3,
+ degraded: 2,
+ healthy: 1,
+};
+
+const parseSearchTerm = (term?: string): string[] => {
+ if (!term) return [];
+ return term
+ .trim()
+ .toLowerCase()
+ .split(/\s+/)
+ .filter(Boolean);
+};
+
+const serviceMatchesSearch = (service: ClusterService, tokens: string[]): boolean => {
+ if (tokens.length === 0) return true;
+
+ const searchableText = [
+ service.service.name || '',
+ service.service.stack || '',
+ service.service.image || '',
+ service.service.mode || '',
+ ...service.nodes.map((n) => n.hostname),
+ ]
+ .join(' ')
+ .toLowerCase();
+
+ return tokens.every((token) => searchableText.includes(token));
+};
+
+const ClusterGroupHeader: Component<{
+ cluster: SwarmCluster;
+ serviceCount: number;
+ isExpanded: boolean;
+ onToggle: () => void;
+}> = (props) => {
+ const managerCount = createMemo(
+ () => props.cluster.hosts.filter((h) => h.swarm?.nodeRole === 'manager').length
+ );
+ const workerCount = createMemo(
+ () => props.cluster.hosts.filter((h) => h.swarm?.nodeRole === 'worker').length
+ );
+
+ return (
+ <button
+ type="button"
+ onClick={props.onToggle}
+ class="w-full flex items-center gap-3 px-4 py-3 bg-purple-50 dark:bg-purple-900/20 hover:bg-purple-100 dark:hover:bg-purple-900/30 border-b border-purple-200 dark:border-purple-800 transition-colors"
+ >
+ <svg
+ class={`w-4 h-4 text-purple-600 dark:text-purple-400 transition-transform ${props.isExpanded ? 'rotate-90' : ''}`}
+ fill="none"
+ viewBox="0 0 24 24"
+ stroke="currentColor"
+ >
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
+ </svg>
+
+ <div class="flex items-center gap-2">
+ <svg
+ class="w-5 h-5 text-purple-600 dark:text-purple-400"
+ fill="none"
+ viewBox="0 0 24 24"
+ stroke="currentColor"
+ >
+ <path
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ stroke-width="2"
+ d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10"
+ />
+ </svg>
+ <span class="font-medium text-purple-900 dark:text-purple-100">
+ {props.cluster.clusterName || 'Swarm Cluster'}
+ </span>
+ </div>
+
+ <div class="flex items-center gap-4 text-sm text-purple-700 dark:text-purple-300">
+ <span class="flex items-center gap-1">
+ <span class="font-medium">{props.cluster.hosts.length}</span>
+ <span class="text-purple-500 dark:text-purple-400">
+ {props.cluster.hosts.length === 1 ? 'node' : 'nodes'}
+ </span>
+ </span>
+ <Show when={managerCount() > 0}>
+ <span class="text-purple-500 dark:text-purple-400">
+ ({managerCount()} manager{managerCount() !== 1 ? 's' : ''}, {workerCount()} worker
+ {workerCount() !== 1 ? 's' : ''})
+ </span>
+ </Show>
+ <span class="text-purple-500 dark:text-purple-400">|</span>
+ <span>
+ <span class="font-medium">{props.serviceCount}</span>{' '}
+ <span class="text-purple-500 dark:text-purple-400">
+ {props.serviceCount === 1 ? 'service' : 'services'}
+ </span>
+ </span>
+ </div>
+ </button>
+ );
+};
+
+const ServiceRow: Component<{ service: ClusterService }> = (props) => {
+ const healthStatus = createMemo(() => getServiceHealthStatus(props.service));
+
+ const statusVariant = createMemo(() => {
+ switch (healthStatus()) {
+ case 'critical':
+ return 'danger';
+ case 'degraded':
+ return 'warning';
+ default:
+ return 'success';
+ }
+ });
+
+ const statusLabel = createMemo(() => {
+ const { totalDesired, totalRunning } = props.service;
+ if (totalDesired === 0) return 'No replicas';
+ return `${totalRunning}/${totalDesired} running`;
+ });
+
+ const updatedAt = createMemo(() => {
+ const ts = props.service.service.updatedAt;
+ if (!ts) return null;
+ return typeof ts === 'number' ? ts : Date.parse(ts as unknown as string);
+ });
+
+ return (
+ <tr class="border-b border-gray-100 dark:border-gray-800 hover:bg-gray-50 dark:hover:bg-gray-800/50 transition-colors">
+ {/* Service Name */}
+ <td class="px-4 py-3">
+ <div class="flex flex-col">
+ <span class="font-medium text-gray-900 dark:text-gray-100">
+ {props.service.service.name}
+ </span>
+ <Show when={props.service.service.image}>
+ <span class="text-xs text-gray-500 dark:text-gray-400 truncate max-w-[200px]">
+ {props.service.service.image?.split('@')[0]}
+ </span>
+ </Show>
+ </div>
+ </td>
+
+ {/* Stack */}
+ <td class="px-4 py-3">
+ <Show when={props.service.service.stack} fallback={<span class="text-gray-400">—</span>}>
+ <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-slate-100 text-slate-700 dark:bg-slate-800 dark:text-slate-300">
+ {props.service.service.stack}
+ </span>
+ </Show>
+ </td>
+
+ {/* Mode */}
+ <td class="px-4 py-3">
+ <span class="text-sm text-gray-600 dark:text-gray-400 capitalize">
+ {props.service.service.mode || 'replicated'}
+ </span>
+ </td>
+
+ {/* Replicas */}
+ <td class="px-4 py-3">
+ <div class="flex items-center gap-2">
+ <StatusDot variant={statusVariant()} size="sm" />
+ <span class="text-sm text-gray-700 dark:text-gray-300">{statusLabel()}</span>
+ </div>
+ </td>
+
+ {/* Nodes */}
+ <td class="px-4 py-3">
+ <span class="text-sm text-gray-600 dark:text-gray-400">
+ {formatNodeDistribution(props.service.nodes)}
+ </span>
+ </td>
+
+ {/* Updated */}
+ <td class="px-4 py-3 text-right">
+ <Show when={updatedAt()} fallback={<span class="text-gray-400">—</span>}>
+ <span class="text-sm text-gray-500 dark:text-gray-400">
+ {formatRelativeTime(updatedAt()!)}
+ </span>
+ </Show>
+ </td>
+ </tr>
+ );
+};
+
+const ClusterSection: Component<{
+ cluster: SwarmCluster;
+ services: ClusterService[];
+ sortKey: SortKey;
+ sortDirection: SortDirection;
+}> = (props) => {
+ const [isExpanded, setIsExpanded] = createSignal(true);
+
+ const sortedServices = createMemo(() => {
+ const services = [...props.services];
+ const key = props.sortKey;
+ const dir = props.sortDirection;
+
+ services.sort((a, b) => {
+ let cmp = 0;
+
+ switch (key) {
+ case 'name':
+ cmp = (a.service.name || '').localeCompare(b.service.name || '');
+ break;
+ case 'stack':
+ cmp = (a.service.stack || '').localeCompare(b.service.stack || '');
+ break;
+ case 'mode':
+ cmp = (a.service.mode || '').localeCompare(b.service.mode || '');
+ break;
+ case 'replicas':
+ cmp = a.totalRunning - b.totalRunning;
+ break;
+ case 'nodes':
+ cmp = a.nodes.length - b.nodes.length;
+ break;
+ case 'status':
+ cmp =
+ (STATUS_PRIORITY[getServiceHealthStatus(a)] || 0) -
+ (STATUS_PRIORITY[getServiceHealthStatus(b)] || 0);
+ break;
+ }
+
+ return dir === 'asc' ? cmp : -cmp;
+ });
+
+ return services;
+ });
+
+ return (
+ <div class="mb-4">
+ <ClusterGroupHeader
+ cluster={props.cluster}
+ serviceCount={props.services.length}
+ isExpanded={isExpanded()}
+ onToggle={() => setIsExpanded(!isExpanded())}
+ />
+
+ <Show when={isExpanded()}>
+ <div class="overflow-x-auto">
+ <table class="w-full">
+ <tbody>
+ <For each={sortedServices()}>{(service) => <ServiceRow service={service} />}</For>
+ </tbody>
+ </table>
+ </div>
+ </Show>
+ </div>
+ );
+};
+
+export const DockerClusterServicesTable: Component<DockerClusterServicesTableProps> = (props) => {
+ const [sortKey, setSortKey] = usePersistentSignal<SortKey>(
+ 'dockerClusterSortKey',
+ 'name',
+ { deserialize: (v) => (SORT_KEYS.includes(v as SortKey) ? (v as SortKey) : 'name') }
+ );
+ const [sortDirection, setSortDirection] = usePersistentSignal<SortDirection>(
+ 'dockerClusterSortDirection',
+ 'asc',
+ { deserialize: (v) => (v === 'asc' || v === 'desc' ? v : 'asc') }
+ );
+
+ const handleSort = (key: SortKey) => {
+ if (sortKey() === key) {
+ setSortDirection(sortDirection() === 'asc' ? 'desc' : 'asc');
+ } else {
+ setSortKey(key);
+ setSortDirection(SORT_DEFAULT_DIRECTION[key]);
+ }
+ };
+
+ const renderSortIndicator = (key: SortKey) => {
+ if (sortKey() !== key) return null;
+ return sortDirection() === 'asc' ? ' ▲' : ' ▼';
+ };
+
+ const searchTokens = createMemo(() => parseSearchTerm(props.searchTerm));
+
+ const clusters = createMemo(() => groupHostsByCluster(props.hosts));
+
+ const clustersWithServices = createMemo(() => {
+ const tokens = searchTokens();
+
+ return clusters()
+ .map((cluster) => {
+ const allServices = aggregateClusterServices(cluster);
+ const filteredServices = allServices.filter((s) => serviceMatchesSearch(s, tokens));
+ return { cluster, services: filteredServices };
+ })
+ .filter((c) => c.services.length > 0);
+ });
+
+ const totalServices = createMemo(() =>
+ clustersWithServices().reduce((sum, c) => sum + c.services.length, 0)
+ );
+
+ return (
+ <Card class="docker-cluster-services-table" padding="none">
+ <Show
+ when={clustersWithServices().length > 0}
+ fallback={
+ <EmptyState
+ title="No Swarm clusters found"
+ description="Deploy Docker agents to multiple nodes in the same Swarm cluster to see the cluster view."
+ icon={
+ <svg
+ class="w-12 h-12 text-gray-400"
+ fill="none"
+ viewBox="0 0 24 24"
+ stroke="currentColor"
+ >
+ <path
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ stroke-width="1.5"
+ d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10"
+ />
+ </svg>
+ }
+ />
+ }
+ >
+ {/* Header row */}
+ <div class="sticky top-0 z-10 bg-white dark:bg-gray-900 border-b border-gray-200 dark:border-gray-700">
+ <table class="w-full">
+ <thead>
+ <tr class="text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
+ <th
+ class="px-4 py-3 cursor-pointer hover:text-gray-700 dark:hover:text-gray-200 transition-colors"
+ onClick={() => handleSort('name')}
+ >
+ Service{renderSortIndicator('name')}
+ </th>
+ <th
+ class="px-4 py-3 cursor-pointer hover:text-gray-700 dark:hover:text-gray-200 transition-colors"
+ onClick={() => handleSort('stack')}
+ >
+ Stack{renderSortIndicator('stack')}
+ </th>
+ <th
+ class="px-4 py-3 cursor-pointer hover:text-gray-700 dark:hover:text-gray-200 transition-colors"
+ onClick={() => handleSort('mode')}
+ >
+ Mode{renderSortIndicator('mode')}
+ </th>
+ <th
+ class="px-4 py-3 cursor-pointer hover:text-gray-700 dark:hover:text-gray-200 transition-colors"
+ onClick={() => handleSort('replicas')}
+ >
+ Replicas{renderSortIndicator('replicas')}
+ </th>
+ <th
+ class="px-4 py-3 cursor-pointer hover:text-gray-700 dark:hover:text-gray-200 transition-colors"
+ onClick={() => handleSort('nodes')}
+ >
+ Nodes{renderSortIndicator('nodes')}
+ </th>
+ <th class="px-4 py-3 text-right">Updated</th>
+ </tr>
+ </thead>
+ </table>
+ </div>
+
+ {/* Cluster sections */}
+ <div>
+ <For each={clustersWithServices()}>
+ {({ cluster, services }) => (
+ <ClusterSection
+ cluster={cluster}
+ services={services}
+ sortKey={sortKey()}
+ sortDirection={sortDirection()}
+ />
+ )}
+ </For>
+ </div>
+
+ {/* Footer summary */}
+ <div class="px-4 py-2 bg-gray-50 dark:bg-gray-800/50 border-t border-gray-200 dark:border-gray-700 text-sm text-gray-500 dark:text-gray-400">
+ {clustersWithServices().length} cluster{clustersWithServices().length !== 1 ? 's' : ''},{' '}
+ {totalServices()} service{totalServices() !== 1 ? 's' : ''}
+ </div>
+ </Show>
+ </Card>
+ );
+};
diff --git a/frontend-modern/src/components/Docker/swarmClusterHelpers.ts b/frontend-modern/src/components/Docker/swarmClusterHelpers.ts
new file mode 100644
index 0000000000..36bfbc3878
--- /dev/null
+++ b/frontend-modern/src/components/Docker/swarmClusterHelpers.ts
@@ -0,0 +1,197 @@
+import type { DockerHost, DockerService } from '@/types/api';
+
+/**
+ * Represents a Docker Swarm cluster with its member hosts
+ */
+export interface SwarmCluster {
+ clusterId: string;
+ clusterName: string;
+ hosts: DockerHost[];
+}
+
+/**
+ * Represents an aggregated service across a Swarm cluster
+ */
+export interface ClusterService {
+ service: DockerService;
+ clusterId: string;
+ clusterName: string;
+ nodes: Array<{
+ hostId: string;
+ hostname: string;
+ taskCount: number;
+ runningCount: number;
+ }>;
+ totalDesired: number;
+ totalRunning: number;
+}
+
+/**
+ * Groups Docker hosts by their Swarm cluster ID.
+ * Hosts without a clusterId are returned as individual "clusters" with empty clusterId.
+ */
+export function groupHostsByCluster(hosts: DockerHost[]): SwarmCluster[] {
+ const clusterMap = new Map<string, SwarmCluster>();
+ const standaloneHosts: DockerHost[] = [];
+
+ for (const host of hosts) {
+ const clusterId = host.swarm?.clusterId;
+
+ if (!clusterId) {
+ // Non-Swarm host or no cluster info
+ standaloneHosts.push(host);
+ continue;
+ }
+
+ if (!clusterMap.has(clusterId)) {
+ clusterMap.set(clusterId, {
+ clusterId,
+ clusterName: host.swarm?.clusterName || clusterId.slice(0, 12),
+ hosts: [],
+ });
+ }
+
+ clusterMap.get(clusterId)!.hosts.push(host);
+ }
+
+ // Sort hosts within each cluster by hostname
+ for (const cluster of clusterMap.values()) {
+ cluster.hosts.sort((a, b) => {
+ const aName = a.customDisplayName || a.displayName || a.hostname || '';
+ const bName = b.customDisplayName || b.displayName || b.hostname || '';
+ return aName.localeCompare(bName);
+ });
+ }
+
+ // Convert to array and sort by cluster name
+ const clusters = Array.from(clusterMap.values()).sort((a, b) =>
+ a.clusterName.localeCompare(b.clusterName)
+ );
+
+ return clusters;
+}
+
+/**
+ * Aggregates services across all hosts in a cluster, deduplicating by service ID.
+ * Returns services with node distribution information.
+ */
+export function aggregateClusterServices(cluster: SwarmCluster): ClusterService[] {
+ const serviceMap = new Map<string, ClusterService>();
+
+ for (const host of cluster.hosts) {
+ const services = host.services || [];
+ const tasks = host.tasks || [];
+
+ for (const service of services) {
+ const serviceId = service.id;
+
+ if (!serviceMap.has(serviceId)) {
+ serviceMap.set(serviceId, {
+ service: { ...service },
+ clusterId: cluster.clusterId,
+ clusterName: cluster.clusterName,
+ nodes: [],
+ totalDesired: service.desiredTasks || 0,
+ totalRunning: 0,
+ });
+ }
+
+ const aggregated = serviceMap.get(serviceId)!;
+
+ // Count tasks for this service on this host
+ const serviceTasks = tasks.filter(
+ (t) => t.serviceId === serviceId || t.serviceName === service.name
+ );
+ const runningTasks = serviceTasks.filter(
+ (t) => t.currentState?.toLowerCase() === 'running'
+ );
+
+ if (serviceTasks.length > 0) {
+ aggregated.nodes.push({
+ hostId: host.id,
+ hostname: host.customDisplayName || host.displayName || host.hostname,
+ taskCount: serviceTasks.length,
+ runningCount: runningTasks.length,
+ });
+ aggregated.totalRunning += runningTasks.length;
+ }
+
+ // Update desired count (take max in case of inconsistencies)
+ aggregated.totalDesired = Math.max(
+ aggregated.totalDesired,
+ service.desiredTasks || 0
+ );
+ }
+ }
+
+ // Sort services by stack then name
+ return Array.from(serviceMap.values()).sort((a, b) => {
+ const stackA = a.service.stack || '';
+ const stackB = b.service.stack || '';
+ if (stackA !== stackB) {
+ return stackA.localeCompare(stackB);
+ }
+ return (a.service.name || '').localeCompare(b.service.name || '');
+ });
+}
+
+/**
+ * Checks if there are any Swarm clusters with multiple hosts.
+ * Used to determine whether to show the cluster view toggle.
+ */
+export function hasSwarmClusters(hosts: DockerHost[]): boolean {
+ const clusterIds = new Set<string>();
+
+ for (const host of hosts) {
+ const clusterId = host.swarm?.clusterId;
+ if (clusterId) {
+ if (clusterIds.has(clusterId)) {
+ // Found at least 2 hosts in the same cluster
+ return true;
+ }
+ clusterIds.add(clusterId);
+ }
+ }
+
+ return false;
+}
+
+/**
+ * Gets a display-friendly node list string for a service.
+ * Example: "node1 (2), node2 (1), node3 (1)"
+ */
+export function formatNodeDistribution(
+ nodes: ClusterService['nodes'],
+ maxNodes: number = 3
+): string {
+ if (nodes.length === 0) return 'No nodes';
+
+ const sorted = [...nodes].sort((a, b) => b.taskCount - a.taskCount);
+ const display = sorted.slice(0, maxNodes);
+ const remaining = sorted.length - maxNodes;
+
+ const parts = display.map((n) => {
+ if (n.taskCount === 1) return n.hostname;
+ return `${n.hostname} (${n.taskCount})`;
+ });
+
+ if (remaining > 0) {
+ parts.push(`+${remaining} more`);
+ }
+
+ return parts.join(', ');
+}
+
+/**
+ * Gets the overall health status for a cluster service.
+ */
+export function getServiceHealthStatus(
+ service: ClusterService
+): 'healthy' | 'degraded' | 'critical' {
+ const { totalDesired, totalRunning } = service;
+
+ if (totalDesired === 0) return 'healthy';
+ if (totalRunning === 0) return 'critical';
+ if (totalRunning < totalDesired) return 'degraded';
+ return 'healthy';
+}