create db completion service and mock data

This commit is contained in:
Jesse Lucas 2020-03-26 16:17:03 -04:00
parent bc7d71ee3d
commit 09673ba0c6
9 changed files with 107 additions and 8 deletions

View File

@ -20,9 +20,10 @@ export class DeviceChartComponent implements OnInit {
ngOnInit(): void { }
ngAfterViewInit(): void {
// TODO switch to deviceService
this.deviceService.getAll().subscribe(
device => {
console.log("device", device);
// Get StateType and convert to string
const stateType: Device.StateType = Device.getStateType(device);
const state: string = Device.stateTypeToString(stateType);

6
src/app/completion.ts Normal file
View File

@ -0,0 +1,6 @@
export interface Completion {
completion: number;
globalBytes: number;
needBytes: number;
needDeletes: number;
}

View File

@ -0,0 +1,24 @@
export const dbCompletion =
[
{
"device": "YZJBJFX-RDBL7WY-6ZGKJ2D-4MJB4E7-ZATSDUY-LD6Y3L3-MLFUYWE-AEMXJAC",
"completion": 100,
"globalBytes": 156793013575,
"needBytes": 0,
"needDeletes": 0
},
{
"completion": 80,
"globalBytes": 3013575,
"needBytes": 100,
"needDeletes": 0
}
]
/*
{
"completion": 100,
"globalBytes": 156793013575,
"needBytes": 0,
"needDeletes": 0
}
*/

View File

@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { DbCompletionService } from './db-completion.service';
describe('DbCompletionService', () => {
let service: DbCompletionService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(DbCompletionService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@ -0,0 +1,43 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { environment } from 'src/environments/environment';
import { apiURL, apiRetry } from '../api-utils';
import { Completion } from '../completion';
import { retry, map } from 'rxjs/operators';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class DbCompletionService {
private dbStatusUrl = environment.production ? apiURL + 'rest/db/completion' : 'api/dbCompletion';
constructor(private http: HttpClient) { }
getDeviceCompletion(id: string): Observable<Completion> {
let httpOptions: { params: HttpParams };
if (id) {
httpOptions = {
params: new HttpParams().set('device', id)
};
} else { }
return this.http
.get<Completion>(this.dbStatusUrl, httpOptions)
.pipe(
retry(apiRetry),
map(res => {
// Remove from array in developement
// in-memory-web-api returns arrays
if (!environment.production) {
const a: any = res as any;
if (a.length > 0) {
res = res[0];
}
}
return res;
})
);
}
}

View File

@ -15,7 +15,7 @@ import Folder from '../folder'
export class DbStatusService {
private dbStatusUrl = environment.production ? apiURL + 'rest/db/status' : 'api/dbStatus';
constructor(private http: HttpClient, private cookieService: CookieService) { }
constructor(private http: HttpClient) { }
getFolderStatus(id: string): Observable<Folder.Status> {
let httpOptions: { params: HttpParams };

View File

@ -3,6 +3,7 @@ import Device from '../device';
import { Observable } from 'rxjs';
import { SystemConfigService } from './system-config.service';
import { SystemConnectionsService } from './system-connections.service';
import { DbCompletionService } from './db-completion.service';
@Injectable({
providedIn: 'root'
@ -11,7 +12,8 @@ export class DeviceService {
private devices: Device[];
constructor(
private systemConfigService: SystemConfigService,
private systemConnectionsService: SystemConnectionsService
private systemConnectionsService: SystemConnectionsService,
private dbCompletionService: DbCompletionService
) { }
getAll(): Observable<Device> {
@ -40,9 +42,16 @@ export class DeviceService {
connections => {
// TODO: check connection and request total
this.devices.forEach(device => {
observer.next(device);
// TODO make this synchronous
this.dbCompletionService.getDeviceCompletion(device.deviceID).subscribe(
c => {
device.completion = c.completion;
observer.next(device);
});
//TODO complete observer when finished
// observer.complete();
});
observer.complete();
}
);

View File

@ -2,13 +2,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'
import { dbCompletion } from '../mocks/mock-db-completion'
@Injectable({
providedIn: 'root'
})
export class InMemoryConfigDataService {
createDb() {
return { config, dbStatus, connections };
return { config, dbStatus, connections, dbCompletion };
}
constructor() { }

View File

@ -59,10 +59,9 @@ export class SystemConfigService {
return folderObservable;
}
// TODO switch to devices
getDevices(): Observable<Device[]> {
const deviceObserverable: Observable<Device[]> = new Observable((observer) => {
if (this.folders) {
if (this.devices) {
observer.next(this.devices);
} else {
let t = setInterval(() => {