From aa939bfb520768d7ee26477243c5c8a6111ed98c Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Mon, 22 Jul 2024 11:28:39 -0700 Subject: [PATCH 1/4] Initial refactor --- Client/src/Pages/Incidents/index.jsx | 186 ++------------------------- 1 file changed, 8 insertions(+), 178 deletions(-) diff --git a/Client/src/Pages/Incidents/index.jsx b/Client/src/Pages/Incidents/index.jsx index da9851d11..51568af63 100644 --- a/Client/src/Pages/Incidents/index.jsx +++ b/Client/src/Pages/Incidents/index.jsx @@ -1,180 +1,10 @@ -import * as React from "react"; -import TextField from "@mui/material/TextField"; -import Autocomplete, { createFilterOptions } from "@mui/material/Autocomplete"; -import { useTheme } from "@mui/material/styles"; -import Box from "@mui/material/Box"; -import Typography from "@mui/material/Typography"; -import TuneIcon from "@mui/icons-material/Tune"; -import { StatusLabel } from "../../Components/Label/"; +import { useState } from "react"; +import { useSelector, useDispatch } from "react-redux"; -const filterOptions = createFilterOptions({ - matchFrom: "start", - stringify: (option) => option.title, -}); +const Incidents = () => { + const monitorState = useSelector((state) => state.monitors); + console.log(monitorState); + return
Incidents
; +}; -const titles = [ - { title: "Down" }, - { title: "Cannot resolve" }, - { title: "Clear / show all" }, -]; - -/** - * Filter component with autocomplete for status filtering. - * @returns {JSX.Element} The JSX element representing the filter component. - */ -function Filter() { - return ( - option.title} - filterOptions={filterOptions} - sx={{ - width: 170, - "& .MuiAutocomplete-inputRoot": { - height: "50px", - }, - "& .MuiAutocomplete-listbox": { - maxHeight: "200px", - }, - }} - disableClearable - renderInput={(params) => ( - - - Filter by status - - } - InputProps={{ ...params.InputProps, endAdornment: null }} - /> - )} - /> - ); -} - -const sampleData = [ - { - name: ( - - ), - date: "2024-03-14 21:41:09", - message: "HTTP 350 - NOK", - }, - { - name: ( - - ), - date: "2024-03-14 21:41:09", - message: "timeout of 48000ms exceeded", - }, - { - name: ( - - ), - date: "2024-03-14 21:41:09", - message: "timeout of 48000ms exceeded", - }, - { - name: ( - - ), - date: "2024-03-14 21:41:09", - message: "timeout of 48000ms exceeded", - }, - { - name: ( - - ), - date: "2024-03-14 21:41:09", - message: "HTTP 350 - NOK", - }, -]; - -const columns = [ - { id: "name", header: "Status" }, - { id: "date", header: "Date & Time" }, - { id: "message", header: "Message" }, -]; - -/** - * Customized table component displaying incident history with a filter. - * @returns {JSX.Element} The JSX element representing the customized table with a filter. - */ -export default function CustomizedTables() { - const theme = useTheme(); - - return ( - - - - Incident History - - - - {/* TODO Replace with BasicTable */} - {/* */} - - ); -} +export default Incidents; From a85ae41557d10533a12f1c91f60fc96dedd0b3a3 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Mon, 22 Jul 2024 12:47:50 -0700 Subject: [PATCH 2/4] initial commit --- Server/controllers/monitorController.js | 15 +++++++++++++++ Server/db/MongoDB.js | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/Server/controllers/monitorController.js b/Server/controllers/monitorController.js index 52433c8d7..4e51aca97 100644 --- a/Server/controllers/monitorController.js +++ b/Server/controllers/monitorController.js @@ -98,6 +98,21 @@ const getMonitorsByUserId = async (req, res, next) => { } }; +const getMonitorByIdForIncidents = async (req, res, next) => { + try { + } catch (error) { + error.service = SERVICE_NAME; + next(error); + } +}; +const getMonitorsByUserIdForIncidents = async (req, res, next) => { + try { + } catch (error) { + error.service = SERVICE_NAME; + next(error); + } +}; + /** * Creates a new monitor * @async diff --git a/Server/db/MongoDB.js b/Server/db/MongoDB.js index a59822f98..86160b91c 100644 --- a/Server/db/MongoDB.js +++ b/Server/db/MongoDB.js @@ -338,6 +338,26 @@ const getMonitorsByUserId = async (req, res) => { } }; +/** + * Get monitors by UserID + * @async + * @param {Express.Request} req + * @param {Express.Response} res + * @returns {Promise} + * @throws {Error} + */ +const getMonitorByIdForIncidents = async (req, res, next) => {}; + +/** + * Get monitors by UserID + * @async + * @param {Express.Request} req + * @param {Express.Response} res + * @returns {Promise>} + * @throws {Error} + */ +const getMonitorsByUserIdForIncidents = async (req, res, next) => {}; + /** * Create a monitor * @async From e63d577fa5bcd8872be27ed83aa3517e72fb30dd Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Mon, 22 Jul 2024 14:28:03 -0700 Subject: [PATCH 3/4] Added endponts and routes for fetching monitors with incidents --- Server/controllers/monitorController.js | 41 +++++++++++++++++++++++++ Server/db/MongoDB.js | 41 ++++++++++++++++++++++--- Server/routes/monitorRoute.js | 9 ++++++ 3 files changed, 87 insertions(+), 4 deletions(-) diff --git a/Server/controllers/monitorController.js b/Server/controllers/monitorController.js index 4e51aca97..45a55a973 100644 --- a/Server/controllers/monitorController.js +++ b/Server/controllers/monitorController.js @@ -98,15 +98,54 @@ const getMonitorsByUserId = async (req, res, next) => { } }; +/** + * Returns monitor with matching ID and incidents + * @async + * @param {Express.Request} req + * @param {Express.Response} res + * @returns {Promise} + * @throws {Error} + */ const getMonitorByIdForIncidents = async (req, res, next) => { try { + await getMonitorByIdValidation.validateAsync(req.params); + + let monitorWithIncidents = await req.db.getMonitorByIdForIncidents( + req, + res + ); + + return res.json({ + success: true, + msg: successMessages.MONTIOR_GET_BY_ID, + data: monitorWithIncidents, + }); } catch (error) { error.service = SERVICE_NAME; next(error); } }; + +/** + * Returns all monitors with incidents that belong to User with UserID + * @async + * @param {Express.Request} req + * @param {Express.Response} res + * @returns {Promise} + * @throws {Error} + */ const getMonitorsByUserIdForIncidents = async (req, res, next) => { try { + await getMonitorsByUserIdValidation.validateAsync(req.params); + let monitorsWithIncidents = await req.db.getMonitorsByUserIdForIncidents( + req, + res + ); + return res.json({ + success: true, + msg: successMessages.MONITOR_GET_BY_USER_ID(req.params.userId), + data: monitorsWithIncidents, + }); } catch (error) { error.service = SERVICE_NAME; next(error); @@ -242,6 +281,8 @@ module.exports = { getAllMonitors, getMonitorById, getMonitorsByUserId, + getMonitorByIdForIncidents, + getMonitorsByUserIdForIncidents, createMonitor, deleteMonitor, deleteAllMonitors, diff --git a/Server/db/MongoDB.js b/Server/db/MongoDB.js index 86160b91c..644d446ac 100644 --- a/Server/db/MongoDB.js +++ b/Server/db/MongoDB.js @@ -131,7 +131,6 @@ const updateUser = async (req, res) => { ) .select("-password") .select("-profileImage"); - console.log(updatedUser); return updatedUser; } catch (error) { throw error; @@ -346,7 +345,21 @@ const getMonitorsByUserId = async (req, res) => { * @returns {Promise} * @throws {Error} */ -const getMonitorByIdForIncidents = async (req, res, next) => {}; +const getMonitorByIdForIncidents = async (req, res, next) => { + try { + const monitor = await Monitor.findById(req.params.monitorId); + const checks = await Check.find({ + monitorId: monitor._id, + status: false, + }).sort({ + createdAt: 1, + }); + const monitorWithChecks = { ...monitor.toObject(), checks }; + return monitorWithChecks; + } catch (error) { + throw error; + } +}; /** * Get monitors by UserID @@ -356,7 +369,26 @@ const getMonitorByIdForIncidents = async (req, res, next) => {}; * @returns {Promise>} * @throws {Error} */ -const getMonitorsByUserIdForIncidents = async (req, res, next) => {}; +const getMonitorsByUserIdForIncidents = async (req, res) => { + try { + const monitors = await Monitor.find({ userId: req.params.userId }); + // Map each monitor to include its associated checks + const monitorsWithChecks = await Promise.all( + monitors.map(async (monitor) => { + const checks = await Check.find({ + monitorId: monitor._id, + status: false, + }).sort({ + createdAt: 1, + }); + return { ...monitor.toObject(), checks }; + }) + ); + return monitorsWithChecks; + } catch (error) { + throw error; + } +}; /** * Create a monitor @@ -468,7 +500,6 @@ const editMonitor = async (req, res) => { const createCheck = async (checkData) => { try { - console.log(checkData); const check = await new Check({ ...checkData }).save(); return check; } catch (error) { @@ -657,6 +688,8 @@ module.exports = { getAllMonitors, getMonitorById, getMonitorsByUserId, + getMonitorByIdForIncidents, + getMonitorsByUserIdForIncidents, createMonitor, deleteMonitor, deleteAllMonitors, diff --git a/Server/routes/monitorRoute.js b/Server/routes/monitorRoute.js index ae7a5852e..9ba703244 100644 --- a/Server/routes/monitorRoute.js +++ b/Server/routes/monitorRoute.js @@ -7,6 +7,15 @@ router.get("/", monitorController.getAllMonitors); router.get("/:monitorId", monitorController.getMonitorById); router.get("/user/:userId", monitorController.getMonitorsByUserId); +router.get( + "/incidents/:monitorId", + monitorController.getMonitorByIdForIncidents +); +router.get( + "/incidents/user/:userId", + monitorController.getMonitorsByUserIdForIncidents +); + router.post("/", monitorController.createMonitor); router.post( "/delete/:monitorId", From 761260cc1d8ceb8bb878da9d927d1c7378ed1cb3 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Mon, 22 Jul 2024 15:44:40 -0700 Subject: [PATCH 4/4] Implement barebones incidents page, ready for styling --- Client/src/Pages/Incidents/index.jsx | 135 ++++++++++++++++++++++++++- 1 file changed, 130 insertions(+), 5 deletions(-) diff --git a/Client/src/Pages/Incidents/index.jsx b/Client/src/Pages/Incidents/index.jsx index 51568af63..17f0323aa 100644 --- a/Client/src/Pages/Incidents/index.jsx +++ b/Client/src/Pages/Incidents/index.jsx @@ -1,10 +1,135 @@ -import { useState } from "react"; -import { useSelector, useDispatch } from "react-redux"; +import { useState, useEffect } from "react"; +import { useSelector } from "react-redux"; +import { Select, MenuItem, Typography, ButtonGroup } from "@mui/material"; + +import Button from "../../Components/Button"; +import axiosInstance from "../../Utils/axiosConfig"; +import BasicTable from "../../Components/BasicTable"; +import { StatusLabel } from "../../Components/Label"; const Incidents = () => { - const monitorState = useSelector((state) => state.monitors); - console.log(monitorState); - return
Incidents
; + const authState = useSelector((state) => state.auth); + const [monitors, setMonitors] = useState({}); + const [selectedMonitor, setSelectedMonitor] = useState("0"); + + // TODO do something with these filters + const [filter, setFilter] = useState("all"); + + let data = { + cols: [ + { id: 1, name: "Status" }, + { id: 2, name: "Timestamp" }, + { id: 3, name: "Monitor" }, + { id: 4, name: "Message" }, + ], + rows: [], + }; + + useEffect(() => { + const fetchIncidents = async () => { + const res = await axiosInstance.get( + `/monitors/incidents/user/${authState.user._id}`, + { + headers: { + Authorization: `Bearer ${authState.authToken}`, + }, + } + ); + + // Reduce to a lookup object for 0(1) lookup + if (res.data && res.data.data.length > 0) { + const monitorLookup = res.data.data.reduce((acc, monitor) => { + acc[monitor._id] = monitor; + return acc; + }, {}); + setMonitors(monitorLookup); + } + }; + + fetchIncidents(); + }, [authState]); + + const handleSelect = (event) => { + setSelectedMonitor(event.target.value); + }; + let incidents = []; + if (selectedMonitor === "0") { + Object.values(monitors).forEach((monitor) => { + incidents = incidents.concat(monitor.checks); + }); + } else { + const monitor = monitors[selectedMonitor]; + incidents = monitor.checks; + } + incidents = incidents + .sort((a, b) => { + return new Date(b.createdAt) - new Date(a.createdAt); + }) + .map((incident, idx) => { + const params = { + status: "Down", + backgroundColor: "var(--env-var-color-21)", + statusDotColor: "var(--env-var-color-19)", + }; + + return { + id: idx, + data: [ + { + id: idx + 0, + data: ( + + ), + }, + { id: idx + 1, data: new Date(incident.createdAt).toLocaleString() }, + { id: idx + 2, data: incident.statusCode }, + { id: idx + 3, data: monitors[incident.monitorId].name }, + ], + }; + }); + data.rows = incidents; + + return ( +
+
+
+ Incident history for: + +
+ +
+ +
+ ); }; export default Incidents;