Consumer service
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / ei-coordinator / jobs-list / jobs-list.component.ts
index 58e437a..43b3ad8 100644 (file)
@@ -22,30 +22,18 @@ import { FormControl, FormGroup } from "@angular/forms";
 import { MatPaginator } from "@angular/material/paginator";
 import { Sort } from "@angular/material/sort";
 import { MatTableDataSource } from "@angular/material/table";
-import {
-  forkJoin,
-  Subscription,
-  timer,
-} from "rxjs";
+import { EMPTY, forkJoin, of, Subscription, timer } from "rxjs";
 import { BehaviorSubject } from "rxjs/BehaviorSubject";
-import {
-  mergeMap,
-  finalize,
-  map,
-  tap,
-  switchMap,
-} from "rxjs/operators";
-import { EIJob } from "@interfaces/ei.types";
-import { EIService } from "@services/ei/ei.service";
+import { mergeMap, finalize, map, tap, switchMap } from "rxjs/operators";
+import { ConsumerService } from "@services/ei/consumer.service";
 import { UiService } from "@services/ui/ui.service";
 
 export interface Job {
   jobId: string;
-  jobData: any;
   typeId: string;
   targetUri: string;
   owner: string;
-  prodId: string;
+  prodIds: string[];
 }
 
 @Component({
@@ -59,26 +47,29 @@ export class JobsListComponent implements OnInit {
   jobForm: FormGroup;
   darkMode: boolean;
 
-  private jobsSubject = new BehaviorSubject<Job[]>([]);
+  private jobsSubject$ = new BehaviorSubject<Job[]>([]);
   private refresh$ = new BehaviorSubject("");
-  private loadingSubject = new BehaviorSubject<boolean>(false);
-  public loading$ = this.loadingSubject.asObservable();
+  private loadingSubject$ = new BehaviorSubject<boolean>(false);
+  private polling$ = new BehaviorSubject(0);
+  public loading$ = this.loadingSubject$.asObservable();
   subscription: Subscription;
+  checked: boolean = false;
+  firstTime: boolean = true;
 
-  constructor(private eiSvc: EIService, private ui: UiService) {
+  constructor(private consumerService: ConsumerService, private ui: UiService) {
     this.jobForm = new FormGroup({
       jobId: new FormControl(""),
       typeId: new FormControl(""),
       owner: new FormControl(""),
       targetUri: new FormControl(""),
-      prodId: new FormControl(""),
+      prodIds: new FormControl(""),
     });
   }
 
   ngOnInit(): void {
     this.subscription = this.dataSubscription();
 
-    this.jobsSubject.subscribe((data) => {
+    this.jobsSubject$.subscribe((data) => {
       this.jobsDataSource = new MatTableDataSource<Job>(data);
       this.jobsDataSource.paginator = this.paginator;
 
@@ -89,7 +80,7 @@ export class JobsListComponent implements OnInit {
           this.isDataIncluding(data.jobId, searchTerms.jobId) &&
           this.isDataIncluding(data.owner, searchTerms.owner) &&
           this.isDataIncluding(data.typeId, searchTerms.typeId) &&
-          this.isDataIncluding(data.prodId, searchTerms.prodId)
+          this.isArrayIncluding(data.prodIds, searchTerms.prodIds)
         );
       }) as (data: Job, filter: any) => boolean;
     });
@@ -104,27 +95,38 @@ export class JobsListComponent implements OnInit {
   }
 
   dataSubscription(): Subscription {
-    let prodId = [];
-    const jobs$ = this.eiSvc.getProducerIds().pipe(
-      tap((data) => (prodId = data)),
-      mergeMap((prodIds) =>
-        forkJoin(prodIds.map((id) => this.eiSvc.getJobsForProducer(id)))
+    const jobsInfo$ = this.consumerService.getJobIds().pipe(
+      mergeMap((jobIds) =>
+        forkJoin(jobIds.map((jobId) => {
+          return forkJoin([
+            of(jobId),
+            this.consumerService.getJobInfo(jobId),
+            this.consumerService.getConsumerStatus(jobId)
+          ])
+        }))
       ),
-      finalize(() => this.loadingSubject.next(false))
+      finalize(() => this.loadingSubject$.next(false))
     );
 
-    return this.refresh$
-      .pipe(
-        switchMap((_) =>
-          timer(0, 10000).pipe(
-            tap((_) => {
-              this.loadingSubject.next(true);
-            }),
-            switchMap((_) => jobs$),
-            map((response) => this.extractJobs(prodId, response))
-          )
+    const refreshedJobs$ = this.refresh$.pipe(
+      switchMap((_) =>
+        timer(0, 10000).pipe(
+          tap((_) => {
+            this.loadingSubject$.next(true);
+          }),
+          switchMap((_) => jobsInfo$),
+          map((response) => this.extractJobs(response))
         )
       )
+    );
+
+    return this.polling$
+      .pipe(
+        switchMap((value) => {
+          let pollCondition = value == 0 || this.checked;
+          return pollCondition ? refreshedJobs$ : EMPTY;
+        })
+      )
       .subscribe();
   }
 
@@ -137,7 +139,7 @@ export class JobsListComponent implements OnInit {
     this.jobForm.get("typeId").setValue("");
     this.jobForm.get("owner").setValue("");
     this.jobForm.get("targetUri").setValue("");
-    this.jobForm.get("prodId").setValue("");
+    this.jobForm.get("prodIds").setValue("");
   }
 
   sortJobs(sort: Sort) {
@@ -153,8 +155,8 @@ export class JobsListComponent implements OnInit {
           return this.compare(a.owner, b.owner, isAsc);
         case "targetUri":
           return this.compare(a.targetUri, b.targetUri, isAsc);
-        case "prodId":
-          return this.compare(a.prodId, b.prodId, isAsc);
+        case "prodIds":
+          return this.compare(a.prodIds, b.prodIds, isAsc);
         default:
           return 0;
       }
@@ -162,6 +164,11 @@ export class JobsListComponent implements OnInit {
     this.jobsDataSource.data = data;
   }
 
+  stopPolling(checked) {
+    this.checked = checked;
+    this.polling$.next(this.jobs().length);
+  }
+
   compare(a: any, b: any, isAsc: boolean) {
     return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
   }
@@ -175,54 +182,53 @@ export class JobsListComponent implements OnInit {
     return data.toLowerCase().includes(transformedFilter);
   }
 
-  getJobTypeId(eiJob: Job): string {
-    if (eiJob.typeId) {
-      return eiJob.typeId;
+  isArrayIncluding(data: string[], filter: string): boolean {
+    if(!data)
+      return true;
+    for (let i = 0; i < data.length; i++) {
+      return this.isDataIncluding(data[i], filter);
+    }
+  }
+
+  getJobTypeId(job: Job): string {
+    if (job.typeId) {
+      return job.typeId;
     }
     return "< No type >";
   }
 
-  getJobOwner(eiJob: Job): string {
-    if (eiJob.owner) {
-      return eiJob.owner;
+  getJobOwner(job: Job): string {
+    if (job.owner) {
+      return job.owner;
     }
     return "< No owner >";
   }
 
   public jobs(): Job[] {
-    return this.jobsSubject.value;
+    return this.jobsSubject$.value;
   }
 
-  private extractJobs(prodId: number[], res: EIJob[][]) {
-    console.log(res.length);
+  private extractJobs(res: any) {
+    this.clearFilter();
     let jobList = [];
-    prodId.forEach((element, index) => {
-      let jobs = res[index];
-      jobList = jobList.concat(jobs.map((job) => this.createJob(element, job)));
-    });
-    this.jobsSubject.next(jobList);
-    return jobList;
-  }
-
-  createJobList(prodId: any[], result: EIJob[][]) {
-    let jobList = [];
-    prodId.forEach((element, index) => {
-      let jobs = result[index];
-      jobList = jobList.concat(jobs.map((job) => this.createJob(element, job)));
-    });
+    res.forEach(element => {
+      let jobObj = <Job>{};
+      jobObj.jobId = element[0];
+      jobObj.owner = element[1].job_owner;
+      jobObj.targetUri = element[1].job_result_uri;
+      jobObj.typeId = element[1].info_type_id;
+      jobObj.prodIds = (element[2].producers) ? element[2].producers : ["No Producers"];      
+      jobList = jobList.concat(jobObj);
+    });  
+   
+    this.jobsSubject$.next(jobList);
+    if (this.firstTime && jobList.length > 0) {
+      this.polling$.next(jobList.length);
+      this.firstTime = false;
+    }
     return jobList;
   }
 
-  createJob(element: any, job: EIJob): any {
-    let eiJob = <Job>{};
-    eiJob.jobId = job.ei_job_identity;
-    eiJob.typeId = job.ei_type_identity;
-    eiJob.owner = job.owner;
-    eiJob.targetUri = job.target_uri;
-    eiJob.prodId = element;
-    return eiJob;
-  }
-
   refreshDataClick() {
     this.refresh$.next("");
   }