Skip to content

Commit

Permalink
Fix: sum throughput data for docker stats (gethomepage#2334)
Browse files Browse the repository at this point in the history
  • Loading branch information
shamoon authored Nov 17, 2023
1 parent c9991bc commit 7f50f6c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/widgets/docker/component.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import useSWR from "swr";
import { useTranslation } from "next-i18next";

import { calculateCPUPercent, calculateUsedMemory } from "./stats-helpers";
import { calculateCPUPercent, calculateUsedMemory, calculateThroughput } from "./stats-helpers";

import Container from "components/services/widget/container";
import Block from "components/services/widget/block";
Expand Down Expand Up @@ -41,18 +41,18 @@ export default function Component({ service }) {
);
}

const network = statsData.stats.networks?.eth0 || statsData.stats.networks?.network;
const { rx_bytes, tx_bytes } = calculateThroughput(statsData.stats);

return (
<Container service={service}>
<Block label="docker.cpu" value={t("common.percent", { value: calculateCPUPercent(statsData.stats) })} />
{statsData.stats.memory_stats.usage && (
<Block label="docker.mem" value={t("common.bytes", { value: calculateUsedMemory(statsData.stats) })} />
)}
{network && (
{statsData.stats.networks && (
<>
<Block label="docker.rx" value={t("common.bytes", { value: network.rx_bytes })} />
<Block label="docker.tx" value={t("common.bytes", { value: network.tx_bytes })} />
<Block label="docker.rx" value={t("common.bytes", { value: rx_bytes })} />
<Block label="docker.tx" value={t("common.bytes", { value: tx_bytes })} />
</>
)}
</Container>
Expand Down
15 changes: 15 additions & 0 deletions src/widgets/docker/stats-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,18 @@ export function calculateUsedMemory(stats) {
stats.memory_stats.usage - (stats.memory_stats.total_inactive_file ?? stats.memory_stats.stats?.inactive_file ?? 0)
);
}

export function calculateThroughput(stats) {
let rx_bytes = 0;
let tx_bytes = 0;
if (stats.networks?.network) {
rx_bytes = stats.networks?.network.rx_bytes;
tx_bytes = stats.networks?.network.tx_bytes;
} else if (stats.networks && Array.isArray(Object.values(stats.networks))) {
Object.values(stats.networks).forEach((containerInterface) => {
rx_bytes += containerInterface.rx_bytes;
tx_bytes += containerInterface.tx_bytes;
});
}
return { rx_bytes, tx_bytes };
}

0 comments on commit 7f50f6c

Please sign in to comment.