import { Box, Stack, Typography } from "@mui/material";
import { useTheme } from "@emotion/react";
import DataTable from "@/Components/v1/Table/index.jsx";
import { StatusLabel } from "@/Components/v1/Label/index.jsx";
const DockerContainersTable = ({ containers = [], errors = [], captureVersion }) => {
const theme = useTheme();
// Helper to format uptime
const formatUptime = (startedAt, finishedAt, running) => {
if (!running && finishedAt > 0) {
const stoppedAgo = Math.floor(Date.now() / 1000) - finishedAt;
return formatDuration(stoppedAgo) + " ago";
}
if (running && startedAt > 0) {
const uptimeSeconds = Math.floor(Date.now() / 1000) - startedAt;
return formatDuration(uptimeSeconds);
}
return "N/A";
};
// Helper to format duration
const formatDuration = (seconds) => {
const days = Math.floor(seconds / 86400);
const hours = Math.floor((seconds % 86400) / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
if (days > 0) return `${days}d ${hours}h`;
if (hours > 0) return `${hours}h ${minutes}m`;
if (minutes > 0) return `${minutes}m`;
return `${seconds}s`;
};
// Helper to get status color
const getStatusColor = (status, running, healthy) => {
if (status === "running" && healthy) return "success";
if (status === "running" && !healthy) return "warning";
if (status === "exited") return "error";
if (status === "paused") return "warning";
if (status === "restarting") return "warning";
return "default";
};
const headers = [
{
id: "name",
content: "Container Name",
render: (row) => (
{row.container_name || "Unnamed"}
{row.container_id ? row.container_id.substring(0, 12) : "N/A"}
),
},
{
id: "status",
content: "Status",
render: (row) => (
),
},
{
id: "health",
content: "Health",
render: (row) => (
{row.health?.healthy ? "Healthy" : "Unhealthy"}
{row.health?.source === "container_health_check" ? "HEALTHCHECK" : "State-based"}
),
},
{
id: "image",
content: "Image",
render: (row) => (
{row.base_image || "N/A"}
),
},
{
id: "uptime",
content: "Uptime",
render: (row) => (
{formatUptime(row.started_at, row.finished_at, row.running)}
),
},
{
id: "ports",
content: "Ports",
render: (row) => (
{row.exposed_ports && row.exposed_ports.length > 0 ? (
row.exposed_ports.map((port, idx) => (
{port.port}/{port.protocol}
))
) : (
None
)}
),
},
];
// Check if there are critical errors (Docker daemon down)
const dockerClientError = errors.find((e) => e.metric && e.metric.includes("docker.client"));
if (!containers || containers.length === 0) {
// Determine the message based on error type
let message = "No containers found";
let errorDetails = null;
if (dockerClientError) {
message = "Docker daemon unavailable";
errorDetails = dockerClientError.err;
} else if (errors && errors.length > 0) {
message = "Partial data available";
errorDetails = "Some containers could not be inspected";
}
return (
{message}
{errorDetails && (
{errorDetails}
)}
{captureVersion && (
Capture version: {captureVersion}
)}
);
}
return (
Docker Containers ({containers.length})
{errors && errors.length > 0 && (
⚠ {errors.length} error(s) encountered while fetching container data
)}
{captureVersion && (
Capture version: {captureVersion}
)}
);
};
export default DockerContainersTable;