From b32426871a37266c1acdeeddde89de9e867fdf6f Mon Sep 17 00:00:00 2001
From: paflov <paf@titelfrei.de>
Date: Thu, 26 Sep 2019 16:01:15 +0200
Subject: [PATCH] make use of modern browser's NetworkInformation-Interface to
 collect even more data

---
 .gitignore                                    |  4 ++
 .../network-check.component.html              | 10 +++
 .../network-check/network-check.component.ts  | 63 ++++++++++++++++++-
 3 files changed, 75 insertions(+), 2 deletions(-)

diff --git a/.gitignore b/.gitignore
index ee5c9d83..1f84d74f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -37,3 +37,7 @@ testem.log
 # System Files
 .DS_Store
 Thumbs.db
+
+
+src/environments/environment.ts
+.gitignore
diff --git a/src/app/sys-check/network-check/network-check.component.html b/src/app/sys-check/network-check/network-check.component.html
index f68601b5..d57508de 100644
--- a/src/app/sys-check/network-check/network-check.component.html
+++ b/src/app/sys-check/network-check/network-check.component.html
@@ -34,4 +34,14 @@
     </mat-card-content>
   </mat-card>
 
+  <mat-card>
+    <mat-card-title>Netzwerkeigenschaften</mat-card-title>
+    <mat-card-content>
+      <p *ngIf="!detectedNetworkInformations.available">Ihr Browser ist veraltet, daher konnten keine weiteren Netzwerkeigenschaften ermittelt werden.</p>
+      <p *ngIf="detectedNetworkInformations.available">Verbindung: {{detectedNetworkInformations.effectiveNetworkType}} {{detectedNetworkInformations.networkType}}</p>
+      <p *ngIf="detectedNetworkInformations.roundTripTimeMs">Network RoundTrip: {{detectedNetworkInformations.roundTripTimeMs}} Millisekunden</p>
+      <p *ngIf="detectedNetworkInformations.downlinkMegabitPerSecond">Network Downlink: {{detectedNetworkInformations.downlinkMegabitPerSecond}} mbps</p>
+    </mat-card-content>
+  </mat-card>
+
 </div>
diff --git a/src/app/sys-check/network-check/network-check.component.ts b/src/app/sys-check/network-check/network-check.component.ts
index 7c3ea8f6..d482fdae 100644
--- a/src/app/sys-check/network-check/network-check.component.ts
+++ b/src/app/sys-check/network-check/network-check.component.ts
@@ -32,6 +32,14 @@ interface NetworkRating {
   overallRating: TechCheckRating;
 }
 
+interface DetectedNetworkInformations {
+  available: boolean;
+  downlinkMegabitPerSecond: number;
+  effectiveNetworkType: string;
+  roundTripTimeMs: number;
+  networkType: string;
+}
+
 @Component({
   selector: 'iqb-network-check',
   templateUrl: './network-check.component.html',
@@ -75,11 +83,18 @@ export class NetworkCheckComponent implements OnInit {
     overallRating: 'N/A'
   };
 
+  private detectedNetworkInformations: DetectedNetworkInformations = {
+    downlinkMegabitPerSecond: null,
+    effectiveNetworkType: null,
+    roundTripTimeMs: null,
+    networkType: null,
+    available: false
+  };
+
   constructor(
     private ds: SyscheckDataService,
     private bs: BackendService
-  ) {
-  }
+  ) {}
 
   ngOnInit() {}
 
@@ -101,6 +116,8 @@ export class NetworkCheckComponent implements OnInit {
     this.plotPrepare(BenchmarkType.down);
     this.plotPrepare(BenchmarkType.up);
 
+    this.getBrowsersNativeNetworkInformations();
+
     this.loopBenchmarkSequence(BenchmarkType.down)
       .then(() => this.loopBenchmarkSequence(BenchmarkType.up))
       .then(() => this.reportResults())
@@ -270,6 +287,32 @@ export class NetworkCheckComponent implements OnInit {
     reportEntry.push({id: '0', type: 'network', label: 'Uploadbewertung', value: this.networkRating.uploadRating});
     reportEntry.push({id: '0', type: 'network', label: 'Allgemeine Bewertung der Verbindung', value: this.networkRating.overallRating});
 
+    if (this.detectedNetworkInformations.available) {
+      if (this.detectedNetworkInformations.roundTripTimeMs) {
+        reportEntry.push({
+          id: '0', type: 'network', label: 'RoundTrip in Ms',
+          value: this.detectedNetworkInformations.roundTripTimeMs.toString()
+        });
+      }
+      if (this.detectedNetworkInformations.effectiveNetworkType) {
+        reportEntry.push({
+          id: '0', type: 'network', label: 'Netzwerktyp nach Leistung',
+          value: this.detectedNetworkInformations.effectiveNetworkType
+        });
+      }
+      if (this.detectedNetworkInformations.networkType) {
+        reportEntry.push({
+          id: '0', type: 'network', label: 'Netzwerktyp',
+          value: this.detectedNetworkInformations.networkType
+        });
+      }
+      if (this.detectedNetworkInformations.downlinkMegabitPerSecond) {
+        reportEntry.push({
+          id: '0', type: 'network', label: 'Downlink mbps',
+          value: this.detectedNetworkInformations.downlinkMegabitPerSecond.toString()
+        });
+      }
+    }
     this.ds.networkData$.next(reportEntry);
   }
 
@@ -330,6 +373,22 @@ export class NetworkCheckComponent implements OnInit {
   }
 
 
+  private getBrowsersNativeNetworkInformations() {
+
+    const connection = navigator['connection'] || navigator['mozConnection'] || navigator['webkitConnection'];
+    console.log('connection', connection);
+    if (connection) {
+      this.detectedNetworkInformations = {
+        available: true,
+        downlinkMegabitPerSecond: connection.downlink || null,
+        effectiveNetworkType: connection.effectiveType || null,
+        roundTripTimeMs: connection.rtt || null,
+        networkType: connection.type || null,
+      };
+    }
+  }
+
+
   private humanReadableBytes(bytes: number): string {
 
     const units = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB'];
-- 
GitLab