Refactor Folder interface to use Folder namespace

This commit is contained in:
Jesse Lucas 2020-03-19 19:01:45 -04:00
parent c27fe5694d
commit 32c849e18c
7 changed files with 112 additions and 63 deletions

View File

@ -1,10 +1,9 @@
import { Component, OnInit, ViewChild } from '@angular/core'; import { Component, OnInit, ViewChild } from '@angular/core';
import { Folder } from '../../folder' import Folder from '../../folder'
import { cardElevation } from '../../style' import { cardElevation } from '../../style'
import { FolderService } from 'src/app/folder.service'; import { FolderService } from 'src/app/folder.service';
import { SystemConfigService } from 'src/app/system-config.service'; import { SystemConfigService } from 'src/app/system-config.service';
import { DbStatusService } from 'src/app/db-status.service'; import { DbStatusService } from 'src/app/db-status.service';
import { flatMap } from 'rxjs/operators';
import { DonutChartComponent } from '../donut-chart/donut-chart.component'; import { DonutChartComponent } from '../donut-chart/donut-chart.component';
@Component({ @Component({
@ -16,6 +15,7 @@ export class FolderChartComponent implements OnInit {
@ViewChild(DonutChartComponent) donutChart: DonutChartComponent; @ViewChild(DonutChartComponent) donutChart: DonutChartComponent;
chartID: string = 'foldersChart'; chartID: string = 'foldersChart';
elevation: string = cardElevation; elevation: string = cardElevation;
folderStates: Folder.stateType[];
constructor( constructor(
private systemConfigService: SystemConfigService, private systemConfigService: SystemConfigService,
@ -24,6 +24,7 @@ export class FolderChartComponent implements OnInit {
) { } ) { }
ngOnInit(): void { ngOnInit(): void {
//
} }
ngAfterViewInit() { ngAfterViewInit() {
@ -33,7 +34,9 @@ export class FolderChartComponent implements OnInit {
// TODO: Clear existing data // TODO: Clear existing data
this.donutChart.data([40]); this.donutChart.data([40]);
console.log("folder?", folder) // state?
const state: string = Folder.statusToString(folder);
console.log("folder state?", state, folder);
} }
); );

View File

@ -7,7 +7,7 @@ import { map, retry, catchError } from 'rxjs/operators';
import { environment } from '../environments/environment' import { environment } from '../environments/environment'
import { apiURL, apiRetry } from './api-utils' import { apiURL, apiRetry } from './api-utils'
import { FolderStatus, Folder } from './folder' import Folder from './folder'
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -21,7 +21,7 @@ export class DbStatusService {
this.headers = new HttpHeaders(this.cookieService.getCSRFHeader()) this.headers = new HttpHeaders(this.cookieService.getCSRFHeader())
} }
getFolderStatus(id: string): Observable<FolderStatus> { getFolderStatus(id: string): Observable<Folder.Status> {
let httpOptions: { headers: HttpHeaders } | let httpOptions: { headers: HttpHeaders } |
{ headers: HttpHeaders, params: HttpParams }; { headers: HttpHeaders, params: HttpParams };
if (id) { if (id) {
@ -34,17 +34,19 @@ export class DbStatusService {
} }
return this.http return this.http
.get<FolderStatus>(this.dbStatusUrl, httpOptions) .get<Folder.Status>(this.dbStatusUrl, httpOptions)
.pipe( .pipe(
retry(apiRetry), retry(apiRetry),
map(res => { map(res => {
// Remove from array in developement // Remove from array in developement
// in-memory-web-api returns arrays // in-memory-web-api returns arrays
if (!environment.production) { if (!environment.production) {
console.log("status res!", res);
const a: any = res as any; const a: any = res as any;
if (a.length > 0) { if (a.length > 0) {
return res[0]; return res[0];
} }
return {};
} }
return res; return res;
}) })

View File

@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
import { SystemConfigService } from './system-config.service'; import { SystemConfigService } from './system-config.service';
import { Observable, Subscriber } from 'rxjs'; import { Observable, Subscriber } from 'rxjs';
import { map } from 'rxjs/operators'; import { map } from 'rxjs/operators';
import { Folder } from './folder'; import Folder from './folder';
import { DbStatusService } from './db-status.service'; import { DbStatusService } from './db-status.service';
@Injectable({ @Injectable({
@ -40,30 +40,16 @@ export class FolderService {
* set all their statuses * set all their statuses
*/ */
getAll(): Observable<Folder> { getAll(): Observable<Folder> {
const _this = this;
const folderObservable: Observable<Folder> = new Observable((observer) => { const folderObservable: Observable<Folder> = new Observable((observer) => {
this.systemConfigService.getFolders().subscribe({ this.systemConfigService.getFolders().subscribe(
next(folders) { folders => {
_this.folders = folders; this.folders = folders;
// Synchronously get the status of each folder // Synchronously get the status of each folder
_this.getFolderStatusInOrder(observer, 0); this.getFolderStatusInOrder(observer, 0);
/*
for (let folder of folders) {
// Get the status of each folder
dbs.getFolderStatus(folder.id).subscribe(
status => {
folder["status"] = status;
observer.next(folder);
}
);
}
*/
}, },
error(err) { console.log("getAll error!", err) } err => { console.log("getAll error!", err) }
}); );
}); });
return folderObservable return folderObservable
} }

View File

@ -1,10 +1,59 @@
export interface Folder { import { Device } from './device';
interface Folder {
id: string; id: string;
label: string; label: string;
status: FolderStatus; devices: Device[];
status: Folder.Status;
paused: boolean;
} }
export interface FolderStatus { namespace Folder {
export enum stateType {
}
export function statusToString(f: Folder): string {
const fs: Folder.Status = f.status;
const state: string = fs.state;
if (f.paused) {
return 'paused';
}
if (!f.status || (Object.keys(f.status).length === 0)) {
return 'unknown';
}
if (state === 'error') {
return 'stopped'; // legacy, the state is called "stopped" in the GUI
}
if (state !== 'idle') {
return state;
}
const needTotalItems = fs.needDeletes + fs.needDirectories +
fs.needFiles + fs.needSymlinks;
const receiveOnlyTotalItems = fs.receiveOnlyChangedDeletes + fs.receiveOnlyChangedDirectories +
fs.receiveOnlyChangedFiles + fs.receiveOnlyChangedSymlinks;
if (needTotalItems > 0) {
return 'outofsync';
}
if (f.status.pullErrors > 0) {
return 'faileditems';
}
if (receiveOnlyTotalItems > 0) {
return 'localadditions';
}
if (f.devices.length <= 1) {
return 'unshared';
}
return state;
}
export interface Status {
globalBytes: number; globalBytes: number;
globalDeleted: number; globalDeleted: number;
globalDirectories: number; globalDirectories: number;
@ -35,3 +84,12 @@ export interface FolderStatus {
stateChanged: string; stateChanged: string;
version: number; version: number;
} }
}
export default Folder;

View File

@ -4,7 +4,7 @@ import { MatSort } from '@angular/material/sort';
import { map } from 'rxjs/operators'; import { map } from 'rxjs/operators';
import { Observable, of as observableOf, merge, Subject } from 'rxjs'; import { Observable, of as observableOf, merge, Subject } from 'rxjs';
import { Folder } from '../../folder'; import Folder from '../../folder';
import { SystemConfigService } from '../../system-config.service'; import { SystemConfigService } from '../../system-config.service';
/** /**

View File

@ -4,7 +4,7 @@ import { MatSort } from '@angular/material/sort';
import { MatTable } from '@angular/material/table'; import { MatTable } from '@angular/material/table';
import { FolderListDataSource } from './folder-list-datasource'; import { FolderListDataSource } from './folder-list-datasource';
import { Folder } from '../../folder'; import Folder from '../../folder';
import { SystemConfigService } from '../../system-config.service'; import { SystemConfigService } from '../../system-config.service';
import { dataTableElevation } from '../../style'; import { dataTableElevation } from '../../style';
import { Subject } from 'rxjs'; import { Subject } from 'rxjs';

View File

@ -4,7 +4,7 @@ import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, Subject } from 'rxjs'; import { Observable, Subject } from 'rxjs';
import { map, retry } from 'rxjs/operators'; import { map, retry } from 'rxjs/operators';
import { Folder } from './folder'; import Folder from './folder';
import { Device } from './device'; import { Device } from './device';
import { CookieService } from './cookie.service'; import { CookieService } from './cookie.service';
import { environment } from '../environments/environment' import { environment } from '../environments/environment'