-
-
Notifications
You must be signed in to change notification settings - Fork 755
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
69dac0f
commit f9ffd04
Showing
7 changed files
with
287 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
213 changes: 108 additions & 105 deletions
213
apps/dokploy/components/dashboard/monitoring/container/container-cpu-chart.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.