* ========================LICENSE_END===================================
*/
-import { CollectionViewer, DataSource } from '@angular/cdk/collections';
-import { HttpErrorResponse } from '@angular/common/http';
-import { MatSort } from '@angular/material';
-import { Observable } from 'rxjs/Observable';
+import { Injectable } from '@angular/core';
+
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
-import { merge } from 'rxjs';
-import { of } from 'rxjs/observable/of';
-import { catchError, finalize, map, tap } from 'rxjs/operators';
-import { EIProducer } from '../interfaces/ei.jobs';
+import { mergeMap, finalize } from 'rxjs/operators';
+import { Observable, forkJoin, of } from 'rxjs';
+
+import { EIProducer } from '../interfaces/ei.types';
import { EIService } from '../services/ei/ei.service';
-import { NotificationService } from '../services/ui/notification.service';
-export class EIProducerDataSource extends DataSource<EIProducer> {
+@Injectable({
+ providedIn: 'root'
+})
+
+export class EIProducerDataSource {
- private producerSubject = new BehaviorSubject<EIProducer[]>([]);
+ private producers: Array<EIProducer> = [];
+
+ public eiProducers(): EIProducer[] {
+ return this.producers;
+ }
+
+ public eiProducersSubject(): Observable<EIProducer[]> {
+ return this.producersSubject.asObservable() as Observable<EIProducer[]>;
+ }
private loadingSubject = new BehaviorSubject<boolean>(false);
+ private producersSubject = new BehaviorSubject<EIProducer[]>([]);
public loading$ = this.loadingSubject.asObservable();
public rowCount = 1; // hide footer during intial load
constructor(
- private eiSvc: EIService,
- public sort: MatSort,
- private notificationService: NotificationService) {
- super();
+ private eiSvc: EIService) {
}
- loadTable() {
+ loadProducers() {
this.loadingSubject.next(true);
- this.eiSvc.getEIProducers()
- .pipe(
- catchError((her: HttpErrorResponse) => {
- this.notificationService.error('Failed to get producers: ' + her.error);
- return of([]);
- }),
- finalize(() => this.loadingSubject.next(false))
- )
- .subscribe((prods: EIProducer[]) => {
- console.log("Producers: " + prods);
- this.rowCount = prods.length;
- this.producerSubject.next(prods);
- });
- this.connect();
- }
+ this.producers = [];
- connect(): Observable<EIProducer[]> {
- const dataMutations = [
- this.producerSubject.asObservable(),
- this.sort.sortChange
- ];
- return merge(...dataMutations).pipe(map(() => {
- return this.getSortedData([...this.producerSubject.getValue()]);
- }));
- }
-
- disconnect(): void {
- this.producerSubject.complete();
- this.loadingSubject.complete();
- }
-
- private getSortedData(data: EIProducer[]) {
- if (!this.sort || !this.sort.active || this.sort.direction === '') {
- return data;
- }
-
- return data.sort((a, b) => {
- const isAsc = this.sort.direction === 'asc';
- switch (this.sort.active) {
- case 'id': return compare(a.ei_producer_id, b.ei_producer_id, isAsc);
- case 'type': return compare(a.ei_producer_types[0], b.ei_producer_types[0], isAsc);
- case 'status': return compare(a.status, b.status, isAsc);
- default: return 0;
- }
+ this.eiSvc.getProducerIds().pipe(
+ mergeMap(prodIds =>
+ forkJoin(prodIds.map(id => {
+ return forkJoin([
+ of(id),
+ this.eiSvc.getProducer(id),
+ this.eiSvc.getProducerStatus(id)
+ ])
+ })
+ )),
+ finalize(() => this.loadingSubject.next(false))
+ ).subscribe(result => {
+ this.producers = result.map(producer => {
+ let eiProducer = <EIProducer>{};
+ eiProducer.ei_producer_id = producer[0];
+ eiProducer.ei_producer_types = producer[1].supported_ei_types;
+ eiProducer.status = producer[2].operational_state.toString();
+ return eiProducer;
+ });
+ this.producersSubject.next(this.producers);
});
+ this.rowCount = this.producers.length;
}
-
- getProducers(): Observable<EIProducer[]> {
- return this.eiSvc.getEIProducers()
- .pipe(tap(console.log));
- }
-}
-
-function compare(a: string, b: string, isAsc: boolean) {
- return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
-}
+}
\ No newline at end of file