2024-11-29 14:06:59 -05:00
import { useEffect , useState } from "react" ;
2024-11-27 21:24:04 -05:00
import { useNavigate } from "react-router-dom" ;
import { /* useDispatch, */ useSelector } from "react-redux" ;
import { useTheme } from "@emotion/react" ;
import useUtils from "../Monitors/utils" ;
import { jwtDecode } from "jwt-decode" ;
2024-11-29 13:48:29 -05:00
// import GearIcon from "../../Assets/icons/settings-bold.svg?react";
2024-11-27 21:24:04 -05:00
import CPUChipIcon from "../../Assets/icons/cpu-chip.svg?react" ;
2024-11-15 14:55:05 -05:00
import {
Box ,
Button ,
2024-11-17 18:16:25 -05:00
IconButton ,
Paper ,
2024-11-15 14:55:05 -05:00
Stack ,
Table ,
TableBody ,
TableCell ,
TableContainer ,
TableHead ,
TableRow ,
} from "@mui/material" ;
import Breadcrumbs from "../../Components/Breadcrumbs" ;
2024-11-16 22:48:46 -05:00
import { StatusLabel } from "../../Components/Label" ;
2024-11-27 17:38:39 -05:00
import { Heading } from "../../Components/Heading" ;
import { Pagination } from "./components/TablePagination" ;
2024-11-27 21:24:04 -05:00
// import { getInfrastructureMonitorsByTeamId } from "../../Features/InfrastructureMonitors/infrastructureMonitorsSlice";
import { networkService } from "../../Utils/NetworkService.js" ;
2024-11-28 17:54:04 -05:00
import CustomGauge from "../../Components/Charts/CustomGauge/index.jsx" ;
2024-11-29 10:35:07 -05:00
import Host from "../Monitors/Home/host.jsx" ;
2024-11-29 13:48:29 -05:00
import { useIsAdmin } from "../../Hooks/useIsAdmin.js" ;
import { InfrastructureMenu } from "./components/Menu" ;
2024-11-15 14:55:05 -05:00
const columns = [
2024-11-17 18:16:25 -05:00
{ label : "Host" },
{ label : "Status" },
{ label : "Frequency" },
{ label : "CPU" },
{ label : "Mem" },
{ label : "Disk" },
{ label : "Actions" },
2024-11-15 14:55:05 -05:00
];
2024-11-29 10:17:09 -05:00
const BREADCRUMBS = [{ name : `infrastructure` , path : "/infrastructure" }];
2024-11-17 18:16:25 -05:00
/* TODO
Create reusable table component.
It should receive as a parameter the following object:
tableData = [
columns = [
{
id: example,
label: Example Extendable,
align: "center" | "left" (default)
}
],
rows: [
{
**Number of keys will be equal to number of columns**
key1: string,
key2: number,
key3: Component
}
]
]
Apply to Monitor Table, and Account/Team.
Analyze existing BasicTable
2024-11-15 14:55:05 -05:00
*/
2024-11-08 18:36:43 -05:00
2024-11-13 21:10:00 -05:00
/**
* This is the Infrastructure monitoring page. This is a work in progress
*
* @param - Define params.
* @returns {JSX.Element} The infrastructure monitoring page.
*/
2024-11-29 13:48:29 -05:00
function Infrastructure () {
/* Adding this custom hook so we can avoid using the HOC approach that can lower performance (we are calling the admin logic N times on initializing the project. using a custom hook will cal it ass needed ) */
const isAdmin = useIsAdmin ();
2024-11-15 14:55:05 -05:00
const theme = useTheme ();
2024-11-27 21:24:04 -05:00
2024-11-25 18:57:23 -05:00
const navigate = useNavigate ();
2024-11-27 21:24:04 -05:00
const navigateToCreate = () => navigate ( "/infrastructure/create" );
const [ page , setPage ] = useState ( 0 );
/* TODO refactor this, so it is not aware of the MUI implementation. First argument only exists because of MUI. This should require onlu the new page. Adapting for MUI should happen inside of table pagination component */
const handleChangePage = ( _ , newPage ) => {
setPage ( newPage );
};
const [ rowsPerPage , setRowsPerPage ] = useState ( 5 );
const handleChangeRowsPerPage = ( event ) => {
setRowsPerPage ( parseInt ( event . target . value ));
setPage ( 0 );
};
const [ monitorState , setMonitorState ] = useState ({ monitors : [], total : 0 });
2024-11-27 17:38:39 -05:00
const { authToken } = useSelector (( state ) => state . auth );
2024-11-27 21:24:04 -05:00
const user = jwtDecode ( authToken );
2024-11-29 13:48:29 -05:00
const fetchMonitors = async () => {
2024-11-27 21:24:04 -05:00
try {
const response = await networkService . getMonitorsByTeamId ({
authToken ,
teamId : user . teamId ,
limit : 1 ,
types : [ "hardware" ],
status : null ,
checkOrder : "desc" ,
normalize : true ,
page : page ,
rowsPerPage : rowsPerPage ,
});
2024-11-29 13:48:29 -05:00
console . log ({ response });
2024-11-27 21:24:04 -05:00
setMonitorState ({
monitors : response ? . data ? . data ? . monitors ?? [],
total : response ? . data ? . data ? . monitorCount ?? 0 ,
});
} catch ( error ) {
console . error ( error );
}
2024-11-29 13:48:29 -05:00
};
2024-11-27 17:38:39 -05:00
2024-11-27 21:24:04 -05:00
useEffect (() => {
fetchMonitors ();
}, [ page , rowsPerPage ]);
const { determineState } = useUtils ();
const { monitors , total : totalMonitors } = monitorState ;
2024-11-27 17:38:39 -05:00
const monitorsAsRows = monitors . map (( monitor ) => ({
2024-11-29 10:53:53 -05:00
id : monitor . _id ,
2024-11-29 10:35:07 -05:00
url : monitor . url ,
name : monitor . name ,
2024-11-27 17:38:39 -05:00
status : determineState ( monitor ),
2024-11-29 10:17:09 -05:00
processor : ( monitor ? . checks [ 0 ] ? . cpu . frequency / 1000 ). toFixed ( 2 ) + " GHz" ,
cpu : monitor ? . checks [ 0 ] ? . cpu . usage_percent * 100 ,
mem : monitor ? . checks [ 0 ] ? . memory . usage_percent * 100 ,
disk : monitor ? . checks [ 0 ] ? . disk [ 0 ] ? . usage_percent * 100 ,
2024-11-27 17:38:39 -05:00
}));
2024-11-29 13:48:29 -05:00
2024-11-29 10:53:53 -05:00
function openDetails ( id ) {
navigate ( `/infrastructure/ ${ id } ` );
}
2024-11-29 13:48:29 -05:00
function handleActionMenuDelete () {
fetchMonitors ();
}
2024-11-27 17:38:39 -05:00
2024-11-08 18:36:43 -05:00
return (
2024-11-15 14:55:05 -05:00
< Stack
component = "main"
style = {{ width : "100%" , gap : "1rem" }}
>
2024-11-29 10:17:09 -05:00
< Breadcrumbs list = { BREADCRUMBS } />
2024-11-15 14:55:05 -05:00
< Stack
direction = "row"
sx = {{
2024-11-29 10:38:31 -05:00
justifyContent : "end" ,
2024-11-15 14:55:05 -05:00
alignItems : "center" ,
gap : "1rem" ,
flexWrap : "wrap" ,
2024-11-29 10:38:31 -05:00
marginBottom : "2rem" ,
2024-11-15 14:55:05 -05:00
}}
>
2024-11-29 10:38:31 -05:00
{ /*
This will be removed from here, but keeping the commented code to remind me to add a max width to the greeting component
2024-11-16 22:48:46 -05:00
<Box style={{ maxWidth: "65ch" }}>
<Greeting type="uptime" />
2024-11-29 10:38:31 -05:00
</Box> */ }
2024-11-15 14:55:05 -05:00
< Button
variant = "contained"
color = "primary"
2024-11-27 21:24:04 -05:00
onClick = { navigateToCreate }
2024-11-15 14:55:05 -05:00
sx = {{ fontWeight : 500 }}
>
Create infrastructure monitor
</ Button >
</ Stack >
< Stack
sx = {{
gap : "1rem" ,
2024-11-08 18:36:43 -05:00
}}
>
2024-11-15 14:55:05 -05:00
< Stack
direction = "row"
sx = {{
alignItems : "center" ,
gap : ".25rem" ,
flexWrap : "wrap" ,
}}
>
< Heading component = "h2" > Infrastructure monitors </ Heading >
2024-11-17 18:16:25 -05:00
{ /* TODO Correct the class current-monitors-counter, there are some unnecessary things there */ }
2024-11-15 14:55:05 -05:00
< Box
component = "span"
className = "current-monitors-counter"
color = { theme . palette . text . primary }
border = { 1 }
borderColor = { theme . palette . border . light }
backgroundColor = { theme . palette . background . accent }
>
2024-11-27 17:38:39 -05:00
{ totalMonitors }
2024-11-15 14:55:05 -05:00
</ Box >
</ Stack >
2024-11-29 10:38:31 -05:00
< TableContainer component = { Paper }>
2024-11-15 14:55:05 -05:00
< Table stickyHeader >
2024-11-17 18:16:25 -05:00
< TableHead sx = {{ backgroundColor : theme . palette . background . accent }}>
2024-11-15 14:55:05 -05:00
< TableRow >
{ columns . map (( column , index ) => (
< TableCell
key = { index }
2024-11-17 18:16:25 -05:00
align = { index === 0 ? "left" : "center" }
2024-11-17 19:01:27 -05:00
sx = {{
backgroundColor : theme . palette . background . accent ,
}}
2024-11-15 14:55:05 -05:00
>
{ column . label }
</ TableCell >
))}
</ TableRow >
</ TableHead >
< TableBody >
2024-11-29 10:53:53 -05:00
{ monitorsAsRows . map (( row ) => (
< TableRow
key = { row . id }
onClick = {() => openDetails ( row . id )}
sx = {{
cursor : "pointer" ,
"&:hover" : {
backgroundColor : theme . palette . background . accent ,
},
}}
>
{ /* TODO iterate over column and get column id, applying row[column.id] */ }
< TableCell >
< Host
title = { row . name }
url = { row . url }
/>
</ TableCell >
< TableCell align = "center" >
< StatusLabel
status = { row . status }
text = { row . status }
/* Use capitalize inside of Status Label */
/* Update component so we don ' t need to pass text and status separately */
customStyles = {{ textTransform : "capitalize" }}
/>
</ TableCell >
< TableCell align = "center" >
< Stack
direction = { "row" }
justifyContent = { "center" }
alignItems = { "center" }
gap = ".25rem"
>
< CPUChipIcon
width = { 20 }
height = { 20 }
2024-11-29 10:35:07 -05:00
/>
2024-11-29 10:53:53 -05:00
{ row . processor }
</ Stack >
</ TableCell >
< TableCell align = "center" >
< CustomGauge progress = { row . cpu } />
</ TableCell >
< TableCell align = "center" >
< CustomGauge progress = { row . mem } />
</ TableCell >
< TableCell align = "center" >
< CustomGauge progress = { row . disk } />
</ TableCell >
< TableCell align = "center" >
{ /* Get ActionsMenu from Monitor Table and create a component */ }
< IconButton
sx = {{
"& svg path" : {
stroke : theme . palette . other . icon ,
},
}}
>
2024-11-29 13:48:29 -05:00
< InfrastructureMenu
monitor = { row }
isAdmin = { isAdmin }
updateCallback = { handleActionMenuDelete }
/>
{ /* <GearIcon
2024-11-29 10:53:53 -05:00
width={20}
height={20}
2024-11-29 13:48:29 -05:00
/> */ }
2024-11-29 10:53:53 -05:00
</ IconButton >
</ TableCell >
</ TableRow >
))}
2024-11-15 14:55:05 -05:00
</ TableBody >
</ Table >
</ TableContainer >
2024-11-27 21:24:04 -05:00
< Pagination
monitorCount = { totalMonitors }
page = { page }
rowsPerPage = { rowsPerPage }
handleChangePage = { handleChangePage }
handleChangeRowsPerPage = { handleChangeRowsPerPage }
/>
2024-11-15 14:55:05 -05:00
</ Stack >
</ Stack >
2024-11-08 18:36:43 -05:00
);
2024-11-07 04:21:32 -05:00
}
export { Infrastructure };