Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion client/src/Hooks/monitorHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ const useDeleteMonitor = () => {

const useUpdateMonitor = () => {
const [isLoading, setIsLoading] = useState(false);

const navigate = useNavigate();
const updateMonitor = async ({ monitor, redirect }) => {
try {
Expand All @@ -378,10 +379,11 @@ const useUpdateMonitor = () => {
expectedValue: monitor.expectedValue,
ignoreTlsErrors: monitor.ignoreTlsErrors,
jsonPath: monitor.jsonPath,
url: monitor.url,
...((monitor.type === "port" || monitor.type === "game") && {
port: monitor.port,
}),
...(monitor.type == "game" && {
...(monitor.type === "game" && {
gameId: monitor.gameId,
}),
...(monitor.type === "hardware" && {
Expand Down
92 changes: 68 additions & 24 deletions client/src/Pages/Infrastructure/Create/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ const CreateInfrastructureMonitor = () => {
{ name: "Configure", path: `/infrastructure/configure/${monitorId}` },
]),
];
const METRICS = ["cpu", "memory", "disk", "temperature"];
const METRIC_PREFIX = "usage_";
const MS_PER_MINUTE = 60000;

const hasAlertError = (errors) => {
return Object.keys(errors).filter((k) => k.startsWith(METRIC_PREFIX)).length > 0;
};

const getAlertError = (errors) => {
const errorKey = Object.keys(errors).find((key) => key.startsWith(METRIC_PREFIX));
return errorKey ? errors[errorKey] : null;
};

const pageSchema = infrastructureMonitorValidation.fork(["url"], (s) =>
isCreate ? s.required() : s.optional()
);
// Populate form fields if editing
useEffect(() => {
if (isCreate) {
Expand All @@ -96,6 +112,35 @@ const CreateInfrastructureMonitor = () => {
if (error) {
return;
}
// Build the thresholds for the form
const {
cpu,
usage_cpu,
memory,
usage_memory,
disk,
usage_disk,
temperature,
usage_temperature,
...rest
} = form;

const thresholds = {
...(cpu ? { usage_cpu: usage_cpu / 100 } : {}),
...(memory ? { usage_memory: usage_memory / 100 } : {}),
...(disk ? { usage_disk: usage_disk / 100 } : {}),
...(temperature ? { usage_temperature: usage_temperature / 100 } : {}),
};

form = {
...(isCreate ? {} : { _id: monitorId }),
...rest,
url: `http${https ? "s" : ""}://` + infrastructureMonitor.url,
description: form.name,
type: "hardware",
notifications: infrastructureMonitor.notifications,
thresholds,
};
submitInfrastructureForm(infrastructureMonitor, form, isCreate, monitorId);
Comment on lines +128 to 144
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix: const reassignment of form (compile error).

You reassign form after declaring it as const. Let's not trigger a cross-border incident between const and let—just build a separate payload.

-    form = {
+    const payload = {
       ...(isCreate ? {} : { _id: monitorId }),
       ...rest,
       url: `http${https ? "s" : ""}://` + infrastructureMonitor.url,
       description: form.name,
       type: "hardware",
       notifications: infrastructureMonitor.notifications,
       thresholds,
     };
-    submitInfrastructureForm(infrastructureMonitor, form, isCreate, monitorId);
+    submitInfrastructureForm(infrastructureMonitor, payload, isCreate, monitorId);

Optional: to harden threshold math, coerce numbers and bound to [0, 1] when the toggle is on (prevents accidental NaN/negatives sneaking in).

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const thresholds = {
...(cpu ? { usage_cpu: usage_cpu / 100 } : {}),
...(memory ? { usage_memory: usage_memory / 100 } : {}),
...(disk ? { usage_disk: usage_disk / 100 } : {}),
...(temperature ? { usage_temperature: usage_temperature / 100 } : {}),
};
form = {
...(isCreate ? {} : { _id: monitorId }),
...rest,
url: `http${https ? "s" : ""}://` + infrastructureMonitor.url,
description: form.name,
type: "hardware",
notifications: infrastructureMonitor.notifications,
thresholds,
};
submitInfrastructureForm(infrastructureMonitor, form, isCreate, monitorId);
const thresholds = {
...(cpu ? { usage_cpu: usage_cpu / 100 } : {}),
...(memory ? { usage_memory: usage_memory / 100 } : {}),
...(disk ? { usage_disk: usage_disk / 100 } : {}),
...(temperature ? { usage_temperature: usage_temperature / 100 } : {}),
};
const payload = {
...(isCreate ? {} : { _id: monitorId }),
...rest,
url: `http${https ? "s" : ""}://` + infrastructureMonitor.url,
description: form.name,
type: "hardware",
notifications: infrastructureMonitor.notifications,
thresholds,
};
submitInfrastructureForm(infrastructureMonitor, payload, isCreate, monitorId);
🧰 Tools
🪛 Biome (2.1.2)

[error] 135-135: Can't assign form because it's a constant.

This is where the variable is defined as constant.

Unsafe fix: Replace const with let if you assign it to a new value.

(lint/correctness/noConstAssign)

🤖 Prompt for AI Agents
In client/src/Pages/Infrastructure/Create/index.jsx around lines 128 to 144, you
are reassigning the const variable form which causes a compile error; instead,
build a new payload object (e.g., payload or formPayload) that spreads the
existing rest values and adds the computed fields (_id when !isCreate, url,
description, type, notifications, thresholds) and pass that payload to
submitInfrastructureForm; also compute thresholds into a separate object using
Number(...) and clamp each value to the [0,1] range when the corresponding
toggle is true to avoid NaN/negatives.

};

Expand Down Expand Up @@ -217,31 +262,30 @@ const CreateInfrastructureMonitor = () => {
onChange={onChange}
error={errors["url"] ? true : false}
helperText={errors["url"]}
disabled={!isCreate}
/>
{isCreate && (
<FieldWrapper
label={t("infrastructureProtocol")}
labelVariant="p"
>
<ButtonGroup>
<Button
variant="group"
filled={https.toString()}
onClick={() => setHttps(true)}
>
{t("https")}
</Button>
<Button
variant="group"
filled={(!https).toString()}
onClick={() => setHttps(false)}
>
{t("http")}
</Button>
</ButtonGroup>
</FieldWrapper>
)}

<FieldWrapper
label={t("infrastructureProtocol")}
labelVariant="p"
>
<ButtonGroup>
<Button
variant="group"
filled={https.toString()}
onClick={() => setHttps(true)}
>
{t("https")}
</Button>
<Button
variant="group"
filled={(!https).toString()}
onClick={() => setHttps(false)}
>
{t("http")}
</Button>
</ButtonGroup>
</FieldWrapper>

<TextInput
type="text"
id="name"
Expand Down
10 changes: 9 additions & 1 deletion server/src/validation/joi.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,13 @@ const createMonitorBodyValidation = joi.object({
name: joi.string().required(),
description: joi.string().required(),
type: joi.string().required(),
url: joi
.string()
.trim()
.uri({ scheme: ["http", "https"] })
.required(),
statusWindowSize: joi.number().min(1).max(20).default(5),
statusWindowThreshold: joi.number().min(1).max(100).default(60),
url: joi.string().required(),
ignoreTlsErrors: joi.boolean().default(false),
port: joi.number(),
isActive: joi.boolean(),
Expand All @@ -184,6 +188,10 @@ const createMonitorsBodyValidation = joi.array().items(
);

const editMonitorBodyValidation = joi.object({
url: joi
.string()
.uri({ scheme: ["http", "https"] })
.optional(),
name: joi.string(),
statusWindowSize: joi.number().min(1).max(20).default(5),
statusWindowThreshold: joi.number().min(1).max(100).default(60),
Expand Down