Added null entries

This commit is contained in:
Daniel Cojocea committed 2024-09-10 17:43:43 -04:00
1 parent 34f0a1e067
commit db3c7e6b52
2 files changed
+132 -17

No files matched your search

@@ -8,42 +8,144 @@ import {
} from "recharts"; } from "recharts";
import { useTheme } from "@emotion/react"; import { useTheme } from "@emotion/react";
import { useMemo } from "react"; import { useMemo } from "react";
import { Box, Stack, Typography } from "@mui/material";
import { formatDate } from "../../../../Utils/timeUtils";
const config = { const config = {
accessibility: { accessibility: {
id: "accessibility", id: "accessibility",
text: "accessibility",
color: "primary", color: "primary",
}, },
bestPractices: { bestPractices: {
id: "bestPractices", id: "bestPractices",
text: "best practices",
color: "warning", color: "warning",
}, },
performance: { performance: {
id: "performance", id: "performance",
text: "performance",
color: "success", color: "success",
}, },
seo: { seo: {
id: "seo", id: "seo",
text: "SEO",
color: "unresolved", color: "unresolved",
}, },
}; };
const PagespeedDetailsAreaChart = ({ data }) => { const CustomToolTip = ({ active, payload, label }) => {
const formatDate = (timestamp) => { const theme = useTheme();
const date = new Date(timestamp);
return date.toLocaleTimeString("en-US", { if (active && payload && payload.length) {
hour: "numeric", return (
minute: "2-digit", <Box
hour12: true, sx={{
backgroundColor: theme.palette.background.main,
border: 1,
borderColor: theme.palette.border.dark,
borderRadius: theme.shape.borderRadius,
py: theme.spacing(2),
px: theme.spacing(4),
}}
>
<Typography
sx={{
color: theme.palette.text.tertiary,
fontSize: 12,
fontWeight: 500,
}}
>
{formatDate(new Date(label))}
</Typography>
{Object.keys(config).map((key) => {
const { color } = config[key];
const dotColor = theme.palette[color].main;
return (
<Stack
key={`${key}-tooltip`}
direction="row"
alignItems="center"
gap={theme.spacing(3)}
mt={theme.spacing(1)}
sx={{
"& span": {
color: theme.palette.text.tertiary,
fontSize: 11,
fontWeight: 500,
},
}}
>
<Box
width={theme.spacing(4)}
height={theme.spacing(4)}
backgroundColor={dotColor}
sx={{ borderRadius: "50%" }}
/>
<Typography
component="span"
textTransform="capitalize"
sx={{ opacity: 0.8 }}
>
{config[key].text}
</Typography>{" "}
<Typography component="span">
{payload[0].payload[key]}
</Typography>
</Stack>
);
})}
</Box>
);
}
return null;
};
const processDataWithGaps = (data, interval) => {
if (data.length === 0) return [];
let formattedData = [];
let last = new Date(data[0].createdAt).getTime();
// Helper function to add a null entry
const addNullEntry = (timestamp) => {
formattedData.push({
accessibility: "N/A",
bestPractices: "N/A",
performance: "N/A",
seo: "N/A",
createdAt: timestamp,
}); });
}; };
const memoizedData = useMemo(() => data, [data[0]]); data.forEach((entry) => {
const current = new Date(entry.createdAt).getTime();
if (current - last > interval * 2) {
// Insert null entries for each interval
let temp = last + interval;
while (temp < current) {
addNullEntry(new Date(temp).toISOString());
temp += interval;
}
}
formattedData.push(entry);
last = current;
});
return formattedData;
};
const PagespeedDetailsAreaChart = ({ data, interval }) => {
const theme = useTheme(); const theme = useTheme();
const memoizedData = useMemo(
() => processDataWithGaps(data, interval),
[data]
);
return ( return (
<ResponsiveContainer width="100%" minWidth={25} height={220}> <ResponsiveContainer width="100%" minWidth={25} height={215}>
<AreaChart <AreaChart
width="100%" width="100%"
height="100%" height="100%"
@@ -60,17 +162,28 @@ const PagespeedDetailsAreaChart = ({ data }) => {
<XAxis <XAxis
stroke={theme.palette.border.dark} stroke={theme.palette.border.dark}
dataKey="createdAt" dataKey="createdAt"
tickFormatter={formatDate} tickFormatter={(timestamp) =>
formatDate(new Date(timestamp), {
year: undefined,
month: undefined,
day: undefined,
})
}
tick={{ tick={{
fontSize: 12, fontSize: 11,
fontWeight: 100, fontWeight: 100,
opacity: 0.8, opacity: 0.8,
stroke: theme.palette.text.tertiary, stroke: theme.palette.text.tertiary,
}} }}
tickLine={false} tickLine={false}
minTickGap={20}
height={18} height={18}
interval="preserveEnd"
/>
<Tooltip
cursor={{ stroke: theme.palette.border.light }}
content={<CustomToolTip />}
/> />
<Tooltip />
<defs> <defs>
{Object.values(config).map(({ id, color }) => { {Object.values(config).map(({ id, color }) => {
const startColor = theme.palette[color].main; const startColor = theme.palette[color].main;
@@ -87,17 +200,18 @@ const PagespeedDetailsAreaChart = ({ data }) => {
{Object.keys(config).map((key) => { {Object.keys(config).map((key) => {
const { color } = config[key]; const { color } = config[key];
const strokeColor = theme.palette[color].main; const strokeColor = theme.palette[color].main;
const activeDotColor = theme.palette[color].bg; const bgColor = theme.palette.background.main;
return ( return (
<Area <Area
connectNulls
key={key} key={key}
dataKey={key} dataKey={key}
stackId={1} stackId={1}
stroke={strokeColor} stroke={strokeColor}
strokeWidth={1.5} strokeWidth={1.5}
fill={`url(#${config[key].id})`} fill={`url(#${config[key].id})`}
activeDot={{ stroke: activeDotColor, r: 5 }} activeDot={{ stroke: bgColor, fill: strokeColor, r: 4.5 }}
/> />
); );
})} })}
+4 -3
View File
@@ -265,8 +265,6 @@ const PageSpeedDetails = () => {
let loading = Object.keys(monitor).length === 0; let loading = Object.keys(monitor).length === 0;
const data = monitor?.checks ? [...monitor.checks].reverse() : []; const data = monitor?.checks ? [...monitor.checks].reverse() : [];
console.log(data);
const splitDuration = (duration) => { const splitDuration = (duration) => {
const { time, format } = formatDurationSplit(duration); const { time, format } = formatDurationSplit(duration);
return ( return (
@@ -415,7 +413,10 @@ const PageSpeedDetails = () => {
</IconBox> </IconBox>
<Typography component="h2">Score history</Typography> <Typography component="h2">Score history</Typography>
</Stack> </Stack>
<PagespeedDetailsAreaChart data={data} /> <PagespeedDetailsAreaChart
data={data}
interval={monitor?.interval}
/>
</ChartBox> </ChartBox>
<ChartBox flex={1}> <ChartBox flex={1}>
<Stack direction="row" alignItems="center" gap={theme.spacing(6)}> <Stack direction="row" alignItems="center" gap={theme.spacing(6)}>