- code refine

This commit is contained in:
Shemy Gan committed 2024-11-08 15:23:33 -05:00
1 parent b35fe8d616
commit 2aa792d820
1 file changed
+23 -19
@@ -26,10 +26,15 @@ const CreateInfrastructureMonitor = () => {
interval: 15, interval: 15,
cpu: false, cpu: false,
usage_cpu: "", usage_cpu: "",
memory: false,
usage_memory: "",
disk: false,
usage_disk: "",
secret: "", secret: "",
}); });
const MS_PER_MINUTE = 60000; const MS_PER_MINUTE = 60000;
const THRESHOLD_FIELD_PREFIX = "usage_"
const { user, authToken } = useSelector((state) => state.auth); const { user, authToken } = useSelector((state) => state.auth);
const monitorState = useSelector((state) => state.infrastructureMonitor); const monitorState = useSelector((state) => state.infrastructureMonitor);
const dispatch = useDispatch(); const dispatch = useDispatch();
@@ -100,12 +105,14 @@ const CreateInfrastructureMonitor = () => {
return { return {
...prev, ...prev,
...newState, ...newState,
[`usage_${id}`]: newState[id] ? prev[`usage_${id}`] : "", [`${THRESHOLD_FIELD_PREFIX}${id}`]: newState[id]
? prev[`${THRESHOLD_FIELD_PREFIX}${id}`]
: "",
}; };
}); });
// Remove the error if unchecked // Remove the error if unchecked
setErrors((prev) => { setErrors((prev) => {
return buildErrors(prev, [`usage_${id}`]); return buildErrors(prev, [`${THRESHOLD_FIELD_PREFIX}${id}`]);
}); });
}; };
@@ -162,17 +169,14 @@ const CreateInfrastructureMonitor = () => {
const generatePayload = (form) => { const generatePayload = (form) => {
let thresholds = {}; let thresholds = {};
if (form.usage_cpu) thresholds.usage_cpu = form.usage_cpu; Object.keys(form)
if (form.usage_memory) thresholds.usage_memory = form.usage_memory; .filter((k) => k.startsWith(`${THRESHOLD_FIELD_PREFIX}`))
if (form.usage_disk) thresholds.usage_disk = form.usage_disk; .map((k) => {
if (form[k]) thresholds[k] = form[k];
delete form[k];
delete form[k.substring(`${THRESHOLD_FIELD_PREFIX}`.length)];
});
delete form.cpu;
delete form.memory;
delete form.disk;
delete form.usage_cpu;
delete form.usage_memory;
delete form.usage_disk;
form = { form = {
...form, ...form,
description: form.name, description: form.name,
@@ -362,8 +366,8 @@ const CreateInfrastructureMonitor = () => {
checkboxId="cpu" checkboxId="cpu"
checkboxLabel="CPU" checkboxLabel="CPU"
checkboxValue={""} checkboxValue={""}
fieldId={"usage_cpu"} fieldId={`${THRESHOLD_FIELD_PREFIX}cpu`}
fieldValue={infrastructureMonitor.usage_cpu ?? ""} fieldValue={infrastructureMonitor[`${THRESHOLD_FIELD_PREFIX}cpu`] ?? ""}
onChange={handleChange} onChange={handleChange}
onBlur={handleBlur} onBlur={handleBlur}
alertUnit="%" alertUnit="%"
@@ -372,8 +376,8 @@ const CreateInfrastructureMonitor = () => {
checkboxId="memory" checkboxId="memory"
checkboxLabel="Memory" checkboxLabel="Memory"
checkboxValue={""} checkboxValue={""}
fieldId={"usage_memory"} fieldId={`${THRESHOLD_FIELD_PREFIX}memory`}
fieldValue={infrastructureMonitor.usage_memory??""} fieldValue={infrastructureMonitor[`${THRESHOLD_FIELD_PREFIX}memory`] ?? ""}
onChange={handleChange} onChange={handleChange}
onBlur={handleBlur} onBlur={handleBlur}
alertUnit="%" alertUnit="%"
@@ -382,8 +386,8 @@ const CreateInfrastructureMonitor = () => {
checkboxId="disk" checkboxId="disk"
checkboxLabel="Disk" checkboxLabel="Disk"
checkboxValue={""} checkboxValue={""}
fieldId={"usage_disk"} fieldId={`${THRESHOLD_FIELD_PREFIX}disk`}
fieldValue={infrastructureMonitor.usage_disk??""} fieldValue={infrastructureMonitor[`${THRESHOLD_FIELD_PREFIX}disk`]??""}
onChange={handleChange} onChange={handleChange}
onBlur={handleBlur} onBlur={handleBlur}
alertUnit="%" alertUnit="%"
@@ -418,7 +422,7 @@ const CreateInfrastructureMonitor = () => {
<Box> <Box>
<Typography component="h2">Logging retention</Typography> <Typography component="h2">Logging retention</Typography>
<Typography component="p"> <Typography component="p">
Configure how logn logs are stored. After this period, the Uptime Manager Configure how logs are stored. After this period, the Uptime Manager
will start deleting oldest data. will start deleting oldest data.
</Typography> </Typography>
</Box> </Box>