Skip to content

Commit 1c0f6a8

Browse files
authored
Merge pull request #1262 from Dokploy/1235-monitoring-404-not-found---mcpuvalue-is-null
refactor: update docker stats
2 parents ee2fed0 + c41aa0c commit 1c0f6a8

File tree

11 files changed

+98
-137
lines changed

11 files changed

+98
-137
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,11 @@ const CustomTooltip = ({ active, payload }: CustomTooltipProps) => {
9090
if (active && payload && payload.length && payload[0]) {
9191
return (
9292
<div className="custom-tooltip bg-background p-2 shadow-lg rounded-md text-primary border">
93-
<p>{`Date: ${format(new Date(payload[0].payload.time), "PPpp")}`}</p>
94-
<p>{`Read ${payload[0].payload.readMb.toFixed(2)} MB`}</p>
95-
<p>{`Write: ${payload[0].payload.writeMb.toFixed(3)} MB`}</p>
93+
{payload[0].payload.time && (
94+
<p>{`Date: ${format(new Date(payload[0].payload.time), "PPpp")}`}</p>
95+
)}
96+
<p>{`Read ${payload[0].payload.readMb} `}</p>
97+
<p>{`Write: ${payload[0].payload.writeMb} `}</p>
9698
</div>
9799
);
98100
}

apps/dokploy/components/dashboard/monitoring/docker/docker-cpu-chart.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const DockerCpuChart = ({ acummulativeData }: Props) => {
1919
return {
2020
name: `Point ${index + 1}`,
2121
time: item.time,
22-
usage: item.value.toFixed(2),
22+
usage: item.value.toString().split("%")[0],
2323
};
2424
});
2525
return (
@@ -75,7 +75,9 @@ const CustomTooltip = ({ active, payload }: CustomTooltipProps) => {
7575
if (active && payload && payload.length && payload[0]) {
7676
return (
7777
<div className="custom-tooltip bg-background p-2 shadow-lg rounded-md text-primary border">
78-
<p>{`Date: ${format(new Date(payload[0].payload.time), "PPpp")}`}</p>
78+
{payload[0].payload.time && (
79+
<p>{`Date: ${format(new Date(payload[0].payload.time), "PPpp")}`}</p>
80+
)}
7981
<p>{`CPU Usage: ${payload[0].payload.usage}%`}</p>
8082
</div>
8183
);

apps/dokploy/components/dashboard/monitoring/docker/docker-memory-chart.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
YAxis,
1010
} from "recharts";
1111
import type { DockerStatsJSON } from "./show";
12+
import { convertMemoryToBytes } from "./show";
1213

1314
interface Props {
1415
acummulativeData: DockerStatsJSON["memory"];
@@ -23,7 +24,8 @@ export const DockerMemoryChart = ({
2324
return {
2425
time: item.time,
2526
name: `Point ${index + 1}`,
26-
usage: (item.value.used / 1024 ** 3).toFixed(2),
27+
// @ts-ignore
28+
usage: (convertMemoryToBytes(item.value.used) / 1024 ** 3).toFixed(2),
2729
};
2830
});
2931
return (
@@ -75,10 +77,13 @@ interface CustomTooltipProps {
7577
}
7678

7779
const CustomTooltip = ({ active, payload }: CustomTooltipProps) => {
78-
if (active && payload && payload.length && payload[0]) {
80+
if (active && payload && payload.length && payload[0] && payload[0].payload) {
7981
return (
8082
<div className="custom-tooltip bg-background p-2 shadow-lg rounded-md text-primary border">
81-
<p>{`Date: ${format(new Date(payload[0].payload.time), "PPpp")}`}</p>
83+
{payload[0].payload.time && (
84+
<p>{`Date: ${format(new Date(payload[0].payload.time), "PPpp")}`}</p>
85+
)}
86+
8287
<p>{`Memory usage: ${payload[0].payload.usage} GB`}</p>
8388
</div>
8489
);

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ export const DockerNetworkChart = ({ acummulativeData }: Props) => {
1919
return {
2020
time: item.time,
2121
name: `Point ${index + 1}`,
22-
inMB: item.value.inputMb.toFixed(2),
23-
outMB: item.value.outputMb.toFixed(2),
22+
inMB: item.value.inputMb,
23+
outMB: item.value.outputMb,
2424
};
2525
});
2626
return (
@@ -86,9 +86,11 @@ const CustomTooltip = ({ active, payload }: CustomTooltipProps) => {
8686
if (active && payload && payload.length && payload[0]) {
8787
return (
8888
<div className="custom-tooltip bg-background p-2 shadow-lg rounded-md text-primary border">
89-
<p>{`Date: ${format(new Date(payload[0].payload.time), "PPpp")}`}</p>
90-
<p>{`In MB Usage: ${payload[0].payload.inMB} MB`}</p>
91-
<p>{`Out MB Usage: ${payload[0].payload.outMB} MB`}</p>
89+
{payload[0].payload.time && (
90+
<p>{`Date: ${format(new Date(payload[0].payload.time), "PPpp")}`}</p>
91+
)}
92+
<p>{`In Usage: ${payload[0].payload.inMB} `}</p>
93+
<p>{`Out Usage: ${payload[0].payload.outMB} `}</p>
9294
</div>
9395
);
9496
}

apps/dokploy/components/dashboard/monitoring/docker/show.tsx

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ const defaultData = {
2222
memory: {
2323
value: {
2424
used: 0,
25-
free: 0,
26-
usedPercentage: 0,
2725
total: 0,
2826
},
2927
time: "",
@@ -60,8 +58,6 @@ export interface DockerStats {
6058
memory: {
6159
value: {
6260
used: number;
63-
free: number;
64-
usedPercentage: number;
6561
total: number;
6662
};
6763
time: string;
@@ -100,6 +96,30 @@ export type DockerStatsJSON = {
10096
disk: DockerStats["disk"][];
10197
};
10298

99+
export const convertMemoryToBytes = (
100+
memoryString: string | undefined,
101+
): number => {
102+
if (!memoryString || typeof memoryString !== "string") {
103+
return 0;
104+
}
105+
106+
const value = Number.parseFloat(memoryString) || 0;
107+
const unit = memoryString.replace(/[0-9.]/g, "").trim();
108+
109+
switch (unit) {
110+
case "KiB":
111+
return value * 1024;
112+
case "MiB":
113+
return value * 1024 * 1024;
114+
case "GiB":
115+
return value * 1024 * 1024 * 1024;
116+
case "TiB":
117+
return value * 1024 * 1024 * 1024 * 1024;
118+
default:
119+
return value;
120+
}
121+
};
122+
103123
export const DockerMonitoring = ({
104124
appName,
105125
appType = "application",
@@ -208,7 +228,7 @@ export const DockerMonitoring = ({
208228
<CardContent>
209229
<div className="flex flex-col gap-2 w-full">
210230
<span className="text-sm text-muted-foreground">
211-
Used: {currentData.cpu.value.toFixed(2)}%
231+
Used: {currentData.cpu.value}%
212232
</span>
213233
<Progress
214234
value={currentData.cpu.value}
@@ -218,7 +238,6 @@ export const DockerMonitoring = ({
218238
</div>
219239
</CardContent>
220240
</Card>
221-
222241
<Card className="bg-background">
223242
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
224243
<CardTitle className="text-sm font-medium">
@@ -228,20 +247,29 @@ export const DockerMonitoring = ({
228247
<CardContent>
229248
<div className="flex flex-col gap-2 w-full">
230249
<span className="text-sm text-muted-foreground">
231-
{`Used: ${(currentData.memory.value.used / 1024 ** 3).toFixed(2)} GB / Limit: ${(currentData.memory.value.total / 1024 ** 3).toFixed(2)} GB`}
250+
{`Used: ${currentData.memory.value.used} / Limit: ${currentData.memory.value.total} `}
232251
</span>
233252
<Progress
234-
value={currentData.memory.value.usedPercentage}
253+
value={
254+
// @ts-ignore
255+
(convertMemoryToBytes(currentData.memory.value.used) /
256+
// @ts-ignore
257+
convertMemoryToBytes(currentData.memory.value.total)) *
258+
100
259+
}
235260
className="w-[100%]"
236261
/>
237262
<DockerMemoryChart
238263
acummulativeData={acummulativeData.memory}
239-
memoryLimitGB={currentData.memory.value.total / 1024 ** 3}
264+
memoryLimitGB={
265+
// @ts-ignore
266+
convertMemoryToBytes(currentData.memory.value.total) /
267+
1024 ** 3
268+
}
240269
/>
241270
</div>
242271
</CardContent>
243272
</Card>
244-
245273
{appName === "dokploy" && (
246274
<Card className="bg-background">
247275
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
@@ -274,17 +302,12 @@ export const DockerMonitoring = ({
274302
<CardContent>
275303
<div className="flex flex-col gap-2 w-full">
276304
<span className="text-sm text-muted-foreground">
277-
{`Read: ${currentData.block.value.readMb.toFixed(
278-
2,
279-
)} MB / Write: ${currentData.block.value.writeMb.toFixed(
280-
3,
281-
)} MB`}
305+
{`Read: ${currentData.block.value.readMb} / Write: ${currentData.block.value.writeMb} `}
282306
</span>
283307
<DockerBlockChart acummulativeData={acummulativeData.block} />
284308
</div>
285309
</CardContent>
286310
</Card>
287-
288311
<Card className="bg-background">
289312
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
290313
<CardTitle className="text-sm font-medium">
@@ -294,11 +317,7 @@ export const DockerMonitoring = ({
294317
<CardContent>
295318
<div className="flex flex-col gap-2 w-full">
296319
<span className="text-sm text-muted-foreground">
297-
{`In MB: ${currentData.network.value.inputMb.toFixed(
298-
2,
299-
)} MB / Out MB: ${currentData.network.value.outputMb.toFixed(
300-
2,
301-
)} MB`}
320+
{`In MB: ${currentData.network.value.inputMb} / Out MB: ${currentData.network.value.outputMb} `}
302321
</span>
303322
<DockerNetworkChart
304323
acummulativeData={acummulativeData.network}

apps/dokploy/pages/dashboard/project/[projectId]/services/application/[applicationId].tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import { appRouter } from "@/server/api/root";
4141
import { api } from "@/utils/api";
4242
import { validateRequest } from "@dokploy/server";
4343
import { createServerSideHelpers } from "@trpc/react-query/server";
44+
import copy from "copy-to-clipboard";
4445
import { GlobeIcon, HelpCircle, ServerOff, Trash2 } from "lucide-react";
4546
import type {
4647
GetServerSidePropsContext,
@@ -52,7 +53,6 @@ import { useRouter } from "next/router";
5253
import React, { useState, useEffect, type ReactElement } from "react";
5354
import { toast } from "sonner";
5455
import superjson from "superjson";
55-
import copy from 'copy-to-clipboard';
5656

5757
type TabState =
5858
| "projects"

apps/dokploy/pages/dashboard/project/[projectId]/services/compose/[composeId].tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { appRouter } from "@/server/api/root";
3333
import { api } from "@/utils/api";
3434
import { validateRequest } from "@dokploy/server";
3535
import { createServerSideHelpers } from "@trpc/react-query/server";
36+
import copy from "copy-to-clipboard";
3637
import { CircuitBoard, ServerOff } from "lucide-react";
3738
import { HelpCircle } from "lucide-react";
3839
import type {
@@ -43,9 +44,8 @@ import Head from "next/head";
4344
import Link from "next/link";
4445
import { useRouter } from "next/router";
4546
import React, { useState, useEffect, type ReactElement } from "react";
46-
import superjson from "superjson";
4747
import { toast } from "sonner";
48-
import copy from 'copy-to-clipboard';
48+
import superjson from "superjson";
4949

5050
type TabState =
5151
| "projects"

apps/dokploy/server/wss/docker-stats.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type http from "node:http";
22
import {
33
docker,
4+
execAsync,
45
getLastAdvancedStatsFile,
56
recordAdvancedStats,
67
validateWebSocketRequest,
@@ -70,12 +71,16 @@ export const setupDockerStatsMonitoringSocketServer = (
7071
ws.close(4000, "Container not running");
7172
return;
7273
}
74+
const { stdout, stderr } = await execAsync(
75+
`docker stats ${container.Id} --no-stream --format \'{"BlockIO":"{{.BlockIO}}","CPUPerc":"{{.CPUPerc}}","Container":"{{.Container}}","ID":"{{.ID}}","MemPerc":"{{.MemPerc}}","MemUsage":"{{.MemUsage}}","Name":"{{.Name}}","NetIO":"{{.NetIO}}"}\'`,
76+
);
77+
if (stderr) {
78+
console.error("Docker stats error:", stderr);
79+
return;
80+
}
81+
const stat = JSON.parse(stdout);
7382

74-
const stats = await docker.getContainer(container.Id).stats({
75-
stream: false,
76-
});
77-
78-
await recordAdvancedStats(stats, appName);
83+
await recordAdvancedStats(stat, appName);
7984
const data = await getLastAdvancedStatsFile(appName);
8085

8186
ws.send(

packages/server/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export * from "./utils/access-log/types";
110110
export * from "./utils/access-log/utils";
111111
export * from "./constants/index";
112112

113-
export * from "./monitoring/utilts";
113+
export * from "./monitoring/utils";
114114

115115
export * from "./db/validations/domain";
116116
export * from "./db/validations/index";

0 commit comments

Comments
 (0)