Merge pull request #304 from bluewave-labs/feat/monitor-details-data

Feat/monitor details data
This commit is contained in:
Alexander Holliday authored and GitHub committed 2024-07-10 12:34:02 -07:00
commit 9277a37559
2 files changed
+46 -65

No files matched your search

+43 -64
View File
@@ -1,63 +1,42 @@
import React from 'react'; import React, { useEffect, useState } from "react";
import CustomizedTables from '../../Components/CustomizedTables'; import CustomizedTables from "../../Components/CustomizedTables";
import { Box, Typography, useTheme } from '@mui/material'; import { Box, Typography, useTheme } from "@mui/material";
import { useSelector } from "react-redux";
import { useParams } from "react-router-dom";
import axiosInstance from "../../Utils/axiosConfig";
/** /**
* Details page component displaying monitor details and related information. * Details page component displaying monitor details and related information.
* @component * @component
*/ */
const DetailsPage = () => { const DetailsPage = () => {
const [monitor, setMonitor] = useState({});
const { monitorId } = useParams();
const { authToken } = useSelector((state) => state.auth);
useEffect(() => {
const fetchMonitor = async () => {
const res = await axiosInstance.get(`/monitors/${monitorId}`, {
headers: {
Authorization: `Bearer ${authToken}`,
},
});
setMonitor(res.data.data);
};
fetchMonitor();
}, [monitorId, authToken]);
const theme = useTheme(); const theme = useTheme();
// Example monitor data (replace with actual data or fetch from API) // Example monitor data (replace with actual data or fetch from API)
const monitorData = {
"_id": "66863aa2437936b46e225d1f",
"userId": "66863a8f437936b46e225d1a",
"name": "Google",
"description": "Google",
"status": false,
"type": "http",
"url": "https://www.google.com/404",
"isActive": true,
"interval": 60000,
"createdAt": "2024-07-04T06:01:06.527Z",
"updatedAt": "2024-07-04T06:02:00.250Z",
"__v": 0,
"checks": [
{
"_id": "66863ad8437936b46e225d6d",
"monitorId": "66863aa2437936b46e225d1f",
"status": false,
"responseTime": 144,
"expiry": "2024-07-04T06:02:00.247Z",
"statusCode": 404,
"createdAt": "2024-07-04T06:02:00.248Z",
"updatedAt": "2024-07-04T06:02:00.248Z",
"__v": 0
},
{
"_id": "66863b14437936b46e225d79",
"monitorId": "66863aa2437936b46e225d1f",
"status": false,
"responseTime": 140,
"expiry": "2024-07-04T06:03:00.167Z",
"statusCode": 404,
"createdAt": "2024-07-04T06:03:00.168Z",
"updatedAt": "2024-07-04T06:03:00.168Z",
"__v": 0
}
]
};
/** /**
* Function to calculate uptime duration based on the most recent check. * Function to calculate uptime duration based on the most recent check.
* @param {Array} checks Array of check objects. * @param {Array} checks Array of check objects.
* @returns {string} Uptime duration in hours, minutes, and seconds. * @returns {string} Uptime duration in hours, minutes, and seconds.
* TODO - NEED TO REVISIT THIS FOR PROPER LOGIC. * TODO - NEED TO REVISIT THIS FOR PROPER LOGIC.
*/ */
const calculateUptimeDuration = (checks) => { const calculateUptimeDuration = (checks) => {
if (!checks || checks.length === 0) { if (!checks || checks.length === 0) {
return 'N/A'; return "N/A";
} }
const mostRecentCheck = checks[0]; const mostRecentCheck = checks[0];
@@ -68,9 +47,9 @@ const DetailsPage = () => {
// Calculate hours, minutes, and seconds from uptime duration // Calculate hours, minutes, and seconds from uptime duration
// TODO - NEED TO REVISIT THIS FOR PROPER LOGIC. // TODO - NEED TO REVISIT THIS FOR PROPER LOGIC.
let uptimeHours = Math.floor(uptimeDuration / (1000 * 60 * 60)); let uptimeHours = Math.floor(uptimeDuration / (1000 * 60 * 60));
uptimeDuration %= (1000 * 60 * 60); uptimeDuration %= 1000 * 60 * 60;
let uptimeMinutes = Math.floor(uptimeDuration / (1000 * 60)); let uptimeMinutes = Math.floor(uptimeDuration / (1000 * 60));
uptimeDuration %= (1000 * 60); uptimeDuration %= 1000 * 60;
let uptimeSeconds = Math.floor(uptimeDuration / 1000); let uptimeSeconds = Math.floor(uptimeDuration / 1000);
return `${uptimeHours}h ${uptimeMinutes}m ${uptimeSeconds}s`; return `${uptimeHours}h ${uptimeMinutes}m ${uptimeSeconds}s`;
@@ -84,7 +63,7 @@ const DetailsPage = () => {
*/ */
const getLastCheckedTimestamp = (checks) => { const getLastCheckedTimestamp = (checks) => {
if (!checks || checks.length === 0) { if (!checks || checks.length === 0) {
return 'N/A'; // Handle case when no checks are available return "N/A"; // Handle case when no checks are available
} }
const mostRecentCheck = checks[0]; const mostRecentCheck = checks[0];
@@ -104,57 +83,57 @@ const DetailsPage = () => {
// Count checks with status === false // Count checks with status === false
// TODO - NEED TO REVISIT THIS FOR PROPER LOGIC. // TODO - NEED TO REVISIT THIS FOR PROPER LOGIC.
const incidentCount = checks.filter(check => !check.status).length; const incidentCount = checks.filter((check) => !check.status).length;
return incidentCount; return incidentCount;
}; };
return ( return (
<Box> <Box>
{/* Customized Tables Component */} {/* Customized Tables Component */}
<CustomizedTables monitor={monitorData} /> <CustomizedTables monitor={monitor} />
{/* Uptime duration */} {/* Uptime duration */}
<Typography <Typography
variant="body1" variant="body1"
sx={{ sx={{
fontFamily: 'Roboto', fontFamily: "Roboto",
fontWeight: 600, fontWeight: 600,
fontSize: '13px', fontSize: "13px",
lineHeight: '20px', lineHeight: "20px",
color: '#344054', color: "#344054",
marginTop: theme.spacing(2), marginTop: theme.spacing(2),
}} }}
> >
Up for: {calculateUptimeDuration(monitorData.checks)} Up for: {calculateUptimeDuration(monitor.checks)}
</Typography> </Typography>
{/* Last checked */} {/* Last checked */}
<Typography <Typography
variant="body2" variant="body2"
sx={{ sx={{
fontFamily: 'Roboto', fontFamily: "Roboto",
fontWeight: 400, fontWeight: 400,
fontSize: '16px', fontSize: "16px",
lineHeight: '24px', lineHeight: "24px",
color: '#1570EF', color: "#1570EF",
marginBottom: theme.spacing(1), marginBottom: theme.spacing(1),
}} }}
> >
Last checked: {getLastCheckedTimestamp(monitorData.checks)} Last checked: {getLastCheckedTimestamp(monitor.checks)}
</Typography> </Typography>
{/* Incidents */} {/* Incidents */}
<Typography <Typography
variant="body1" variant="body1"
sx={{ sx={{
fontFamily: 'Roboto', fontFamily: "Roboto",
fontWeight: 600, fontWeight: 600,
fontSize: '13px', fontSize: "13px",
lineHeight: '20px', lineHeight: "20px",
color: '#344054', color: "#344054",
}} }}
> >
Incidents: {countIncidents(monitorData.checks)} Incidents: {countIncidents(monitor.checks)}
</Typography> </Typography>
</Box> </Box>
); );
+3 -1
View File
@@ -273,7 +273,9 @@ const getAllMonitors = async (req, res) => {
const getMonitorById = async (req, res) => { const getMonitorById = async (req, res) => {
try { try {
const monitor = await Monitor.findById(req.params.monitorId); const monitor = await Monitor.findById(req.params.monitorId);
const checks = await Check.find({ monitorId: monitor._id }); const checks = await Check.find({ monitorId: monitor._id }).sort({
createdAt: -1,
});
const monitorWithChecks = { ...monitor.toObject(), checks }; const monitorWithChecks = { ...monitor.toObject(), checks };
return monitorWithChecks; return monitorWithChecks;
} catch (error) { } catch (error) {