From 546d699799b9f0c5652c8b6ff7912c76d7a485b7 Mon Sep 17 00:00:00 2001
From: shamoon <4887959+shamoon@users.noreply.github.com>
Date: Sun, 26 Jan 2025 00:07:17 -0800
Subject: [PATCH] Feature: use datasets for truenas pools
---
src/widgets/truenas/component.jsx | 38 +++++++++++++++++++------------
src/widgets/truenas/pool.jsx | 17 +++-----------
src/widgets/truenas/widget.js | 6 ++---
3 files changed, 29 insertions(+), 32 deletions(-)
diff --git a/src/widgets/truenas/component.jsx b/src/widgets/truenas/component.jsx
index ccdab297..e7a3bc9e 100644
--- a/src/widgets/truenas/component.jsx
+++ b/src/widgets/truenas/component.jsx
@@ -12,14 +12,15 @@ export default function Component({ service }) {
const { data: alertData, error: alertError } = useWidgetAPI(widget, "alerts");
const { data: statusData, error: statusError } = useWidgetAPI(widget, "status");
- const { data: poolsData, error: poolsError } = useWidgetAPI(widget, "pools");
+ const { data: poolsData, error: poolsError } = useWidgetAPI(widget, widget?.enablePools ? "pools" : null);
+ const { data: datasetData, error: datasetError } = useWidgetAPI(widget, widget?.enablePools ? "dataset" : null);
if (alertError || statusError || poolsError) {
- const finalError = alertError ?? statusError ?? poolsError;
+ const finalError = alertError ?? statusError ?? poolsError ?? datasetError;
return ;
}
- if (!alertData || !statusData) {
+ if (!alertData || !statusData || (widget?.enablePools && (!poolsData || !datasetData))) {
return (
@@ -29,7 +30,22 @@ export default function Component({ service }) {
);
}
- const enablePools = widget?.enablePools && Array.isArray(poolsData) && poolsData.length > 0;
+ let pools = [];
+ const showPools =
+ Array.isArray(poolsData) && poolsData.length > 0 && Array.isArray(datasetData) && datasetData.length > 0;
+
+ if (showPools) {
+ pools = poolsData.map((pool) => {
+ const dataset = datasetData.find((d) => d.pool === pool.name);
+ return {
+ id: pool.id,
+ name: pool.name,
+ healthy: pool.healthy,
+ allocated: dataset.children.reduce((acc, d) => acc + parseInt(d.used.rawvalue, 10), 0),
+ free: dataset.children.reduce((acc, d) => acc + parseInt(d.available.rawvalue, 10), 0),
+ };
+ });
+ }
return (
<>
@@ -38,19 +54,11 @@ export default function Component({ service }) {
- {enablePools &&
- poolsData
+ {showPools &&
+ pools
.sort((a, b) => a.name.localeCompare(b.name))
.map((pool) => (
-
+
))}
>
);
diff --git a/src/widgets/truenas/pool.jsx b/src/widgets/truenas/pool.jsx
index b92ecb68..619d10ab 100644
--- a/src/widgets/truenas/pool.jsx
+++ b/src/widgets/truenas/pool.jsx
@@ -1,19 +1,8 @@
import classNames from "classnames";
import prettyBytes from "pretty-bytes";
-export default function Pool({ name, free, allocated, healthy, data, nasType }) {
- let total = 0;
- if (nasType === "scale") {
- total = free + allocated;
- } else {
- allocated = 0; // eslint-disable-line no-param-reassign
- for (let i = 0; i < data.length; i += 1) {
- total += data[i].stats.size;
- allocated += data[i].stats.allocated; // eslint-disable-line no-param-reassign
- }
- }
-
- const usedPercent = Math.round((allocated / total) * 100);
+export default function Pool({ name, free, allocated, healthy }) {
+ const usedPercent = Math.round((allocated / (free + allocated)) * 100);
const statusColor = healthy ? "bg-green-500" : "bg-yellow-500";
return (
@@ -32,7 +21,7 @@ export default function Pool({ name, free, allocated, healthy, data, nasType })
- {prettyBytes(allocated)} / {prettyBytes(total)}
+ {prettyBytes(allocated)} / {prettyBytes(free + allocated)}
({usedPercent}%)
diff --git a/src/widgets/truenas/widget.js b/src/widgets/truenas/widget.js
index 8e50956b..d322753f 100644
--- a/src/widgets/truenas/widget.js
+++ b/src/widgets/truenas/widget.js
@@ -23,11 +23,11 @@ const widget = {
id: entry.name,
name: entry.name,
healthy: entry.healthy,
- allocated: entry.allocated,
- free: entry.free,
- data: entry.topology?.data ?? [],
})),
},
+ dataset: {
+ endpoint: "pool/dataset",
+ },
},
};