From cafedbed04a61dfde0778db7f8db91b74f6a6b5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rik=20Levente?= Date: Wed, 1 Nov 2023 17:47:18 +0100 Subject: [PATCH] Network usage page --- core/Classes/HardwareInfo.cs | 10 +- core/Classes/Interfaces.cs | 6 +- core/MainWindow.xaml | 5 + interface/layout/app.svelte | 34 ++++- interface/layout/types.d.ts | 9 ++ interface/pages/network.svelte | 166 +++++++++++++++++++++++++ interface/pages/system.svelte | 35 +----- interface/stores/hardwareStatistics.ts | 2 +- 8 files changed, 228 insertions(+), 39 deletions(-) create mode 100644 interface/pages/network.svelte diff --git a/core/Classes/HardwareInfo.cs b/core/Classes/HardwareInfo.cs index 5c3a4dc..52d12f6 100644 --- a/core/Classes/HardwareInfo.cs +++ b/core/Classes/HardwareInfo.cs @@ -381,11 +381,17 @@ public void GetInfo() { } // Load - if (sensor[j].SensorType == SensorType.Load) { + if (sensor[j].SensorType == SensorType.Data) { // find interface by name and overwrite value for (int k = 0; k < API.System.Network.Interfaces.Count; k++) { if (API.System.Network.Interfaces[k].Name == computerHardware[i].Name) { - API.System.Network.Interfaces[k].Load = (float)Math.Round((float)sensor[j].Value); + if (sensor[j].Name.Contains("Download")) { + API.System.Network.Interfaces[k].DownloadData = (float)Math.Round((float)sensor[j].Value, 1); + } + + if (sensor[j].Name.Contains("Upload")) { + API.System.Network.Interfaces[k].UploadData = (float)Math.Round((float)sensor[j].Value, 1); + } } } } diff --git a/core/Classes/Interfaces.cs b/core/Classes/Interfaces.cs index 1564a10..2b3d5a9 100644 --- a/core/Classes/Interfaces.cs +++ b/core/Classes/Interfaces.cs @@ -116,7 +116,11 @@ public string Speed { get; set; } - public float Load { + public float UploadData { + get; set; + } = new(); + + public float DownloadData { get; set; } = new(); diff --git a/core/MainWindow.xaml b/core/MainWindow.xaml index c98ae48..6dde3d2 100644 --- a/core/MainWindow.xaml +++ b/core/MainWindow.xaml @@ -57,6 +57,11 @@ + + + + + diff --git a/interface/layout/app.svelte b/interface/layout/app.svelte index fb13edd..7c46233 100644 --- a/interface/layout/app.svelte +++ b/interface/layout/app.svelte @@ -21,6 +21,10 @@ + + + + @@ -48,6 +52,7 @@ // @ts-ignore import { Boundary } from "@crownframework/svelte-error-boundary" import { setHardwareInfo } from "../stores/hardwareInfo" + import Network from "../pages/network.svelte" onMount(() => { // Navigate to the home page on load (webview bug) @@ -79,7 +84,6 @@ // @ts-ignore - Receive navigation info window.chrome.webview.addEventListener("message", (arg: { data: Message }) => { - console.log(arg.data) if (arg.data.name === "navigation") { if (arg.data.content === "home") { router.goto("/") @@ -158,6 +162,13 @@ fan: Math.round(input.gpu.fan.reduce((a, b) => a + b.value, 0)), memory: parseFloat(input.gpu.memory[0].value.toFixed(1)), }, + + network: input.system.network.interfaces.map((int) => { + return { + throughputUpload: parseFloat((int.throughputUpload / 1_048_576).toFixed(2)), + throughputDownload: parseFloat((int.throughputDownload / 1_048_576).toFixed(2)), + } + }), } if (date.getTime() < new Date().getTime()) { @@ -270,6 +281,27 @@ ).toFixed(1), ), }, + + network: input.system.network.interfaces.map((item, i) => { + let throughputDownload = parseFloat( + ( + $hardwareStatistics.seconds.map((sensor) => sensor.network[i]).reduce((a, b) => a + b.throughputDownload, 0) / + $hardwareStatistics.seconds.length + ).toFixed(2), + ) + + let throughputUpload = parseFloat( + ( + $hardwareStatistics.seconds.map((sensor) => sensor.network[i]).reduce((a, b) => a + b.throughputUpload, 0) / + $hardwareStatistics.seconds.length + ).toFixed(2), + ) + + return { + throughputUpload, + throughputDownload, + } + }), } // Update 60s timer diff --git a/interface/layout/types.d.ts b/interface/layout/types.d.ts index 0d2d9a0..1dc627e 100644 --- a/interface/layout/types.d.ts +++ b/interface/layout/types.d.ts @@ -75,6 +75,10 @@ declare global { gateway: string dns: string speed: string + throughputDownload: number + throughputUpload: number + downloadData: number + uploadData: number } interface HardwareInfo { @@ -161,6 +165,11 @@ declare global { physicalUsage: number virtualUsage: number } + + network: { + throughputDownload: number + throughputUpload: number + }[] } interface HardwareStatistics { diff --git a/interface/pages/network.svelte b/interface/pages/network.svelte new file mode 100644 index 0000000..5d45fe1 --- /dev/null +++ b/interface/pages/network.svelte @@ -0,0 +1,166 @@ +
+
+
+ +
+
+ +

Interfaces

+
+ {#each $hardwareInfo.system.network.interfaces as { name, description, ipAddress, mask, gateway, dns, speed, macAddress }} +
+

Name: {name}

+

Description: {description}

+

Address: {ipAddress} ({mask})

+

Gateway: {gateway} ({dns})

+

Speed: {speed} Mbit/s

+
+ {/each} +
+ + +
+
+ +

Data usage

+
+ {#each $hardwareInfo.system.network.interfaces as { name, downloadData, uploadData }} +
+

Name: {name}

+

Downloaded data: {downloadData} GB

+

Uploaded data: {uploadData} GB

+
+ {/each} +
+
+ +
+ + {#each $hardwareInfo.system.network.interfaces as item, i} +
+
+
+ +

{item.name} Download speed

+
+
+
(minutes = !minutes)}>{minutes ? "Last 60 minutes" : "Last 60 seconds"}
+
+
+
+ value.network[i].throughputDownload) + : $hardwareStatistics.seconds.map((value) => value.network[i].throughputDownload)} + type={"Download speed"} + unit={" MB/s"} + color={"#00bbf9"} + time={minutes ? "m" : "s"} + zero={true} + /> +
+
+ {/each} +
+ +
+ + {#each $hardwareInfo.system.network.interfaces as item, i} +
+
+
+ +

{item.name} Upload speed

+
+
+
(minutes = !minutes)}>{minutes ? "Last 60 minutes" : "Last 60 seconds"}
+
+
+
+ value.network[i].throughputUpload) + : $hardwareStatistics.seconds.map((value) => value.network[i].throughputUpload)} + type={"Upload speed"} + unit={" MB/s"} + color={"#00bbf9"} + time={minutes ? "m" : "s"} + zero={true} + /> +
+
+ {/each} +
+
+
+ + diff --git a/interface/pages/system.svelte b/interface/pages/system.svelte index e158695..c7868c0 100644 --- a/interface/pages/system.svelte +++ b/interface/pages/system.svelte @@ -22,7 +22,7 @@
-
+
{/each}
- - -
-
- -

Interfaces

-
- {#each $hardwareInfo.system.network.interfaces as { name, description, ipAddress, mask, gateway, dns, speed, macAddress }} -
-

Name: {name}

-

Description: {description}

-

Address: {ipAddress} ({mask})

-

Gateway: {gateway} ({dns})

-

Speed: {speed} Mbit/s

-
- {/each} -
diff --git a/interface/stores/hardwareStatistics.ts b/interface/stores/hardwareStatistics.ts index 7452308..dd7ec47 100644 --- a/interface/stores/hardwareStatistics.ts +++ b/interface/stores/hardwareStatistics.ts @@ -8,7 +8,7 @@ const defaultHardwareStatistics: HardwareStatistics = { export const hardwareStatistics = writable(sessionStorage.hardwareStatistics ? JSON.parse(sessionStorage.hardwareStatistics) : defaultHardwareStatistics) hardwareStatistics.subscribe((data) => { - // console.log("HardwareStatistics changed: ", data) + console.log("HardwareStatistics changed: ", data) sessionStorage.setItem("hardwareStatistics", JSON.stringify(data)) })