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

111 lines
3.3 KiB
React
Raw Normal View History

import React, { useEffect, useState } from "react";
2024-07-10 10:10:02 -07:00
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-16 14:05:38 -07:00
import BasicTable from "../../Components/BasicTable";
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-16 14:05:38 -07:00
const [data, setData] = 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);
2024-07-16 14:05:38 -07:00
const data = {
cols: [
{ id: 1, name: "Status" },
{ id: 2, name: "Date & Time" },
{ id: 3, name: "Message" },
],
rows: res.data.data.checks.map((check, idx) => {
return {
id: check._id,
data: [
{ id: idx, data: check.status ? "Up" : "Down" },
{ id: idx + 1, data: new Date(check.createdAt).toLocaleString() },
{ id: idx + 2, data: check.statusCode },
],
};
}),
};
setData(data);
};
fetchMonitor();
}, [monitorId, authToken]);
2024-07-10 10:10:02 -07:00
const theme = useTheme();
2024-07-05 21:24:59 -07:00
/**
* 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;
}
});
2024-07-16 10:02:48 -07:00
lastDownTimestamp = null;
2024-07-15 22:28:03 -07:00
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.
2024-07-16 10:07:04 -07:00
* @returns {number} Timestamp of the most recent check.
2024-07-05 21:24:59 -07:00
*/
const getLastCheckedTimestamp = (checks) => {
if (!checks || checks.length === 0) {
2024-07-16 10:07:04 -07:00
return 0; // Handle case when no checks are available
}
const mostRecentCheck = checks[0];
2024-07-16 10:07:04 -07:00
return mostRecentCheck.createdAt;
};
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.
*/
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-16 10:07:04 -07:00
return checks.reduce((acc, check) => {
check.status === false ? (acc += 1) : acc;
}, 0);
};
2024-07-16 14:05:38 -07:00
return <BasicTable data={data} paginated={false} />;
2024-07-05 19:51:07 -07:00
};
export default DetailsPage;