From addf0911a0e642cf7bac2b88000a9a2982527865 Mon Sep 17 00:00:00 2001 From: Mbarmem <52536342+Mbarmem@users.noreply.github.com> Date: Sun, 19 Feb 2023 00:58:01 +0300 Subject: [PATCH] Add Kopia widget (#1018) * Add Kopia widget * Add Kopia widget * Modify Kopia widget blocks * Kopia next run / last run --------- Co-authored-by: shamoon <4887959+shamoon@users.noreply.github.com> --- public/locales/en/common.json | 7 ++++ src/widgets/components.js | 1 + src/widgets/kopia/component.jsx | 68 +++++++++++++++++++++++++++++++++ src/widgets/kopia/widget.js | 14 +++++++ src/widgets/widgets.js | 2 + 5 files changed, 92 insertions(+) create mode 100755 src/widgets/kopia/component.jsx create mode 100755 src/widgets/kopia/widget.js diff --git a/public/locales/en/common.json b/public/locales/en/common.json index a24ca877..0bf28285 100755 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -495,5 +495,12 @@ "memoryusage": "Memory Usage", "freespace": "Free Space", "activeusers": "Active Users" + }, + "kopia": { + "status": "Status", + "size": "Size", + "lastrun": "Last Run", + "nextrun": "Next Run", + "failed": "Failed" } } \ No newline at end of file diff --git a/src/widgets/components.js b/src/widgets/components.js index d39a7cc7..e4ecb947 100644 --- a/src/widgets/components.js +++ b/src/widgets/components.js @@ -25,6 +25,7 @@ const components = { jellyfin: dynamic(() => import("./emby/component")), jellyseerr: dynamic(() => import("./jellyseerr/component")), komga: dynamic(() => import("./komga/component")), + kopia: dynamic(() => import("./kopia/component")), lidarr: dynamic(() => import("./lidarr/component")), mastodon: dynamic(() => import("./mastodon/component")), medusa: dynamic(() => import("./medusa/component")), diff --git a/src/widgets/kopia/component.jsx b/src/widgets/kopia/component.jsx new file mode 100755 index 00000000..dcc763e6 --- /dev/null +++ b/src/widgets/kopia/component.jsx @@ -0,0 +1,68 @@ +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"; + +function relativeDate(date) { + const seconds = Math.abs(Math.floor((new Date() - date) / 1000)); + + let interval = Math.abs(seconds / 31536000); + + if (interval > 1) { + return `${Math.floor(interval)} y`; + } + interval = seconds / 2592000; + if (interval > 1) { + return `${Math.floor(interval)} mo`; + } + interval = seconds / 86400; + if (interval > 1) { + return `${Math.floor(interval)} d`; + } + interval = seconds / 3600; + if (interval > 1) { + return `${Math.floor(interval)} h`; + } + interval = seconds / 60; + if (interval > 1) { + return `${Math.floor(interval)} m`; + } + return `${Math.floor(seconds)} s`; +} + +export default function Component({ service }) { + const { t } = useTranslation(); + + const { widget } = service; + const { data: statusData, error: statusError } = useWidgetAPI(widget, "status"); + + if (statusError) { + return ; + } + + const source = statusData?.sources[0]; + + if (!statusData || !source) { + return ( + + + + + + + ); + } + + const lastRun = source.lastSnapshot.stats.errorCount === 0 ? new Date(source.lastSnapshot.startTime) : t("kopia.failed"); + const nextTime = source.nextSnapshotTime ? new Date(source.nextSnapshotTime) : null; + + return ( + + + + + {nextTime && } + + ); +} \ No newline at end of file diff --git a/src/widgets/kopia/widget.js b/src/widgets/kopia/widget.js new file mode 100755 index 00000000..58ed46c4 --- /dev/null +++ b/src/widgets/kopia/widget.js @@ -0,0 +1,14 @@ +import genericProxyHandler from "utils/proxy/handlers/generic"; + +const widget = { + api: "{url}/{endpoint}", + proxyHandler: genericProxyHandler, + + mappings: { + status: { + endpoint: "api/v1/sources", + }, + }, +}; + +export default widget; \ No newline at end of file diff --git a/src/widgets/widgets.js b/src/widgets/widgets.js index 72fe803b..bf9457c6 100644 --- a/src/widgets/widgets.js +++ b/src/widgets/widgets.js @@ -19,6 +19,7 @@ import homebridge from "./homebridge/widget"; import jackett from "./jackett/widget"; import jellyseerr from "./jellyseerr/widget"; import komga from "./komga/widget"; +import kopia from "./kopia/widget"; import lidarr from "./lidarr/widget"; import mastodon from "./mastodon/widget"; import medusa from "./medusa/widget"; @@ -89,6 +90,7 @@ const widgets = { jellyfin: emby, jellyseerr, komga, + kopia, lidarr, mastodon, medusa,