From b7f3425f362951467dcdda7a9c3081cdb2d5a861 Mon Sep 17 00:00:00 2001 From: Jesse Lucas Date: Fri, 27 Mar 2020 09:59:40 -0400 Subject: [PATCH] add system status service and mock data --- src/app/mocks/mock-system-status.ts | 59 +++++++++++++++++++ src/app/services/device.service.ts | 56 +++++++++++------- .../services/in-memory-config-data.service.ts | 11 ++-- .../services/system-status.service.spec.ts | 16 +++++ src/app/services/system-status.service.ts | 28 +++++++++ src/app/system-status.ts | 17 ++++++ 6 files changed, 162 insertions(+), 25 deletions(-) create mode 100644 src/app/mocks/mock-system-status.ts create mode 100644 src/app/services/system-status.service.spec.ts create mode 100644 src/app/services/system-status.service.ts create mode 100644 src/app/system-status.ts diff --git a/src/app/mocks/mock-system-status.ts b/src/app/mocks/mock-system-status.ts new file mode 100644 index 000000000..1a6472f19 --- /dev/null +++ b/src/app/mocks/mock-system-status.ts @@ -0,0 +1,59 @@ +export const systemStatus = { + "alloc": 30618136, + "connectionServiceStatus": { + "dynamic+https://relays.syncthing.net/endpoint": { + "error": null, + "lanAddresses": [ + "relay://23.92.71.120:443/?id=53STGR7-YBM6FCX-PAZ2RHM-YPY6OEJ-WYHVZO7-PCKQRCK-PZLTP7T-434XCAD&pingInterval=1m0s&networkTimeout=2m0s&sessionLimitBps=0&globalLimitBps=0&statusAddr=:22070&providedBy=canton7" + ], + "wanAddresses": [ + "relay://23.92.71.120:443/?id=53STGR7-YBM6FCX-PAZ2RHM-YPY6OEJ-WYHVZO7-PCKQRCK-PZLTP7T-434XCAD&pingInterval=1m0s&networkTimeout=2m0s&sessionLimitBps=0&globalLimitBps=0&statusAddr=:22070&providedBy=canton7" + ] + }, + "tcp://0.0.0.0:22000": { + "error": null, + "lanAddresses": [ + "tcp://0.0.0.0:22000" + ], + "wanAddresses": [ + "tcp://0.0.0.0:22000" + ] + } + }, + "cpuPercent": 0, + "discoveryEnabled": true, + "discoveryErrors": { + "global@https://discovery-v4-1.syncthing.net/v2/": "500 Internal Server Error", + "global@https://discovery-v4-2.syncthing.net/v2/": "Post https://discovery-v4-2.syncthing.net/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)", + "global@https://discovery-v4-3.syncthing.net/v2/": "Post https://discovery-v4-3.syncthing.net/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)", + "global@https://discovery-v6-1.syncthing.net/v2/": "Post https://discovery-v6-1.syncthing.net/v2/: dial tcp [2001:470:28:4d6::5]:443: connect: no route to host", + "global@https://discovery-v6-2.syncthing.net/v2/": "Post https://discovery-v6-2.syncthing.net/v2/: dial tcp [2604:a880:800:10::182:a001]:443: connect: no route to host", + "global@https://discovery-v6-3.syncthing.net/v2/": "Post https://discovery-v6-3.syncthing.net/v2/: dial tcp [2400:6180:0:d0::d9:d001]:443: connect: no route to host" + }, + "discoveryMethods": 8, + "goroutines": 49, + "lastDialStatus": { + "tcp://10.20.30.40": { + "when": "2019-05-16T07:41:23Z", + "error": "dial tcp 10.20.30.40:22000: i/o timeout" + }, + "tcp://172.16.33.3:22000": { + "when": "2019-05-16T07:40:43Z", + "ok": true + }, + "tcp://83.233.120.221:22000": { + "when": "2019-05-16T07:41:13Z", + "error": "dial tcp 83.233.120.221:22000: connect: connection refused" + } + }, + "myID": "YZJBJFX-RDBL7WY-6ZGKJ2D-4MJB4E7-ZATSDUY-LD6Y3L3-MLFUYWE-AEMXJAC", + "pathSeparator": "/", + "startTime": "2016-06-06T19:41:43.039284753+02:00", + "sys": 42092792, + "themes": [ + "default", + "dark" + ], + "tilde": "/Users/jb", + "uptime": 2635 +} \ No newline at end of file diff --git a/src/app/services/device.service.ts b/src/app/services/device.service.ts index 37f7d84f7..a988af366 100644 --- a/src/app/services/device.service.ts +++ b/src/app/services/device.service.ts @@ -5,6 +5,7 @@ import { SystemConfigService } from './system-config.service'; import { SystemConnectionsService } from './system-connections.service'; import { DbCompletionService } from './db-completion.service'; import { SystemConnections } from '../connections'; +import { SystemStatusService } from './system-status.service'; @Injectable({ providedIn: 'root' @@ -12,11 +13,13 @@ import { SystemConnections } from '../connections'; export class DeviceService { private devices: Device[]; private sysConns: SystemConnections; + thisDevice: Device; constructor( private systemConfigService: SystemConfigService, private systemConnectionsService: SystemConnectionsService, - private dbCompletionService: DbCompletionService + private dbCompletionService: DbCompletionService, + private systemStatusService: SystemStatusService ) { } getDeviceStatusInOrder(observer: Subscriber, startIndex: number) { @@ -56,29 +59,42 @@ export class DeviceService { devices => { this.devices = devices; - // Check folder devices to see if the device is used - this.systemConfigService.getFolders().subscribe( - folders => { - // Loop through all folder devices to see if the device is used + // First check to see which device is local 'thisDevice' + this.systemStatusService.getSystemStatus().subscribe( + status => { this.devices.forEach(device => { - folders.forEach(folder => { - folder.devices.forEach(fdevice => { - if (device.deviceID === fdevice.deviceID) { - device.used = true; - } - }); - }); + if (device.deviceID === status.myID) { + console.log("found thisDevice!", device); + this.thisDevice = device; + } }); - // See if the connection is connected or undefined - this.systemConnectionsService.getSystemConnections().subscribe( - c => { - this.sysConns = c; + // TODO Determine if it should ignore thisDevice - // Synchronously get the status of each device - this.getDeviceStatusInOrder(observer, 0); - } - ); + // Check folder devices to see if the device is used + this.systemConfigService.getFolders().subscribe( + folders => { + // Loop through all folder devices to see if the device is used + this.devices.forEach(device => { + folders.forEach(folder => { + folder.devices.forEach(fdevice => { + if (device.deviceID === fdevice.deviceID) { + device.used = true; + } + }); + }); + }); + + // See if the connection is connected or undefined + this.systemConnectionsService.getSystemConnections().subscribe( + c => { + this.sysConns = c; + + // Synchronously get the status of each device + this.getDeviceStatusInOrder(observer, 0); + } + ); + }); } ) }, diff --git a/src/app/services/in-memory-config-data.service.ts b/src/app/services/in-memory-config-data.service.ts index b9b6c8089..d44fbb50c 100644 --- a/src/app/services/in-memory-config-data.service.ts +++ b/src/app/services/in-memory-config-data.service.ts @@ -1,15 +1,16 @@ import { Injectable } from '@angular/core'; -import { config } from '../mocks/mock-system-config' -import { dbStatus } from '../mocks/mock-db-status' -import { connections } from '../mocks/mock-system-connections' -import { dbCompletion } from '../mocks/mock-db-completion' +import { config } from '../mocks/mock-system-config'; +import { dbStatus } from '../mocks/mock-db-status'; +import { connections } from '../mocks/mock-system-connections'; +import { dbCompletion } from '../mocks/mock-db-completion'; +import { systemStatus } from '../mocks/mock-system-status'; @Injectable({ providedIn: 'root' }) export class InMemoryConfigDataService { createDb() { - return { config, dbStatus, connections, dbCompletion }; + return { config, dbStatus, connections, dbCompletion, systemStatus }; } constructor() { } diff --git a/src/app/services/system-status.service.spec.ts b/src/app/services/system-status.service.spec.ts new file mode 100644 index 000000000..8b47a3a6a --- /dev/null +++ b/src/app/services/system-status.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { SystemStatusService } from './system-status.service'; + +describe('SystemStatusService', () => { + let service: SystemStatusService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(SystemStatusService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/system-status.service.ts b/src/app/services/system-status.service.ts new file mode 100644 index 000000000..b5e88f1a5 --- /dev/null +++ b/src/app/services/system-status.service.ts @@ -0,0 +1,28 @@ +import { Injectable } from '@angular/core'; +import { environment } from 'src/environments/environment'; +import { apiURL, apiRetry } from '../api-utils'; +import { Observable } from 'rxjs'; +import { HttpClient } from '@angular/common/http'; +import { retry, map } from 'rxjs/operators'; +import { SystemStatus } from '../system-status'; + +@Injectable({ + providedIn: 'root' +}) +export class SystemStatusService { + + private systemStatusUrl = environment.production ? apiURL + 'rest/system/status' : 'api/systemStatus'; + + constructor(private http: HttpClient) { } + + getSystemStatus(): Observable { + return this.http + .get(this.systemStatusUrl) + .pipe( + retry(apiRetry), + map(res => { + return res; + }) + ); + } +} diff --git a/src/app/system-status.ts b/src/app/system-status.ts new file mode 100644 index 000000000..5f4be45ed --- /dev/null +++ b/src/app/system-status.ts @@ -0,0 +1,17 @@ +export interface SystemStatus { + alloc: number; + connectionServiceStatus: any; + cpuPercent: number; // allows returns 0 + discoveryEnabled: boolean; + discoveryErrors: any; + discoveryMethods: number; + goroutines: number; + lastDialStatus: any; + myID: string; + pathSeparator: string; + startTime: string; + sys: number; + themes: string[]; + tilde: string; + uptime: number; +} \ No newline at end of file