Merge pull request #1933 from bluewave-labs/feat/fe/edit-infra-monitor
Implement edit functionality for infrastructure monitors
This commit is contained in:
8 files changed
+104
-36
No files matched your search
@@ -96,19 +96,20 @@ export const getInfrastructureMonitorsByTeamId = createAsyncThunk(
|
|||||||
|
|
||||||
export const updateInfrastructureMonitor = createAsyncThunk(
|
export const updateInfrastructureMonitor = createAsyncThunk(
|
||||||
"infrastructureMonitors/updateMonitor",
|
"infrastructureMonitors/updateMonitor",
|
||||||
async (data, thunkApi) => {
|
async ({ monitorId, monitor }, thunkApi) => {
|
||||||
try {
|
try {
|
||||||
const { monitor } = data;
|
|
||||||
const updatedFields = {
|
const updatedFields = {
|
||||||
name: monitor.name,
|
name: monitor.name,
|
||||||
description: monitor.description,
|
description: monitor.description,
|
||||||
interval: monitor.interval,
|
interval: monitor.interval,
|
||||||
notifications: monitor.notifications,
|
notifications: monitor.notifications,
|
||||||
threshold: monitor.threshold,
|
thresholds: monitor.thresholds,
|
||||||
|
secret: monitor.secret,
|
||||||
};
|
};
|
||||||
const res = await networkService.updateMonitor({
|
const res = await networkService.updateMonitor({
|
||||||
monitorId: monitor._id,
|
monitorId,
|
||||||
updatedFields: updatedFields,
|
monitor,
|
||||||
|
updatedFields,
|
||||||
});
|
});
|
||||||
return res.data;
|
return res.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
// React, Redux, Router
|
// React, Redux, Router
|
||||||
import { useTheme } from "@emotion/react";
|
import { useTheme } from "@emotion/react";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate, useParams } from "react-router-dom";
|
||||||
import { useState } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import { useSelector, useDispatch } from "react-redux";
|
import { useSelector, useDispatch } from "react-redux";
|
||||||
|
|
||||||
// Utility and Network
|
// Utility and Network
|
||||||
import { infrastructureMonitorValidation } from "../../../Validation/validation";
|
import { infrastructureMonitorValidation } from "../../../Validation/validation";
|
||||||
import { createInfrastructureMonitor } from "../../../Features/InfrastructureMonitors/infrastructureMonitorsSlice";
|
import { createInfrastructureMonitor, updateInfrastructureMonitor } from "../../../Features/InfrastructureMonitors/infrastructureMonitorsSlice";
|
||||||
|
import { useHardwareMonitorsFetch } from "../Details/Hooks/useHardwareMonitorsFetch";
|
||||||
import { capitalizeFirstLetter } from "../../../Utils/stringUtils";
|
import { capitalizeFirstLetter } from "../../../Utils/stringUtils";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
@@ -53,8 +54,15 @@ const CreateInfrastructureMonitor = () => {
|
|||||||
const monitorState = useSelector((state) => state.infrastructureMonitor);
|
const monitorState = useSelector((state) => state.infrastructureMonitor);
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
const { monitorId } = useParams();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
|
// Determine if we are creating or editing
|
||||||
|
const isCreate = typeof monitorId === "undefined";
|
||||||
|
|
||||||
|
// Fetch monitor details if editing
|
||||||
|
const { monitor, isLoading, networkError } = useHardwareMonitorsFetch({ monitorId });
|
||||||
|
|
||||||
// State
|
// State
|
||||||
const [errors, setErrors] = useState({});
|
const [errors, setErrors] = useState({});
|
||||||
const [https, setHttps] = useState(false);
|
const [https, setHttps] = useState(false);
|
||||||
@@ -62,6 +70,7 @@ const CreateInfrastructureMonitor = () => {
|
|||||||
url: "",
|
url: "",
|
||||||
name: "",
|
name: "",
|
||||||
notifications: [],
|
notifications: [],
|
||||||
|
notify_email: false,
|
||||||
interval: 0.25,
|
interval: 0.25,
|
||||||
cpu: false,
|
cpu: false,
|
||||||
usage_cpu: "",
|
usage_cpu: "",
|
||||||
@@ -74,10 +83,44 @@ const CreateInfrastructureMonitor = () => {
|
|||||||
secret: "",
|
secret: "",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Populate form fields if editing
|
||||||
|
useEffect(() => {
|
||||||
|
if (isCreate || !monitor) return;
|
||||||
|
|
||||||
|
setInfrastructureMonitor({
|
||||||
|
url: monitor.url.replace(/^https?:\/\//, ""),
|
||||||
|
name: monitor.name || "",
|
||||||
|
notifications: monitor.notifications?.filter(n => typeof n === "object") || [],
|
||||||
|
notify_email: (monitor.notifications?.length ?? 0) > 0,
|
||||||
|
interval: monitor.interval / MS_PER_MINUTE,
|
||||||
|
cpu: monitor.thresholds?.usage_cpu !== undefined,
|
||||||
|
usage_cpu: monitor.thresholds?.usage_cpu ? monitor.thresholds.usage_cpu * 100 : "",
|
||||||
|
|
||||||
|
memory: monitor.thresholds?.usage_memory !== undefined,
|
||||||
|
usage_memory: monitor.thresholds?.usage_memory ? monitor.thresholds.usage_memory * 100 : "",
|
||||||
|
|
||||||
|
disk: monitor.thresholds?.usage_disk !== undefined,
|
||||||
|
usage_disk: monitor.thresholds?.usage_disk ? monitor.thresholds.usage_disk * 100 : "",
|
||||||
|
|
||||||
|
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]);
|
||||||
|
|
||||||
// Handlers
|
// Handlers
|
||||||
const handleCreateInfrastructureMonitor = async (event) => {
|
const handleCreateInfrastructureMonitor = async (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
|
const formattedNotifications = infrastructureMonitor.notifications.map((n) =>
|
||||||
|
typeof n === "string" ? { type: "email", address: n } : n
|
||||||
|
);
|
||||||
|
|
||||||
|
if (infrastructureMonitor.notify_email) {
|
||||||
|
formattedNotifications.push({ type: "email", address: user.email });
|
||||||
|
}
|
||||||
|
|
||||||
// Build the form
|
// Build the form
|
||||||
let form = {
|
let form = {
|
||||||
url: `http${https ? "s" : ""}://` + infrastructureMonitor.url,
|
url: `http${https ? "s" : ""}://` + infrastructureMonitor.url,
|
||||||
@@ -103,6 +146,7 @@ const CreateInfrastructureMonitor = () => {
|
|||||||
? { usage_temperature: infrastructureMonitor.usage_temperature }
|
? { usage_temperature: infrastructureMonitor.usage_temperature }
|
||||||
: {}),
|
: {}),
|
||||||
secret: infrastructureMonitor.secret,
|
secret: infrastructureMonitor.secret,
|
||||||
|
notifications: formattedNotifications,
|
||||||
};
|
};
|
||||||
|
|
||||||
const { error } = infrastructureMonitorValidation.validate(form, {
|
const { error } = infrastructureMonitorValidation.validate(form, {
|
||||||
@@ -149,12 +193,17 @@ const CreateInfrastructureMonitor = () => {
|
|||||||
thresholds,
|
thresholds,
|
||||||
};
|
};
|
||||||
|
|
||||||
const action = await dispatch(createInfrastructureMonitor({ monitor: form }));
|
// Handle create or update
|
||||||
|
const action = isCreate
|
||||||
|
? await dispatch(createInfrastructureMonitor({ monitor: form }))
|
||||||
|
: await dispatch(updateInfrastructureMonitor({ monitorId, monitor: form }));
|
||||||
if (action.meta.requestStatus === "fulfilled") {
|
if (action.meta.requestStatus === "fulfilled") {
|
||||||
createToast({ body: "Infrastructure monitor created successfully!" });
|
createToast({
|
||||||
|
body: isCreate ? t("infrastructureMonitorCreated") : t("infrastructureMonitorUpdated"),
|
||||||
|
});
|
||||||
navigate("/infrastructure");
|
navigate("/infrastructure");
|
||||||
} else {
|
} else {
|
||||||
createToast({ body: "Failed to create monitor." });
|
createToast({ body: "Failed to save monitor." });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -185,37 +234,34 @@ const CreateInfrastructureMonitor = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleNotifications = (event, type) => {
|
const handleNotifications = (event, type) => {
|
||||||
const { value } = event.target;
|
const { value, checked } = event.target;
|
||||||
let notifications = [...infrastructureMonitor.notifications];
|
let notifications = [...infrastructureMonitor.notifications];
|
||||||
const notificationExists = notifications.some((notification) => {
|
|
||||||
if (notification.type === type && notification.address === value) {
|
if (checked) {
|
||||||
return true;
|
if (!notifications.some((n) => n.type === type && n.address === value)) {
|
||||||
|
notifications.push({ type, address: value });
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
});
|
|
||||||
if (notificationExists) {
|
|
||||||
notifications = notifications.filter((notification) => {
|
|
||||||
if (notification.type === type && notification.address === value) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
notifications.push({ type, address: value });
|
notifications = notifications.filter((n) => !(n.type === type && n.address === value));
|
||||||
}
|
}
|
||||||
|
|
||||||
setInfrastructureMonitor((prev) => ({
|
setInfrastructureMonitor((prev) => ({
|
||||||
...prev,
|
...prev,
|
||||||
notifications,
|
notifications,
|
||||||
|
...(type === "email" ? { notify_email: checked } : {})
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box className="create-infrastructure-monitor">
|
<Box className="create-infrastructure-monitor">
|
||||||
<Breadcrumbs
|
<Breadcrumbs
|
||||||
list={[
|
list={[
|
||||||
{ name: "Infrastructure monitors", path: "/infrastructure" },
|
{ name: "Infrastructure monitors", path: "/infrastructure" },
|
||||||
{ name: "create", path: `/infrastructure/create` },
|
...(isCreate
|
||||||
|
? [{ name: "Create", path: "/infrastructure/create" }]
|
||||||
|
: [{ name: "Details", path: `/infrastructure/${monitorId}` }, { name: "Configure", path: `/infrastructure/configure/${monitorId}` }]
|
||||||
|
),
|
||||||
]}
|
]}
|
||||||
/>
|
/>
|
||||||
<Stack
|
<Stack
|
||||||
@@ -235,7 +281,7 @@ const CreateInfrastructureMonitor = () => {
|
|||||||
component="span"
|
component="span"
|
||||||
fontSize="inherit"
|
fontSize="inherit"
|
||||||
>
|
>
|
||||||
{t("infrastructureCreateYour")}{" "}
|
{t(isCreate? "infrastructureCreateYour": "infrastructureEditYour")}{" "}
|
||||||
</Typography>
|
</Typography>
|
||||||
<Typography
|
<Typography
|
||||||
component="span"
|
component="span"
|
||||||
@@ -274,7 +320,9 @@ const CreateInfrastructureMonitor = () => {
|
|||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
error={errors["url"] ? true : false}
|
error={errors["url"] ? true : false}
|
||||||
helperText={errors["url"]}
|
helperText={errors["url"]}
|
||||||
|
disabled={!isCreate}
|
||||||
/>
|
/>
|
||||||
|
{isCreate && (
|
||||||
<Box>
|
<Box>
|
||||||
<Typography component="p">{t("infrastructureProtocol")}</Typography>
|
<Typography component="p">{t("infrastructureProtocol")}</Typography>
|
||||||
<ButtonGroup>
|
<ButtonGroup>
|
||||||
@@ -294,6 +342,7 @@ const CreateInfrastructureMonitor = () => {
|
|||||||
</Button>
|
</Button>
|
||||||
</ButtonGroup>
|
</ButtonGroup>
|
||||||
</Box>
|
</Box>
|
||||||
|
)}
|
||||||
<TextInput
|
<TextInput
|
||||||
type="text"
|
type="text"
|
||||||
id="name"
|
id="name"
|
||||||
@@ -328,9 +377,7 @@ const CreateInfrastructureMonitor = () => {
|
|||||||
<Checkbox
|
<Checkbox
|
||||||
id="notify-email-default"
|
id="notify-email-default"
|
||||||
label={`Notify via email (to ${user.email})`}
|
label={`Notify via email (to ${user.email})`}
|
||||||
isChecked={infrastructureMonitor.notifications.some(
|
isChecked={infrastructureMonitor.notify_email}
|
||||||
(notification) => notification.type === "email"
|
|
||||||
)}
|
|
||||||
value={user?.email}
|
value={user?.email}
|
||||||
onChange={(event) => handleNotifications(event, "email")}
|
onChange={(event) => handleNotifications(event, "email")}
|
||||||
/>
|
/>
|
||||||
@@ -361,7 +408,7 @@ const CreateInfrastructureMonitor = () => {
|
|||||||
isChecked={infrastructureMonitor[metric]}
|
isChecked={infrastructureMonitor[metric]}
|
||||||
fieldId={METRIC_PREFIX + metric}
|
fieldId={METRIC_PREFIX + metric}
|
||||||
fieldName={METRIC_PREFIX + metric}
|
fieldName={METRIC_PREFIX + metric}
|
||||||
fieldValue={infrastructureMonitor[METRIC_PREFIX + metric]}
|
fieldValue={String(infrastructureMonitor[METRIC_PREFIX + metric])}
|
||||||
onFieldChange={handleChange}
|
onFieldChange={handleChange}
|
||||||
alertUnit={metric == "temperature" ? "°C" : "%"}
|
alertUnit={metric == "temperature" ? "°C" : "%"}
|
||||||
/>
|
/>
|
||||||
@@ -408,7 +455,7 @@ const CreateInfrastructureMonitor = () => {
|
|||||||
onClick={handleCreateInfrastructureMonitor}
|
onClick={handleCreateInfrastructureMonitor}
|
||||||
loading={monitorState?.isLoading}
|
loading={monitorState?.isLoading}
|
||||||
>
|
>
|
||||||
{t("infrastructureCreateMonitor")}
|
{t(isCreate? "infrastructureCreateMonitor": "infrastructureEditMonitor")}
|
||||||
</Button>
|
</Button>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Stack>
|
</Stack>
|
||||||
|
|||||||
@@ -2,6 +2,11 @@ import { useEffect, useState } from "react";
|
|||||||
import { networkService } from "../../../../main";
|
import { networkService } from "../../../../main";
|
||||||
|
|
||||||
const useHardwareMonitorsFetch = ({ monitorId, dateRange }) => {
|
const useHardwareMonitorsFetch = ({ monitorId, dateRange }) => {
|
||||||
|
|
||||||
|
// Abort early if creating monitor
|
||||||
|
if (!monitorId) {
|
||||||
|
return { monitor: undefined, isLoading: false, networkError: undefined };
|
||||||
|
}
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
const [networkError, setNetworkError] = useState(false);
|
const [networkError, setNetworkError] = useState(false);
|
||||||
const [monitor, setMonitor] = useState(undefined);
|
const [monitor, setMonitor] = useState(undefined);
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ const InfrastructureDetails = () => {
|
|||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const { monitorId } = useParams();
|
const { monitorId } = useParams();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
const isAdmin = useIsAdmin();
|
||||||
|
|
||||||
const { isLoading, networkError, monitor } = useHardwareMonitorsFetch({
|
const { isLoading, networkError, monitor } = useHardwareMonitorsFetch({
|
||||||
monitorId,
|
monitorId,
|
||||||
@@ -74,7 +75,7 @@ const InfrastructureDetails = () => {
|
|||||||
<Breadcrumbs list={BREADCRUMBS} />
|
<Breadcrumbs list={BREADCRUMBS} />
|
||||||
<MonitorStatusHeader
|
<MonitorStatusHeader
|
||||||
path={"infrastructure"}
|
path={"infrastructure"}
|
||||||
isAdmin={false}
|
isAdmin={isAdmin}
|
||||||
shouldRender={!isLoading}
|
shouldRender={!isLoading}
|
||||||
monitor={monitor}
|
monitor={monitor}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -148,6 +148,10 @@ const Routes = () => {
|
|||||||
path="infrastructure/create"
|
path="infrastructure/create"
|
||||||
element={<InfrastructureCreate />}
|
element={<InfrastructureCreate />}
|
||||||
/>
|
/>
|
||||||
|
<Route
|
||||||
|
path="/infrastructure/configure/:monitorId"
|
||||||
|
element={<InfrastructureCreate />}
|
||||||
|
/>
|
||||||
<Route
|
<Route
|
||||||
path="infrastructure/:monitorId"
|
path="infrastructure/:monitorId"
|
||||||
element={<InfrastructureDetails />}
|
element={<InfrastructureDetails />}
|
||||||
|
|||||||
@@ -257,14 +257,14 @@ class NetworkService {
|
|||||||
* @returns {Promise<AxiosResponse>} The response from the axios PUT request.
|
* @returns {Promise<AxiosResponse>} The response from the axios PUT request.
|
||||||
*/
|
*/
|
||||||
async updateMonitor(config) {
|
async updateMonitor(config) {
|
||||||
const { monitorId, monitor } = config;
|
const { monitorId, monitor, updatedFields } = config;
|
||||||
const updatedFields = {
|
const payload = updatedFields ?? {
|
||||||
name: monitor.name,
|
name: monitor.name,
|
||||||
description: monitor.description,
|
description: monitor.description,
|
||||||
interval: monitor.interval,
|
interval: monitor.interval,
|
||||||
notifications: monitor.notifications,
|
notifications: monitor.notifications,
|
||||||
};
|
};
|
||||||
return this.axiosInstance.put(`/monitors/${monitorId}`, updatedFields, {
|
return this.axiosInstance.put(`/monitors/${monitorId}`, payload, {
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -349,6 +349,12 @@ const infrastructureMonitorValidation = joi.object({
|
|||||||
"number.base": "Frequency must be a number.",
|
"number.base": "Frequency must be a number.",
|
||||||
"any.required": "Frequency is required.",
|
"any.required": "Frequency is required.",
|
||||||
}),
|
}),
|
||||||
|
notifications: joi.array().items(
|
||||||
|
joi.object({
|
||||||
|
type: joi.string().valid("email").required(),
|
||||||
|
address: joi.string().email({ tlds: { allow: false } }).required(),
|
||||||
|
})
|
||||||
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
|||||||
@@ -225,11 +225,15 @@
|
|||||||
"incidentsOptionsHeaderLastWeek": "Last week",
|
"incidentsOptionsHeaderLastWeek": "Last week",
|
||||||
"incidentsOptionsPlaceholderAllServers": "All servers",
|
"incidentsOptionsPlaceholderAllServers": "All servers",
|
||||||
"infrastructureCreateYour": "Create your",
|
"infrastructureCreateYour": "Create your",
|
||||||
|
"infrastructureEditYour": "Edit your",
|
||||||
"infrastructureCreateGeneralSettingsDescription": "Here you can select the URL of the host, together with the friendly name and authorization secret to connect to the server agent.",
|
"infrastructureCreateGeneralSettingsDescription": "Here you can select the URL of the host, together with the friendly name and authorization secret to connect to the server agent.",
|
||||||
"infrastructureServerRequirement": "The server you are monitoring must be running the",
|
"infrastructureServerRequirement": "The server you are monitoring must be running the",
|
||||||
"infrastructureCustomizeAlerts": "Customize alerts",
|
"infrastructureCustomizeAlerts": "Customize alerts",
|
||||||
"infrastructureAlertNotificationDescription": "Send a notification to user(s) when thresholds exceed a specified percentage.",
|
"infrastructureAlertNotificationDescription": "Send a notification to user(s) when thresholds exceed a specified percentage.",
|
||||||
"infrastructureCreateMonitor": "Create Infrastructure Monitor",
|
"infrastructureCreateMonitor": "Create Infrastructure Monitor",
|
||||||
|
"infrastructureEditMonitor": "Save Infrastructure Monitor",
|
||||||
|
"infrastructureMonitorCreated": "Infrastructure monitor created successfully!",
|
||||||
|
"infrastructureMonitorUpdated": "Infrastructure monitor updated successfully!",
|
||||||
"infrastructureProtocol": "Protocol",
|
"infrastructureProtocol": "Protocol",
|
||||||
"infrastructureServerUrlLabel": "Server URL",
|
"infrastructureServerUrlLabel": "Server URL",
|
||||||
"infrastructureDisplayNameLabel": "Display name",
|
"infrastructureDisplayNameLabel": "Display name",
|
||||||
|
|||||||
Reference in new issue
Block a user