Files
Checkmate/Client/src/Pages/Details/index.jsx
T

144 lines
4.1 KiB
React
Raw Normal View History

import React, { useEffect, useState } from "react";
2024-07-10 10:10:02 -07:00
import CustomizedTables from "../../Components/CustomizedTables";
import { Box, Typography, useTheme } from "@mui/material";
import { useSelector } from "react-redux";
import { useParams } from "react-router-dom";
import axiosInstance from "../../Utils/axiosConfig";
2024-07-05 21:24:59 -07:00
/**
* Details page component displaying monitor details and related information.
* @component
*/
const DetailsPage = () => {
const [monitor, setMonitor] = useState({});
2024-07-10 10:10:02 -07:00
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]);
2024-07-10 10:10:02 -07:00
const theme = useTheme();
2024-07-05 21:24:59 -07:00
// Example monitor data (replace with actual data or fetch from API)
/**
* Function to calculate uptime duration based on the most recent check.
* @param {Array} checks Array of check objects.
2024-07-15 22:28:03 -07:00
* @returns {number} Uptime duration in ms.
2024-07-05 21:24:59 -07:00
*/
const calculateUptimeDuration = (checks) => {
if (!checks || checks.length === 0) {
2024-07-15 22:28:03 -07:00
return 0;
}
2024-07-15 22:28:03 -07:00
let longestDuration = 0;
let lastDownTimestamp = null;
let currentDuration = longestDuration;
2024-07-15 22:28:03 -07:00
// Loop over the checks to find the most recent uptime duration
checks.forEach((check) => {
if (check.status === false) {
lastDownTimestamp = check.createdAt;
} else if (check.status === true && lastDownTimestamp) {
currentDuration = check.createdAt - lastDownTimestamp;
if (currentDuration > longestDuration) {
longestDuration = currentDuration;
}
lastDownTimestamp = null;
}
});
return longestDuration;
};
2024-07-05 21:24:59 -07:00
/**
* Helper function to get timestamp of the most recent check.
* @param {Array} checks Array of check objects.
* @returns {string} Timestamp of the most recent check.
2024-07-09 17:35:00 -07:00
* TODO - NEED TO REVISIT THIS FOR PROPER LOGIC.
2024-07-05 21:24:59 -07:00
*/
const getLastCheckedTimestamp = (checks) => {
if (!checks || checks.length === 0) {
2024-07-10 10:10:02 -07:00
return "N/A"; // Handle case when no checks are available
}
const mostRecentCheck = checks[0];
return new Date(mostRecentCheck.createdAt).toLocaleString();
};
2024-07-05 21:24:59 -07:00
/**
* Helper function to count incidents (checks with status === false).
* @param {Array} checks Array of check objects.
* @returns {number} Number of incidents.
2024-07-09 17:35:00 -07:00
* TODO - NEED TO REVISIT THIS FOR PROPER LOGIC.
2024-07-05 21:24:59 -07:00
*/
const countIncidents = (checks) => {
if (!checks || checks.length === 0) {
2024-07-05 21:24:59 -07:00
return 0; // Handle case when no checks are available
}
2024-07-05 21:24:59 -07:00
// Count checks with status === false
2024-07-09 17:35:00 -07:00
// TODO - NEED TO REVISIT THIS FOR PROPER LOGIC.
2024-07-10 10:10:02 -07:00
const incidentCount = checks.filter((check) => !check.status).length;
return incidentCount;
};
2024-07-03 20:17:28 -07:00
return (
<Box>
2024-07-05 21:24:59 -07:00
{/* Customized Tables Component */}
2024-07-10 10:10:02 -07:00
<CustomizedTables monitor={monitor} />
2024-07-05 21:24:59 -07:00
{/* Uptime duration */}
<Typography
variant="body1"
sx={{
2024-07-10 10:10:02 -07:00
fontFamily: "Roboto",
fontWeight: 600,
2024-07-10 10:10:02 -07:00
fontSize: "13px",
lineHeight: "20px",
color: "#344054",
marginTop: theme.spacing(2),
}}
>
2024-07-10 10:10:02 -07:00
Up for: {calculateUptimeDuration(monitor.checks)}
</Typography>
2024-07-05 21:24:59 -07:00
{/* Last checked */}
<Typography
variant="body2"
sx={{
2024-07-10 10:10:02 -07:00
fontFamily: "Roboto",
fontWeight: 400,
2024-07-10 10:10:02 -07:00
fontSize: "16px",
lineHeight: "24px",
color: "#1570EF",
marginBottom: theme.spacing(1),
}}
>
2024-07-10 10:10:02 -07:00
Last checked: {getLastCheckedTimestamp(monitor.checks)}
</Typography>
2024-07-05 21:24:59 -07:00
{/* Incidents */}
<Typography
variant="body1"
sx={{
2024-07-10 10:10:02 -07:00
fontFamily: "Roboto",
fontWeight: 600,
2024-07-10 10:10:02 -07:00
fontSize: "13px",
lineHeight: "20px",
color: "#344054",
}}
>
2024-07-10 10:10:02 -07:00
Incidents: {countIncidents(monitor.checks)}
</Typography>
</Box>
2024-07-03 20:17:28 -07:00
);
2024-07-05 19:51:07 -07:00
};
export default DetailsPage;