Merge pull request #1752 from bluewave-labs/feat/fe/config-du-monitor

feat: fe/config du monitor
This commit is contained in:
Alexander Holliday authored and GitHub committed 2025-02-13 10:58:48 -08:00
commit 1ccea4541b
18 files changed
+448 -125

No files matched your search

@@ -92,7 +92,7 @@ export const updateUptimeMonitor = createAsyncThunk(
const res = await networkService.updateMonitor({ const res = await networkService.updateMonitor({
authToken: authToken, authToken: authToken,
monitorId: monitor._id, monitorId: monitor._id,
updatedFields: updatedFields, monitor,
}); });
return res.data; return res.data;
} catch (error) { } catch (error) {
@@ -0,0 +1,33 @@
import { useState } from "react";
import { networkService } from "../../../../main";
import { useSelector } from "react-redux";
import { createToast } from "../../../../Utils/toastUtils";
const useCreateDistributedUptimeMonitor = ({ isCreate, monitorId }) => {
const { authToken, user } = useSelector((state) => state.auth);
const [isLoading, setIsLoading] = useState(false);
const [networkError, setNetworkError] = useState(false);
const createDistributedUptimeMonitor = async ({ form }) => {
setIsLoading(true);
try {
if (isCreate) {
await networkService.createMonitor({ authToken, monitor: form });
} else {
await networkService.updateMonitor({ authToken, monitor: form, monitorId });
}
return true;
} catch (error) {
setNetworkError(true);
createToast({ body: error?.response?.data?.msg ?? error.message });
return false;
} finally {
setIsLoading(false);
}
};
return [createDistributedUptimeMonitor, isLoading, networkError];
};
export { useCreateDistributedUptimeMonitor };
@@ -0,0 +1,32 @@
import { useEffect, useState } from "react";
import { networkService } from "../../../../main";
import { createToast } from "../../../../Utils/toastUtils";
export const useMonitorFetch = ({ authToken, monitorId, isCreate }) => {
const [networkError, setNetworkError] = useState(false);
const [isLoading, setIsLoading] = useState(true);
const [monitor, setMonitor] = useState(undefined);
useEffect(() => {
const fetchMonitors = async () => {
try {
if (isCreate) return;
const res = await networkService.getUptimeDetailsById({
authToken: authToken,
monitorId: monitorId,
normalize: true,
});
setMonitor(res?.data?.data ?? {});
} catch (error) {
setNetworkError(true);
createToast({ body: error.message });
} finally {
setIsLoading(false);
}
};
fetchMonitors();
}, [authToken, monitorId, isCreate]);
return [monitor, isLoading, networkError];
};
export default useMonitorFetch;
@@ -13,18 +13,15 @@ import { createToast } from "../../../Utils/toastUtils";
// Utility // Utility
import { useTheme } from "@emotion/react"; import { useTheme } from "@emotion/react";
import { useState } from "react"; import { useState, useEffect } from "react";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import { useSelector, useDispatch } from "react-redux"; import { useSelector } from "react-redux";
import { monitorValidation } from "../../../Validation/validation"; import { monitorValidation } from "../../../Validation/validation";
import { createUptimeMonitor } from "../../../Features/UptimeMonitors/uptimeMonitorsSlice"; import { useParams } from "react-router-dom";
import { useLocation } from "react-router-dom"; import { useCreateDistributedUptimeMonitor } from "./Hooks/useCreateDistributedUptimeMonitor";
import { useMonitorFetch } from "./Hooks/useMonitorFetch";
// Constants // Constants
const BREADCRUMBS = [
{ name: `distributed uptime`, path: "/distributed-uptime" },
{ name: "create", path: `/distributed-uptime/create` },
];
const MS_PER_MINUTE = 60000; const MS_PER_MINUTE = 60000;
const SELECT_VALUES = [ const SELECT_VALUES = [
{ _id: 1, name: "1 minute" }, { _id: 1, name: "1 minute" },
@@ -34,18 +31,30 @@ const SELECT_VALUES = [
{ _id: 5, name: "5 minutes" }, { _id: 5, name: "5 minutes" },
]; ];
const parseUrl = (url) => {
try {
return new URL(url);
} catch (error) {
return null;
}
};
const CreateDistributedUptime = () => { const CreateDistributedUptime = () => {
const location = useLocation(); const { monitorId } = useParams();
const isCreate = location.pathname.startsWith("/distributed-uptime/create"); const isCreate = typeof monitorId === "undefined";
const BREADCRUMBS = [
{ name: `distributed uptime`, path: "/distributed-uptime" },
{ name: isCreate ? "create" : "configure", path: `` },
];
// Redux state // Redux state
const { user, authToken } = useSelector((state) => state.auth); const { user, authToken } = useSelector((state) => state.auth);
const isLoading = useSelector((state) => state.uptimeMonitors.isLoading);
// Local state // Local state
const [https, setHttps] = useState(true); const [https, setHttps] = useState(true);
const [notifications, setNotifications] = useState([]); const [notifications, setNotifications] = useState([]);
const [monitor, setMonitor] = useState({ const [form, setForm] = useState({
type: "distributed_http", type: "distributed_http",
name: "", name: "",
url: "", url: "",
@@ -55,12 +64,37 @@ const CreateDistributedUptime = () => {
//utils //utils
const theme = useTheme(); const theme = useTheme();
const dispatch = useDispatch();
const navigate = useNavigate(); const navigate = useNavigate();
const [createDistributedUptimeMonitor, isLoading, networkError] =
useCreateDistributedUptimeMonitor({ isCreate, monitorId });
const [monitor, monitorIsLoading, monitorNetworkError] = useMonitorFetch({
authToken,
monitorId,
isCreate,
});
// Effect to set monitor to fetched monitor
useEffect(() => {
if (typeof monitor !== "undefined") {
const parsedUrl = parseUrl(monitor?.url);
const protocol = parsedUrl?.protocol?.replace(":", "") || "";
setHttps(protocol === "https");
const newForm = {
name: monitor.name,
interval: monitor.interval / MS_PER_MINUTE,
url: parsedUrl.host,
type: monitor.type,
};
setForm(newForm);
}
}, [monitor]);
// Handlers // Handlers
const handleCreateMonitor = async (event) => { const handleCreateMonitor = async () => {
const monitorToSubmit = { ...monitor }; const monitorToSubmit = { ...form };
// Prepend protocol to url // Prepend protocol to url
monitorToSubmit.url = `http${https ? "s" : ""}://` + monitorToSubmit.url; monitorToSubmit.url = `http${https ? "s" : ""}://` + monitorToSubmit.url;
@@ -68,7 +102,6 @@ const CreateDistributedUptime = () => {
const { error } = monitorValidation.validate(monitorToSubmit, { const { error } = monitorValidation.validate(monitorToSubmit, {
abortEarly: false, abortEarly: false,
}); });
if (error) { if (error) {
const newErrors = {}; const newErrors = {};
error.details.forEach((err) => { error.details.forEach((err) => {
@@ -80,16 +113,14 @@ const CreateDistributedUptime = () => {
} }
// Append needed fields // Append needed fields
monitorToSubmit.description = monitor.name; monitorToSubmit.description = form.name;
monitorToSubmit.interval = monitor.interval * MS_PER_MINUTE; monitorToSubmit.interval = form.interval * MS_PER_MINUTE;
monitorToSubmit.teamId = user.teamId; monitorToSubmit.teamId = user.teamId;
monitorToSubmit.userId = user._id; monitorToSubmit.userId = user._id;
monitorToSubmit.notifications = notifications; monitorToSubmit.notifications = notifications;
const action = await dispatch( const success = await createDistributedUptimeMonitor({ form: monitorToSubmit });
createUptimeMonitor({ authToken, monitor: monitorToSubmit }) if (success) {
);
if (action.meta.requestStatus === "fulfilled") {
createToast({ body: "Monitor created successfully!" }); createToast({ body: "Monitor created successfully!" });
navigate("/distributed-uptime"); navigate("/distributed-uptime");
} else { } else {
@@ -98,9 +129,10 @@ const CreateDistributedUptime = () => {
}; };
const handleChange = (event) => { const handleChange = (event) => {
const { name, value } = event.target; let { name, value } = event.target;
setMonitor({
...monitor, setForm({
...form,
[name]: value, [name]: value,
}); });
const { error } = monitorValidation.validate( const { error } = monitorValidation.validate(
@@ -178,7 +210,8 @@ const CreateDistributedUptime = () => {
label="URL to monitor" label="URL to monitor"
https={https} https={https}
placeholder={"www.google.com"} placeholder={"www.google.com"}
value={monitor.url} disabled={!isCreate}
value={form.url}
name="url" name="url"
onChange={handleChange} onChange={handleChange}
error={errors["url"] ? true : false} error={errors["url"] ? true : false}
@@ -190,7 +223,7 @@ const CreateDistributedUptime = () => {
label="Display name" label="Display name"
isOptional={true} isOptional={true}
placeholder={"Google"} placeholder={"Google"}
value={monitor.name} value={form.name}
name="name" name="name"
onChange={handleChange} onChange={handleChange}
error={errors["name"] ? true : false} error={errors["name"] ? true : false}
@@ -217,8 +250,11 @@ const CreateDistributedUptime = () => {
checked={true} checked={true}
onChange={handleChange} onChange={handleChange}
/> />
{monitor.type === "http" || monitor.type === "distributed_http" ? ( {form.type === "http" || form.type === "distributed_http" ? (
<ButtonGroup sx={{ ml: theme.spacing(16) }}> <ButtonGroup
disabled={!isCreate}
sx={{ ml: theme.spacing(16) }}
>
<Button <Button
variant="group" variant="group"
filled={https.toString()} filled={https.toString()}
@@ -282,7 +318,7 @@ const CreateDistributedUptime = () => {
id="monitor-interval" id="monitor-interval"
label="Check frequency" label="Check frequency"
name="interval" name="interval"
value={monitor.interval || 1} value={form.interval}
onChange={handleChange} onChange={handleChange}
items={SELECT_VALUES} items={SELECT_VALUES}
/> />
@@ -299,7 +335,7 @@ const CreateDistributedUptime = () => {
disabled={!Object.values(errors).every((value) => value === undefined)} disabled={!Object.values(errors).every((value) => value === undefined)}
loading={isLoading} loading={isLoading}
> >
Create monitor {isCreate ? "Create monitor" : "Configure monitor"}
</LoadingButton> </LoadingButton>
</Stack> </Stack>
</Stack> </Stack>
@@ -0,0 +1,91 @@
// Components
import { Box, Stack, Typography, Button } from "@mui/material";
import Image from "../../../../../Components/Image";
import SettingsIcon from "../../../../../assets/icons/settings-bold.svg?react";
//Utils
import { useTheme } from "@mui/material/styles";
import { useNavigate } from "react-router-dom";
import { useLocation } from "react-router-dom";
import PropTypes from "prop-types";
const Controls = ({ isDeleteOpen, setIsDeleteOpen, isDeleting, monitorId }) => {
const theme = useTheme();
const navigate = useNavigate();
return (
<Stack
direction="row"
gap={theme.spacing(2)}
>
<Box>
<Button
variant="contained"
color="error"
onClick={() => setIsDeleteOpen(!isDeleteOpen)}
loading={isDeleting}
>
Delete
</Button>
</Box>
<Box>
<Button
variant="contained"
color="secondary"
onClick={() => {
navigate(`/distributed-uptime/configure/${monitorId}`);
}}
sx={{
px: theme.spacing(5),
"& svg": {
mr: theme.spacing(3),
"& path": {
stroke: theme.palette.secondary.contrastText,
},
},
}}
>
<SettingsIcon /> Configure
</Button>
</Box>
</Stack>
);
};
Controls.propTypes = {
isDeleting: PropTypes.bool,
monitorId: PropTypes.string,
isDeleteOpen: PropTypes.bool.isRequired,
setIsDeleteOpen: PropTypes.func.isRequired,
};
const ControlsHeader = ({ isDeleting, isDeleteOpen, setIsDeleteOpen, monitorId }) => {
const theme = useTheme();
return (
<Stack
alignSelf="flex-start"
direction="row"
width="100%"
gap={theme.spacing(2)}
justifyContent="flex-end"
alignItems="flex-end"
>
<Controls
isDeleting={isDeleting}
isDeleteOpen={isDeleteOpen}
setIsDeleteOpen={setIsDeleteOpen}
monitorId={monitorId}
/>
</Stack>
);
};
ControlsHeader.propTypes = {
monitorId: PropTypes.string,
isDeleting: PropTypes.bool,
isDeleteOpen: PropTypes.bool.isRequired,
setIsDeleteOpen: PropTypes.func.isRequired,
};
export default ControlsHeader;
@@ -0,0 +1,27 @@
import { useSelector } from "react-redux";
import { useState } from "react";
import { networkService } from "../../../../main";
import { createToast } from "../../../../Utils/toastUtils";
const useDeleteMonitor = ({ monitorId }) => {
const [isLoading, setIsLoading] = useState(false);
const { authToken } = useSelector((state) => state.auth);
const deleteMonitor = async () => {
try {
setIsLoading(true);
await networkService.deleteMonitorById({ authToken, monitorId });
return true;
} catch (error) {
createToast({
body: error.message,
});
return false;
} finally {
setIsLoading(false);
}
};
return [deleteMonitor, isLoading];
};
export { useDeleteMonitor };
@@ -12,23 +12,31 @@ import MonitorTimeFrameHeader from "../../../Components/MonitorTimeFrameHeader";
import GenericFallback from "../../../Components/GenericFallback"; import GenericFallback from "../../../Components/GenericFallback";
import MonitorCreateHeader from "../../../Components/MonitorCreateHeader"; import MonitorCreateHeader from "../../../Components/MonitorCreateHeader";
import SkeletonLayout from "./Components/Skeleton"; import SkeletonLayout from "./Components/Skeleton";
import ControlsHeader from "./Components/ControlsHeader";
import Dialog from "../../../Components/Dialog";
//Utils //Utils
import { useTheme } from "@mui/material/styles"; import { useTheme } from "@mui/material/styles";
import { useState } from "react"; import { useState } from "react";
import { useParams } from "react-router-dom"; import { useParams } from "react-router-dom";
import { useIsAdmin } from "../../../Hooks/useIsAdmin"; import { useIsAdmin } from "../../../Hooks/useIsAdmin";
import { useSubscribeToDetails } from "./Hooks/useSubscribeToDetails"; import { useSubscribeToDetails } from "./Hooks/useSubscribeToDetails";
import { useDeleteMonitor } from "./Hooks/useDeleteMonitor";
import { useNavigate } from "react-router-dom";
const DistributedUptimeDetails = () => { const DistributedUptimeDetails = () => {
const { monitorId } = useParams(); const { monitorId } = useParams();
// Local State // Local State
const [dateRange, setDateRange] = useState("day"); const [dateRange, setDateRange] = useState("day");
const [isDeleteOpen, setIsDeleteOpen] = useState(false);
// Utils // Utils
const theme = useTheme(); const theme = useTheme();
const isAdmin = useIsAdmin(); const isAdmin = useIsAdmin();
const navigate = useNavigate();
const [isLoading, networkError, connectionStatus, monitor, lastUpdateTrigger] = const [isLoading, networkError, connectionStatus, monitor, lastUpdateTrigger] =
useSubscribeToDetails({ monitorId, dateRange }); useSubscribeToDetails({ monitorId, dateRange });
const [deleteMonitor, isDeleting] = useDeleteMonitor({ monitorId });
// Constants // Constants
const BREADCRUMBS = [ const BREADCRUMBS = [
{ name: "Distributed Uptime", path: "/distributed-uptime" }, { name: "Distributed Uptime", path: "/distributed-uptime" },
@@ -76,6 +84,12 @@ const DistributedUptimeDetails = () => {
isAdmin={isAdmin} isAdmin={isAdmin}
path={`/status/distributed/create/${monitorId}`} path={`/status/distributed/create/${monitorId}`}
/> />
<ControlsHeader
isDeleting={isDeleting}
isDeleteOpen={isDeleteOpen}
setIsDeleteOpen={setIsDeleteOpen}
monitorId={monitorId}
/>
<MonitorHeader monitor={monitor} /> <MonitorHeader monitor={monitor} />
<StatBoxes <StatBoxes
monitor={monitor} monitor={monitor}
@@ -107,6 +121,21 @@ const DistributedUptimeDetails = () => {
/> />
</Stack> </Stack>
<Footer /> <Footer />
<Dialog
title="Do you want to delete this monitor?"
onConfirm={() => {
deleteMonitor();
setIsDeleteOpen(false);
navigate("/distributed-uptime");
}}
onCancel={() => {
setIsDeleteOpen(false);
}}
open={isDeleteOpen}
confirmationButtonLabel="Yes, delete monitor"
description="Once deleted, your monitor cannot be retrieved."
isLoading={isDeleting || isLoading}
/>
</Stack> </Stack>
); );
}; };
@@ -29,7 +29,7 @@ const MonitorTable = ({ isLoading, monitors }) => {
<Host <Host
key={row._id} key={row._id}
url={row.url} url={row.url}
title={row.title} title={row.name}
percentageColor={row.percentageColor} percentageColor={row.percentageColor}
percentage={row.percentage} percentage={row.percentage}
/> />
@@ -41,7 +41,7 @@ const DistributedUptimeMonitors = () => {
); );
} }
if (typeof monitorsSummary !== "undefined" && monitorsSummary.totalMonitors === 0) { if (typeof monitorsSummary === "undefined" || monitorsSummary.totalMonitors === 0) {
return ( return (
<Fallback <Fallback
vowelStart={false} vowelStart={false}
@@ -13,7 +13,6 @@ import { useState, useEffect } from "react";
import { useParams } from "react-router-dom"; import { useParams } from "react-router-dom";
import { useStatusPageFetch } from "../../StatusPage/Status/Hooks/useStatusPageFetch"; import { useStatusPageFetch } from "../../StatusPage/Status/Hooks/useStatusPageFetch";
import { useCreateStatusPage } from "../../StatusPage/Create/Hooks/useCreateStatusPage"; import { useCreateStatusPage } from "../../StatusPage/Create/Hooks/useCreateStatusPage";
import { useLocation } from "react-router-dom";
import { statusPageValidation } from "../../../Validation/validation"; import { statusPageValidation } from "../../../Validation/validation";
import { buildErrors } from "../../../Validation/error"; import { buildErrors } from "../../../Validation/error";
import { createToast } from "../../../Utils/toastUtils"; import { createToast } from "../../../Utils/toastUtils";
@@ -23,8 +22,7 @@ const CreateStatus = () => {
const theme = useTheme(); const theme = useTheme();
const { monitorId, url } = useParams(); const { monitorId, url } = useParams();
const navigate = useNavigate(); const navigate = useNavigate();
const location = useLocation(); const isCreate = typeof url === "undefined";
const isCreate = location.pathname.startsWith("/status/distributed/create");
const [createStatusPage, isLoading, networkError] = useCreateStatusPage(isCreate); const [createStatusPage, isLoading, networkError] = useCreateStatusPage(isCreate);
const [statusPage, statusPageMonitors, statusPageIsLoading, statusPageNetworkError] = const [statusPage, statusPageMonitors, statusPageIsLoading, statusPageNetworkError] =
+1 -3
View File
@@ -12,7 +12,6 @@ import { useMonitorsFetch } from "./Hooks/useMonitorsFetch";
import { useCreateStatusPage } from "./Hooks/useCreateStatusPage"; import { useCreateStatusPage } from "./Hooks/useCreateStatusPage";
import { createToast } from "../../../Utils/toastUtils"; import { createToast } from "../../../Utils/toastUtils";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import { useLocation } from "react-router-dom";
import { useStatusPageFetch } from "../Status/Hooks/useStatusPageFetch"; import { useStatusPageFetch } from "../Status/Hooks/useStatusPageFetch";
import { useParams } from "react-router-dom"; import { useParams } from "react-router-dom";
@@ -47,8 +46,7 @@ const CreateStatusPage = () => {
const intervalRef = useRef(null); const intervalRef = useRef(null);
// Setup // Setup
const location = useLocation(); const isCreate = typeof url === "undefined";
const isCreate = location.pathname === "/status/uptime/create";
//Utils //Utils
const theme = useTheme(); const theme = useTheme();
@@ -101,7 +101,7 @@ const UptimeDataTable = ({
<Host <Host
key={row._id} key={row._id}
url={row.url} url={row.url}
title={row.title} title={row.name}
percentageColor={row.percentageColor} percentageColor={row.percentageColor}
percentage={row.percentage} percentage={row.percentage}
/> />
+8
View File
@@ -107,6 +107,14 @@ const Routes = () => {
</ProtectedDistributedUptimeRoute> </ProtectedDistributedUptimeRoute>
} }
/> />
<Route
path="/distributed-uptime/configure/:monitorId"
element={
<ProtectedDistributedUptimeRoute>
<CreateDistributedUptime />
</ProtectedDistributedUptimeRoute>
}
/>
<Route <Route
path="/distributed-uptime/:monitorId" path="/distributed-uptime/:monitorId"
element={ element={
+13 -4
View File
@@ -99,9 +99,11 @@ class NetworkService {
* @returns {Promise<AxiosResponse>} The response from the axios POST request. * @returns {Promise<AxiosResponse>} The response from the axios POST request.
*/ */
async createMonitor(config) { async createMonitor(config) {
return this.axiosInstance.post(`/monitors`, config.monitor, { const { authToken, monitor } = config;
return this.axiosInstance.post(`/monitors`, monitor, {
headers: { headers: {
Authorization: `Bearer ${config.authToken}`, Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
}); });
@@ -284,9 +286,16 @@ 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) {
return this.axiosInstance.put(`/monitors/${config.monitorId}`, config.updatedFields, { const { authToken, monitorId, monitor } = config;
const updatedFields = {
name: monitor.name,
description: monitor.description,
interval: monitor.interval,
notifications: monitor.notifications,
};
return this.axiosInstance.put(`/monitors/${monitorId}`, updatedFields, {
headers: { headers: {
Authorization: `Bearer ${config.authToken}`, Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
}); });
+46 -9
View File
@@ -295,12 +295,49 @@ class MonitorController {
try { try {
const monitor = await this.db.deleteMonitor(req, res, next); const monitor = await this.db.deleteMonitor(req, res, next);
// Delete associated checks,alerts,and notifications // Delete associated checks,alerts,and notifications
try { try {
await this.jobQueue.deleteJob(monitor); const operations = [
await this.db.deleteChecks(monitor._id); { name: "deleteJob", fn: () => this.jobQueue.deleteJob(monitor) },
await this.db.deletePageSpeedChecksByMonitorId(monitor._id); { name: "deleteChecks", fn: () => this.db.deleteChecks(monitor._id) },
await this.db.deleteNotificationsByMonitorId(monitor._id); {
await this.db.deleteHardwareChecksByMonitorId(monitor._id); name: "deletePageSpeedChecks",
fn: () => this.db.deletePageSpeedChecksByMonitorId(monitor._id),
},
{
name: "deleteNotifications",
fn: () => this.db.deleteNotificationsByMonitorId(monitor._id),
},
{
name: "deleteHardwareChecks",
fn: () => this.db.deleteHardwareChecksByMonitorId(monitor._id),
},
{
name: "deleteDistributedUptimeChecks",
fn: () => this.db.deleteDistributedChecksByMonitorId(monitor._id),
},
// TODO We don't actually want to delete the status page if there are other monitors in it
// We actually just want to remove the monitor being deleted from the status page.
// Only delete he status page if there are no other monitors in it.
{
name: "deleteStatusPages",
fn: () => this.db.deleteStatusPagesByMonitorId(monitor._id),
},
];
const results = await Promise.allSettled(operations.map((op) => op.fn()));
results.forEach((result, index) => {
if (result.status === "rejected") {
const operationName = operations[index].name;
logger.error({
message: `Failed to ${operationName} for monitor ${monitor._id}`,
service: SERVICE_NAME,
method: "deleteMonitor",
stack: result.reason.stack,
});
}
});
} catch (error) { } catch (error) {
logger.error({ logger.error({
message: `Error deleting associated records for monitor ${monitor._id} with name ${monitor.name}`, message: `Error deleting associated records for monitor ${monitor._id} with name ${monitor.name}`,
@@ -390,10 +427,10 @@ class MonitorController {
await Promise.all( await Promise.all(
notifications && notifications &&
notifications.map(async (notification) => { notifications.map(async (notification) => {
notification.monitorId = editedMonitor._id; notification.monitorId = editedMonitor._id;
await this.db.createNotification(notification); await this.db.createNotification(notification);
}) })
); );
// Delete the old job(editedMonitor has the same ID as the old monitor) // Delete the old job(editedMonitor has the same ID as the old monitor)
@@ -12,4 +12,15 @@ const createDistributedCheck = async (checkData) => {
} }
}; };
export { createDistributedCheck }; const deleteDistributedChecksByMonitorId = async (monitorId) => {
try {
const result = await DistributedUptimeCheck.deleteMany({ monitorId });
return result.deletedCount;
} catch (error) {
error.service = SERVICE_NAME;
error.method = "deleteDistributedChecksByMonitorId";
throw error;
}
};
export { createDistributedCheck, deleteDistributedChecksByMonitorId };
+73 -70
View File
@@ -313,7 +313,7 @@ const calculateGroupStats = (group) => {
avgResponseTime: avgResponseTime:
checksWithResponseTime.length > 0 checksWithResponseTime.length > 0
? checksWithResponseTime.reduce((sum, check) => sum + check.responseTime, 0) / ? checksWithResponseTime.reduce((sum, check) => sum + check.responseTime, 0) /
checksWithResponseTime.length checksWithResponseTime.length
: 0, : 0,
}; };
}; };
@@ -372,7 +372,10 @@ const getUptimeDetailsById = async (req) => {
const getDistributedUptimeDetailsById = async (req) => { const getDistributedUptimeDetailsById = async (req) => {
try { try {
const { monitorId } = req.params; const { monitorId } = req?.params ?? {};
if (typeof monitorId === "undefined") {
throw new Error();
}
const monitor = await Monitor.findById(monitorId); const monitor = await Monitor.findById(monitorId);
if (monitor === null || monitor === undefined) { if (monitor === null || monitor === undefined) {
throw new Error(this.stringService.dbFindMonitorById(monitorId)); throw new Error(this.stringService.dbFindMonitorById(monitorId));
@@ -605,98 +608,98 @@ const getMonitorsByTeamId = async (req) => {
filteredMonitors: [ filteredMonitors: [
...(filter !== undefined ...(filter !== undefined
? [ ? [
{ {
$match: { $match: {
$or: [ $or: [
{ name: { $regex: filter, $options: "i" } }, { name: { $regex: filter, $options: "i" } },
{ url: { $regex: filter, $options: "i" } }, { url: { $regex: filter, $options: "i" } },
], ],
},
}, },
}, ]
]
: []), : []),
{ $sort: sort }, { $sort: sort },
{ $skip: skip }, { $skip: skip },
...(rowsPerPage ? [{ $limit: rowsPerPage }] : []), ...(rowsPerPage ? [{ $limit: rowsPerPage }] : []),
...(limit ...(limit
? [ ? [
{ {
$lookup: { $lookup: {
from: "checks", from: "checks",
let: { monitorId: "$_id" }, let: { monitorId: "$_id" },
pipeline: [ pipeline: [
{ {
$match: { $match: {
$expr: { $eq: ["$monitorId", "$$monitorId"] }, $expr: { $eq: ["$monitorId", "$$monitorId"] },
},
}, },
}, { $sort: { createdAt: -1 } },
{ $sort: { createdAt: -1 } }, ...(limit ? [{ $limit: limit }] : []),
...(limit ? [{ $limit: limit }] : []), ],
], as: "standardchecks",
as: "standardchecks", },
}, },
}, ]
]
: []), : []),
...(limit ...(limit
? [ ? [
{ {
$lookup: { $lookup: {
from: "pagespeedchecks", from: "pagespeedchecks",
let: { monitorId: "$_id" }, let: { monitorId: "$_id" },
pipeline: [ pipeline: [
{ {
$match: { $match: {
$expr: { $eq: ["$monitorId", "$$monitorId"] }, $expr: { $eq: ["$monitorId", "$$monitorId"] },
},
}, },
}, { $sort: { createdAt: -1 } },
{ $sort: { createdAt: -1 } }, ...(limit ? [{ $limit: limit }] : []),
...(limit ? [{ $limit: limit }] : []), ],
], as: "pagespeedchecks",
as: "pagespeedchecks", },
}, },
}, ]
]
: []), : []),
...(limit ...(limit
? [ ? [
{ {
$lookup: { $lookup: {
from: "hardwarechecks", from: "hardwarechecks",
let: { monitorId: "$_id" }, let: { monitorId: "$_id" },
pipeline: [ pipeline: [
{ {
$match: { $match: {
$expr: { $eq: ["$monitorId", "$$monitorId"] }, $expr: { $eq: ["$monitorId", "$$monitorId"] },
},
}, },
}, { $sort: { createdAt: -1 } },
{ $sort: { createdAt: -1 } }, ...(limit ? [{ $limit: limit }] : []),
...(limit ? [{ $limit: limit }] : []), ],
], as: "hardwarechecks",
as: "hardwarechecks", },
}, },
}, ]
]
: []), : []),
...(limit ...(limit
? [ ? [
{ {
$lookup: { $lookup: {
from: "distributeduptimechecks", from: "distributeduptimechecks",
let: { monitorId: "$_id" }, let: { monitorId: "$_id" },
pipeline: [ pipeline: [
{ {
$match: { $match: {
$expr: { $eq: ["$monitorId", "$$monitorId"] }, $expr: { $eq: ["$monitorId", "$$monitorId"] },
},
}, },
}, { $sort: { createdAt: -1 } },
{ $sort: { createdAt: -1 } }, ...(limit ? [{ $limit: limit }] : []),
...(limit ? [{ $limit: limit }] : []), ],
], as: "distributeduptimechecks",
as: "distributeduptimechecks", },
}, },
}, ]
]
: []), : []),
{ {
@@ -190,6 +190,16 @@ const deleteStatusPage = async (url) => {
} }
}; };
const deleteStatusPagesByMonitorId = async (monitorId) => {
try {
await StatusPage.deleteMany({ monitors: { $in: [monitorId] } });
} catch (error) {
error.service = SERVICE_NAME;
error.method = "deleteStatusPageByMonitorId";
throw error;
}
};
export { export {
createStatusPage, createStatusPage,
updateStatusPage, updateStatusPage,
@@ -197,4 +207,5 @@ export {
getStatusPage, getStatusPage,
getStatusPageByUrl, getStatusPageByUrl,
deleteStatusPage, deleteStatusPage,
deleteStatusPagesByMonitorId,
}; };