Merge pull request #360 from bluewave-labs/feat/monitor-details-table

Add status label and StatBox component
This commit is contained in:
Alexander Holliday authored and GitHub committed 2024-07-17 12:36:45 -07:00
commit e68e956b43
2 files changed
+90 -32

No files matched your search

+81 -21
View File
@@ -1,9 +1,38 @@
import React, { useEffect, useState } from "react"; import React, { useEffect, useState } from "react";
import PropTypes from "prop-types";
import { Box, Typography, useTheme } from "@mui/material"; import { Box, Typography, useTheme } from "@mui/material";
import { useSelector } from "react-redux"; import { useSelector } from "react-redux";
import { useParams } from "react-router-dom"; import { useParams } from "react-router-dom";
import axiosInstance from "../../Utils/axiosConfig"; import axiosInstance from "../../Utils/axiosConfig";
import BasicTable from "../../Components/BasicTable"; import BasicTable from "../../Components/BasicTable";
import StatusLabel from "../../Components/StatusLabel";
const formatDuration = (ms) => {
const seconds = Math.floor(ms / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
let dateStr = "";
days && (dateStr += `${days}d `);
hours && (dateStr += `${hours % 24}h `);
minutes && (dateStr += `${minutes % 60}m `);
seconds && (dateStr += `${seconds % 60}s `);
dateStr === "" && (dateStr = "0s");
return dateStr;
};
const StatBox = ({ title, value }) => {
return (
<Box>
<Typography variant="h6">{title}</Typography>
<Typography variant="h4">{value}</Typography>
</Box>
);
};
/** /**
* Details page component displaying monitor details and related information. * Details page component displaying monitor details and related information.
@@ -29,10 +58,22 @@ const DetailsPage = () => {
{ id: 3, name: "Message" }, { id: 3, name: "Message" },
], ],
rows: res.data.data.checks.map((check, idx) => { rows: res.data.data.checks.map((check, idx) => {
const params = {
status: check.status === true ? "up" : "down",
backgroundColor:
check.status === true
? "var(--env-var-color-20)"
: "var(--env-var-color-21)",
statusDotColor:
check.status === true
? "var(--env-var-color-17)"
: "var(--env-var-color-19)",
};
return { return {
id: check._id, id: check._id,
data: [ data: [
{ id: idx, data: check.status ? "Up" : "Down" }, { id: idx, data: <StatusLabel params={params} /> },
{ id: idx + 1, data: new Date(check.createdAt).toLocaleString() }, { id: idx + 1, data: new Date(check.createdAt).toLocaleString() },
{ id: idx + 2, data: check.statusCode }, { id: idx + 2, data: check.statusCode },
], ],
@@ -52,43 +93,47 @@ const DetailsPage = () => {
* @param {Array} checks Array of check objects. * @param {Array} checks Array of check objects.
* @returns {number} Uptime duration in ms. * @returns {number} Uptime duration in ms.
*/ */
// TODO: This can be done more efficiently by iteratting backwards
// and breaking when the first down check is found, calculate current time - downtime
const calculateUptimeDuration = (checks) => { const calculateUptimeDuration = (checks) => {
if (!checks || checks.length === 0) { if (!checks || checks.length === 0) {
return 0; return 0;
} }
let longestDuration = 0; const arr = [...checks];
let lastDownTimestamp = null;
let currentDuration = longestDuration;
// Loop over the checks to find the most recent uptime duration let lastDownTimestamp = null;
checks.forEach((check) => { let lastUpTimestamp = null;
arr.sort((a, b) => new Date(a.createdAt) - new Date(b.createdAt));
arr.forEach((check) => {
if (check.status === false) { if (check.status === false) {
lastDownTimestamp = check.createdAt; lastDownTimestamp = new Date(check.createdAt);
} else if (check.status === true && lastDownTimestamp) { } else if (check.status === true) {
currentDuration = check.createdAt - lastDownTimestamp; lastUpTimestamp = new Date(check.createdAt);
if (currentDuration > longestDuration) {
longestDuration = currentDuration;
}
lastDownTimestamp = null;
} }
}); });
lastDownTimestamp = null;
return longestDuration; if (lastDownTimestamp === null) {
return new Date() - new Date(arr[0].createdAt);
} else if (lastUpTimestamp === null) {
return 0;
} else if (lastDownTimestamp && lastUpTimestamp) {
return lastUpTimestamp - lastDownTimestamp;
}
}; };
/** /**
* Helper function to get timestamp of the most recent check. * Helper function to get duration since last check
* @param {Array} checks Array of check objects. * @param {Array} checks Array of check objects.
* @returns {number} Timestamp of the most recent check. * @returns {number} Timestamp of the most recent check.
*/ */
const getLastCheckedTimestamp = (checks) => { const getLastChecked = (checks) => {
if (!checks || checks.length === 0) { if (!checks || checks.length === 0) {
return 0; // Handle case when no checks are available return 0; // Handle case when no checks are available
} }
return new Date() - new Date(checks[0].createdAt);
const mostRecentCheck = checks[0];
return mostRecentCheck.createdAt;
}; };
/** /**
@@ -101,15 +146,30 @@ const DetailsPage = () => {
return 0; // Handle case when no checks are available return 0; // Handle case when no checks are available
} }
return checks.reduce((acc, check) => { return checks.reduce((acc, check) => {
check.status === false ? (acc += 1) : acc; return check.status === false ? (acc += 1) : acc;
}, 0); }, 0);
}; };
return ( return (
<div> <div>
<div style={{ display: "flex", justifyContent: "space-between" }}>
<StatBox
title="Currently up for"
value={formatDuration(calculateUptimeDuration(monitor.checks))}
/>
<StatBox
title="Last checked"
value={`${formatDuration(getLastChecked(monitor.checks))} ago`}
/>
<StatBox title="Incidents" value={countIncidents(monitor.checks)} />
</div>
<BasicTable data={data} paginated={true} /> <BasicTable data={data} paginated={true} />
</div> </div>
); );
}; };
StatBox.propTypes = {
title: PropTypes.string,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
};
export default DetailsPage; export default DetailsPage;
+9 -11
View File
@@ -13,15 +13,13 @@ import { injectStore } from "./Utils/axiosConfig.js";
injectStore(store); injectStore(store);
ReactDOM.createRoot(document.getElementById("root")).render( ReactDOM.createRoot(document.getElementById("root")).render(
<React.StrictMode> <Provider store={store}>
<Provider store={store}> <PersistGate loading={null} persistor={persistor}>
<PersistGate loading={null} persistor={persistor}> <ThemeProvider theme={theme}>
<ThemeProvider theme={theme}> <Router>
<Router> <App />
<App /> </Router>
</Router> </ThemeProvider>
</ThemeProvider> </PersistGate>
</PersistGate> </Provider>
</Provider>
</React.StrictMode>
); );