import { AlertTriangle, Container, Info, Loader2, RefreshCw, } from "lucide-react"; import Link from "next/link"; import { useEffect, useState } from "react"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; import { CardTitle } from "@/components/ui/card"; import { api } from "@/utils/api"; import { NoRunningContainers, NoServices, ServicesError, SwarmNotAvailable, } from "./empty-states"; import { NodeSection } from "./node-section"; import { SummaryCards } from "./summary-cards"; import type { ContainerInfo, ContainerStat, SwarmNode } from "./types"; interface Props { serverId?: string; } export const ShowSwarmContainers = ({ serverId }: Props) => { const [expandedNodes, setExpandedNodes] = useState>(new Set()); const { data: nodes, isLoading: nodesLoading, isError: nodesError, error: nodesErrorDetail, refetch: refetchNodes, } = api.swarm.getNodes.useQuery({ serverId }); const { data: nodeApps, isLoading: appsLoading, isError: appsError, error: appsErrorDetail, refetch: refetchApps, } = api.swarm.getNodeApps.useQuery( { serverId }, { enabled: !nodesError && nodes !== undefined }, ); const applicationList = nodeApps && nodeApps.length > 0 ? nodeApps.map((app: { Name: string }) => app.Name) : []; const { data: appDetails, isLoading: detailsLoading, refetch: refetchDetails, } = api.swarm.getAppInfos.useQuery( { appName: applicationList, serverId }, { enabled: applicationList.length > 0 }, ); const { data: stats, isLoading: statsLoading } = api.swarm.getContainerStats.useQuery( { serverId }, { refetchInterval: 5000, enabled: applicationList.length > 0 && !nodesError && !appsError, }, ); const isLoading = nodesLoading || appsLoading || (applicationList.length > 0 && detailsLoading); // Build container list const containers: ContainerInfo[] = []; if (nodeApps && appDetails) { for (const app of nodeApps) { const details = appDetails?.filter((detail: { Name: string }) => detail.Name.startsWith(`${app.Name}.`), ) || []; if (details.length === 0) { containers.push({ ...app, CurrentState: "N/A", DesiredState: "N/A", Error: "", Node: "N/A", ID: app.ID, }); } else { for (const detail of details) { containers.push({ Name: detail.Name, Image: detail.Image || app.Image, CurrentState: detail.CurrentState, DesiredState: detail.DesiredState, Error: detail.Error, Node: detail.Node, Ports: detail.Ports || app.Ports, ID: detail.ID, }); } } } } const runningContainers = containers.filter( (c) => c.Node !== "N/A" && (c.DesiredState === "Running" || c.CurrentState.startsWith("Running")), ); const unscheduledServices = containers.filter((c) => c.Node === "N/A"); const downNodes = (nodes ?? []).filter( (n: SwarmNode) => n.Status !== "Ready" || n.Availability !== "Active", ); const isMultiNode = (nodes?.length ?? 0) > 1; const nodeStatusMap = new Map(); if (nodes) { for (const node of nodes) { nodeStatusMap.set(node.Hostname, node); } } const statsMap = new Map(); if (stats) { for (const stat of stats) { statsMap.set(stat.Name, stat); } } const findStatsForContainer = ( taskName: string, ): ContainerStat | undefined => { for (const [containerName, stat] of statsMap) { if (containerName.startsWith(`${taskName}.`)) { return stat; } } return undefined; }; useEffect(() => { if (runningContainers.length > 0 && expandedNodes.size === 0) { const nodeNames = new Set(); for (const c of runningContainers) { if (c.Node) { nodeNames.add(c.Node); } } setExpandedNodes(nodeNames); } }, [runningContainers.length]); const toggleNode = (nodeName: string) => { setExpandedNodes((prev: Set) => { const next = new Set(prev); if (next.has(nodeName)) { next.delete(nodeName); } else { next.add(nodeName); } return next; }); }; const handleRefresh = () => { refetchApps(); refetchDetails(); }; // Build node groups const nodeMap = new Map(); for (const c of runningContainers) { const nodeName = c.Node || "Unknown"; if (!nodeMap.has(nodeName)) { nodeMap.set(nodeName, []); } nodeMap.get(nodeName)!.push(c); } const nodeGroups = []; for (const [nodeName, nodeContainers] of nodeMap) { nodeGroups.push({ nodeName, containers: nodeContainers, nodeStatus: nodeStatusMap.get(nodeName), }); } nodeGroups.sort((a, b) => a.nodeName.localeCompare(b.nodeName)); if (isLoading) { return (
Loading containers...
); } if (nodesError) { return ( refetchNodes()} /> ); } if (!nodesError && nodes === undefined) { return ( refetchNodes()} /> ); } const isRealAppsError = appsError && !appsErrorDetail?.message?.includes("data is undefined"); if (isRealAppsError) { return ( refetchApps()} /> ); } if (!nodeApps || nodeApps.length === 0) { return ( refetchApps()} /> ); } if (runningContainers.length === 0) { return ( ); } return (
Container Breakdown by Node

Showing containers across {nodes?.length ?? 0} swarm node(s) {statsLoading ? "" : " (metrics refresh every 5s)"}

{downNodes.length > 0 && ( {downNodes.length} Node(s) Unavailable

The following nodes are not ready or have been drained. Containers scheduled on these nodes may not be running.

    {downNodes.map((node: SwarmNode) => (
  • {node.Hostname} — Status: {node.Status} , Availability: {node.Availability} {node.ManagerStatus && ` (${node.ManagerStatus})`}
  • ))}

Manage nodes in{" "} Cluster Settings

)} {isMultiNode && ( Multi-Node Metrics Note CPU, memory, and I/O metrics are collected from the manager node via{" "} docker stats . Containers running on worker nodes will show “--” for metrics. )}
{nodeGroups.map((group) => ( ))}
{unscheduledServices.length > 0 && ( {unscheduledServices.length} Service(s) With No Running Tasks

These services exist in the swarm but have no running containers. They may be scaled to 0 replicas or failing to start.

    {unscheduledServices.map((svc) => (
  • {svc.Name} {svc.Error && svc.Error.trim() !== "" && ( — {svc.Error} )}
  • ))}
)}
); };