072762594ca2165efe84bd201431375be89f71a5
[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 import { MatTableDataSource } from '@angular/material';
28 import { ViewChild } from '@angular/core';
29 import { MatSort } from '@angular/material/sort';
30 import { delay } from 'rxjs/operators';
31
32 @Injectable({
33     providedIn: 'root'
34 })
35
36 export class EIJobDataSource {
37
38     private jobs: Array<EIJob> = [];
39     private dataSource: MatTableDataSource<any> = new MatTableDataSource();
40     @ViewChild(MatSort, { static: true }) sort: MatSort;
41
42
43     public jobsDataSource(): MatTableDataSource<any> {
44         return this.dataSource;
45     }
46
47     private loadingSubject = new BehaviorSubject<boolean>(false);
48
49     public loading$ = this.loadingSubject.asObservable();
50
51     public rowCount = 1; // hide footer during intial load
52
53     constructor(
54         private eiSvc: EIService) {
55     }
56
57     loadJobs() {
58         this.loadingSubject.next(true);
59         this.jobs = [];
60         this.eiSvc.getProducerIds()
61             .subscribe((producerIds: string[]) => {
62                 producerIds.forEach(id => {
63                     this.getJobsForProducer(id);
64                 });
65                 //this.dataSource = new MatTableDataSource();  
66                 this.dataSource.data = this.jobs;
67                 this.dataSource.sort = this.sort;
68                 console.log("datasource: "+this.dataSource.data);  
69             });
70         this.rowCount = this.jobs.length;
71     }
72
73     private getJobsForProducer(id: string) {
74         console.log('Getting jobs for producer ID: ', id);
75         this.eiSvc.getJobsForProducer(id)//.pipe(delay(5000))
76         .subscribe(producerJobs => {
77             this.jobs = this.jobs.concat(producerJobs);    
78             console.log("producerJobs: "+producerJobs);  
79             
80         });
81     }
82 }