From bc7d71ee3d18de1345a59cf8398f719ff7237655 Mon Sep 17 00:00:00 2001 From: Jesse Lucas Date: Wed, 25 Mar 2020 21:36:24 -0400 Subject: [PATCH] Create system connections service, mocks, and work on updating devices chart --- .../device-chart/device-chart.component.ts | 18 +++---- .../folder-chart/folder-chart.component.ts | 4 +- src/app/connections.ts | 16 ++++++ src/app/mocks/mock-config.ts | 4 +- src/app/mocks/mock-connections.ts | 44 ++++++++++++++++ src/app/services/device.service.ts | 52 ++++++++++++++++++- .../services/in-memory-config-data.service.ts | 3 +- .../system-connections.service.spec.ts | 16 ++++++ .../services/system-connections.service.ts | 27 ++++++++++ 9 files changed, 167 insertions(+), 17 deletions(-) create mode 100644 src/app/connections.ts create mode 100644 src/app/mocks/mock-connections.ts create mode 100644 src/app/services/system-connections.service.spec.ts create mode 100644 src/app/services/system-connections.service.ts diff --git a/src/app/charts/device-chart/device-chart.component.ts b/src/app/charts/device-chart/device-chart.component.ts index b98aa113c..5fcdb69ed 100644 --- a/src/app/charts/device-chart/device-chart.component.ts +++ b/src/app/charts/device-chart/device-chart.component.ts @@ -1,8 +1,8 @@ import { Component, OnInit, ViewChild } from '@angular/core'; import { cardElevation } from '../../style'; import { DonutChartComponent } from '../donut-chart/donut-chart.component'; -import Folder from '../../folder' -import { FolderService } from 'src/app/services/folder.service'; +import { DeviceService } from 'src/app/services/device.service'; +import Device from '../../device'; @Component({ selector: 'app-device-chart', @@ -15,19 +15,17 @@ export class DeviceChartComponent implements OnInit { elevation: string = cardElevation; states: { label: string, count: number }[] = []; - constructor(private folderService: FolderService) { } + constructor(private deviceService: DeviceService) { } - ngOnInit(): void { - - } + ngOnInit(): void { } ngAfterViewInit(): void { // TODO switch to deviceService - this.folderService.getAll().subscribe( - folder => { + this.deviceService.getAll().subscribe( + device => { // Get StateType and convert to string - const stateType: Folder.StateType = Folder.getStateType(folder); - const state: string = Folder.stateTypeToString(stateType); + const stateType: Device.StateType = Device.getStateType(device); + const state: string = Device.stateTypeToString(stateType); // Check if state exists let found: boolean = false; diff --git a/src/app/charts/folder-chart/folder-chart.component.ts b/src/app/charts/folder-chart/folder-chart.component.ts index 8e965e71b..d3b184747 100644 --- a/src/app/charts/folder-chart/folder-chart.component.ts +++ b/src/app/charts/folder-chart/folder-chart.component.ts @@ -18,9 +18,7 @@ export class FolderChartComponent implements OnInit { constructor(private folderService: FolderService) { } ngOnInit(): void { - for (let state in Folder.StateType) { - // console.log(state); - } + // for (let state in Folder.StateType) { } } ngAfterViewInit() { diff --git a/src/app/connections.ts b/src/app/connections.ts new file mode 100644 index 000000000..bf5da1a55 --- /dev/null +++ b/src/app/connections.ts @@ -0,0 +1,16 @@ +export interface SystemConnections { + connections: { deviceId?: Connection }; + total: Connection; +} + +export interface Connection { + address: string; + at: string; + clientVersion: string; + connected: boolean; + crypto: string; + inBytesTotal: number; + outBytesTotal: number; + paused: boolean; + type: string; +} \ No newline at end of file diff --git a/src/app/mocks/mock-config.ts b/src/app/mocks/mock-config.ts index b09809ec0..b5cbe80a0 100644 --- a/src/app/mocks/mock-config.ts +++ b/src/app/mocks/mock-config.ts @@ -1,7 +1,7 @@ export const config = { "version": 15, "folders": [ - { "id": "GXWxf-3zgnU", "label": "MyFolder", "path": "...", "type": "sendreceive", "devices": [{ "deviceID": "..." }], "rescanIntervalS": 60, "ignorePerms": false, "autoNormalize": true, "minDiskFreePct": 1, "versioning": { "type": "simple", "params": { "keep": "5" } }, "copiers": 0, "pullers": 0, "hashers": 0, "order": "random", "ignoreDelete": false, "scanProgressIntervalS": 0, "pullerSleepS": 0, "pullerPauseS": 0, "maxConflicts": 10, "disableSparseFiles": false, "disableTempIndexes": false, "fsync": false, "invalid": "" }, + { "id": "GXWxf-3zgnU", "label": "MyFolder", "path": "...", "type": "sendreceive", "devices": [{ "deviceID": "YZJBJFX-RDBL7WY-6ZGKJ2D-4MJB4E7-ZATSDUY-LD6Y3L3-MLFUYWE-AEMXJAC" }], "rescanIntervalS": 60, "ignorePerms": false, "autoNormalize": true, "minDiskFreePct": 1, "versioning": { "type": "simple", "params": { "keep": "5" } }, "copiers": 0, "pullers": 0, "hashers": 0, "order": "random", "ignoreDelete": false, "scanProgressIntervalS": 0, "pullerSleepS": 0, "pullerPauseS": 0, "maxConflicts": 10, "disableSparseFiles": false, "disableTempIndexes": false, "fsync": false, "invalid": "" }, { "id": "Tyeho-ncvqp", "label": "MyFolder", "path": "...", "type": "sendreceive", "devices": [{ "deviceID": "..." }], "rescanIntervalS": 60, "ignorePerms": false, "autoNormalize": true, "minDiskFreePct": 1, "versioning": { "type": "simple", "params": { "keep": "5" } }, "copiers": 0, "pullers": 0, "hashers": 0, "order": "random", "ignoreDelete": false, "scanProgressIntervalS": 0, "pullerSleepS": 0, "pullerPauseS": 0, "maxConflicts": 10, "disableSparseFiles": false, "disableTempIndexes": false, "fsync": false, "invalid": "" }, { "id": "Ihpqp-3zgnq", "label": "MyFolder", "path": "...", "type": "sendreceive", "devices": [{ "deviceID": "..." }], "rescanIntervalS": 60, "ignorePerms": false, "autoNormalize": true, "minDiskFreePct": 1, "versioning": { "type": "simple", "params": { "keep": "5" } }, "copiers": 0, "pullers": 0, "hashers": 0, "order": "random", "ignoreDelete": false, "scanProgressIntervalS": 0, "pullerSleepS": 0, "pullerPauseS": 0, "maxConflicts": 10, "disableSparseFiles": false, "disableTempIndexes": false, "fsync": false, "invalid": "" }, { "id": "Abqqp-3zgnU", "label": "MyFolder", "path": "...", "type": "sendreceive", "devices": [{ "deviceID": "..." }], "rescanIntervalS": 60, "ignorePerms": false, "autoNormalize": true, "minDiskFreePct": 1, "versioning": { "type": "simple", "params": { "keep": "5" } }, "copiers": 0, "pullers": 0, "hashers": 0, "order": "random", "ignoreDelete": false, "scanProgressIntervalS": 0, "pullerSleepS": 0, "pullerPauseS": 0, "maxConflicts": 10, "disableSparseFiles": false, "disableTempIndexes": false, "fsync": false, "invalid": "" }, @@ -11,7 +11,7 @@ export const config = { { "id": "Poqff-3zgnU", "label": "MyFolder", "path": "...", "type": "sendreceive", "devices": [{ "deviceID": "..." }], "rescanIntervalS": 60, "ignorePerms": false, "autoNormalize": true, "minDiskFreePct": 1, "versioning": { "type": "simple", "params": { "keep": "5" } }, "copiers": 0, "pullers": 0, "hashers": 0, "order": "random", "ignoreDelete": false, "scanProgressIntervalS": 0, "pullerSleepS": 0, "pullerPauseS": 0, "maxConflicts": 10, "disableSparseFiles": false, "disableTempIndexes": false, "fsync": false, "invalid": "" }, ], "devices": [ - { "deviceID": "...", "name": "Laptop", "addresses": ["dynamic", "tcp://192.168.1.2:22000"], "compression": "metadata", "certName": "", "introducer": false }, + { "deviceID": "YZJBJFX-RDBL7WY-6ZGKJ2D-4MJB4E7-ZATSDUY-LD6Y3L3-MLFUYWE-AEMXJAC", "name": "Laptop", "addresses": ["dynamic", "tcp://192.168.1.2:22000"], "compression": "metadata", "certName": "", "introducer": false }, { "deviceID": "...", "name": "Server", "addresses": ["dynamic", "tcp://192.168.1.3:22000"], "compression": "metadata", "certName": "", "introducer": false }, ], "gui": { diff --git a/src/app/mocks/mock-connections.ts b/src/app/mocks/mock-connections.ts new file mode 100644 index 000000000..4e77f506f --- /dev/null +++ b/src/app/mocks/mock-connections.ts @@ -0,0 +1,44 @@ +export const connections = { + "total": { + "paused": false, + "clientVersion": "", + "at": "2015-11-07T17:29:47.691637262+01:00", + "connected": false, + "inBytesTotal": 1479, + "type": "", + "outBytesTotal": 1318, + "address": "" + }, + "connections": { + "YZJBJFX-RDBL7WY-6ZGKJ2D-4MJB4E7-ZATSDUY-LD6Y3L3-MLFUYWE-AEMXJAC": { + "connected": true, + "inBytesTotal": 556, + "paused": false, + "at": "2015-11-07T17:29:47.691548971+01:00", + "clientVersion": "v0.12.1", + "address": "127.0.0.1:22002", + "type": "TCP (Client)", + "outBytesTotal": 550 + }, + "DOVII4U-SQEEESM-VZ2CVTC-CJM4YN5-QNV7DCU-5U3ASRL-YVFG6TH-W5DV5AA": { + "outBytesTotal": 0, + "type": "", + "address": "", + "at": "0001-01-01T00:00:00Z", + "clientVersion": "", + "paused": false, + "inBytesTotal": 0, + "connected": false + }, + "UYGDMA4-TPHOFO5-2VQYDCC-7CWX7XW-INZINQT-LE4B42N-4JUZTSM-IWCSXA4": { + "address": "", + "type": "", + "outBytesTotal": 0, + "connected": false, + "inBytesTotal": 0, + "paused": false, + "at": "0001-01-01T00:00:00Z", + "clientVersion": "" + } + } +} \ No newline at end of file diff --git a/src/app/services/device.service.ts b/src/app/services/device.service.ts index 2f8081871..3de7277dc 100644 --- a/src/app/services/device.service.ts +++ b/src/app/services/device.service.ts @@ -1,10 +1,60 @@ import { Injectable } from '@angular/core'; import Device from '../device'; +import { Observable } from 'rxjs'; +import { SystemConfigService } from './system-config.service'; +import { SystemConnectionsService } from './system-connections.service'; @Injectable({ providedIn: 'root' }) export class DeviceService { private devices: Device[]; - constructor() { } + constructor( + private systemConfigService: SystemConfigService, + private systemConnectionsService: SystemConnectionsService + ) { } + + getAll(): Observable { + const deviceObservable: Observable = new Observable((observer) => { + this.systemConfigService.getDevices().subscribe( + devices => { + this.devices = devices; + + // Check folder devices to see if the device is used + this.systemConfigService.getFolders().subscribe( + folders => { + // TODO: streamline + // 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( + connections => { + // TODO: check connection and request total + this.devices.forEach(device => { + observer.next(device); + }); + observer.complete(); + } + ); + + // Synchronously get the status of each device + // this.getDeviceStatusInOrder(observer, 0); + } + ) + }, + err => { console.log("getAll error!", err) }, + () => { console.log("get all complete!") } + ); + }); + return deviceObservable + } } diff --git a/src/app/services/in-memory-config-data.service.ts b/src/app/services/in-memory-config-data.service.ts index 5e24e334c..d2cb7b073 100644 --- a/src/app/services/in-memory-config-data.service.ts +++ b/src/app/services/in-memory-config-data.service.ts @@ -1,13 +1,14 @@ import { Injectable } from '@angular/core'; import { config } from '../mocks/mock-config' import { dbStatus } from '../mocks/mock-db-status' +import { connections } from '../mocks/mock-connections' @Injectable({ providedIn: 'root' }) export class InMemoryConfigDataService { createDb() { - return { config, dbStatus }; + return { config, dbStatus, connections }; } constructor() { } diff --git a/src/app/services/system-connections.service.spec.ts b/src/app/services/system-connections.service.spec.ts new file mode 100644 index 000000000..bac82fabb --- /dev/null +++ b/src/app/services/system-connections.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { SystemConnectionsService } from './system-connections.service'; + +describe('SystemConnectionsService', () => { + let service: SystemConnectionsService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(SystemConnectionsService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/system-connections.service.ts b/src/app/services/system-connections.service.ts new file mode 100644 index 000000000..09a66c8a4 --- /dev/null +++ b/src/app/services/system-connections.service.ts @@ -0,0 +1,27 @@ +import { Injectable } from '@angular/core'; +import { environment } from 'src/environments/environment'; +import { apiURL, apiRetry } from '../api-utils'; +import { HttpClient } from '@angular/common/http'; +import { retry, map } from 'rxjs/operators'; +import { Observable } from 'rxjs'; +import { SystemConnections } from '../connections'; + +@Injectable({ + providedIn: 'root' +}) +export class SystemConnectionsService { + private systemConfigUrl = environment.production ? apiURL + 'rest/system/connections' : 'api/connections'; + + constructor(private http: HttpClient) { } + + getSystemConnections(): Observable { + return this.http + .get(this.systemConfigUrl) + .pipe( + retry(apiRetry), + map(res => { + return res; + }) + ); + } +}