From db3c7e6b525e45b26efc41af04a27598e98e8e5c Mon Sep 17 00:00:00 2001 From: Daniel Cojocea Date: Tue, 10 Sep 2024 17:43:43 -0400 Subject: [PATCH] Added null entries --- .../PageSpeed/Details/Charts/AreaChart.jsx | 142 ++++++++++++++++-- Client/src/Pages/PageSpeed/Details/index.jsx | 7 +- 2 files changed, 132 insertions(+), 17 deletions(-) diff --git a/Client/src/Pages/PageSpeed/Details/Charts/AreaChart.jsx b/Client/src/Pages/PageSpeed/Details/Charts/AreaChart.jsx index 1c5c8df8a..6c26c43bd 100644 --- a/Client/src/Pages/PageSpeed/Details/Charts/AreaChart.jsx +++ b/Client/src/Pages/PageSpeed/Details/Charts/AreaChart.jsx @@ -8,42 +8,144 @@ import { } from "recharts"; import { useTheme } from "@emotion/react"; import { useMemo } from "react"; +import { Box, Stack, Typography } from "@mui/material"; +import { formatDate } from "../../../../Utils/timeUtils"; const config = { accessibility: { id: "accessibility", + text: "accessibility", color: "primary", }, bestPractices: { id: "bestPractices", + text: "best practices", color: "warning", }, performance: { id: "performance", + text: "performance", color: "success", }, seo: { id: "seo", + text: "SEO", color: "unresolved", }, }; -const PagespeedDetailsAreaChart = ({ data }) => { - const formatDate = (timestamp) => { - const date = new Date(timestamp); - return date.toLocaleTimeString("en-US", { - hour: "numeric", - minute: "2-digit", - hour12: true, +const CustomToolTip = ({ active, payload, label }) => { + const theme = useTheme(); + + if (active && payload && payload.length) { + return ( + + + {formatDate(new Date(label))} + + {Object.keys(config).map((key) => { + const { color } = config[key]; + const dotColor = theme.palette[color].main; + + return ( + + + + {config[key].text} + {" "} + + {payload[0].payload[key]} + + + ); + })} + + ); + } + 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 memoizedData = useMemo( + () => processDataWithGaps(data, interval), + [data] + ); return ( - + { + formatDate(new Date(timestamp), { + year: undefined, + month: undefined, + day: undefined, + }) + } tick={{ - fontSize: 12, + fontSize: 11, fontWeight: 100, opacity: 0.8, stroke: theme.palette.text.tertiary, }} tickLine={false} + minTickGap={20} height={18} + interval="preserveEnd" + /> + } /> - {Object.values(config).map(({ id, color }) => { const startColor = theme.palette[color].main; @@ -87,17 +200,18 @@ const PagespeedDetailsAreaChart = ({ data }) => { {Object.keys(config).map((key) => { const { color } = config[key]; const strokeColor = theme.palette[color].main; - const activeDotColor = theme.palette[color].bg; + const bgColor = theme.palette.background.main; return ( ); })} diff --git a/Client/src/Pages/PageSpeed/Details/index.jsx b/Client/src/Pages/PageSpeed/Details/index.jsx index 5fdc186b2..657ecc36d 100644 --- a/Client/src/Pages/PageSpeed/Details/index.jsx +++ b/Client/src/Pages/PageSpeed/Details/index.jsx @@ -265,8 +265,6 @@ const PageSpeedDetails = () => { let loading = Object.keys(monitor).length === 0; const data = monitor?.checks ? [...monitor.checks].reverse() : []; - console.log(data); - const splitDuration = (duration) => { const { time, format } = formatDurationSplit(duration); return ( @@ -415,7 +413,10 @@ const PageSpeedDetails = () => { Score history - +