From 3483fe1188f1c6c5d319db73e9462c2631c33d95 Mon Sep 17 00:00:00 2001 From: Dimitar Ilkov Date: Wed, 26 Oct 2022 11:36:15 +0300 Subject: [PATCH 1/2] Add widget for Truenas --- public/locales/en/common.json | 5 +++++ src/widgets/components.js | 1 + src/widgets/truenas/component.jsx | 37 +++++++++++++++++++++++++++++++ src/widgets/truenas/widget.js | 21 ++++++++++++++++++ src/widgets/widgets.js | 2 ++ 5 files changed, 66 insertions(+) create mode 100644 src/widgets/truenas/component.jsx create mode 100644 src/widgets/truenas/widget.js diff --git a/public/locales/en/common.json b/public/locales/en/common.json index aa1d6601a..48a3ef7f3 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -292,5 +292,10 @@ "up_to_date": "Up to Date", "child_bridges": "Child Bridges", "child_bridges_status": "{{ok}}/{{total}}" + }, + "truenas": { + "load": "System Load", + "uptime": "Uptime", + "alerts": "Alerts" } } diff --git a/src/widgets/components.js b/src/widgets/components.js index 8f7bfeb93..c339946dd 100644 --- a/src/widgets/components.js +++ b/src/widgets/components.js @@ -35,6 +35,7 @@ const components = { tautulli: dynamic(() => import("./tautulli/component")), traefik: dynamic(() => import("./traefik/component")), transmission: dynamic(() => import("./transmission/component")), + truenas: dynamic(() => import("./truenas/component")), unifi: dynamic(() => import("./unifi/component")), }; diff --git a/src/widgets/truenas/component.jsx b/src/widgets/truenas/component.jsx new file mode 100644 index 000000000..9529ae0f6 --- /dev/null +++ b/src/widgets/truenas/component.jsx @@ -0,0 +1,37 @@ +import { useTranslation } from "next-i18next"; + +import Container from "components/services/widget/container"; +import Block from "components/services/widget/block"; +import useWidgetAPI from "utils/proxy/use-widget-api"; + +export default function Component({ service }) { + const { t } = useTranslation(); + + const { widget } = service; + + const { data: alertData, error: alertError } = useWidgetAPI(widget, "alerts"); + const { data: statusData, error: statusError } = useWidgetAPI(widget, "status"); + + if (alertError || statusError) { + return ; + } + + if (!alertData || !statusData) { + return ( + + + + + + ); + } + + return ( + + + + + + + ); +} diff --git a/src/widgets/truenas/widget.js b/src/widgets/truenas/widget.js new file mode 100644 index 000000000..7269e36a1 --- /dev/null +++ b/src/widgets/truenas/widget.js @@ -0,0 +1,21 @@ +import { jsonArrayFilter } from "utils/proxy/api-helpers"; +import genericProxyHandler from "utils/proxy/handlers/generic"; + +const widget = { + api: "{url}/api/v2.0/{endpoint}", + proxyHandler: genericProxyHandler, + + mappings: { + alerts: { + endpoint: "alert/list", + map: (data) => ({ + pending: jsonArrayFilter(data, (item) => item?.dismissed === false).length, + }), + }, + status: { + endpoint: "system/info", + }, + }, +}; + +export default widget; diff --git a/src/widgets/widgets.js b/src/widgets/widgets.js index 55379e986..c82003fea 100644 --- a/src/widgets/widgets.js +++ b/src/widgets/widgets.js @@ -30,6 +30,7 @@ import strelaysrv from "./strelaysrv/widget"; import tautulli from "./tautulli/widget"; import traefik from "./traefik/widget"; import transmission from "./transmission/widget"; +import truenas from "./truenas/widget"; import unifi from "./unifi/widget"; const widgets = { @@ -66,6 +67,7 @@ const widgets = { tautulli, traefik, transmission, + truenas, unifi, unifi_console: unifi, }; From 300bb3487e14c6a4a214bbe37b1c6a31d3420720 Mon Sep 17 00:00:00 2001 From: Dimitar Ilkov Date: Thu, 27 Oct 2022 10:44:21 +0300 Subject: [PATCH 2/2] format uptime --- public/locales/en/common.json | 3 ++- src/widgets/truenas/component.jsx | 34 ++++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/public/locales/en/common.json b/public/locales/en/common.json index 36c336cd2..724d405d5 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -302,6 +302,7 @@ "truenas": { "load": "System Load", "uptime": "Uptime", - "alerts": "Alerts" + "alerts": "Alerts", + "time": "{{value, number(style: unit; unitDisplay: long;)}}" } } diff --git a/src/widgets/truenas/component.jsx b/src/widgets/truenas/component.jsx index 9529ae0f6..1e5c7b2d3 100644 --- a/src/widgets/truenas/component.jsx +++ b/src/widgets/truenas/component.jsx @@ -4,6 +4,35 @@ import Container from "components/services/widget/container"; import Block from "components/services/widget/block"; import useWidgetAPI from "utils/proxy/use-widget-api"; +const processUptime = uptime => { + + let seconds = uptime.toFixed(0); + + var levels = [ + [Math.floor(seconds / 31536000), 'year'], + [Math.floor((seconds % 31536000) / 2592000), 'month'], + [Math.floor(((seconds % 31536000) % 2592000) / 86400), 'day'], + [Math.floor(((seconds % 31536000) % 86400) / 3600), 'hour'], + [Math.floor((((seconds % 31536000) % 86400) % 3600) / 60), 'minute'], + [(((seconds % 31536000) % 86400) % 3600) % 60, 'second'], + ]; + + for(let i = 0; i< levels.length; i++){ + let level = levels[i]; + if(level[0] > 0){ + return { + value: level[0], + unit: level[1] + } + } + } + + return { + value: 0, + unit: 'second' + }; +} + export default function Component({ service }) { const { t } = useTranslation(); @@ -25,13 +54,12 @@ export default function Component({ service }) { ); } - + return ( - + - ); }