70 lines
2.6 KiB
TypeScript
70 lines
2.6 KiB
TypeScript
import type { Container, DiscoveredContainer } from "@/types/appops";
|
|
|
|
export const discoveredToContainerView = (item: DiscoveredContainer): Container => ({
|
|
id: item.containerId,
|
|
name: item.name,
|
|
appId: item.matchedAppId || item.suggestedAppId || undefined,
|
|
state: item.state === "running" ? "running" : item.state === "exited" ? "exited" : item.state === "restarting" ? "restarting" : "paused",
|
|
health: item.status === "unhealthy" ? "unhealthy" : item.status === "running" ? "healthy" : item.state === "running" ? "healthy" : "warning",
|
|
image: item.image,
|
|
ports: (item.ports || []).map(String),
|
|
cpu: "n/a",
|
|
ram: "n/a",
|
|
restartCount: 0,
|
|
criticality: item.suggestedCriticality,
|
|
voiceEnabled: false,
|
|
protected: item.protected || item.isProtected,
|
|
appType: item.appType || item.suggestedAppType,
|
|
connectorType: item.connectorType || item.suggestedConnector,
|
|
policyProfile: item.policyProfile || item.suggestedPolicyProfile,
|
|
importStatus: item.importStatus,
|
|
isManaged: item.isManaged || item.importStatus === "managed",
|
|
portsRaw: item.ports,
|
|
mounts: item.mounts,
|
|
networks: item.networks,
|
|
labels: item.labels,
|
|
});
|
|
|
|
export const mergeInventoryContainersWithLive = (
|
|
liveContainers: Container[],
|
|
inventoryContainers: DiscoveredContainer[],
|
|
): Container[] => {
|
|
if (!inventoryContainers.length) {
|
|
return liveContainers;
|
|
}
|
|
|
|
const inventoryViews = inventoryContainers.map(discoveredToContainerView);
|
|
|
|
if (!liveContainers.length) {
|
|
return inventoryViews;
|
|
}
|
|
|
|
const inventoryById = new Map(inventoryViews.map((container) => [container.id, container]));
|
|
const inventoryByName = new Map(
|
|
inventoryViews.map((container) => [container.name.trim().toLowerCase(), container]),
|
|
);
|
|
|
|
return liveContainers.map((live) => {
|
|
const inventoryContainer = inventoryById.get(live.id)
|
|
?? inventoryByName.get(live.name.trim().toLowerCase());
|
|
|
|
if (!inventoryContainer) {
|
|
return live;
|
|
}
|
|
|
|
return {
|
|
...live,
|
|
...inventoryContainer,
|
|
portainerUrl: live?.portainerUrl ?? inventoryContainer.portainerUrl,
|
|
state: live?.state ?? inventoryContainer.state,
|
|
health: live?.health ?? inventoryContainer.health,
|
|
ports: live?.ports?.length ? live.ports : inventoryContainer.ports,
|
|
cpu: live?.cpu ?? inventoryContainer.cpu,
|
|
ram: live?.ram ?? inventoryContainer.ram,
|
|
metricsStatus: live?.metricsStatus ?? inventoryContainer.metricsStatus,
|
|
metricsSource: live?.metricsSource ?? inventoryContainer.metricsSource,
|
|
metricsError: live?.metricsError ?? inventoryContainer.metricsError,
|
|
};
|
|
});
|
|
};
|