Przeglądaj źródła

add speedtest-tracker integration

Ben Phelps 2 lat temu
rodzic
commit
ac718c852a

+ 2 - 1
README.md

@@ -9,7 +9,8 @@
     - Docker Integration
       - Status light + CPU, Memory & Network Reporting *(click on the status light)*
     - Service Integration
-      - Currently supports Sonarr, Radarr, Ombi, Emby, Jellyfin, NZBGet, ruTorrent, Portainer & PiHole
+      - Currently supports Sonarr, Radarr, Ombi, Emby, Jellyfin, NZBGet, ruTorrent
+      - Portainer, Speedtest Tracker, PiHole
   * Homepage Widgets
     - System Stats (Disk, CPU, Memory)
     - Weather (via weatherapi.com)

+ 2 - 0
src/components/services/widget.jsx

@@ -8,6 +8,7 @@ import Docker from "./widgets/service/docker";
 import Pihole from "./widgets/service/pihole";
 import Rutorrent from "./widgets/service/rutorrent";
 import Jellyfin from "./widgets/service/jellyfin";
+import Speedtest from "./widgets/service/speedtest";
 
 const widgetMappings = {
   docker: Docker,
@@ -20,6 +21,7 @@ const widgetMappings = {
   nzbget: Nzbget,
   pihole: Pihole,
   rutorrent: Rutorrent,
+  speedtest: Speedtest,
 };
 
 export default function Widget({ service }) {

+ 39 - 0
src/components/services/widgets/service/speedtest.jsx

@@ -0,0 +1,39 @@
+import useSWR from "swr";
+
+import Widget from "../widget";
+import Block from "../block";
+
+import { formatBits } from "utils/stats-helpers";
+
+export default function Speedtest({ service }) {
+  const config = service.widget;
+
+  function buildApiUrl(endpoint) {
+    const { url } = config;
+    return `${url}/api/${endpoint}`;
+  }
+
+  const { data: speedtestData, error: speedtestError } = useSWR(buildApiUrl("speedtest/latest"));
+
+  if (speedtestError) {
+    return <Widget error="Speedtest API Error" />;
+  }
+
+  if (!speedtestData) {
+    return (
+      <Widget>
+        <Block label="Download" />
+        <Block label="Upload" />
+        <Block label="Ping" />
+      </Widget>
+    );
+  }
+
+  return (
+    <Widget>
+      <Block label="Download" value={`${formatBits(speedtestData.data.download * 1024 * 1024)}ps`} />
+      <Block label="Upload" value={`${formatBits(speedtestData.data.upload * 1024 * 1024)}ps`} />
+      <Block label="Ping" value={`${speedtestData.data.ping} ms`} />
+    </Widget>
+  );
+}

+ 12 - 0
src/utils/stats-helpers.js

@@ -21,3 +21,15 @@ export function formatBytes(bytes, decimals = 2) {
 
   return parseFloat(bytes / Math.pow(k, i)).toFixed(dm) + " " + sizes[i];
 }
+
+export function formatBits(bytes, decimals = 2) {
+  if (bytes === 0) return "0 Bytes";
+
+  const k = 1024;
+  const dm = decimals < 0 ? 0 : decimals;
+  const sizes = ["B", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"];
+
+  const i = Math.floor(Math.log(bytes) / Math.log(k));
+
+  return parseFloat(bytes / Math.pow(k, i)).toFixed(dm) + " " + sizes[i];
+}