2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 import { Injectable } from '@angular/core';
23 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
24 import { mergeMap, finalize } from 'rxjs/operators';
25 import { Observable, forkJoin, of } from 'rxjs';
27 import { EIProducer } from '../interfaces/ei.types';
28 import { EIService } from '../services/ei/ei.service';
34 export class EIProducerDataSource {
36 private producers: Array<EIProducer> = [];
38 public eiProducers(): EIProducer[] {
39 return this.producers;
42 public eiProducersSubject(): Observable<EIProducer[]> {
43 return this.producersSubject.asObservable() as Observable<EIProducer[]>;
46 private loadingSubject = new BehaviorSubject<boolean>(false);
47 private producersSubject = new BehaviorSubject<EIProducer[]>([]);
49 public loading$ = this.loadingSubject.asObservable();
51 public rowCount = 1; // hide footer during intial load
54 private eiSvc: EIService) {
58 this.loadingSubject.next(true);
61 this.eiSvc.getProducerIds().pipe(
63 forkJoin(prodIds.map(id => {
66 this.eiSvc.getProducer(id),
67 this.eiSvc.getProducerStatus(id)
71 finalize(() => this.loadingSubject.next(false))
72 ).subscribe(result => {
73 this.producers = result.map(producer => {
74 let eiProducer = <EIProducer>{};
75 eiProducer.ei_producer_id = producer[0];
76 eiProducer.ei_producer_types = producer[1].supported_ei_types;
77 eiProducer.status = producer[2].operational_state.toString();
80 this.producersSubject.next(this.producers);
82 this.rowCount = this.producers.length;