Skip to content

Commit b3e7a8a

Browse files
committed
fix: parse Block I/O and Network I/O stats to numeric MB so monitoring charts render
1 parent 309dd56 commit b3e7a8a

5 files changed

Lines changed: 57 additions & 12 deletions

File tree

‎apps/dokploy/components/dashboard/monitoring/free/container/docker-block-chart.tsx‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { formatMb, toMb } from "@dokploy/server/monitoring/units";
12
import { format } from "date-fns";
23
import { Area, AreaChart, CartesianGrid, YAxis } from "recharts";
34
import {
@@ -29,8 +30,8 @@ export const DockerBlockChart = ({ accumulativeData }: Props) => {
2930
const transformedData = accumulativeData.map((item, index) => ({
3031
time: item.time,
3132
name: `Point ${index + 1}`,
32-
readMb: item.value.readMb,
33-
writeMb: item.value.writeMb,
33+
readMb: toMb(item.value.readMb),
34+
writeMb: toMb(item.value.writeMb),
3435
}));
3536

3637
return (
@@ -77,7 +78,7 @@ export const DockerBlockChart = ({ accumulativeData }: Props) => {
7778
}}
7879
formatter={(value, name) => {
7980
const label = name === "readMb" ? "Read" : "Write";
80-
return [`${value} MB`, label];
81+
return [formatMb(Number(value)), label];
8182
}}
8283
/>
8384
}

‎apps/dokploy/components/dashboard/monitoring/free/container/docker-network-chart.tsx‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { formatMb, toMb } from "@dokploy/server/monitoring/units";
12
import { format } from "date-fns";
23
import { Area, AreaChart, CartesianGrid, YAxis } from "recharts";
34
import {
@@ -29,8 +30,8 @@ export const DockerNetworkChart = ({ accumulativeData }: Props) => {
2930
const transformedData = accumulativeData.map((item, index) => ({
3031
time: item.time,
3132
name: `Point ${index + 1}`,
32-
inMB: item.value.inputMb,
33-
outMB: item.value.outputMb,
33+
inMB: toMb(item.value.inputMb),
34+
outMB: toMb(item.value.outputMb),
3435
}));
3536

3637
return (
@@ -73,7 +74,7 @@ export const DockerNetworkChart = ({ accumulativeData }: Props) => {
7374
}}
7475
formatter={(value, name) => {
7576
const label = name === "inMB" ? "In" : "Out";
76-
return [`${value} MB`, label];
77+
return [formatMb(Number(value)), label];
7778
}}
7879
/>
7980
}

‎apps/dokploy/components/dashboard/monitoring/free/container/show-free-container-monitoring.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { formatMb } from "@dokploy/server/monitoring/units";
12
import { useEffect, useState } from "react";
23
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
34
import { Progress } from "@/components/ui/progress";
@@ -305,7 +306,7 @@ export const ContainerFreeMonitoring = ({
305306
<CardContent>
306307
<div className="flex flex-col gap-2 w-full">
307308
<span className="text-sm text-muted-foreground">
308-
{`Read: ${currentData.block.value.readMb} / Write: ${currentData.block.value.writeMb} `}
309+
{`Read: ${formatMb(currentData.block.value.readMb)} / Write: ${formatMb(currentData.block.value.writeMb)}`}
309310
</span>
310311
<DockerBlockChart accumulativeData={accumulativeData.block} />
311312
</div>
@@ -318,7 +319,7 @@ export const ContainerFreeMonitoring = ({
318319
<CardContent>
319320
<div className="flex flex-col gap-2 w-full">
320321
<span className="text-sm text-muted-foreground">
321-
{`In MB: ${currentData.network.value.inputMb} / Out MB: ${currentData.network.value.outputMb} `}
322+
{`In: ${formatMb(currentData.network.value.inputMb)} / Out: ${formatMb(currentData.network.value.outputMb)}`}
322323
</span>
323324
<DockerNetworkChart accumulativeData={accumulativeData.network} />
324325
</div>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const IO_UNIT_FACTORS: Record<string, number> = {
2+
B: 1 / 1e6,
3+
kB: 1e-3,
4+
KB: 1e-3,
5+
MB: 1,
6+
GB: 1e3,
7+
TB: 1e6,
8+
KiB: 1024 / 1e6,
9+
MiB: 1024 ** 2 / 1e6,
10+
GiB: 1024 ** 3 / 1e6,
11+
TiB: 1024 ** 4 / 1e6,
12+
};
13+
14+
export const toMb = (value: number | string | undefined): number => {
15+
if (typeof value === "number") {
16+
return value;
17+
}
18+
if (!value) {
19+
return 0;
20+
}
21+
const parsed = Number.parseFloat(value);
22+
if (Number.isNaN(parsed)) {
23+
return 0;
24+
}
25+
const unit = value.replace(/[0-9.\s]/g, "");
26+
return parsed * (IO_UNIT_FACTORS[unit] ?? 1);
27+
};
28+
29+
export const parseIoToMb = (raw: string | undefined): number =>
30+
Number(toMb(raw).toFixed(6));
31+
32+
export const formatMb = (value: number | string | undefined): string => {
33+
const mb = toMb(value);
34+
if (mb >= 1000) {
35+
return `${(mb / 1000).toFixed(2)} GB`;
36+
}
37+
if (mb < 0.01 && mb > 0) {
38+
return `${(mb * 1000).toFixed(2)} kB`;
39+
}
40+
return `${mb.toFixed(2)} MB`;
41+
};

‎packages/server/src/monitoring/utils.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { promises } from "node:fs";
22
import { OSUtils } from "node-os-utils";
33
import { paths } from "../constants";
4+
import { parseIoToMb } from "./units";
45

56
export interface Container {
67
BlockIO: string;
@@ -28,13 +29,13 @@ export const recordAdvancedStats = async (
2829
});
2930

3031
await updateStatsFile(appName, "block", {
31-
readMb: stats.BlockIO.split(" ")[0],
32-
writeMb: stats.BlockIO.split(" ")[2],
32+
readMb: parseIoToMb(stats.BlockIO.split(" ")[0]),
33+
writeMb: parseIoToMb(stats.BlockIO.split(" ")[2]),
3334
});
3435

3536
await updateStatsFile(appName, "network", {
36-
inputMb: stats.NetIO.split(" ")[0],
37-
outputMb: stats.NetIO.split(" ")[2],
37+
inputMb: parseIoToMb(stats.NetIO.split(" ")[0]),
38+
outputMb: parseIoToMb(stats.NetIO.split(" ")[2]),
3839
});
3940

4041
if (appName === "dokploy") {

0 commit comments

Comments
 (0)