CI: Migrate Sonar Scan job to GHA
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / ei-coordinator / jobs-list / jobs-list.component.ts
index 43b3ad8..ca39330 100644 (file)
@@ -22,11 +22,12 @@ 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 { EMPTY, forkJoin, of, Subscription, timer } from "rxjs";
+import { EMPTY, forkJoin, of, Subscription, concat } from "rxjs";
 import { BehaviorSubject } from "rxjs/BehaviorSubject";
-import { mergeMap, finalize, map, tap, switchMap } from "rxjs/operators";
+import { mergeMap, finalize, map, tap, concatMap, delay, skip, catchError } from "rxjs/operators";
 import { ConsumerService } from "@services/ei/consumer.service";
 import { UiService } from "@services/ui/ui.service";
+import { OperationalState } from '@app/interfaces/consumer.types';
 
 export interface Job {
   jobId: string;
@@ -34,6 +35,7 @@ export interface Job {
   targetUri: string;
   owner: string;
   prodIds: string[];
+  status: OperationalState;
 }
 
 @Component({
@@ -55,6 +57,7 @@ export class JobsListComponent implements OnInit {
   subscription: Subscription;
   checked: boolean = false;
   firstTime: boolean = true;
+  jobList: Job[] = [];
 
   constructor(private consumerService: ConsumerService, private ui: UiService) {
     this.jobForm = new FormGroup({
@@ -63,6 +66,7 @@ export class JobsListComponent implements OnInit {
       owner: new FormControl(""),
       targetUri: new FormControl(""),
       prodIds: new FormControl(""),
+      status: new FormControl("")
     });
   }
 
@@ -80,7 +84,8 @@ export class JobsListComponent implements OnInit {
           this.isDataIncluding(data.jobId, searchTerms.jobId) &&
           this.isDataIncluding(data.owner, searchTerms.owner) &&
           this.isDataIncluding(data.typeId, searchTerms.typeId) &&
-          this.isArrayIncluding(data.prodIds, searchTerms.prodIds)
+          this.isArrayIncluding(data.prodIds, searchTerms.prodIds) &&
+          this.isDataIncluding(data.status, searchTerms.status)
         );
       }) as (data: Job, filter: any) => boolean;
     });
@@ -96,33 +101,54 @@ export class JobsListComponent implements OnInit {
 
   dataSubscription(): Subscription {
     const jobsInfo$ = this.consumerService.getJobIds().pipe(
+      catchError(_ => { return EMPTY }),
+      tap((_) => {
+        this.jobList = [] as Job[];
+      }),
       mergeMap((jobIds) =>
         forkJoin(jobIds.map((jobId) => {
           return forkJoin([
-            of(jobId),
-            this.consumerService.getJobInfo(jobId),
-            this.consumerService.getConsumerStatus(jobId)
+            of(jobId).pipe(
+              catchError(err => {
+                return of([-1]);
+              })
+            ),
+            this.consumerService.getJobInfo(jobId).pipe(
+              catchError(err => {
+                return of([-1]);
+              })),
+            this.consumerService.getConsumerStatus(jobId).pipe(
+              catchError(err => {
+                return of([-1]);
+              })),
           ])
         }))
       ),
-      finalize(() => this.loadingSubject$.next(false))
+      finalize(() => {
+        this.loadingSubject$.next(false);
+        this.jobsSubject$.next(this.jobList);
+      })
     );
 
+    const whenToRefresh$ = of('').pipe(
+      delay(10000),
+      tap((_) => this.refresh$.next('')),
+      skip(1),
+    );
+
+    const poll$ = concat(jobsInfo$, whenToRefresh$);
+
     const refreshedJobs$ = this.refresh$.pipe(
-      switchMap((_) =>
-        timer(0, 10000).pipe(
-          tap((_) => {
-            this.loadingSubject$.next(true);
-          }),
-          switchMap((_) => jobsInfo$),
-          map((response) => this.extractJobs(response))
-        )
-      )
+      tap((_) => {
+        this.loadingSubject$.next(true);
+      }),
+      concatMap((_) => this.checked ? poll$ : jobsInfo$),
+      map((response) => this.extractJobs(response))
     );
 
     return this.polling$
       .pipe(
-        switchMap((value) => {
+        concatMap((value) => {
           let pollCondition = value == 0 || this.checked;
           return pollCondition ? refreshedJobs$ : EMPTY;
         })
@@ -140,6 +166,7 @@ export class JobsListComponent implements OnInit {
     this.jobForm.get("owner").setValue("");
     this.jobForm.get("targetUri").setValue("");
     this.jobForm.get("prodIds").setValue("");
+    this.jobForm.get("status").setValue("");
   }
 
   sortJobs(sort: Sort) {
@@ -157,6 +184,8 @@ export class JobsListComponent implements OnInit {
           return this.compare(a.targetUri, b.targetUri, isAsc);
         case "prodIds":
           return this.compare(a.prodIds, b.prodIds, isAsc);
+        case "status":
+          return this.compare(a.status, b.status, isAsc);
         default:
           return 0;
       }
@@ -167,6 +196,9 @@ export class JobsListComponent implements OnInit {
   stopPolling(checked) {
     this.checked = checked;
     this.polling$.next(this.jobs().length);
+    if (this.checked) {
+      this.refreshDataClick();
+    }
   }
 
   compare(a: any, b: any, isAsc: boolean) {
@@ -183,7 +215,7 @@ export class JobsListComponent implements OnInit {
   }
 
   isArrayIncluding(data: string[], filter: string): boolean {
-    if(!data)
+    if (!data)
       return true;
     for (let i = 0; i < data.length; i++) {
       return this.isDataIncluding(data[i], filter);
@@ -210,26 +242,51 @@ export class JobsListComponent implements OnInit {
 
   private extractJobs(res: any) {
     this.clearFilter();
-    let jobList = [];
     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);
+      if (element[0] != -1) {
+        if (element[1] != -1 && element[2] != -1) {
+          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"];
+          jobObj.status = element[2].info_job_status;
+          this.jobList = this.jobList.concat(jobObj);
+        } else {
+          let jobObj = <Job>{};
+          jobObj.jobId = element[0];
+          if (element[1] == -1) {
+            jobObj.owner = "--Missing information--";
+            jobObj.targetUri = "--Missing information--";
+            jobObj.typeId = "--Missing information--";
+          }
+          if (element[2] == -1) {
+            jobObj.prodIds = "--Missing information--" as unknown as [];
+            jobObj.status = "--Missing information--" as OperationalState;
+          }
+          this.jobList = this.jobList.concat(jobObj);
+        }
+      }
+    });
+
+    if (this.firstTime && this.jobList.length > 0) {
+      this.polling$.next(this.jobList.length);
       this.firstTime = false;
     }
-    return jobList;
+    return this.jobList;
   }
 
   refreshDataClick() {
     this.refresh$.next("");
   }
+
+  jobsNumber() : number {
+    return this.jobsDataSource.data.length;
+  }
+
+  hasJobs(): boolean {
+    return this.jobsNumber() > 0;
+  }
+
 }