From dbea93f9babf46562921c0b0471aaf203c86b05b Mon Sep 17 00:00:00 2001 From: singh-kanwarpreet Date: Tue, 29 Jul 2025 21:35:30 +0530 Subject: [PATCH] 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 //****************************************