From bf5d7f3c5322d49fe1c4780966aea013ebe6713a Mon Sep 17 00:00:00 2001 From: singh-kanwarpreet Date: Sat, 26 Jul 2025 15:24:25 +0530 Subject: [PATCH 01/13] Adding Global Threshold Settings --- .../src/Pages/Infrastructure/Create/index.jsx | 74 +++++++---- .../Settings/SettingsGlobalThresholds.jsx | 117 ++++++++++++++++++ client/src/Pages/Settings/index.jsx | 11 ++ client/src/Validation/validation.js | 7 ++ client/src/locales/en.json | 4 + server/src/db/models/AppSettings.js | 8 +- server/src/validation/joi.js | 8 ++ 7 files changed, 203 insertions(+), 26 deletions(-) create mode 100644 client/src/Pages/Settings/SettingsGlobalThresholds.jsx diff --git a/client/src/Pages/Infrastructure/Create/index.jsx b/client/src/Pages/Infrastructure/Create/index.jsx index 0800bb500..fa6ac4fcb 100644 --- a/client/src/Pages/Infrastructure/Create/index.jsx +++ b/client/src/Pages/Infrastructure/Create/index.jsx @@ -3,7 +3,7 @@ import { useTheme } from "@emotion/react"; import { useParams } from "react-router-dom"; import { useState, useEffect } from "react"; import { useSelector } from "react-redux"; - +import axios from "axios"; // Utility and Network import { infrastructureMonitorValidation } from "../../../Validation/validation"; import { useFetchHardwareMonitorById } from "../../../Hooks/monitorHooks"; @@ -87,35 +87,59 @@ const CreateInfrastructureMonitor = () => { }); // Populate form fields if editing + // Populate global thresholds from /settings if creating + const { authToken } = useSelector((state) => state.auth); + useEffect(() => { - if (isCreate || !monitor) return; + const fetchGlobalThresholds = async () => { + if (!isCreate) return; - setInfrastructureMonitor({ - url: monitor.url.replace(/^https?:\/\//, ""), - name: monitor.name || "", - notifications: monitor.notifications, - interval: monitor.interval / MS_PER_MINUTE, - cpu: monitor.thresholds?.usage_cpu !== undefined, - usage_cpu: monitor.thresholds?.usage_cpu ? monitor.thresholds.usage_cpu * 100 : "", + try { + const token = authToken; + if (!token) { + console.warn("No token found for fetching global thresholds."); + return; + } - memory: monitor.thresholds?.usage_memory !== undefined, - usage_memory: monitor.thresholds?.usage_memory - ? monitor.thresholds.usage_memory * 100 - : "", + const response = await axios.get(`${import.meta.env.VITE_APP_API_BASE_URL}/settings`, { + headers: { + Authorization: `Bearer ${token}`, + }, + }); - disk: monitor.thresholds?.usage_disk !== undefined, - usage_disk: monitor.thresholds?.usage_disk - ? monitor.thresholds.usage_disk * 100 - : "", + const globalThresholds = response.data?.data?.settings?.globalThresholds; - temperature: monitor.thresholds?.usage_temperature !== undefined, - usage_temperature: monitor.thresholds?.usage_temperature - ? monitor.thresholds.usage_temperature * 100 - : "", - secret: monitor.secret || "", - }); - setHttps(monitor.url.startsWith("https")); - }, [isCreate, monitor]); + if (!globalThresholds) { + console.warn("No global thresholds found in /settings response."); + return; + } + + setInfrastructureMonitor((prev) => ({ + ...prev, + cpu: globalThresholds.cpu != null, + usage_cpu: globalThresholds.cpu != null ? globalThresholds.cpu.toString() : "", + + memory: globalThresholds.memory != null, + usage_memory: + globalThresholds.memory != null ? globalThresholds.memory.toString() : "", + + disk: globalThresholds.disk != null, + usage_disk: + globalThresholds.disk != null ? globalThresholds.disk.toString() : "", + + temperature: globalThresholds.temperature != null, + usage_temperature: + globalThresholds.temperature != null + ? globalThresholds.temperature.toString() + : "", + })); + } catch (error) { + console.error("Failed to fetch global thresholds:", error); + } + }; + + fetchGlobalThresholds(); + }, [isCreate, user]); // Handlers const onSubmit = async (event) => { diff --git a/client/src/Pages/Settings/SettingsGlobalThresholds.jsx b/client/src/Pages/Settings/SettingsGlobalThresholds.jsx new file mode 100644 index 000000000..eca752688 --- /dev/null +++ b/client/src/Pages/Settings/SettingsGlobalThresholds.jsx @@ -0,0 +1,117 @@ +import Box from "@mui/material/Box"; +import Stack from "@mui/material/Stack"; +import Typography from "@mui/material/Typography"; +import ConfigBox from "../../Components/ConfigBox"; +import TextInput from "../../Components/Inputs/TextInput"; + +import { useTheme } from "@emotion/react"; +import { PropTypes } from "prop-types"; +import { useState, useEffect } from "react"; +import { useTranslation } from "react-i18next"; + +const SettingsGlobalThresholds = ({ + isAdmin, + HEADING_SX, + settingsData, + setSettingsData, +}) => { + const { t } = useTranslation(); // For language translation + const theme = useTheme(); // MUI theme access + + // Local state for thresholds + const [thresholds, setThresholds] = useState({ + cpu: "", + memory: "", + disk: "", + temperature: "", + }); + + // Load existing thresholds from settingsData on first render or when settingsData changes + useEffect(() => { + if (settingsData?.settings?.globalThresholds) { + setThresholds({ + cpu: settingsData.settings.globalThresholds.cpu || "", + memory: settingsData.settings.globalThresholds.memory || "", + disk: settingsData.settings.globalThresholds.disk || "", + temperature: settingsData.settings.globalThresholds.temperature || "", + }); + } + }, [settingsData]); + + // Handles input change and updates state & parent data + const handleChange = (e, min, max) => { + const { name, value } = e.target; + + // Allow empty or numeric value within range + if ( + value === "" || + (/^\d+$/.test(value) && Number(value) >= min && Number(value) <= max) + ) { + const updatedThresholds = { + ...thresholds, + [name]: value, + }; + + setThresholds(updatedThresholds); + + // Update settingsData without enabling/disabling metrics + setSettingsData({ + ...settingsData, + settings: { + ...settingsData.settings, + globalThresholds: updatedThresholds, + }, + }); + } + }; + + // Only render this section for admins + if (!isAdmin) return null; + + return ( + + {/* Header and description */} + + + {t("settingsPage.globalThresholds.title", "Global Thresholds")} + + + {t( + "settingsPage.globalThresholds.description", + "Configure global CPU, Memory, Disk, and Temperature thresholds." + )} + + + + {/* Threshold inputs */} + + {[ + ["CPU Threshold (%)", "cpu", 1, 100], + ["Memory Threshold (%)", "memory", 1, 100], + ["Disk Threshold (%)", "disk", 1, 100], + ["Temperature Threshold (°C)", "temperature", 1, 150], + ].map(([label, name, min, max]) => ( + handleChange(e, min, max)} + /> + ))} + + + ); +}; + +// Prop types +SettingsGlobalThresholds.propTypes = { + isAdmin: PropTypes.bool, + HEADING_SX: PropTypes.object, + settingsData: PropTypes.object, + setSettingsData: PropTypes.func, +}; + +export default SettingsGlobalThresholds; diff --git a/client/src/Pages/Settings/index.jsx b/client/src/Pages/Settings/index.jsx index dae6bd680..ab529eb68 100644 --- a/client/src/Pages/Settings/index.jsx +++ b/client/src/Pages/Settings/index.jsx @@ -8,6 +8,7 @@ import SettingsPagespeed from "./SettingsPagespeed"; import SettingsDemoMonitors from "./SettingsDemoMonitors"; import SettingsAbout from "./SettingsAbout"; import SettingsEmail from "./SettingsEmail"; +import SettingsGlobalThresholds from "./SettingsGlobalThresholds"; import Button from "@mui/material/Button"; // Utils import { settingsValidation } from "../../Validation/validation"; @@ -48,6 +49,7 @@ const Settings = () => { setIsApiKeySet, setIsEmailPasswordSet, }); + const [addDemoMonitors, isAddingDemoMonitors] = useAddDemoMonitors(); const [isSaving, saveError, saveSettings] = useSaveSettings({ @@ -60,6 +62,7 @@ const Settings = () => { const [deleteAllMonitors, isDeletingMonitors] = useDeleteAllMonitors(); const [deleteMonitorStats, isDeletingMonitorStats] = useDeleteMonitorStats(); + // Setup const isAdmin = useIsAdmin(); const theme = useTheme(); @@ -149,6 +152,7 @@ const Settings = () => { error.details.forEach((err) => { newErrors[err.path[0]] = err.message; }); + setErrors(newErrors); } saveSettings(settingsData?.settings); @@ -190,6 +194,13 @@ const Settings = () => { handleChange={handleChange} errors={errors} /> + + { if (!dayjs(value).isValid()) { return helpers.error("any.invalid"); diff --git a/client/src/locales/en.json b/client/src/locales/en.json index 78d0f2aed..5c69ecd81 100644 --- a/client/src/locales/en.json +++ b/client/src/locales/en.json @@ -837,6 +837,10 @@ "title": "Display timezone" }, "title": "Settings", + "globalThresholds": { + "title": "Global Thresholds", + "description": "Configure global CPU, Memory, Disk, and Temperature thresholds. If a value is provided, it will automatically be enabled for monitoring." + }, "uiSettings": { "description": "Switch between light and dark mode, or change user interface language.", "labelLanguage": "Language", diff --git a/server/src/db/models/AppSettings.js b/server/src/db/models/AppSettings.js index cd63118e9..be7c856a4 100755 --- a/server/src/db/models/AppSettings.js +++ b/server/src/db/models/AppSettings.js @@ -65,7 +65,13 @@ const AppSettingsSchema = mongoose.Schema( type: Number, default: 1, }, - }, + globalThresholds: { + cpu: { type: Number }, + memory: { type: Number}, + disk: { type: Number}, + temperature: { type: Number}, + } +}, { timestamps: true, } diff --git a/server/src/validation/joi.js b/server/src/validation/joi.js index 744b0ec74..51d8a94a2 100755 --- a/server/src/validation/joi.js +++ b/server/src/validation/joi.js @@ -416,8 +416,16 @@ const updateAppSettingsBodyValidation = joi.object({ systemEmailIgnoreTLS: joi.boolean(), systemEmailRequireTLS: joi.boolean(), systemEmailRejectUnauthorized: joi.boolean(), + + globalThresholds: joi.object({ + cpu: joi.number().min(1).max(100).allow("").optional(), + memory: joi.number().min(1).max(100).allow("").optional(), + disk: joi.number().min(1).max(100).allow("").optional(), + temperature: joi.number().min(1).max(150).allow("").optional(), + }).optional(), }); + //**************************************** // Status Page Validation //**************************************** From 8d105e636db84b64678dfcfd0662098542c1a0ee Mon Sep 17 00:00:00 2001 From: singh-kanwarpreet Date: Tue, 29 Jul 2025 00:42:56 +0530 Subject: [PATCH 02/13] Updated Global THreshold Code Quality --- client/src/Hooks/monitorHooks.js | 26 +++++++ .../src/Pages/Infrastructure/Create/index.jsx | 72 +++++++------------ 2 files changed, 50 insertions(+), 48 deletions(-) diff --git a/client/src/Hooks/monitorHooks.js b/client/src/Hooks/monitorHooks.js index 4f0e9c049..6b8cb983b 100644 --- a/client/src/Hooks/monitorHooks.js +++ b/client/src/Hooks/monitorHooks.js @@ -300,6 +300,31 @@ const useCreateMonitor = () => { return [createMonitor, isLoading]; }; +const useFetchGlobalSettings = () => { + const [isLoading, setIsLoading] = useState(true); + const [globalSettings, setGlobalSettings] = useState(null); + + useEffect(() => { + const fetchGlobalSettings = async () => { + try { + const res = await networkService.getAppSettings(); + setGlobalSettings(res?.data); + setIsLoading(false); + } catch (error) { + console.error("Failed to fetch global settings:", error); + } finally { + setIsLoading(false); + } + }; + + fetchGlobalSettings(); + }, []); + + return [globalSettings, isLoading]; +}; + + + const useDeleteMonitor = () => { const [isLoading, setIsLoading] = useState(false); const navigate = useNavigate(); @@ -499,6 +524,7 @@ export { useFetchUptimeMonitorById, useFetchHardwareMonitorById, useCreateMonitor, + useFetchGlobalSettings, useDeleteMonitor, useUpdateMonitor, usePauseMonitor, diff --git a/client/src/Pages/Infrastructure/Create/index.jsx b/client/src/Pages/Infrastructure/Create/index.jsx index fa6ac4fcb..151383cfb 100644 --- a/client/src/Pages/Infrastructure/Create/index.jsx +++ b/client/src/Pages/Infrastructure/Create/index.jsx @@ -11,7 +11,11 @@ import { capitalizeFirstLetter } from "../../../Utils/stringUtils"; import { useTranslation } from "react-i18next"; import { useGetNotificationsByTeamId } from "../../../Hooks/useNotifications"; import NotificationsConfig from "../../../Components/NotificationConfig"; -import { useUpdateMonitor, useCreateMonitor } from "../../../Hooks/monitorHooks"; +import { + useUpdateMonitor, + useCreateMonitor, + useFetchGlobalSettings, +} from "../../../Hooks/monitorHooks"; // MUI import { Box, Stack, Typography, Button, ButtonGroup } from "@mui/material"; @@ -65,6 +69,7 @@ const CreateInfrastructureMonitor = () => { useGetNotificationsByTeamId(); const [updateMonitor, isUpdating] = useUpdateMonitor(); const [createMonitor, isCreating] = useCreateMonitor(); + const [globalSettings, globalSettingsLoading] = useFetchGlobalSettings(); // State const [errors, setErrors] = useState({}); @@ -87,59 +92,30 @@ const CreateInfrastructureMonitor = () => { }); // Populate form fields if editing - // Populate global thresholds from /settings if creating - const { authToken } = useSelector((state) => state.auth); useEffect(() => { - const fetchGlobalThresholds = async () => { - if (!isCreate) return; + if (!isCreate || globalSettingsLoading) return; + const globalThresholds = globalSettings?.data?.settings?.globalThresholds; - try { - const token = authToken; - if (!token) { - console.warn("No token found for fetching global thresholds."); - return; - } + setInfrastructureMonitor((prev) => ({ + ...prev, + cpu: globalThresholds.cpu != null, + usage_cpu: globalThresholds.cpu != null ? globalThresholds.cpu.toString() : "", - const response = await axios.get(`${import.meta.env.VITE_APP_API_BASE_URL}/settings`, { - headers: { - Authorization: `Bearer ${token}`, - }, - }); + memory: globalThresholds.memory != null, + usage_memory: + globalThresholds.memory != null ? globalThresholds.memory.toString() : "", - const globalThresholds = response.data?.data?.settings?.globalThresholds; + disk: globalThresholds.disk != null, + usage_disk: globalThresholds.disk != null ? globalThresholds.disk.toString() : "", - if (!globalThresholds) { - console.warn("No global thresholds found in /settings response."); - return; - } - - setInfrastructureMonitor((prev) => ({ - ...prev, - cpu: globalThresholds.cpu != null, - usage_cpu: globalThresholds.cpu != null ? globalThresholds.cpu.toString() : "", - - memory: globalThresholds.memory != null, - usage_memory: - globalThresholds.memory != null ? globalThresholds.memory.toString() : "", - - disk: globalThresholds.disk != null, - usage_disk: - globalThresholds.disk != null ? globalThresholds.disk.toString() : "", - - temperature: globalThresholds.temperature != null, - usage_temperature: - globalThresholds.temperature != null - ? globalThresholds.temperature.toString() - : "", - })); - } catch (error) { - console.error("Failed to fetch global thresholds:", error); - } - }; - - fetchGlobalThresholds(); - }, [isCreate, user]); + temperature: globalThresholds.temperature != null, + usage_temperature: + globalThresholds.temperature != null + ? globalThresholds.temperature.toString() + : "", + })); + }, [isCreate, globalSettings]); // Handlers const onSubmit = async (event) => { From ad06544a649b436568088bc649941b2d4737bff6 Mon Sep 17 00:00:00 2001 From: singh-kanwarpreet Date: Tue, 29 Jul 2025 00:54:25 +0530 Subject: [PATCH 03/13] Removed Consecutive same statements --- client/src/Hooks/monitorHooks.js | 1 - 1 file changed, 1 deletion(-) diff --git a/client/src/Hooks/monitorHooks.js b/client/src/Hooks/monitorHooks.js index 6b8cb983b..767ef5af8 100644 --- a/client/src/Hooks/monitorHooks.js +++ b/client/src/Hooks/monitorHooks.js @@ -309,7 +309,6 @@ const useFetchGlobalSettings = () => { try { const res = await networkService.getAppSettings(); setGlobalSettings(res?.data); - setIsLoading(false); } catch (error) { console.error("Failed to fetch global settings:", error); } finally { From dbea93f9babf46562921c0b0471aaf203c86b05b Mon Sep 17 00:00:00 2001 From: singh-kanwarpreet Date: Tue, 29 Jul 2025 21:35:30 +0530 Subject: [PATCH 04/13] Improved Code Quality --- client/src/Hooks/monitorHooks.js | 36 +++++++++---------- .../src/Pages/Infrastructure/Create/index.jsx | 27 ++++++++++---- .../Settings/SettingsGlobalThresholds.jsx | 20 ++++++----- server/src/db/models/AppSettings.js | 12 +++---- server/src/validation/joi.js | 15 ++++---- 5 files changed, 63 insertions(+), 47 deletions(-) diff --git a/client/src/Hooks/monitorHooks.js b/client/src/Hooks/monitorHooks.js index 767ef5af8..1d57ffd5f 100644 --- a/client/src/Hooks/monitorHooks.js +++ b/client/src/Hooks/monitorHooks.js @@ -301,29 +301,27 @@ const useCreateMonitor = () => { }; const useFetchGlobalSettings = () => { - const [isLoading, setIsLoading] = useState(true); - const [globalSettings, setGlobalSettings] = useState(null); + const [isLoading, setIsLoading] = useState(true); + const [globalSettings, setGlobalSettings] = useState(null); + useEffect(() => { + const fetchGlobalSettings = async () => { + try { + const res = await networkService.getAppSettings(); + setGlobalSettings(res?.data); + } catch (error) { + console.error("Failed to fetch global settings:", error); + createToast({ body: "Failed to load global settings" }); + } finally { + setIsLoading(false); + } + }; - useEffect(() => { - const fetchGlobalSettings = async () => { - try { - const res = await networkService.getAppSettings(); - setGlobalSettings(res?.data); - } catch (error) { - console.error("Failed to fetch global settings:", error); - } finally { - setIsLoading(false); - } - }; + fetchGlobalSettings(); + }, []); - fetchGlobalSettings(); - }, []); - - return [globalSettings, isLoading]; + return [globalSettings, isLoading]; }; - - const useDeleteMonitor = () => { const [isLoading, setIsLoading] = useState(false); const navigate = useNavigate(); diff --git a/client/src/Pages/Infrastructure/Create/index.jsx b/client/src/Pages/Infrastructure/Create/index.jsx index 151383cfb..3159a3529 100644 --- a/client/src/Pages/Infrastructure/Create/index.jsx +++ b/client/src/Pages/Infrastructure/Create/index.jsx @@ -3,7 +3,6 @@ import { useTheme } from "@emotion/react"; import { useParams } from "react-router-dom"; import { useState, useEffect } from "react"; import { useSelector } from "react-redux"; -import axios from "axios"; // Utility and Network import { infrastructureMonitorValidation } from "../../../Validation/validation"; import { useFetchHardwareMonitorById } from "../../../Hooks/monitorHooks"; @@ -95,25 +94,41 @@ const CreateInfrastructureMonitor = () => { useEffect(() => { if (!isCreate || globalSettingsLoading) return; - const globalThresholds = globalSettings?.data?.settings?.globalThresholds; + const globalThresholds = globalSettings?.data?.settings?.globalThresholds || {}; + + // Define default empty string for all thresholds + const defaultThresholds = { + cpu: "", + memory: "", + disk: "", + temperature: "", + }; setInfrastructureMonitor((prev) => ({ ...prev, cpu: globalThresholds.cpu != null, - usage_cpu: globalThresholds.cpu != null ? globalThresholds.cpu.toString() : "", + usage_cpu: + globalThresholds.cpu != null + ? globalThresholds.cpu.toString() + : defaultThresholds.cpu, memory: globalThresholds.memory != null, usage_memory: - globalThresholds.memory != null ? globalThresholds.memory.toString() : "", + globalThresholds.memory != null + ? globalThresholds.memory.toString() + : defaultThresholds.memory, disk: globalThresholds.disk != null, - usage_disk: globalThresholds.disk != null ? globalThresholds.disk.toString() : "", + usage_disk: + globalThresholds.disk != null + ? globalThresholds.disk.toString() + : defaultThresholds.disk, temperature: globalThresholds.temperature != null, usage_temperature: globalThresholds.temperature != null ? globalThresholds.temperature.toString() - : "", + : defaultThresholds.temperature, })); }, [isCreate, globalSettings]); diff --git a/client/src/Pages/Settings/SettingsGlobalThresholds.jsx b/client/src/Pages/Settings/SettingsGlobalThresholds.jsx index eca752688..cd1c50927 100644 --- a/client/src/Pages/Settings/SettingsGlobalThresholds.jsx +++ b/client/src/Pages/Settings/SettingsGlobalThresholds.jsx @@ -15,8 +15,8 @@ const SettingsGlobalThresholds = ({ settingsData, setSettingsData, }) => { - const { t } = useTranslation(); // For language translation - const theme = useTheme(); // MUI theme access + const { t } = useTranslation(); // For language translation + const theme = useTheme(); // MUI theme access // Local state for thresholds const [thresholds, setThresholds] = useState({ @@ -42,19 +42,18 @@ const SettingsGlobalThresholds = ({ const handleChange = (e, min, max) => { const { name, value } = e.target; - // Allow empty or numeric value within range - if ( + const numValue = parseFloat(value); + const isValidNumber = value === "" || - (/^\d+$/.test(value) && Number(value) >= min && Number(value) <= max) - ) { + (!isNaN(numValue) && isFinite(numValue) && numValue >= min && numValue <= max); + + if (isValidNumber) { const updatedThresholds = { ...thresholds, [name]: value, }; setThresholds(updatedThresholds); - - // Update settingsData without enabling/disabling metrics setSettingsData({ ...settingsData, settings: { @@ -72,7 +71,10 @@ const SettingsGlobalThresholds = ({ {/* Header and description */} - + {t("settingsPage.globalThresholds.title", "Global Thresholds")} diff --git a/server/src/db/models/AppSettings.js b/server/src/db/models/AppSettings.js index be7c856a4..2313d638c 100755 --- a/server/src/db/models/AppSettings.js +++ b/server/src/db/models/AppSettings.js @@ -66,12 +66,12 @@ const AppSettingsSchema = mongoose.Schema( default: 1, }, globalThresholds: { - cpu: { type: Number }, - memory: { type: Number}, - disk: { type: Number}, - temperature: { type: Number}, - } -}, + cpu: { type: Number }, + memory: { type: Number }, + disk: { type: Number }, + temperature: { type: Number }, + }, + }, { timestamps: true, } diff --git a/server/src/validation/joi.js b/server/src/validation/joi.js index 51d8a94a2..f2c15205b 100755 --- a/server/src/validation/joi.js +++ b/server/src/validation/joi.js @@ -417,15 +417,16 @@ const updateAppSettingsBodyValidation = joi.object({ systemEmailRequireTLS: joi.boolean(), systemEmailRejectUnauthorized: joi.boolean(), - globalThresholds: joi.object({ - cpu: joi.number().min(1).max(100).allow("").optional(), - memory: joi.number().min(1).max(100).allow("").optional(), - disk: joi.number().min(1).max(100).allow("").optional(), - temperature: joi.number().min(1).max(150).allow("").optional(), - }).optional(), + globalThresholds: joi + .object({ + cpu: joi.number().min(1).max(100).allow("").optional(), + memory: joi.number().min(1).max(100).allow("").optional(), + disk: joi.number().min(1).max(100).allow("").optional(), + temperature: joi.number().min(1).max(150).allow("").optional(), + }) + .optional(), }); - //**************************************** // Status Page Validation //**************************************** From af010e138d93fd023faf57ca18583f1050892d93 Mon Sep 17 00:00:00 2001 From: singh-kanwarpreet Date: Wed, 30 Jul 2025 00:01:27 +0530 Subject: [PATCH 05/13] Some changes in ui --- .../Settings/SettingsGlobalThresholds.jsx | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/client/src/Pages/Settings/SettingsGlobalThresholds.jsx b/client/src/Pages/Settings/SettingsGlobalThresholds.jsx index cd1c50927..96f14e62d 100644 --- a/client/src/Pages/Settings/SettingsGlobalThresholds.jsx +++ b/client/src/Pages/Settings/SettingsGlobalThresholds.jsx @@ -26,17 +26,16 @@ const SettingsGlobalThresholds = ({ temperature: "", }); - // Load existing thresholds from settingsData on first render or when settingsData changes - useEffect(() => { - if (settingsData?.settings?.globalThresholds) { - setThresholds({ - cpu: settingsData.settings.globalThresholds.cpu || "", - memory: settingsData.settings.globalThresholds.memory || "", - disk: settingsData.settings.globalThresholds.disk || "", - temperature: settingsData.settings.globalThresholds.temperature || "", - }); - } - }, [settingsData]); + // Load existing thresholds from settingsData + + if (settingsData?.settings?.globalThresholds) { + setThresholds({ + cpu: settingsData.settings.globalThresholds.cpu || "", + memory: settingsData.settings.globalThresholds.memory || "", + disk: settingsData.settings.globalThresholds.disk || "", + temperature: settingsData.settings.globalThresholds.temperature || "", + }); + } // Handles input change and updates state & parent data const handleChange = (e, min, max) => { From da7d9c02c870856bf721a99d95df91493b189278 Mon Sep 17 00:00:00 2001 From: singh-kanwarpreet Date: Wed, 30 Jul 2025 00:25:53 +0530 Subject: [PATCH 06/13] Used Parent State And Set Minor Changes --- client/src/Hooks/monitorHooks.js | 2 +- .../Settings/SettingsGlobalThresholds.jsx | 43 +++++-------------- 2 files changed, 11 insertions(+), 34 deletions(-) diff --git a/client/src/Hooks/monitorHooks.js b/client/src/Hooks/monitorHooks.js index 1d57ffd5f..616a37877 100644 --- a/client/src/Hooks/monitorHooks.js +++ b/client/src/Hooks/monitorHooks.js @@ -302,7 +302,7 @@ const useCreateMonitor = () => { const useFetchGlobalSettings = () => { const [isLoading, setIsLoading] = useState(true); - const [globalSettings, setGlobalSettings] = useState(null); + const [globalSettings, setGlobalSettings] = useState(undefined); useEffect(() => { const fetchGlobalSettings = async () => { try { diff --git a/client/src/Pages/Settings/SettingsGlobalThresholds.jsx b/client/src/Pages/Settings/SettingsGlobalThresholds.jsx index 96f14e62d..b4d2581d2 100644 --- a/client/src/Pages/Settings/SettingsGlobalThresholds.jsx +++ b/client/src/Pages/Settings/SettingsGlobalThresholds.jsx @@ -6,7 +6,6 @@ import TextInput from "../../Components/Inputs/TextInput"; import { useTheme } from "@emotion/react"; import { PropTypes } from "prop-types"; -import { useState, useEffect } from "react"; import { useTranslation } from "react-i18next"; const SettingsGlobalThresholds = ({ @@ -18,26 +17,7 @@ const SettingsGlobalThresholds = ({ const { t } = useTranslation(); // For language translation const theme = useTheme(); // MUI theme access - // Local state for thresholds - const [thresholds, setThresholds] = useState({ - cpu: "", - memory: "", - disk: "", - temperature: "", - }); - - // Load existing thresholds from settingsData - - if (settingsData?.settings?.globalThresholds) { - setThresholds({ - cpu: settingsData.settings.globalThresholds.cpu || "", - memory: settingsData.settings.globalThresholds.memory || "", - disk: settingsData.settings.globalThresholds.disk || "", - temperature: settingsData.settings.globalThresholds.temperature || "", - }); - } - - // Handles input change and updates state & parent data + // Handles input change and updates parent state const handleChange = (e, min, max) => { const { name, value } = e.target; @@ -47,19 +27,16 @@ const SettingsGlobalThresholds = ({ (!isNaN(numValue) && isFinite(numValue) && numValue >= min && numValue <= max); if (isValidNumber) { - const updatedThresholds = { - ...thresholds, - [name]: value, - }; - - setThresholds(updatedThresholds); - setSettingsData({ - ...settingsData, + setSettingsData((prev) => ({ + ...prev, settings: { - ...settingsData.settings, - globalThresholds: updatedThresholds, + ...prev.settings, + globalThresholds: { + ...prev.settings?.globalThresholds, + [name]: value, + }, }, - }); + })); } }; @@ -98,7 +75,7 @@ const SettingsGlobalThresholds = ({ label={label} placeholder={`${min} - ${max}`} type="number" - value={thresholds[name]} + value={settingsData?.settings?.globalThresholds?.[name] || ""} onChange={(e) => handleChange(e, min, max)} /> ))} From e04ac77ddd11bf53fb456ddd525ec2fc183fc0be Mon Sep 17 00:00:00 2001 From: singh-kanwarpreet Date: Wed, 30 Jul 2025 16:39:41 +0530 Subject: [PATCH 07/13] Ran npm start format --- .../Settings/SettingsGlobalThresholds.jsx | 190 +++++++++--------- client/src/Pages/Settings/index.jsx | 5 +- client/src/Validation/validation.js | 15 +- client/src/locales/en.json | 4 +- 4 files changed, 107 insertions(+), 107 deletions(-) diff --git a/client/src/Pages/Settings/SettingsGlobalThresholds.jsx b/client/src/Pages/Settings/SettingsGlobalThresholds.jsx index b4d2581d2..acc101ab4 100644 --- a/client/src/Pages/Settings/SettingsGlobalThresholds.jsx +++ b/client/src/Pages/Settings/SettingsGlobalThresholds.jsx @@ -1,95 +1,95 @@ -import Box from "@mui/material/Box"; -import Stack from "@mui/material/Stack"; -import Typography from "@mui/material/Typography"; -import ConfigBox from "../../Components/ConfigBox"; -import TextInput from "../../Components/Inputs/TextInput"; - -import { useTheme } from "@emotion/react"; -import { PropTypes } from "prop-types"; -import { useTranslation } from "react-i18next"; - -const SettingsGlobalThresholds = ({ - isAdmin, - HEADING_SX, - settingsData, - setSettingsData, -}) => { - const { t } = useTranslation(); // For language translation - const theme = useTheme(); // MUI theme access - - // Handles input change and updates parent state - const handleChange = (e, min, max) => { - const { name, value } = e.target; - - const numValue = parseFloat(value); - const isValidNumber = - value === "" || - (!isNaN(numValue) && isFinite(numValue) && numValue >= min && numValue <= max); - - if (isValidNumber) { - setSettingsData((prev) => ({ - ...prev, - settings: { - ...prev.settings, - globalThresholds: { - ...prev.settings?.globalThresholds, - [name]: value, - }, - }, - })); - } - }; - - // Only render this section for admins - if (!isAdmin) return null; - - return ( - - {/* Header and description */} - - - {t("settingsPage.globalThresholds.title", "Global Thresholds")} - - - {t( - "settingsPage.globalThresholds.description", - "Configure global CPU, Memory, Disk, and Temperature thresholds." - )} - - - - {/* Threshold inputs */} - - {[ - ["CPU Threshold (%)", "cpu", 1, 100], - ["Memory Threshold (%)", "memory", 1, 100], - ["Disk Threshold (%)", "disk", 1, 100], - ["Temperature Threshold (°C)", "temperature", 1, 150], - ].map(([label, name, min, max]) => ( - handleChange(e, min, max)} - /> - ))} - - - ); -}; - -// Prop types -SettingsGlobalThresholds.propTypes = { - isAdmin: PropTypes.bool, - HEADING_SX: PropTypes.object, - settingsData: PropTypes.object, - setSettingsData: PropTypes.func, -}; - -export default SettingsGlobalThresholds; +import Box from "@mui/material/Box"; +import Stack from "@mui/material/Stack"; +import Typography from "@mui/material/Typography"; +import ConfigBox from "../../Components/ConfigBox"; +import TextInput from "../../Components/Inputs/TextInput"; + +import { useTheme } from "@emotion/react"; +import { PropTypes } from "prop-types"; +import { useTranslation } from "react-i18next"; + +const SettingsGlobalThresholds = ({ + isAdmin, + HEADING_SX, + settingsData, + setSettingsData, +}) => { + const { t } = useTranslation(); // For language translation + const theme = useTheme(); // MUI theme access + + // Handles input change and updates parent state + const handleChange = (e, min, max) => { + const { name, value } = e.target; + + const numValue = parseFloat(value); + const isValidNumber = + value === "" || + (!isNaN(numValue) && isFinite(numValue) && numValue >= min && numValue <= max); + + if (isValidNumber) { + setSettingsData((prev) => ({ + ...prev, + settings: { + ...prev.settings, + globalThresholds: { + ...prev.settings?.globalThresholds, + [name]: value, + }, + }, + })); + } + }; + + // Only render this section for admins + if (!isAdmin) return null; + + return ( + + {/* Header and description */} + + + {t("settingsPage.globalThresholds.title", "Global Thresholds")} + + + {t( + "settingsPage.globalThresholds.description", + "Configure global CPU, Memory, Disk, and Temperature thresholds." + )} + + + + {/* Threshold inputs */} + + {[ + ["CPU Threshold (%)", "cpu", 1, 100], + ["Memory Threshold (%)", "memory", 1, 100], + ["Disk Threshold (%)", "disk", 1, 100], + ["Temperature Threshold (°C)", "temperature", 1, 150], + ].map(([label, name, min, max]) => ( + handleChange(e, min, max)} + /> + ))} + + + ); +}; + +// Prop types +SettingsGlobalThresholds.propTypes = { + isAdmin: PropTypes.bool, + HEADING_SX: PropTypes.object, + settingsData: PropTypes.object, + setSettingsData: PropTypes.func, +}; + +export default SettingsGlobalThresholds; diff --git a/client/src/Pages/Settings/index.jsx b/client/src/Pages/Settings/index.jsx index ab529eb68..92f58b4c0 100644 --- a/client/src/Pages/Settings/index.jsx +++ b/client/src/Pages/Settings/index.jsx @@ -49,7 +49,7 @@ const Settings = () => { setIsApiKeySet, setIsEmailPasswordSet, }); - + const [addDemoMonitors, isAddingDemoMonitors] = useAddDemoMonitors(); const [isSaving, saveError, saveSettings] = useSaveSettings({ @@ -62,7 +62,6 @@ const Settings = () => { const [deleteAllMonitors, isDeletingMonitors] = useDeleteAllMonitors(); const [deleteMonitorStats, isDeletingMonitorStats] = useDeleteMonitorStats(); - // Setup const isAdmin = useIsAdmin(); const theme = useTheme(); @@ -152,7 +151,7 @@ const Settings = () => { error.details.forEach((err) => { newErrors[err.path[0]] = err.message; }); - + setErrors(newErrors); } saveSettings(settingsData?.settings); diff --git a/client/src/Validation/validation.js b/client/src/Validation/validation.js index 22ea042d7..9c6699d4b 100644 --- a/client/src/Validation/validation.js +++ b/client/src/Validation/validation.js @@ -302,15 +302,16 @@ const settingsValidation = joi.object({ systemEmailIgnoreTLS: joi.boolean(), systemEmailRequireTLS: joi.boolean(), systemEmailRejectUnauthorized: joi.boolean(), - globalThresholds: joi.object({ - cpu: joi.number().min(1).max(100).allow("").optional(), - memory: joi.number().min(1).max(100).allow("").optional(), - disk: joi.number().min(1).max(100).allow("").optional(), - temperature: joi.number().min(1).max(150).allow("").optional(), - }).optional(), + globalThresholds: joi + .object({ + cpu: joi.number().min(1).max(100).allow("").optional(), + memory: joi.number().min(1).max(100).allow("").optional(), + disk: joi.number().min(1).max(100).allow("").optional(), + temperature: joi.number().min(1).max(150).allow("").optional(), + }) + .optional(), }); - const dayjsValidator = (value, helpers) => { if (!dayjs(value).isValid()) { return helpers.error("any.invalid"); diff --git a/client/src/locales/en.json b/client/src/locales/en.json index 5c69ecd81..86362d3c7 100644 --- a/client/src/locales/en.json +++ b/client/src/locales/en.json @@ -838,8 +838,8 @@ }, "title": "Settings", "globalThresholds": { - "title": "Global Thresholds", - "description": "Configure global CPU, Memory, Disk, and Temperature thresholds. If a value is provided, it will automatically be enabled for monitoring." + "title": "Global Thresholds", + "description": "Configure global CPU, Memory, Disk, and Temperature thresholds. If a value is provided, it will automatically be enabled for monitoring." }, "uiSettings": { "description": "Switch between light and dark mode, or change user interface language.", From d4ecb26ffe8b2de2f77f7e099a8379b4cf512266 Mon Sep 17 00:00:00 2001 From: singh-kanwarpreet Date: Wed, 30 Jul 2025 18:30:04 +0530 Subject: [PATCH 08/13] Updated UseEffect --- client/src/Pages/Infrastructure/Create/index.jsx | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/client/src/Pages/Infrastructure/Create/index.jsx b/client/src/Pages/Infrastructure/Create/index.jsx index 3159a3529..9b8f4f767 100644 --- a/client/src/Pages/Infrastructure/Create/index.jsx +++ b/client/src/Pages/Infrastructure/Create/index.jsx @@ -93,8 +93,8 @@ const CreateInfrastructureMonitor = () => { // Populate form fields if editing useEffect(() => { - if (!isCreate || globalSettingsLoading) return; - const globalThresholds = globalSettings?.data?.settings?.globalThresholds || {}; + if (monitor == undefined && (!isCreate || globalSettingsLoading)) return; + const globalThresholds = globalSettings?.data?.settings?.globalThresholds; // Define default empty string for all thresholds const defaultThresholds = { @@ -106,6 +106,10 @@ const CreateInfrastructureMonitor = () => { setInfrastructureMonitor((prev) => ({ ...prev, + url: monitor != undefined ? monitor.url.replace(/^https?:\/\//, "") : "", + name: monitor != undefined ? monitor.name || "" : "", + notifications: monitor != undefined ? monitor.notifications : [], + interval: monitor != undefined ? monitor.interval / MS_PER_MINUTE : 0.25, cpu: globalThresholds.cpu != null, usage_cpu: globalThresholds.cpu != null @@ -130,7 +134,7 @@ const CreateInfrastructureMonitor = () => { ? globalThresholds.temperature.toString() : defaultThresholds.temperature, })); - }, [isCreate, globalSettings]); + }, [isCreate, globalSettings, monitor, globalSettingsLoading]); // Handlers const onSubmit = async (event) => { From 0ecbf48f5d78f1c0704149bbd1bc94cc425b6dac Mon Sep 17 00:00:00 2001 From: singh-kanwarpreet Date: Wed, 30 Jul 2025 19:18:24 +0530 Subject: [PATCH 09/13] client/src/Pages/Infrastructure/Create/index.jsx --- .../src/Pages/Infrastructure/Create/index.jsx | 73 ++++++++++--------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/client/src/Pages/Infrastructure/Create/index.jsx b/client/src/Pages/Infrastructure/Create/index.jsx index 9b8f4f767..be9cb6062 100644 --- a/client/src/Pages/Infrastructure/Create/index.jsx +++ b/client/src/Pages/Infrastructure/Create/index.jsx @@ -93,10 +93,7 @@ const CreateInfrastructureMonitor = () => { // Populate form fields if editing useEffect(() => { - if (monitor == undefined && (!isCreate || globalSettingsLoading)) return; const globalThresholds = globalSettings?.data?.settings?.globalThresholds; - - // Define default empty string for all thresholds const defaultThresholds = { cpu: "", memory: "", @@ -104,37 +101,45 @@ const CreateInfrastructureMonitor = () => { temperature: "", }; - setInfrastructureMonitor((prev) => ({ - ...prev, - url: monitor != undefined ? monitor.url.replace(/^https?:\/\//, "") : "", - name: monitor != undefined ? monitor.name || "" : "", - notifications: monitor != undefined ? monitor.notifications : [], - interval: monitor != undefined ? monitor.interval / MS_PER_MINUTE : 0.25, - cpu: globalThresholds.cpu != null, - usage_cpu: - globalThresholds.cpu != null - ? globalThresholds.cpu.toString() - : defaultThresholds.cpu, - - memory: globalThresholds.memory != null, - usage_memory: - globalThresholds.memory != null - ? globalThresholds.memory.toString() - : defaultThresholds.memory, - - disk: globalThresholds.disk != null, - usage_disk: - globalThresholds.disk != null - ? globalThresholds.disk.toString() - : defaultThresholds.disk, - - temperature: globalThresholds.temperature != null, - usage_temperature: - globalThresholds.temperature != null - ? globalThresholds.temperature.toString() - : defaultThresholds.temperature, - })); - }, [isCreate, globalSettings, monitor, globalSettingsLoading]); + if (isCreate) { + // If global settings are not loaded yet, use default thresholds + if (globalSettingsLoading) return; + // Create mode: use global thresholds + setInfrastructureMonitor((prev) => ({ + ...prev, + url: "", + name: "", + notifications: [], + interval: 0.25, + cpu: globalThresholds?.cpu != null, + usage_cpu: globalThresholds?.cpu?.toString() || defaultThresholds.cpu, + memory: globalThresholds?.memory != null, + usage_memory: globalThresholds?.memory?.toString() || defaultThresholds.memory, + disk: globalThresholds?.disk != null, + usage_disk: globalThresholds?.disk?.toString() || defaultThresholds.disk, + temperature: globalThresholds?.temperature != null, + usage_temperature: + globalThresholds?.temperature?.toString() || defaultThresholds.temperature, + })); + } else if (monitor) { + // Edit mode: use monitor only, ignore global thresholds + setInfrastructureMonitor((prev) => ({ + ...prev, + url: monitor.url?.replace(/^https?:\/\//, "") || "", + name: monitor.name || "", + notifications: monitor.notifications || [], + interval: monitor.interval / MS_PER_MINUTE, + cpu: !!monitor.thresholds?.usage_cpu ?? false, + usage_cpu: monitor.thresholds?.usage_cpu * 100?.toString() || "", + memory: !!monitor.thresholds?.usage_memory ?? false, + usage_memory: monitor.thresholds?.usage_memory * 100?.toString() || "", + disk: !!monitor.thresholds?.usage_disk ?? false, + usage_disk: monitor.thresholds?.usage_disk * 100?.toString() || "", + temperature: !!monitor.thresholds?.usage_temperature ?? false, + usage_temperature: monitor.thresholds?.usage_temperature * 100?.toString() || "", + })); + } + }, [isCreate, monitor, globalSettings, globalSettingsLoading]); // Handlers const onSubmit = async (event) => { From 5e46a9f2d51e4b93bf0e56b1dee5cceb72df3752 Mon Sep 17 00:00:00 2001 From: singh-kanwarpreet Date: Wed, 30 Jul 2025 19:38:23 +0530 Subject: [PATCH 10/13] Pushed global thresholds inside create if branch --- client/src/Pages/Infrastructure/Create/index.jsx | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/client/src/Pages/Infrastructure/Create/index.jsx b/client/src/Pages/Infrastructure/Create/index.jsx index be9cb6062..73a11c40f 100644 --- a/client/src/Pages/Infrastructure/Create/index.jsx +++ b/client/src/Pages/Infrastructure/Create/index.jsx @@ -93,18 +93,18 @@ const CreateInfrastructureMonitor = () => { // Populate form fields if editing useEffect(() => { - const globalThresholds = globalSettings?.data?.settings?.globalThresholds; - const defaultThresholds = { - cpu: "", - memory: "", - disk: "", - temperature: "", - }; - if (isCreate) { // If global settings are not loaded yet, use default thresholds if (globalSettingsLoading) return; // Create mode: use global thresholds + const globalThresholds = globalSettings?.data?.settings?.globalThresholds; + const defaultThresholds = { + cpu: "", + memory: "", + disk: "", + temperature: "", + }; + setInfrastructureMonitor((prev) => ({ ...prev, url: "", From 2d6fdcb1e2cced589f62ebd96e9fdba3331cf131 Mon Sep 17 00:00:00 2001 From: singh-kanwarpreet Date: Wed, 30 Jul 2025 23:14:35 +0530 Subject: [PATCH 11/13] Made code readable --- .../src/Pages/Infrastructure/Create/index.jsx | 61 +++++++++++++------ 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/client/src/Pages/Infrastructure/Create/index.jsx b/client/src/Pages/Infrastructure/Create/index.jsx index 73a11c40f..d9817aae3 100644 --- a/client/src/Pages/Infrastructure/Create/index.jsx +++ b/client/src/Pages/Infrastructure/Create/index.jsx @@ -96,8 +96,9 @@ const CreateInfrastructureMonitor = () => { if (isCreate) { // If global settings are not loaded yet, use default thresholds if (globalSettingsLoading) return; + // Create mode: use global thresholds - const globalThresholds = globalSettings?.data?.settings?.globalThresholds; + const globalThresholds = globalSettings?.data?.settings?.globalThresholds || {}; const defaultThresholds = { cpu: "", memory: "", @@ -111,15 +112,26 @@ const CreateInfrastructureMonitor = () => { name: "", notifications: [], interval: 0.25, - cpu: globalThresholds?.cpu != null, - usage_cpu: globalThresholds?.cpu?.toString() || defaultThresholds.cpu, - memory: globalThresholds?.memory != null, - usage_memory: globalThresholds?.memory?.toString() || defaultThresholds.memory, - disk: globalThresholds?.disk != null, - usage_disk: globalThresholds?.disk?.toString() || defaultThresholds.disk, - temperature: globalThresholds?.temperature != null, + cpu: globalThresholds.cpu != null, + usage_cpu: + globalThresholds.cpu != null + ? globalThresholds.cpu.toString() + : defaultThresholds.cpu, + memory: globalThresholds.memory != null, + usage_memory: + globalThresholds.memory != null + ? globalThresholds.memory.toString() + : defaultThresholds.memory, + disk: globalThresholds.disk != null, + usage_disk: + globalThresholds.disk != null + ? globalThresholds.disk.toString() + : defaultThresholds.disk, + temperature: globalThresholds.temperature != null, usage_temperature: - globalThresholds?.temperature?.toString() || defaultThresholds.temperature, + globalThresholds.temperature != null + ? globalThresholds.temperature.toString() + : defaultThresholds.temperature, })); } else if (monitor) { // Edit mode: use monitor only, ignore global thresholds @@ -129,15 +141,30 @@ const CreateInfrastructureMonitor = () => { name: monitor.name || "", notifications: monitor.notifications || [], interval: monitor.interval / MS_PER_MINUTE, - cpu: !!monitor.thresholds?.usage_cpu ?? false, - usage_cpu: monitor.thresholds?.usage_cpu * 100?.toString() || "", - memory: !!monitor.thresholds?.usage_memory ?? false, - usage_memory: monitor.thresholds?.usage_memory * 100?.toString() || "", - disk: !!monitor.thresholds?.usage_disk ?? false, - usage_disk: monitor.thresholds?.usage_disk * 100?.toString() || "", - temperature: !!monitor.thresholds?.usage_temperature ?? false, - usage_temperature: monitor.thresholds?.usage_temperature * 100?.toString() || "", + cpu: monitor.thresholds?.usage_cpu != null, + usage_cpu: + monitor.thresholds?.usage_cpu != null + ? (monitor.thresholds.usage_cpu * 100).toString() + : "", + memory: monitor.thresholds?.usage_memory != null, + usage_memory: + monitor.thresholds?.usage_memory != null + ? (monitor.thresholds.usage_memory * 100).toString() + : "", + disk: monitor.thresholds?.usage_disk != null, + usage_disk: + monitor.thresholds?.usage_disk != null + ? (monitor.thresholds.usage_disk * 100).toString() + : "", + temperature: monitor.thresholds?.usage_temperature != null, + usage_temperature: + monitor.thresholds?.usage_temperature != null + ? (monitor.thresholds.usage_temperature * 100).toString() + : "", + secret: monitor.secret || "", })); + + setHttps(monitor.url?.startsWith("https")); } }, [isCreate, monitor, globalSettings, globalSettingsLoading]); From 8bacbcde893a3049f47d5ac4e7a0a2a74bd18f67 Mon Sep 17 00:00:00 2001 From: singh-kanwarpreet Date: Wed, 30 Jul 2025 23:35:51 +0530 Subject: [PATCH 12/13] Removed Unnecassary command --- client/src/Pages/Infrastructure/Create/index.jsx | 1 - 1 file changed, 1 deletion(-) diff --git a/client/src/Pages/Infrastructure/Create/index.jsx b/client/src/Pages/Infrastructure/Create/index.jsx index d9817aae3..f2833d52f 100644 --- a/client/src/Pages/Infrastructure/Create/index.jsx +++ b/client/src/Pages/Infrastructure/Create/index.jsx @@ -164,7 +164,6 @@ const CreateInfrastructureMonitor = () => { secret: monitor.secret || "", })); - setHttps(monitor.url?.startsWith("https")); } }, [isCreate, monitor, globalSettings, globalSettingsLoading]); From 73a6277feb1e24e35918f9c4bcbafaf1dc3c4a50 Mon Sep 17 00:00:00 2001 From: singh-kanwarpreet Date: Thu, 31 Jul 2025 16:04:41 +0530 Subject: [PATCH 13/13] Refactored Code --- .../src/Pages/Infrastructure/Create/index.jsx | 83 ++++++++----------- 1 file changed, 33 insertions(+), 50 deletions(-) diff --git a/client/src/Pages/Infrastructure/Create/index.jsx b/client/src/Pages/Infrastructure/Create/index.jsx index f2833d52f..b03342ef0 100644 --- a/client/src/Pages/Infrastructure/Create/index.jsx +++ b/client/src/Pages/Infrastructure/Create/index.jsx @@ -94,76 +94,59 @@ const CreateInfrastructureMonitor = () => { useEffect(() => { if (isCreate) { - // If global settings are not loaded yet, use default thresholds if (globalSettingsLoading) return; - // Create mode: use global thresholds - const globalThresholds = globalSettings?.data?.settings?.globalThresholds || {}; - const defaultThresholds = { - cpu: "", - memory: "", - disk: "", - temperature: "", - }; + const gt = globalSettings?.data?.settings?.globalThresholds || {}; - setInfrastructureMonitor((prev) => ({ - ...prev, + setHttps(false); + + setInfrastructureMonitor({ url: "", name: "", notifications: [], interval: 0.25, - cpu: globalThresholds.cpu != null, - usage_cpu: - globalThresholds.cpu != null - ? globalThresholds.cpu.toString() - : defaultThresholds.cpu, - memory: globalThresholds.memory != null, - usage_memory: - globalThresholds.memory != null - ? globalThresholds.memory.toString() - : defaultThresholds.memory, - disk: globalThresholds.disk != null, - usage_disk: - globalThresholds.disk != null - ? globalThresholds.disk.toString() - : defaultThresholds.disk, - temperature: globalThresholds.temperature != null, - usage_temperature: - globalThresholds.temperature != null - ? globalThresholds.temperature.toString() - : defaultThresholds.temperature, - })); + cpu: gt.cpu !== undefined, + usage_cpu: gt.cpu !== undefined ? gt.cpu.toString() : "", + memory: gt.memory !== undefined, + usage_memory: gt.memory !== undefined ? gt.memory.toString() : "", + disk: gt.disk !== undefined, + usage_disk: gt.disk !== undefined ? gt.disk.toString() : "", + temperature: gt.temperature !== undefined, + usage_temperature: gt.temperature !== undefined ? gt.temperature.toString() : "", + secret: "", + }); } else if (monitor) { - // Edit mode: use monitor only, ignore global thresholds - setInfrastructureMonitor((prev) => ({ - ...prev, - url: monitor.url?.replace(/^https?:\/\//, "") || "", + const { thresholds = {} } = monitor; + + setHttps(monitor.url.startsWith("https")); + + setInfrastructureMonitor({ + url: monitor.url.replace(/^https?:\/\//, ""), name: monitor.name || "", notifications: monitor.notifications || [], interval: monitor.interval / MS_PER_MINUTE, - cpu: monitor.thresholds?.usage_cpu != null, + cpu: thresholds.usage_cpu !== undefined, usage_cpu: - monitor.thresholds?.usage_cpu != null - ? (monitor.thresholds.usage_cpu * 100).toString() + thresholds.usage_cpu !== undefined + ? (thresholds.usage_cpu * 100).toString() : "", - memory: monitor.thresholds?.usage_memory != null, + memory: thresholds.usage_memory !== undefined, usage_memory: - monitor.thresholds?.usage_memory != null - ? (monitor.thresholds.usage_memory * 100).toString() + thresholds.usage_memory !== undefined + ? (thresholds.usage_memory * 100).toString() : "", - disk: monitor.thresholds?.usage_disk != null, + disk: thresholds.usage_disk !== undefined, usage_disk: - monitor.thresholds?.usage_disk != null - ? (monitor.thresholds.usage_disk * 100).toString() + thresholds.usage_disk !== undefined + ? (thresholds.usage_disk * 100).toString() : "", - temperature: monitor.thresholds?.usage_temperature != null, + temperature: thresholds.usage_temperature !== undefined, usage_temperature: - monitor.thresholds?.usage_temperature != null - ? (monitor.thresholds.usage_temperature * 100).toString() + thresholds.usage_temperature !== undefined + ? (thresholds.usage_temperature * 100).toString() : "", secret: monitor.secret || "", - })); - + }); } }, [isCreate, monitor, globalSettings, globalSettingsLoading]);