import React, { useState, useEffect } from "react"; import { useSelector, useDispatch } from "react-redux"; import { Card, CardHeader, CardTitle, Button } from "reactstrap"; import DataTable from "react-data-table-component"; import { ChevronDown, RefreshCw, Server, Monitor } from "react-feather"; import { useTranslation } from "react-i18next"; import { useSnackbar } from "notistack"; import { getDataCenters } from "../redux/actions/dataCenter"; import SpinnerComponent from "../@core/components/spinner/Fallback-spinner"; const DataCenter = () => { const { t } = useTranslation(); const { enqueueSnackbar } = useSnackbar(); const dispatch = useDispatch(); // Redux state const dataCenterStore = useSelector((state) => state.dataCenter); const [refreshInterval, setRefreshInterval] = useState(null); const getAllPhysicalMachines = (dataCenter) => { // Physical machines are directly in the dataCenter object, not in projects const pms = dataCenter.physicalMachines || []; return pms; }; // Table columns following your pattern const initialColumns = [ { name: "External ID", selector: (row) => row.externalId, sortable: true, minWidth: "100px", }, { name: "Data Centers", selector: (row) => row.dataCenter, sortable: true, minWidth: "200px", }, // Projects - Based on API response, this field might not exist or be structured differently // { // name: "Projects", // selector: (row) => row.projects?.length || 0, // sortable: true, // minWidth: "200px", // cell: (row) => ( //
// {row.projects && row.projects.length > 0 ? ( //
// {row.projects.map((project, index) => ( //
0 ? "mt-1" : "" // }`} // > // {project.name} //
// ))} //
// ) : ( // - // )} //
// ), // }, // Physical Machines { name: "Physical Machines", selector: (row) => getAllPhysicalMachines(row), sortable: false, minWidth: "200px", cell: (row) => { const pms = getAllPhysicalMachines(row); return (
{pms.length} machines
); }, }, { name: "Virtual Machines", selector: (row) => { const pms = getAllPhysicalMachines(row); const vms = pms.reduce( (acc, pm) => { if (!pm.vms) return acc; return { active: acc.active + pm.vms.filter((vm) => vm.state?.toLowerCase() === "active") .length, total: acc.total + pm.vms.length, }; }, { active: 0, total: 0 } ); return vms.total; }, sortable: true, minWidth: "200px", cell: (row) => { const pms = getAllPhysicalMachines(row); const vms = pms.reduce( (acc, pm) => { if (!pm.vms) return acc; return { active: acc.active + pm.vms.filter((vm) => vm.state?.toLowerCase() === "active") .length, total: acc.total + pm.vms.length, }; }, { active: 0, total: 0 } ); return (
{vms.total} Total
{vms.active} Active {vms.total - vms.active} Inactive
); }, }, ]; // Fetch data on component mount useEffect(() => { dispatch(getDataCenters()); // Set up auto-refresh every 2 minutes const interval = setInterval(() => { dispatch(getDataCenters()); }, 2 * 60 * 1000); setRefreshInterval(interval); return () => { if (interval) clearInterval(interval); }; }, [dispatch]); // Handle errors useEffect(() => { if (dataCenterStore.error) { enqueueSnackbar(dataCenterStore.error, { variant: "error", preventDuplicate: true, }); } }, [dataCenterStore.error, enqueueSnackbar]); const handleRefresh = () => { dispatch(getDataCenters()); }; // Expandable component for showing physical machines and VMs const ExpandableComponent = ({ data }) => { const physicalMachines = getAllPhysicalMachines(data); console.log("▶️ Expandable Data", physicalMachines); return (
Physical Machines:
{physicalMachines.length === 0 && (

No physical machines

)} {physicalMachines.map((pm) => (
{pm.name}
{/* All VMs */}
Virtual Machines ({pm.vms?.length || 0})
{pm.vms?.length > 0 ? (
{pm.vms.map((vm) => { const isActive = vm.state && ["ACTIVE", "active"].includes(vm.state); return ( ); })}
Name Status Power (W) Configuration Host
{vm.vmName || vm.vm_name}
{vm.state}
{vm.power ? ( {vm.power.toFixed(2)} ) : ( - )}
CPU {vm.config?.cpu || (vm.confg && vm.confg[1]) || "-"}
RAM {vm.config?.ram || (vm.confg && vm.confg[2]) || "-"}{" "} GB
Disk {vm.config?.disk || (vm.confg && vm.confg[3]) || "-"}{" "} GB
{vm.host || vm.hostingPm || (vm.confg && vm.confg[4]) || "-"}
) : (

No virtual machines found

)}
))}
); }; return (
Test } expandableRows expandableRowsComponent={ExpandableComponent} progressPending={dataCenterStore.loading} progressComponent={
} noDataComponent={

No data centers found

} className="react-dataTable" />
); }; export default DataCenter;