Make ei-coordinator tests work
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / ei-coordinator / ei-job.datasource.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 import { Injectable } from '@angular/core';
22
23 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
24
25 import { EIJob } from '../interfaces/ei.types';
26 import { EIService } from '../services/ei/ei.service';
27
28 @Injectable({
29     providedIn: 'root'
30 })
31
32 export class EIJobDataSource {
33
34     private eiJobsSubject = new BehaviorSubject<EIJob[]>([]);
35
36     public eiJobs(): EIJob[] {
37         return this.eiJobsSubject.value;
38     }
39
40     private loadingSubject = new BehaviorSubject<boolean>(false);
41
42     public loading$ = this.loadingSubject.asObservable();
43
44     public rowCount = 1; // hide footer during intial load
45
46     constructor(
47         private eiSvc: EIService) {
48     }
49
50     loadJobs() {
51         this.loadingSubject.next(true);
52         this.eiSvc.getProducerIds()
53             .subscribe((producerIds: string[]) => {
54                 producerIds.forEach(id => {
55                     this.getJobsForProducer(id);
56                 });
57             });
58     }
59
60     private getJobsForProducer(id: string) {
61         console.log('Getting jobs for producer ID: ', id);
62         this.eiSvc.getJobsForProducer(id).subscribe(jobs => {
63             this.addJobsToSubject(jobs);
64             this.rowCount = this.eiJobsSubject.getValue().length;
65         });
66     }
67
68     private addJobsToSubject(jobs: EIJob[]) {
69         const currentValue = this.eiJobsSubject.value;
70         const updatedValue = [...currentValue, ...jobs];
71         this.eiJobsSubject.next(updatedValue);
72     }
73 }