Add area charts, fix dummy data

This commit is contained in:
Alex Holliday committed 2024-11-19 13:35:00 +08:00
1 parent 6a0d61b73e
commit a0c73469fa
2 files changed
+109 -15

No files matched your search

+20 -15
View File
@@ -1,26 +1,30 @@
function generateMonitorEntries(monitorId, count = 10, options = {}) { function generateMonitorEntries(monitorId, count = 10, options = {}) {
const defaultOptions = { const defaultOptions = {
timeRange: { timeRange: {
start: new Date(Date.now() - 24 * 60 * 60 * 1000), // 24 hours ago start: new Date(Date.now() - 24 * 60 * 60 * 1000 * 20),
end: new Date(), end: new Date(),
}, },
statusVariation: [true, false], statusVariation: [true, false],
responseTimeRange: [50, 500], responseTimeRange: [50, 500],
cpuUsageRange: [0, 100], cpuUsageRange: [0, 1],
memoryUsageRange: [0, 100], memoryUsageRange: [0, 1],
diskUsageRange: [0, 100], diskUsageRange: [0, 1],
}; };
const mergedOptions = { ...defaultOptions, ...options }; const mergedOptions = { ...defaultOptions, ...options };
const startTime = mergedOptions.timeRange.start.getTime();
const endTime = mergedOptions.timeRange.end.getTime();
const totalTimeSpan = endTime - startTime;
return Array.from({ length: count }, (_, index) => { // Generate sorted random time points
const createdAt = new Date( const timePoints = Array.from({ length: count }, (_, index) => {
mergedOptions.timeRange.start.getTime() + // Use a non-linear distribution to create more varied spacing
(index * const progress = Math.pow(Math.random(), 2); // Bias towards earlier times
(mergedOptions.timeRange.end.getTime() - return startTime + progress * totalTimeSpan;
mergedOptions.timeRange.start.getTime())) / }).sort((a, b) => a - b);
count
); return timePoints.map((timestamp) => {
const createdAt = new Date(timestamp);
return { return {
_id: "123", _id: "123",
@@ -34,7 +38,7 @@ function generateMonitorEntries(monitorId, count = 10, options = {}) {
logical_core: randomInRange([4, 16]), logical_core: randomInRange([4, 16]),
frequency: randomInRange([10, 4000]), frequency: randomInRange([10, 4000]),
temperature: randomInRange([20, 90]), temperature: randomInRange([20, 90]),
free_percent: 100 - randomInRange(mergedOptions.cpuUsageRange), free_percent: 1 - randomInRange(mergedOptions.cpuUsageRange),
usage_percent: randomInRange(mergedOptions.cpuUsageRange), usage_percent: randomInRange(mergedOptions.cpuUsageRange),
_id: "123", _id: "123",
}, },
@@ -70,11 +74,12 @@ function generateMonitorEntries(monitorId, count = 10, options = {}) {
}); });
} }
// Helper functions // Modify randomInRange to work with decimal ranges
function randomInRange([min, max]) { function randomInRange([min, max]) {
return Math.floor(Math.random() * (max - min + 1)) + min; return Number((Math.random() * (max - min) + min).toFixed(2));
} }
// ... rest of the code remains the same
function randomFromArray(arr) { function randomFromArray(arr) {
return arr[Math.floor(Math.random() * arr.length)]; return arr[Math.floor(Math.random() * arr.length)];
} }
@@ -4,6 +4,12 @@ import Breadcrumbs from "../../../Components/Breadcrumbs";
import { Stack, Box, Typography } from "@mui/material"; import { Stack, Box, Typography } from "@mui/material";
import { useTheme } from "@emotion/react"; import { useTheme } from "@emotion/react";
import CustomGauge from "../../../Components/Charts/CustomGauge"; import CustomGauge from "../../../Components/Charts/CustomGauge";
import AreaChart from "../../../Components/Charts/AreaChart";
import {
TzTick,
PercentTick,
InfrastructureTooltip,
} from "../../../Components/Charts/Utils/chartUtils";
const bytesToGB = (bytes) => { const bytesToGB = (bytes) => {
if (typeof bytes !== "number") return 0; if (typeof bytes !== "number") return 0;
@@ -92,6 +98,7 @@ const InfrastructureDetails = () => {
{ name: "infrastructure monitors", path: "/infrastructure" }, { name: "infrastructure monitors", path: "/infrastructure" },
{ name: "details", path: `/infrastructure/${monitorId}` }, { name: "details", path: `/infrastructure/${monitorId}` },
]; ];
console.log(testData);
return ( return (
<Box> <Box>
@@ -160,6 +167,88 @@ const InfrastructureDetails = () => {
); );
})} })}
</Stack> </Stack>
<Stack
direction={"row"}
height={300}
gap={theme.spacing(2)}
flexWrap="wrap" // Better way to do this? FE team HELP!
sx={{
"& > *": {
flexBasis: `calc(50% - ${theme.spacing(2)})`,
maxWidth: `calc(50% - ${theme.spacing(2)})`,
},
}}
>
<AreaChart
data={testData}
dataKey="memory.usage_percent"
xKey="createdAt"
yKey="memory.usage_percent"
customTooltip={({ active, payload, label }) => (
<InfrastructureTooltip
label={label?.toString() ?? ""}
yKey="memory.usage_percent"
yLabel="Memory Usage"
active={active}
payload={payload}
/>
)}
xTick={<TzTick />}
yTick={<PercentTick />}
strokeColor={theme.palette.primary.main}
gradient={true}
gradientStartColor={theme.palette.primary.main}
gradientEndColor="#ffffff"
/>
<AreaChart
data={testData}
dataKey="cpu.usage_percent"
xKey="createdAt"
yKey="cpu.usage_percent"
customTooltip={({ active, payload, label }) => (
<InfrastructureTooltip
label={label?.toString() ?? ""}
yKey="cpu.usage_percent"
yLabel="CPU Usage"
active={active}
payload={payload}
/>
)}
xTick={<TzTick />}
yTick={<PercentTick />}
strokeColor={theme.palette.primary.main}
gradient={true}
gradientStartColor={theme.palette.primary.main}
gradientEndColor="#ffffff"
/>
{latestCheck.disk.map((disk, idx) => {
return (
<AreaChart
key={disk._id}
data={testData}
dataKey={`disk[${idx}].usage_percent`}
xKey="createdAt"
yKey={`disk[${idx}].usage_percent`}
customTooltip={({ active, payload, label }) => (
<InfrastructureTooltip
label={label?.toString() ?? ""}
yKey={`disk.usage_percent`}
yLabel="Disk Usage"
yIdx={idx}
active={active}
payload={payload}
/>
)}
xTick={<TzTick />}
yTick={<PercentTick />}
strokeColor={theme.palette.primary.main}
gradient={true}
gradientStartColor={theme.palette.primary.main}
gradientEndColor="#ffffff"
/>
);
})}
</Stack>
</Stack> </Stack>
</Box> </Box>
); );