import Block from "components/services/widget/block"; import Container from "components/services/widget/container"; import { useTranslation } from "next-i18next"; import useWidgetAPI from "utils/proxy/use-widget-api"; const MAX_FIELDS = 4; export default function Component({ service }) { const { t } = useTranslation(); const { widget } = service; if (!widget.fields) { widget.fields = ["running", "stopped", "cpu", "memory"]; } else if (widget.fields.length > MAX_FIELDS) { widget.fields = widget.fields.slice(0, MAX_FIELDS); } const { data: stats, error: statsError } = useWidgetAPI(widget, "dashboard/stats"); if (statsError) { return ; } if (!stats) { return ( ); } let running; let stopped; let paused; let pendingUpdates; let cpuPercent; let memoryPercent; let imagesTotal; let volumesTotal; let stacksRunning; let stacksTotal; let eventsToday; if (widget?.environment) { const environment = stats.find( (env) => env?.name?.toString().toLowerCase() === widget?.environment.toString().toLowerCase() || env?.id?.toString() === widget?.environment.toString(), ); if (environment) { running = environment?.containers?.running; stopped = environment?.containers?.stopped ?? (environment?.containers?.total ?? 0) - (running ?? 0); paused = environment?.containers?.paused; pendingUpdates = environment?.containers?.pendingUpdates; cpuPercent = environment?.metrics?.cpuPercent; memoryPercent = environment?.metrics?.memoryPercent; imagesTotal = environment?.images?.total; volumesTotal = environment?.volumes?.total; stacksRunning = environment?.stacks?.running; stacksTotal = environment?.stacks?.total; eventsToday = environment?.events?.today; } } if (running === undefined) { // Aggregate across all environments running = stats.reduce((sum, env) => sum + (env?.containers?.running ?? 0), 0); const total = stats.reduce((sum, env) => sum + (env?.containers?.total ?? 0), 0); stopped = total - running; paused = stats.reduce((sum, env) => sum + (env?.containers?.paused ?? 0), 0); pendingUpdates = stats.reduce((sum, env) => sum + (env?.containers?.pendingUpdates ?? 0), 0); const totalCpu = stats.reduce((sum, env) => sum + (env?.metrics?.cpuPercent ?? 0), 0); const totalMemory = stats.reduce((sum, env) => sum + (env?.metrics?.memoryPercent ?? 0), 0); const envCount = stats.length; cpuPercent = envCount > 0 ? totalCpu / envCount : 0; memoryPercent = envCount > 0 ? totalMemory / envCount : 0; imagesTotal = stats.reduce((sum, env) => sum + (env?.images?.total ?? 0), 0); volumesTotal = stats.reduce((sum, env) => sum + (env?.volumes?.total ?? 0), 0); stacksRunning = stats.reduce((sum, env) => sum + (env?.stacks?.running ?? 0), 0); stacksTotal = stats.reduce((sum, env) => sum + (env?.stacks?.total ?? 0), 0); eventsToday = stats.reduce((sum, env) => sum + (env?.events?.today ?? 0), 0); } return ( ); }