Refactored ChartUtils to allow for variable normalization parameters, Added day, week, and month filters to Details chart

This commit is contained in:
Alex Holliday committed 2024-07-17 23:00:24 -07:00
1 parent 07719aad2b
commit 9cb2abd62a
4 files changed
+91 -26

No files matched your search

@@ -1,15 +1,33 @@
import PropTypes from "prop-types";
import {
AreaChart,
Area,
XAxis,
CartesianGrid,
Tooltip,
ResponsiveContainer,
} from "recharts";
import { AreaChart, Area, XAxis, Tooltip, ResponsiveContainer } from "recharts";
import { NormalizeData } from "../ChartUtils";
const MonitorDetailsAreaChart = ({ checks }) => {
const CustomToolTip = ({ active, payload, label }) => {
if (active && payload && payload.length) {
return (
<div style={{ backgroundColor: "white" }}>
<p>
{new Date(label).toLocaleDateString("en-US", {
weekday: "short", // Mon
month: "long", // July
day: "numeric", // 17
}) +
", " +
new Date(label).toLocaleTimeString("en-US", {
hour: "numeric", // 12
minute: "2-digit", // 15
hour12: true, // AM/PM format
})}
</p>
<p>Response Time (ms): {payload[0].payload.originalResponseTime}</p>{" "}
{/* Display original value */}
</div>
);
}
return null;
};
const MonitorDetailsAreaChart = ({ checks, filter }) => {
// SQUASH ERROR, NOT PERMANENT SOLUTION
const error = console.error;
console.error = (...args) => {
@@ -28,9 +46,30 @@ const MonitorDetailsAreaChart = ({ checks }) => {
});
};
const filterChecks = (checks, filter) => {
const limits = {
day: 24 * 60 * 60 * 1000,
week: 24 * 60 * 60 * 1000 * 7,
month: 24 * 60 * 60 * 1000 * 30, //TODO better monthly calculations
};
const now = new Date().getTime();
const result = [];
for (let i = 0; i < checks.length; i++) {
const checkTime = new Date(checks[i].createdAt).getTime();
if (now - checkTime < limits[filter]) {
result.push(checks[i]);
} else {
break;
}
}
return result;
};
let normalizedChecks = [];
if (checks && checks.length > 0) {
normalizedChecks = NormalizeData(checks);
const filteredChecks = filterChecks(checks, filter);
normalizedChecks = NormalizeData(filteredChecks, 33, 100);
}
return (
@@ -51,7 +90,7 @@ const MonitorDetailsAreaChart = ({ checks }) => {
tickFormatter={formatDate}
tick={{ fontSize: "13px" }}
/>
<Tooltip />
<Tooltip content={<CustomToolTip />} />
<Area
type="monotone"
dataKey="responseTime"
@@ -65,5 +104,18 @@ const MonitorDetailsAreaChart = ({ checks }) => {
MonitorDetailsAreaChart.propTypes = {
checks: PropTypes.array,
filter: PropTypes.string,
};
CustomToolTip.propTypes = {
active: PropTypes.bool,
payload: PropTypes.arrayOf(
PropTypes.shape({
payload: PropTypes.shape({
originalResponseTime: PropTypes.number.isRequired,
}).isRequired,
})
),
label: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
};
export default MonitorDetailsAreaChart;