Skip to content

Commit

Permalink
refactor: update conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Siumauricio committed Dec 30, 2024
1 parent 69dac0f commit f9ffd04
Show file tree
Hide file tree
Showing 7 changed files with 287 additions and 190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,26 @@ import { Area, AreaChart, CartesianGrid, XAxis, YAxis } from "recharts";

interface ContainerMetric {
timestamp: string;
BlockIO: string;
BlockIO: {
read: number;
write: number;
readUnit: string;
writeUnit: string;
};
}

interface Props {
data: ContainerMetric[];
}

interface FormattedMetric {
timestamp: string;
read: number;
write: number;
readUnit: string;
writeUnit: string;
}

const chartConfig = {
read: {
label: "Read",
Expand All @@ -35,29 +48,22 @@ const chartConfig = {
},
} satisfies ChartConfig;

const parseBlockIO = (blockIO: string) => {
const [read, write] = blockIO.split(" / ");
return {
read: Number.parseFloat(read),
write: parseFloat(write),
readUnit: read?.replace(/[\d.]/g, ""),
writeUnit: write?.replace(/[\d.]/g, ""),
};
};

export const ContainerBlockChart = ({ data }: Props) => {
const formattedData = data.map((metric) => {
const { read, write, readUnit, writeUnit } = parseBlockIO(metric.BlockIO);
return {
timestamp: metric.timestamp,
read,
write,
readUnit,
writeUnit,
};
});
const formattedData = data.map((metric) => ({
timestamp: metric.timestamp,
read: metric.BlockIO.read,
write: metric.BlockIO.write,
readUnit: metric.BlockIO.readUnit,
writeUnit: metric.BlockIO.writeUnit,
}));

const latestData = formattedData[formattedData.length - 1] || {};
const latestData = formattedData[formattedData.length - 1] || {
timestamp: "",
read: 0,
write: 0,
readUnit: "B",
writeUnit: "B",
};

return (
<Card className="bg-transparent">
Expand Down Expand Up @@ -115,7 +121,7 @@ export const ContainerBlockChart = ({ data }: Props) => {
cursor={false}
content={({ active, payload, label }) => {
if (active && payload && payload.length) {
const data = payload[0].payload;
const data = payload?.[0]?.payload;
return (
<div className="rounded-lg border bg-background p-2 shadow-sm">
<div className="grid grid-cols-2 gap-2">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,125 +1,128 @@
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import {
ChartConfig,
ChartContainer,
ChartLegend,
ChartLegendContent,
ChartTooltip,
type ChartConfig,
ChartContainer,
ChartLegend,
ChartLegendContent,
ChartTooltip,
} from "@/components/ui/chart";
import { formatTimestamp } from "@/lib/utils";
import { Area, AreaChart, CartesianGrid, XAxis, YAxis } from "recharts";

interface ContainerMetric {
timestamp: string;
CPUPerc: string;
timestamp: string;
CPU: number;
}

interface Props {
data: ContainerMetric[];
data: ContainerMetric[];
}

const chartConfig = {
cpu: {
label: "CPU",
color: "hsl(var(--chart-1))",
},
cpu: {
label: "CPU",
color: "hsl(var(--chart-1))",
},
} satisfies ChartConfig;

export const ContainerCPUChart = ({ data }: Props) => {
const formattedData = data.map((metric) => ({
timestamp: metric.timestamp,
cpu: parseFloat(metric.CPUPerc.replace("%", "")),
}));
const formattedData = data.map((metric) => ({
timestamp: metric.timestamp,
cpu: metric.CPU,
}));

const latestData = formattedData[formattedData.length - 1] || {};
const latestData = formattedData[formattedData.length - 1] || {
timestamp: "",
cpu: 0,
};

return (
<Card className="bg-transparent">
<CardHeader className="border-b py-5">
<CardTitle>CPU</CardTitle>
<CardDescription>CPU Usage: {latestData.cpu}%</CardDescription>
</CardHeader>
<CardContent className="px-2 pt-4 sm:px-6 sm:pt-6">
<ChartContainer
config={chartConfig}
className="aspect-auto h-[250px] w-full"
>
<AreaChart data={formattedData}>
<defs>
<linearGradient id="fillCPU" x1="0" y1="0" x2="0" y2="1">
<stop
offset="5%"
stopColor="hsl(var(--chart-1))"
stopOpacity={0.8}
/>
<stop
offset="95%"
stopColor="hsl(var(--chart-1))"
stopOpacity={0.1}
/>
</linearGradient>
</defs>
<CartesianGrid vertical={false} />
<XAxis
dataKey="timestamp"
tickLine={false}
axisLine={false}
tickMargin={8}
minTickGap={32}
tickFormatter={(value) => formatTimestamp(value)}
/>
<YAxis tickFormatter={(value) => `${value}%`} domain={[0, 100]} />
<ChartTooltip
cursor={false}
content={({ active, payload, label }) => {
if (active && payload && payload.length) {
const data = payload[0].payload;
return (
<div className="rounded-lg border bg-background p-2 shadow-sm">
<div className="grid grid-cols-2 gap-2">
<div className="flex flex-col">
<span className="text-[0.70rem] uppercase text-muted-foreground">
Time
</span>
<span className="font-bold">
{formatTimestamp(label)}
</span>
</div>
<div className="flex flex-col">
<span className="text-[0.70rem] uppercase text-muted-foreground">
CPU
</span>
<span className="font-bold">{data.cpu}%</span>
</div>
</div>
</div>
);
}
return null;
}}
/>
<Area
name="CPU"
dataKey="cpu"
type="monotone"
fill="url(#fillCPU)"
stroke="hsl(var(--chart-1))"
strokeWidth={2}
/>
<ChartLegend
content={<ChartLegendContent />}
verticalAlign="bottom"
align="center"
/>
</AreaChart>
</ChartContainer>
</CardContent>
</Card>
);
return (
<Card className="bg-transparent">
<CardHeader className="border-b py-5">
<CardTitle>CPU</CardTitle>
<CardDescription>CPU Usage: {latestData.cpu}%</CardDescription>
</CardHeader>
<CardContent className="px-2 pt-4 sm:px-6 sm:pt-6">
<ChartContainer
config={chartConfig}
className="aspect-auto h-[250px] w-full"
>
<AreaChart data={formattedData}>
<defs>
<linearGradient id="fillCPU" x1="0" y1="0" x2="0" y2="1">
<stop
offset="5%"
stopColor="hsl(var(--chart-1))"
stopOpacity={0.8}
/>
<stop
offset="95%"
stopColor="hsl(var(--chart-1))"
stopOpacity={0.1}
/>
</linearGradient>
</defs>
<CartesianGrid vertical={false} />
<XAxis
dataKey="timestamp"
tickLine={false}
axisLine={false}
tickMargin={8}
minTickGap={32}
tickFormatter={(value) => formatTimestamp(value)}
/>
<YAxis tickFormatter={(value) => `${value}%`} domain={[0, 100]} />
<ChartTooltip
cursor={false}
content={({ active, payload, label }) => {
if (active && payload && payload.length) {
const data = payload?.[0]?.payload;
return (
<div className="rounded-lg border bg-background p-2 shadow-sm">
<div className="grid grid-cols-2 gap-2">
<div className="flex flex-col">
<span className="text-[0.70rem] uppercase text-muted-foreground">
Time
</span>
<span className="font-bold">
{formatTimestamp(label)}
</span>
</div>
<div className="flex flex-col">
<span className="text-[0.70rem] uppercase text-muted-foreground">
CPU
</span>
<span className="font-bold">{data.cpu}%</span>
</div>
</div>
</div>
);
}
return null;
}}
/>
<Area
name="CPU"
dataKey="cpu"
type="monotone"
fill="url(#fillCPU)"
stroke="hsl(var(--chart-1))"
strokeWidth={2}
/>
<ChartLegend
content={<ChartLegendContent />}
verticalAlign="bottom"
align="center"
/>
</AreaChart>
</ChartContainer>
</CardContent>
</Card>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ import { Area, AreaChart, CartesianGrid, XAxis, YAxis } from "recharts";

interface ContainerMetric {
timestamp: string;
MemPerc: string;
MemUsage: string;
Memory: {
percentage: number;
used: number;
total: number;
unit: string;
};
}

interface Props {
Expand All @@ -35,11 +39,15 @@ const chartConfig = {
export const ContainerMemoryChart = ({ data }: Props) => {
const formattedData = data.map((metric) => ({
timestamp: metric.timestamp,
memory: parseFloat(metric.MemPerc.replace("%", "")),
usage: metric.MemUsage,
memory: metric.Memory.percentage,
usage: `${metric.Memory.used} / ${metric.Memory.total} ${metric.Memory.unit}`,
}));

const latestData = formattedData[formattedData.length - 1] || {};
const latestData = formattedData[formattedData.length - 1] || {
timestamp: "",
memory: 0,
usage: "0 / 0 B",
};

return (
<Card className="bg-transparent">
Expand Down Expand Up @@ -81,7 +89,7 @@ export const ContainerMemoryChart = ({ data }: Props) => {
cursor={false}
content={({ active, payload, label }) => {
if (active && payload && payload.length) {
const data = payload[0].payload;
const data = payload?.[0]?.payload;
return (
<div className="rounded-lg border bg-background p-2 shadow-sm">
<div className="grid grid-cols-2 gap-2">
Expand Down
Loading

0 comments on commit f9ffd04

Please sign in to comment.