Added color range to uptime chart

This commit is contained in:
Daniel Cojocea committed 2024-09-04 16:41:23 -04:00
1 parent 6bc25fc7da
commit 38fddf5b9b
5 files changed
+62 -84

No files matched your search

+2 -2
View File
@@ -41,8 +41,8 @@ const PulseDot = ({ color }) => {
"&::after": { "&::after": {
content: `""`, content: `""`,
position: "absolute", position: "absolute",
width: "6px", width: "7px",
height: "6px", height: "7px",
borderRadius: "50%", borderRadius: "50%",
backgroundColor: "white", backgroundColor: "white",
top: "50%", top: "50%",
@@ -44,6 +44,14 @@ export const UpBarChart = ({ data, type, onBarHover }) => {
const [chartHovered, setChartHovered] = useState(false); const [chartHovered, setChartHovered] = useState(false);
const [hoveredBarIndex, setHoveredBarIndex] = useState(null); const [hoveredBarIndex, setHoveredBarIndex] = useState(null);
const getColorRange = (uptime) => {
return uptime > 80
? { main: theme.palette.success.main, light: theme.palette.success.light }
: uptime > 50
? { main: theme.palette.warning.main, light: theme.palette.warning.light }
: { main: theme.palette.error.text, light: theme.palette.error.light };
};
return ( return (
<ResponsiveContainer width="100%" height={155}> <ResponsiveContainer width="100%" height={155}>
<BarChart <BarChart
@@ -52,7 +60,7 @@ export const UpBarChart = ({ data, type, onBarHover }) => {
data={data} data={data}
onMouseEnter={() => { onMouseEnter={() => {
setChartHovered(true); setChartHovered(true);
onBarHover({ time: null, totalChecks: null }); onBarHover({ time: null, totalChecks: 0, uptimePercentage: 0 });
}} }}
onMouseLeave={() => { onMouseLeave={() => {
setChartHovered(false); setChartHovered(false);
@@ -78,29 +86,32 @@ export const UpBarChart = ({ data, type, onBarHover }) => {
/> />
<Bar <Bar
dataKey="totalChecks" dataKey="totalChecks"
barSize={5} maxBarSize={7}
background={{ fill: "transparent" }} background={{ fill: "transparent" }}
> >
{data.map((entry, index) => ( {data.map((entry, index) => {
<Cell let { main, light } = getColorRange(entry.uptimePercentage);
key={`cell-${index}`} return (
fill={ <Cell
hoveredBarIndex === index key={`cell-${index}`}
? theme.palette.success.main fill={
: chartHovered hoveredBarIndex === index ? main : chartHovered ? light : main
? theme.palette.success.light }
: theme.palette.success.main onMouseEnter={() => {
} setHoveredBarIndex(index);
onMouseEnter={() => { onBarHover(entry);
setHoveredBarIndex(index); }}
onBarHover(entry); onMouseLeave={() => {
}} setHoveredBarIndex(null);
onMouseLeave={() => { onBarHover({
setHoveredBarIndex(null); time: null,
onBarHover({ time: null, totalChecks: null }); totalChecks: 0,
}} uptimePercentage: 0,
/> });
))} }}
/>
);
})}
</Bar> </Bar>
</BarChart> </BarChart>
</ResponsiveContainer> </ResponsiveContainer>
@@ -121,7 +132,7 @@ export const DownBarChart = ({ data, type, onBarHover }) => {
data={data} data={data}
onMouseEnter={() => { onMouseEnter={() => {
setChartHovered(true); setChartHovered(true);
onBarHover({ time: null, totalIncidents: null }); onBarHover({ time: null, totalIncidents: 0 });
}} }}
onMouseLeave={() => { onMouseLeave={() => {
setChartHovered(false); setChartHovered(false);
@@ -147,7 +158,7 @@ export const DownBarChart = ({ data, type, onBarHover }) => {
/> />
<Bar <Bar
dataKey="totalIncidents" dataKey="totalIncidents"
barSize={5} maxBarSize={7}
background={{ fill: "transparent" }} background={{ fill: "transparent" }}
> >
{data.map((entry, index) => ( {data.map((entry, index) => (
@@ -166,7 +177,7 @@ export const DownBarChart = ({ data, type, onBarHover }) => {
}} }}
onMouseLeave={() => { onMouseLeave={() => {
setHoveredBarIndex(null); setHoveredBarIndex(null);
onBarHover({ time: null, totalIncidents: null }); onBarHover({ time: null, totalIncidents: 0 });
}} }}
/> />
))} ))}
+20 -56
View File
@@ -118,57 +118,9 @@ const DetailsPage = ({ isAdmin }) => {
let loading = Object.keys(monitor).length === 0; let loading = Object.keys(monitor).length === 0;
const [aggregateStats, setAggregateStats] = useState({});
useEffect(() => {
const fetchAggregateStats = async () => {
try {
const res = await networkService.getAggregateStatsById(
authToken,
monitorId,
dateRange
);
setAggregateStats(res?.data?.data);
} catch (error) {
console.error(error);
}
};
fetchAggregateStats();
}, [monitorId, dateRange]);
const calculateStats = () => {
let totalChecks = 0;
let totalIncidents = 0;
let totalResponseTime = 0;
Object.keys(aggregateStats).forEach((entry) => {
totalChecks += aggregateStats[entry].totalChecks;
totalIncidents += aggregateStats[entry].totalIncidents;
totalResponseTime += aggregateStats[entry].avgResponseTime;
});
let uptime =
dateRange === "day"
? monitor.uptime24Hours
: dateRange === "week"
? monitor.uptime7Days
: dateRange === "month" && monitor.uptime30Days;
return {
totalChecks: totalChecks,
totalIncidents: totalIncidents,
averageResponseTime:
Math.floor((totalResponseTime / aggregateStats.length) * 100) / 100,
uptime: Math.floor(uptime),
};
};
const monitorStats = calculateStats();
const [hoveredUptimeData, setHoveredUptimeData] = useState(null); const [hoveredUptimeData, setHoveredUptimeData] = useState(null);
const [hoveredIncidentsData, setHoveredIncidentsData] = useState(null); const [hoveredIncidentsData, setHoveredIncidentsData] = useState(null);
const statusColor = { const statusColor = {
true: theme.palette.success.main, true: theme.palette.success.main,
false: theme.palette.error.main, false: theme.palette.error.main,
@@ -181,6 +133,8 @@ const DetailsPage = ({ isAdmin }) => {
undefined: "Pending...", undefined: "Pending...",
}; };
console.log(monitor);
return ( return (
<Box className="monitor-details"> <Box className="monitor-details">
{loading ? ( {loading ? (
@@ -339,7 +293,13 @@ const DetailsPage = ({ isAdmin }) => {
<Stack direction="row" gap={theme.spacing(8)}> <Stack direction="row" gap={theme.spacing(8)}>
<StatBox <StatBox
sx={ sx={
monitor?.status monitor?.status === undefined
? {
backgroundColor: theme.palette.warning.light,
borderColor: theme.palette.warning.border,
"& h2": { color: theme.palette.warning.main },
}
: monitor?.status
? { ? {
backgroundColor: theme.palette.success.bg, backgroundColor: theme.palette.success.bg,
borderColor: theme.palette.success.light, borderColor: theme.palette.success.light,
@@ -426,8 +386,8 @@ const DetailsPage = ({ isAdmin }) => {
<Typography>Total Checks</Typography> <Typography>Total Checks</Typography>
<Typography component="span"> <Typography component="span">
{hoveredUptimeData !== null {hoveredUptimeData !== null
? hoveredUptimeData.totalChecks || 0 ? hoveredUptimeData.totalChecks
: monitorStats.totalChecks} : monitor?.periodTotalChecks}
</Typography> </Typography>
{hoveredUptimeData !== null && {hoveredUptimeData !== null &&
hoveredUptimeData.time !== null && ( hoveredUptimeData.time !== null && (
@@ -450,13 +410,17 @@ const DetailsPage = ({ isAdmin }) => {
<Box> <Box>
<Typography>Uptime Percentage</Typography> <Typography>Uptime Percentage</Typography>
<Typography component="span"> <Typography component="span">
{monitorStats.uptime} {hoveredUptimeData !== null
? Math.floor(
hoveredUptimeData.uptimePercentage * 10
) / 10
: Math.floor(monitor?.periodUptime * 10) / 10}
<Typography component="span">%</Typography> <Typography component="span">%</Typography>
</Typography> </Typography>
</Box> </Box>
</Stack> </Stack>
<UpBarChart <UpBarChart
data={aggregateStats} data={monitor?.aggregateData}
type={dateRange} type={dateRange}
onBarHover={setHoveredUptimeData} onBarHover={setHoveredUptimeData}
/> />
@@ -472,8 +436,8 @@ const DetailsPage = ({ isAdmin }) => {
<Typography>Total Incidents</Typography> <Typography>Total Incidents</Typography>
<Typography component="span"> <Typography component="span">
{hoveredIncidentsData !== null {hoveredIncidentsData !== null
? hoveredIncidentsData.totalIncidents || 0 ? hoveredIncidentsData.totalIncidents
: monitorStats.totalIncidents} : monitor?.periodIncidents}
</Typography> </Typography>
{hoveredIncidentsData !== null && {hoveredIncidentsData !== null &&
hoveredIncidentsData.time !== null && ( hoveredIncidentsData.time !== null && (
@@ -494,7 +458,7 @@ const DetailsPage = ({ isAdmin }) => {
)} )}
</Box> </Box>
<DownBarChart <DownBarChart
data={aggregateStats} data={monitor?.aggregateData}
type={dateRange} type={dateRange}
onBarHover={setHoveredIncidentsData} onBarHover={setHoveredIncidentsData}
/> />
@@ -33,6 +33,9 @@ export const ChartBox = styled(Stack)(({ theme }) => ({
flexDirection: "row", flexDirection: "row",
gap: theme.spacing(6), gap: theme.spacing(6),
}, },
"& .MuiStack-root:first-of-type": {
alignItems: "center",
},
"& tspan, & text": { "& tspan, & text": {
fontSize: 11, fontSize: 11,
fill: theme.palette.text.tertiary, fill: theme.palette.text.tertiary,
+1 -1
View File
@@ -213,7 +213,7 @@ const PageSpeedDetails = () => {
monitorId, monitorId,
"desc", "desc",
50, 50,
null, "day",
null, null,
null null
); );