Merge pull request #1531 from bluewave-labs/fix/be/get-monitors-by-team-id-query
Fix/be/get monitors by team id query
This commit is contained in:
4 files changed
+109
-43
No files matched your search
@@ -11,7 +11,6 @@ import {
|
|||||||
updateUptimeMonitor,
|
updateUptimeMonitor,
|
||||||
pauseUptimeMonitor,
|
pauseUptimeMonitor,
|
||||||
getUptimeMonitorById,
|
getUptimeMonitorById,
|
||||||
getUptimeSummaryByTeamId,
|
|
||||||
deleteUptimeMonitor,
|
deleteUptimeMonitor,
|
||||||
} from "../../../Features/UptimeMonitors/uptimeMonitorsSlice";
|
} from "../../../Features/UptimeMonitors/uptimeMonitorsSlice";
|
||||||
import TextInput from "../../../Components/Inputs/TextInput";
|
import TextInput from "../../../Components/Inputs/TextInput";
|
||||||
@@ -157,7 +156,6 @@ const Configure = () => {
|
|||||||
const action = await dispatch(updateUptimeMonitor({ authToken, monitor: monitor }));
|
const action = await dispatch(updateUptimeMonitor({ authToken, monitor: monitor }));
|
||||||
if (action.meta.requestStatus === "fulfilled") {
|
if (action.meta.requestStatus === "fulfilled") {
|
||||||
createToast({ body: "Monitor updated successfully!" });
|
createToast({ body: "Monitor updated successfully!" });
|
||||||
dispatch(getUptimeSummaryByTeamId(authToken));
|
|
||||||
} else {
|
} else {
|
||||||
createToast({ body: "Failed to update monitor." });
|
createToast({ body: "Failed to update monitor." });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ import {
|
|||||||
getUptimeSummaryByTeamId,
|
getUptimeSummaryByTeamId,
|
||||||
getUptimeMonitorsByTeamId,
|
getUptimeMonitorsByTeamId,
|
||||||
} from "../../../Features/UptimeMonitors/uptimeMonitorsSlice";
|
} from "../../../Features/UptimeMonitors/uptimeMonitorsSlice";
|
||||||
import { setRowsPerPage } from "../../../Features/UI/uiSlice";
|
|
||||||
import { useIsAdmin } from "../../../Hooks/useIsAdmin";
|
import { useIsAdmin } from "../../../Hooks/useIsAdmin";
|
||||||
import { useSelector, useDispatch } from "react-redux";
|
import { useSelector, useDispatch } from "react-redux";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
@@ -26,15 +25,13 @@ const BREADCRUMBS = [{ name: `Uptime`, path: "/uptime" }];
|
|||||||
|
|
||||||
const UptimeMonitors = () => {
|
const UptimeMonitors = () => {
|
||||||
// Redux state
|
// Redux state
|
||||||
const { isLoading, monitorsSummary } = useSelector((state) => state.uptimeMonitors);
|
|
||||||
const authState = useSelector((state) => state.auth);
|
|
||||||
const { rowsPerPage } = useSelector((state) => state.ui.monitors);
|
|
||||||
|
|
||||||
// Local state
|
// Local state
|
||||||
const [monitors, setMonitors] = useState([]);
|
const [monitors, setMonitors] = useState([]);
|
||||||
const [sort, setSort] = useState({});
|
const [sort, setSort] = useState({});
|
||||||
const [search, setSearch] = useState("");
|
const [search, setSearch] = useState("");
|
||||||
const [page, setPage] = useState(0);
|
const [page, setPage] = useState(0);
|
||||||
|
const [rowsPerPage, setRowsPerPage] = useState(10);
|
||||||
const [isSearching, setIsSearching] = useState(false);
|
const [isSearching, setIsSearching] = useState(false);
|
||||||
const [monitorUpdateTrigger, setMonitorUpdateTrigger] = useState(false);
|
const [monitorUpdateTrigger, setMonitorUpdateTrigger] = useState(false);
|
||||||
|
|
||||||
@@ -46,9 +43,6 @@ const UptimeMonitors = () => {
|
|||||||
const isAdmin = useIsAdmin();
|
const isAdmin = useIsAdmin();
|
||||||
const { isLoading, monitorsSummary } = useSelector((state) => state.uptimeMonitors);
|
const { isLoading, monitorsSummary } = useSelector((state) => state.uptimeMonitors);
|
||||||
const authState = useSelector((state) => state.auth);
|
const authState = useSelector((state) => state.auth);
|
||||||
const dispatch = useDispatch({});
|
|
||||||
const [monitorUpdateTrigger, setMonitorUpdateTrigger] = useState(false);
|
|
||||||
|
|
||||||
|
|
||||||
const fetchParams = useMemo(
|
const fetchParams = useMemo(
|
||||||
() => ({
|
() => ({
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import {
|
|||||||
buildUptimeDetailsPipeline,
|
buildUptimeDetailsPipeline,
|
||||||
buildHardwareDetailsPipeline,
|
buildHardwareDetailsPipeline,
|
||||||
} from "./monitorModuleQueries.js";
|
} from "./monitorModuleQueries.js";
|
||||||
|
import { ObjectId } from "mongodb";
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
const __dirname = path.dirname(__filename);
|
const __dirname = path.dirname(__filename);
|
||||||
|
|
||||||
@@ -570,6 +571,7 @@ const getMonitorsByTeamId = async (req, res) => {
|
|||||||
} = req.query;
|
} = req.query;
|
||||||
|
|
||||||
const monitorQuery = { teamId: req.params.teamId };
|
const monitorQuery = { teamId: req.params.teamId };
|
||||||
|
const monitorCount = await Monitor.countDocuments(monitorQuery);
|
||||||
|
|
||||||
if (type !== undefined) {
|
if (type !== undefined) {
|
||||||
monitorQuery.type = Array.isArray(type) ? { $in: type } : type;
|
monitorQuery.type = Array.isArray(type) ? { $in: type } : type;
|
||||||
@@ -582,46 +584,118 @@ const getMonitorsByTeamId = async (req, res) => {
|
|||||||
{ url: { $regex: filter, $options: "i" } },
|
{ url: { $regex: filter, $options: "i" } },
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
const monitorCount = await Monitor.countDocuments(monitorQuery);
|
|
||||||
|
|
||||||
// Pagination
|
// Pagination
|
||||||
const skip = page && rowsPerPage ? page * rowsPerPage : 0;
|
const skip = page && rowsPerPage ? page * rowsPerPage : 0;
|
||||||
|
|
||||||
// Build Sort option
|
// Build Sort option
|
||||||
const sort = field ? { [field]: order === "asc" ? 1 : -1 } : {};
|
const sort = { [field]: order === "asc" ? 1 : -1 };
|
||||||
|
|
||||||
const monitors = await Monitor.find(monitorQuery)
|
const matchStage = { teamId: new ObjectId(req.params.teamId) };
|
||||||
.skip(skip)
|
if (type !== undefined) {
|
||||||
.limit(rowsPerPage)
|
matchStage.type = Array.isArray(type) ? { $in: type } : type;
|
||||||
.sort(sort);
|
}
|
||||||
|
if (filter !== undefined) {
|
||||||
// Early return if limit is set to -1, indicating we don't want any checks
|
matchStage.$or = [
|
||||||
if (limit === "-1") {
|
{ name: { $regex: filter, $options: "i" } },
|
||||||
return { monitors, monitorCount };
|
{ url: { $regex: filter, $options: "i" } },
|
||||||
|
];
|
||||||
}
|
}
|
||||||
// Map each monitor to include its associated checks
|
|
||||||
const monitorsWithChecks = await Promise.all(
|
|
||||||
monitors.map(async (monitor) => {
|
|
||||||
let model = CHECK_MODEL_LOOKUP[monitor.type];
|
|
||||||
|
|
||||||
// Checks are order newest -> oldest
|
let result = await Monitor.aggregate([
|
||||||
let checks = await model
|
{ $match: matchStage },
|
||||||
.find({
|
{ $skip: parseInt(skip) },
|
||||||
monitorId: monitor._id,
|
...(rowsPerPage ? [{ $limit: parseInt(rowsPerPage) }] : []),
|
||||||
...(status && { status }),
|
{ $sort: sort },
|
||||||
})
|
{
|
||||||
.sort({ createdAt: checkOrder === "asc" ? 1 : -1 })
|
$lookup: {
|
||||||
|
from: "checks",
|
||||||
.limit(limit || 0);
|
let: { monitorId: "$_id" },
|
||||||
|
pipeline: [
|
||||||
//Normalize checks if requested
|
{
|
||||||
if (normalize !== undefined) {
|
$match: {
|
||||||
checks = NormalizeData(checks, 10, 100);
|
$expr: { $eq: ["$monitorId", "$$monitorId"] },
|
||||||
}
|
...(status && { status }),
|
||||||
return { ...monitor.toObject(), checks };
|
},
|
||||||
})
|
},
|
||||||
);
|
{ $sort: { createdAt: checkOrder === "asc" ? 1 : -1 } },
|
||||||
return { monitors: monitorsWithChecks, monitorCount };
|
{ $limit: parseInt(limit) || 0 },
|
||||||
|
],
|
||||||
|
as: "standardchecks",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$lookup: {
|
||||||
|
from: "pagespeedchecks",
|
||||||
|
let: { monitorId: "$_id" },
|
||||||
|
pipeline: [
|
||||||
|
{
|
||||||
|
$match: {
|
||||||
|
$expr: { $eq: ["$monitorId", "$$monitorId"] },
|
||||||
|
...(status && { status }),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ $sort: { createdAt: checkOrder === "asc" ? 1 : -1 } },
|
||||||
|
{ $limit: parseInt(limit) || 0 },
|
||||||
|
],
|
||||||
|
as: "pagespeedchecks",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$lookup: {
|
||||||
|
from: "hardwarechecks",
|
||||||
|
let: { monitorId: "$_id" },
|
||||||
|
pipeline: [
|
||||||
|
{
|
||||||
|
$match: {
|
||||||
|
$expr: { $eq: ["$monitorId", "$$monitorId"] },
|
||||||
|
...(status && { status }),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ $sort: { createdAt: checkOrder === "asc" ? 1 : -1 } },
|
||||||
|
{ $limit: parseInt(limit) || 0 },
|
||||||
|
],
|
||||||
|
as: "hardwarechecks",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$addFields: {
|
||||||
|
checks: {
|
||||||
|
$switch: {
|
||||||
|
branches: [
|
||||||
|
{
|
||||||
|
case: { $in: ["$type", ["http", "ping", "docker", "port"]] },
|
||||||
|
then: "$standardchecks",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
case: { $eq: ["$type", "pagespeed"] },
|
||||||
|
then: "$pagespeedchecks",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
case: { $eq: ["$type", "hardware"] },
|
||||||
|
then: "$hardwarechecks",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
default: [],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$project: {
|
||||||
|
standardchecks: 0,
|
||||||
|
pagespeedchecks: 0,
|
||||||
|
hardwarechecks: 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
if (normalize) {
|
||||||
|
result = result.map((monitor) => {
|
||||||
|
monitor.checks = NormalizeData(monitor.checks, 10, 100);
|
||||||
|
return monitor;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return { monitors: result, monitorCount };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
error.method = "getMonitorsByTeamId";
|
error.method = "getMonitorsByTeamId";
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ const NormalizeData = (checks, rangeMin, rangeMax) => {
|
|||||||
Math.min(rangeMax, normalizedResponseTime)
|
Math.min(rangeMax, normalizedResponseTime)
|
||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
...check._doc,
|
...check,
|
||||||
responseTime: normalizedResponseTime,
|
responseTime: normalizedResponseTime,
|
||||||
originalResponseTime: originalResponseTime,
|
originalResponseTime: originalResponseTime,
|
||||||
};
|
};
|
||||||
@@ -47,7 +47,7 @@ const NormalizeData = (checks, rangeMin, rangeMax) => {
|
|||||||
return normalizedChecks;
|
return normalizedChecks;
|
||||||
} else {
|
} else {
|
||||||
return checks.map((check) => {
|
return checks.map((check) => {
|
||||||
return { ...check._doc, originalResponseTime: check.responseTime };
|
return { ...check, originalResponseTime: check.responseTime };
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user