refactor DU monitors page
This commit is contained in:
1 parent
68202dc88b
commit
1e7e7c7365
3 files changed
+202
-168
No files matched your search
@@ -0,0 +1,91 @@
|
|||||||
|
import DataTable from "../../../../../Components/Table";
|
||||||
|
import BarChart from "../../../../../Components/Charts/BarChart";
|
||||||
|
import { Box } from "@mui/material";
|
||||||
|
import { StatusLabel } from "../../../../../Components/Label";
|
||||||
|
|
||||||
|
//REFACTOR
|
||||||
|
import Host from "../../../../Uptime/Monitors/Components/Host";
|
||||||
|
import ActionsMenu from "../../../../Uptime/Monitors/Components/ActionsMenu";
|
||||||
|
import { useTheme } from "@mui/material/styles";
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
|
import { useMonitorUtils } from "../../../../../Hooks/useMonitorUtils";
|
||||||
|
const TYPE_MAP = {
|
||||||
|
distributed_http: "Distributed HTTP",
|
||||||
|
};
|
||||||
|
|
||||||
|
const MonitorTable = ({ isLoading, monitors }) => {
|
||||||
|
const theme = useTheme();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const { determineState } = useMonitorUtils();
|
||||||
|
|
||||||
|
const headers = [
|
||||||
|
{
|
||||||
|
id: "name",
|
||||||
|
content: <Box>Host</Box>,
|
||||||
|
render: (row) => (
|
||||||
|
<Host
|
||||||
|
key={row._id}
|
||||||
|
url={row.url}
|
||||||
|
title={row.title}
|
||||||
|
percentageColor={row.percentageColor}
|
||||||
|
percentage={row.percentage}
|
||||||
|
/>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "status",
|
||||||
|
content: <Box width="max-content"> Status</Box>,
|
||||||
|
render: (row) => {
|
||||||
|
const status = determineState(row?.monitor);
|
||||||
|
return (
|
||||||
|
<StatusLabel
|
||||||
|
status={status}
|
||||||
|
text={status}
|
||||||
|
customStyles={{ textTransform: "capitalize" }}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "responseTime",
|
||||||
|
content: "Response Time",
|
||||||
|
render: (row) => <BarChart checks={row?.monitor?.checks.slice().reverse()} />,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "type",
|
||||||
|
content: "Type",
|
||||||
|
render: (row) => <span>{TYPE_MAP[row.monitor.type]}</span>,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "actions",
|
||||||
|
content: "Actions",
|
||||||
|
render: (row) => (
|
||||||
|
<ActionsMenu
|
||||||
|
monitor={row.monitor}
|
||||||
|
isAdmin={true}
|
||||||
|
/>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<DataTable
|
||||||
|
shouldRender={!isLoading}
|
||||||
|
headers={headers}
|
||||||
|
data={monitors}
|
||||||
|
config={{
|
||||||
|
rowSX: {
|
||||||
|
cursor: "pointer",
|
||||||
|
"&:hover": {
|
||||||
|
backgroundColor: theme.palette.background.accent,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
onRowClick: (row) => {
|
||||||
|
navigate(`/distributed-uptime/${row.id}`);
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default MonitorTable;
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import { networkService } from "../../../../main";
|
||||||
|
import { useSelector } from "react-redux";
|
||||||
|
import { useTheme } from "@emotion/react";
|
||||||
|
import { useMonitorUtils } from "../../../../Hooks/useMonitorUtils";
|
||||||
|
import { createToast } from "../../../../Utils/toastUtils";
|
||||||
|
|
||||||
|
const useSubscribeToMonitors = () => {
|
||||||
|
// Redux
|
||||||
|
const { authToken, user } = useSelector((state) => state.auth);
|
||||||
|
|
||||||
|
// Local state
|
||||||
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
const [networkError, setNetworkError] = useState(false);
|
||||||
|
const [monitors, setMonitors] = useState(undefined);
|
||||||
|
const [monitorsSummary, setMonitorsSummary] = useState(undefined);
|
||||||
|
const [filteredMonitors, setFilteredMonitors] = useState(undefined);
|
||||||
|
|
||||||
|
const theme = useTheme();
|
||||||
|
const { getMonitorWithPercentage } = useMonitorUtils();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
try {
|
||||||
|
const cleanup = networkService.subscribeToDistributedUptimeMonitors({
|
||||||
|
authToken: authToken,
|
||||||
|
teamId: user.teamId,
|
||||||
|
limit: 25,
|
||||||
|
types: ["distributed_http"],
|
||||||
|
page: 0,
|
||||||
|
rowsPerPage: 10,
|
||||||
|
filter: null,
|
||||||
|
field: null,
|
||||||
|
order: null,
|
||||||
|
onUpdate: (data) => {
|
||||||
|
const res = data.monitors;
|
||||||
|
const { monitors, filteredMonitors, summary } = res;
|
||||||
|
const mappedMonitors = filteredMonitors.map((monitor) =>
|
||||||
|
getMonitorWithPercentage(monitor, theme)
|
||||||
|
);
|
||||||
|
setMonitors(monitors);
|
||||||
|
setMonitorsSummary(summary);
|
||||||
|
setFilteredMonitors(mappedMonitors);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return cleanup;
|
||||||
|
} catch (error) {
|
||||||
|
createToast({
|
||||||
|
body: error.message,
|
||||||
|
});
|
||||||
|
setNetworkError(true);
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
}, [authToken, user, getMonitorWithPercentage, theme]);
|
||||||
|
return [isLoading, networkError, monitors, monitorsSummary, filteredMonitors];
|
||||||
|
};
|
||||||
|
export { useSubscribeToMonitors };
|
||||||
@@ -1,185 +1,70 @@
|
|||||||
// Components
|
// Components
|
||||||
import { Stack, Box, Button } from "@mui/material";
|
import { Stack, Typography } from "@mui/material";
|
||||||
import DataTable from "../../../Components/Table";
|
|
||||||
import Breadcrumbs from "../../../Components/Breadcrumbs";
|
import Breadcrumbs from "../../../Components/Breadcrumbs";
|
||||||
import Host from "../../Uptime/Monitors/Components/Host";
|
import CreateMonitorHeader from "../../../Components/MonitorCreateHeader";
|
||||||
import BarChart from "../../../Components/Charts/BarChart";
|
import MonitorTable from "./Components/MonitorTable";
|
||||||
import ActionsMenu from "../../Uptime/Monitors/Components/ActionsMenu";
|
import Fallback from "../../../Components/Fallback";
|
||||||
import { StatusLabel } from "../../../Components/Label";
|
import GenericFallback from "../../../Components/GenericFallback";
|
||||||
// Utils
|
|
||||||
import { networkService } from "../../../main";
|
|
||||||
import { useTheme } from "@mui/material/styles";
|
|
||||||
import { useNavigate } from "react-router-dom";
|
|
||||||
import useUtils from "../../Uptime/Monitors/Hooks/useUtils";
|
|
||||||
import { useEffect, useState } from "react";
|
|
||||||
import { useSelector } from "react-redux";
|
|
||||||
|
|
||||||
|
// Utils
|
||||||
|
import { useTheme } from "@mui/material/styles";
|
||||||
|
import { useSelector } from "react-redux";
|
||||||
|
import { useIsAdmin } from "../../../Hooks/useIsAdmin";
|
||||||
|
import { useSubscribeToMonitors } from "./Hooks/useSubscribeToMonitors";
|
||||||
// Constants
|
// Constants
|
||||||
const BREADCRUMBS = [{ name: `Distributed Uptime`, path: "/distributed-uptime" }];
|
const BREADCRUMBS = [{ name: `Distributed Uptime`, path: "/distributed-uptime" }];
|
||||||
const TYPE_MAP = {
|
|
||||||
distributed_http: "Distributed HTTP",
|
|
||||||
};
|
|
||||||
|
|
||||||
const DistributedUptimeMonitors = () => {
|
const DistributedUptimeMonitors = () => {
|
||||||
// Redux state
|
// Redux state
|
||||||
const { authToken, user } = useSelector((state) => state.auth);
|
const { authToken, user } = useSelector((state) => state.auth);
|
||||||
// Local state
|
// Local state
|
||||||
const [monitors, setMonitors] = useState([]);
|
|
||||||
const [filteredMonitors, setFilteredMonitors] = useState([]);
|
|
||||||
const [monitorsSummary, setMonitorsSummary] = useState({});
|
|
||||||
// Utils
|
// Utils
|
||||||
|
|
||||||
const { determineState } = useUtils();
|
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const navigate = useNavigate();
|
const isAdmin = useIsAdmin();
|
||||||
const headers = [
|
const [isLoading, networkError, monitors, monitorsSummary, filteredMonitors] =
|
||||||
{
|
useSubscribeToMonitors();
|
||||||
id: "name",
|
|
||||||
content: <Box>Host</Box>,
|
|
||||||
render: (row) => (
|
|
||||||
<Host
|
|
||||||
key={row._id}
|
|
||||||
url={row.url}
|
|
||||||
title={row.title}
|
|
||||||
percentageColor={row.percentageColor}
|
|
||||||
percentage={row.percentage}
|
|
||||||
/>
|
|
||||||
),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "status",
|
|
||||||
content: <Box width="max-content"> Status</Box>,
|
|
||||||
render: (row) => {
|
|
||||||
const status = determineState(row?.monitor);
|
|
||||||
return (
|
|
||||||
<StatusLabel
|
|
||||||
status={status}
|
|
||||||
text={status}
|
|
||||||
customStyles={{ textTransform: "capitalize" }}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "responseTime",
|
|
||||||
content: "Response Time",
|
|
||||||
render: (row) => <BarChart checks={row?.monitor?.checks.slice().reverse()} />,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "type",
|
|
||||||
content: "Type",
|
|
||||||
render: (row) => <span>{TYPE_MAP[row.monitor.type]}</span>,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "actions",
|
|
||||||
content: "Actions",
|
|
||||||
render: (row) => (
|
|
||||||
<ActionsMenu
|
|
||||||
monitor={row.monitor}
|
|
||||||
isAdmin={true}
|
|
||||||
/>
|
|
||||||
),
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
const getMonitorWithPercentage = (monitor, theme) => {
|
if (networkError) {
|
||||||
let uptimePercentage = "";
|
return (
|
||||||
let percentageColor = "";
|
<GenericFallback>
|
||||||
|
<Typography
|
||||||
if (monitor.uptimePercentage !== undefined) {
|
variant="h1"
|
||||||
uptimePercentage =
|
marginY={theme.spacing(4)}
|
||||||
monitor.uptimePercentage === 0
|
color={theme.palette.primary.contrastTextTertiary}
|
||||||
? "0"
|
|
||||||
: (monitor.uptimePercentage * 100).toFixed(2);
|
|
||||||
|
|
||||||
percentageColor =
|
|
||||||
monitor.uptimePercentage < 0.25
|
|
||||||
? theme.palette.error.main
|
|
||||||
: monitor.uptimePercentage < 0.5
|
|
||||||
? theme.palette.warning.main
|
|
||||||
: monitor.uptimePercentage < 0.75
|
|
||||||
? theme.palette.success.main
|
|
||||||
: theme.palette.success.main;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
id: monitor._id,
|
|
||||||
name: monitor.name,
|
|
||||||
url: monitor.url,
|
|
||||||
title: monitor.name,
|
|
||||||
percentage: uptimePercentage,
|
|
||||||
percentageColor,
|
|
||||||
monitor: monitor,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const cleanup = networkService.subscribeToDistributedUptimeMonitors({
|
|
||||||
authToken: authToken,
|
|
||||||
teamId: user.teamId,
|
|
||||||
limit: 25,
|
|
||||||
types: ["distributed_http"],
|
|
||||||
page: 0,
|
|
||||||
rowsPerPage: 10,
|
|
||||||
filter: null,
|
|
||||||
field: null,
|
|
||||||
order: null,
|
|
||||||
onUpdate: (data) => {
|
|
||||||
const res = data.monitors;
|
|
||||||
const { monitors, filteredMonitors, summary } = res;
|
|
||||||
const mappedMonitors = filteredMonitors.map((monitor) =>
|
|
||||||
getMonitorWithPercentage(monitor, theme)
|
|
||||||
);
|
|
||||||
setMonitors(monitors);
|
|
||||||
setFilteredMonitors(mappedMonitors);
|
|
||||||
setMonitorsSummary(summary);
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return cleanup;
|
|
||||||
}, [user.teamId, authToken, theme]);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Stack
|
|
||||||
direction="column"
|
|
||||||
gap={theme.spacing(8)}
|
|
||||||
>
|
|
||||||
<Breadcrumbs list={BREADCRUMBS} />
|
|
||||||
<Stack
|
|
||||||
direction="row"
|
|
||||||
justifyContent="end"
|
|
||||||
alignItems="center"
|
|
||||||
mt={theme.spacing(5)}
|
|
||||||
gap={theme.spacing(6)}
|
|
||||||
>
|
|
||||||
<Button
|
|
||||||
variant="contained"
|
|
||||||
color="primary"
|
|
||||||
onClick={() => {
|
|
||||||
navigate("/distributed-uptime/create");
|
|
||||||
}}
|
|
||||||
sx={{ fontWeight: 500, whiteSpace: "nowrap" }}
|
|
||||||
>
|
>
|
||||||
Create new
|
Network error
|
||||||
</Button>
|
</Typography>
|
||||||
</Stack>
|
<Typography>Please check your connection</Typography>
|
||||||
{monitors.length > 0 && (
|
</GenericFallback>
|
||||||
<DataTable
|
);
|
||||||
headers={headers}
|
}
|
||||||
data={filteredMonitors}
|
|
||||||
config={{
|
if (typeof monitorsSummary !== "undefined" && monitorsSummary.totalMonitors === 0) {
|
||||||
rowSX: {
|
return (
|
||||||
cursor: "pointer",
|
<Fallback
|
||||||
"&:hover": {
|
vowelStart={false}
|
||||||
backgroundColor: theme.palette.background.accent,
|
title="distributed uptime monitor"
|
||||||
},
|
checks={[
|
||||||
},
|
"Check if a website or service is up and running",
|
||||||
onRowClick: (row) => {
|
"Make use of UpRock's distributed network",
|
||||||
navigate(`/distributed-uptime/${row.id}`);
|
]}
|
||||||
},
|
link="/distributed-uptime/create"
|
||||||
}}
|
isAdmin={isAdmin}
|
||||||
/>
|
/>
|
||||||
)}
|
);
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<Stack gap={theme.spacing(10)}>
|
||||||
|
<Breadcrumbs list={BREADCRUMBS} />
|
||||||
|
<CreateMonitorHeader
|
||||||
|
isAdmin={isAdmin}
|
||||||
|
shouldRender={!isLoading}
|
||||||
|
path="/distributed-uptime/create"
|
||||||
|
/>
|
||||||
|
<MonitorTable
|
||||||
|
isLoading={isLoading}
|
||||||
|
monitors={filteredMonitors}
|
||||||
|
/>
|
||||||
</Stack>
|
</Stack>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user