From 55cf4e0cc83258ee96d4d6854a8d6a04b29b29bc Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Sat, 23 Nov 2024 12:00:35 +0800 Subject: [PATCH 1/5] add safety checks and simple empty view for details page --- .../Pages/Infrastructure/Details/index.jsx | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/Client/src/Pages/Infrastructure/Details/index.jsx b/Client/src/Pages/Infrastructure/Details/index.jsx index 5f3ec7f77..eaa3098b3 100644 --- a/Client/src/Pages/Infrastructure/Details/index.jsx +++ b/Client/src/Pages/Infrastructure/Details/index.jsx @@ -16,6 +16,7 @@ import { InfrastructureTooltip, } from "../../../Components/Charts/Utils/chartUtils"; import PropTypes from "prop-types"; +import { DateRangeIcon } from "@mui/x-date-pickers"; const BASE_BOX_PADDING_VERTICAL = 4; const BASE_BOX_PADDING_HORIZONTAL = 8; @@ -106,6 +107,7 @@ StatBox.propTypes = { */ const GaugeBox = ({ value, heading, metricOne, valueOne, metricTwo, valueTwo }) => { const theme = useTheme(); + return ( { numToDisplay: 50, normalize: true, }); + setMonitor(response.data.data); } catch (error) { console.error(error); } }; fetchData(); - }, []); + }, [authToken, monitorId, dateRange]); const statBoxConfigs = [ { id: 0, heading: "CPU", - subHeading: `${monitor?.checks[0]?.cpu?.physical_core} cores`, + subHeading: `${monitor?.checks[0]?.cpu?.physical_core ?? 0} cores`, }, { id: 1, @@ -230,26 +233,26 @@ const InfrastructureDetails = () => { const gaugeBoxConfigs = [ { type: "memory", - value: monitor?.checks[0]?.memory?.usage_percent * 100, + value: monitor?.checks[0]?.memory?.usage_percent ?? 0 * 100, heading: "Memory Usage", metricOne: "Used", - valueOne: formatBytes(monitor?.checks[0]?.memory?.used_bytes), + valueOne: formatBytes(monitor?.checks[0]?.memory?.used_bytes ?? 0), metricTwo: "Total", - valueTwo: formatBytes(monitor?.checks[0]?.memory?.total_bytes), + valueTwo: formatBytes(monitor?.checks[0]?.memory?.total_bytes ?? 0), }, { type: "cpu", - value: monitor?.checks[0]?.cpu?.usage_percent * 100, + value: monitor?.checks[0]?.cpu?.usage_percent ?? 0 * 100, heading: "CPU Usage", metricOne: "Cores", - valueOne: monitor?.checks[0]?.cpu?.physical_core, + valueOne: monitor?.checks[0]?.cpu?.physical_core ?? 0, metricTwo: "Frequency", - valueTwo: `${(monitor?.checks[0]?.cpu?.frequency / 1000).toFixed(2)} Ghz`, + valueTwo: `${(monitor?.checks[0]?.cpu?.frequency ?? 0 / 1000).toFixed(2)} Ghz`, }, - ...(monitor?.checks[0]?.disk ?? []).map((disk, idx) => ({ + ...(monitor?.checks?.[0]?.disk ?? []).map((disk, idx) => ({ type: "disk", diskIndex: idx, - value: disk.usage_percent * 100, + value: disk.usage_percent * 100 ?? 0, heading: `Disk${idx} usage`, metricOne: "Used", valueOne: formatBytes(disk.total_bytes - disk.free_bytes), @@ -284,9 +287,9 @@ const InfrastructureDetails = () => { ]; return ( - monitor && ( - - + + + {monitor?.checks?.length > 0 ? ( { ))} - - ) + ) : ( + + Hang tight, there's no data yet + + )} + ); }; From 2732d5f64c7547f3e2986dee46e67a63ab47a8f1 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Sat, 23 Nov 2024 17:14:05 +0800 Subject: [PATCH 2/5] add empty view according to spec --- .../Pages/Infrastructure/Details/empty.jsx | 37 +++++++++++++++++++ .../Pages/Infrastructure/Details/index.jsx | 18 +++++---- 2 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 Client/src/Pages/Infrastructure/Details/empty.jsx diff --git a/Client/src/Pages/Infrastructure/Details/empty.jsx b/Client/src/Pages/Infrastructure/Details/empty.jsx new file mode 100644 index 000000000..4a311233f --- /dev/null +++ b/Client/src/Pages/Infrastructure/Details/empty.jsx @@ -0,0 +1,37 @@ +import { useTheme } from "@emotion/react"; +import PlaceholderLight from "../../../assets/Images/data_placeholder.svg?react"; +import PlaceholderDark from "../../../assets/Images/data_placeholder_dark.svg?react"; +import { Box, Typography, Stack } from "@mui/material"; +import PropTypes from "prop-types"; +import { useSelector } from "react-redux"; +const Empty = ({ styles }) => { + const theme = useTheme(); + const mode = useSelector((state) => state.ui.mode); + return ( + + + {mode === "light" ? : } + + Your infrastructure dashboard will show here + + Hang tight! When we receive data, we'll show it here. Please check back in a few + minutes. + + + + ); +}; + +Empty.propTypes = { + styles: PropTypes.object, + mode: PropTypes.string, +}; + +export default Empty; diff --git a/Client/src/Pages/Infrastructure/Details/index.jsx b/Client/src/Pages/Infrastructure/Details/index.jsx index eaa3098b3..f9c8dbe1f 100644 --- a/Client/src/Pages/Infrastructure/Details/index.jsx +++ b/Client/src/Pages/Infrastructure/Details/index.jsx @@ -9,6 +9,7 @@ import { useSelector } from "react-redux"; import { networkService } from "../../../main"; import PulseDot from "../../../Components/Animated/PulseDot"; import useUtils from "../../Monitors/utils"; +import Empty from "./empty"; import { formatDurationRounded, formatDurationSplit } from "../../../Utils/timeUtils"; import { TzTick, @@ -16,7 +17,6 @@ import { InfrastructureTooltip, } from "../../../Components/Charts/Utils/chartUtils"; import PropTypes from "prop-types"; -import { DateRangeIcon } from "@mui/x-date-pickers"; const BASE_BOX_PADDING_VERTICAL = 4; const BASE_BOX_PADDING_HORIZONTAL = 8; @@ -396,13 +396,15 @@ const InfrastructureDetails = () => { ) : ( - - Hang tight, there's no data yet - + )} ); From 4fd8d440127606bbc67f051036ba1bd6c58d6ebc Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Sat, 23 Nov 2024 17:20:34 +0800 Subject: [PATCH 3/5] redirect on error --- Client/src/Pages/Infrastructure/Details/index.jsx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Client/src/Pages/Infrastructure/Details/index.jsx b/Client/src/Pages/Infrastructure/Details/index.jsx index f9c8dbe1f..6730ed7ec 100644 --- a/Client/src/Pages/Infrastructure/Details/index.jsx +++ b/Client/src/Pages/Infrastructure/Details/index.jsx @@ -9,6 +9,7 @@ import { useSelector } from "react-redux"; import { networkService } from "../../../main"; import PulseDot from "../../../Components/Animated/PulseDot"; import useUtils from "../../Monitors/utils"; +import { useNavigate } from "react-router-dom"; import Empty from "./empty"; import { formatDurationRounded, formatDurationSplit } from "../../../Utils/timeUtils"; import { @@ -163,6 +164,7 @@ GaugeBox.propTypes = { * @returns {React.ReactElement} Infrastructure details page component */ const InfrastructureDetails = () => { + const navigate = useNavigate(); const theme = useTheme(); const { monitorId } = useParams(); const navList = [ @@ -200,6 +202,8 @@ const InfrastructureDetails = () => { setMonitor(response.data.data); } catch (error) { + navigate("/not-found", { replace: true }); + console.error(error); } }; From fec43778fafcb2ce7531fc24695536680e983ab4 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Mon, 25 Nov 2024 10:22:21 +0800 Subject: [PATCH 4/5] simplify formatting of values --- .../Pages/Infrastructure/Details/index.jsx | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/Client/src/Pages/Infrastructure/Details/index.jsx b/Client/src/Pages/Infrastructure/Details/index.jsx index 6730ed7ec..b76aa3de3 100644 --- a/Client/src/Pages/Infrastructure/Details/index.jsx +++ b/Client/src/Pages/Infrastructure/Details/index.jsx @@ -28,6 +28,7 @@ const TYPOGRAPHY_PADDING = 8; * @returns {number} Converted value in gigabytes */ const formatBytes = (bytes) => { + if (bytes === undefined || bytes === null) return "0 GB"; if (typeof bytes !== "number") return "0 GB"; if (bytes === 0) return "0 GB"; @@ -41,6 +42,22 @@ const formatBytes = (bytes) => { } }; +/** + * Converts a decimal value to a percentage + * + * @function decimalToPercentage + * @param {number} value - Decimal value to convert + * @returns {number} Percentage representation + * + * @example + * decimalToPercentage(0.75) // Returns 75 + * decimalToPercentage(null) // Returns 0 + */ +const decimalToPercentage = (value) => { + if (value === null || undefined) return 0; + return value * 100; +}; + /** * Renders a base box with consistent styling * @param {Object} props - Component properties @@ -237,16 +254,16 @@ const InfrastructureDetails = () => { const gaugeBoxConfigs = [ { type: "memory", - value: monitor?.checks[0]?.memory?.usage_percent ?? 0 * 100, + value: decimalToPercentage(monitor?.checks[0]?.memory?.usage_percent), heading: "Memory Usage", metricOne: "Used", - valueOne: formatBytes(monitor?.checks[0]?.memory?.used_bytes ?? 0), + valueOne: formatBytes(monitor?.checks[0]?.memory?.used_bytes), metricTwo: "Total", - valueTwo: formatBytes(monitor?.checks[0]?.memory?.total_bytes ?? 0), + valueTwo: formatBytes(monitor?.checks[0]?.memory?.total_bytes), }, { type: "cpu", - value: monitor?.checks[0]?.cpu?.usage_percent ?? 0 * 100, + value: decimalToPercentage(monitor?.checks[0]?.cpu?.usage_percent), heading: "CPU Usage", metricOne: "Cores", valueOne: monitor?.checks[0]?.cpu?.physical_core ?? 0, @@ -256,7 +273,7 @@ const InfrastructureDetails = () => { ...(monitor?.checks?.[0]?.disk ?? []).map((disk, idx) => ({ type: "disk", diskIndex: idx, - value: disk.usage_percent * 100 ?? 0, + value: decimalToPercentage(disk.usage_percent), heading: `Disk${idx} usage`, metricOne: "Used", valueOne: formatBytes(disk.total_bytes - disk.free_bytes), From 21546ef65c18a5f181d936208440f1190dc583b7 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Mon, 25 Nov 2024 10:33:52 +0800 Subject: [PATCH 5/5] fix conditional error --- Client/src/Pages/Infrastructure/Details/index.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Client/src/Pages/Infrastructure/Details/index.jsx b/Client/src/Pages/Infrastructure/Details/index.jsx index b76aa3de3..e1611f982 100644 --- a/Client/src/Pages/Infrastructure/Details/index.jsx +++ b/Client/src/Pages/Infrastructure/Details/index.jsx @@ -54,7 +54,7 @@ const formatBytes = (bytes) => { * decimalToPercentage(null) // Returns 0 */ const decimalToPercentage = (value) => { - if (value === null || undefined) return 0; + if (value === null || value === undefined) return 0; return value * 100; };