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, 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 { JobInfo } from "@interfaces/producer.types";
+import { ProducerService } from "@services/ei/producer.service";
import { UiService } from "@services/ui/ui.service";
export interface Job {
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 producerService: ProducerService, private ui: UiService) {
this.jobForm = new FormGroup({
jobId: new FormControl(""),
typeId: 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;
dataSubscription(): Subscription {
let prodId = [];
- const jobs$ = this.eiSvc.getProducerIds().pipe(
+ const jobs$ = this.producerService.getProducerIds().pipe(
tap((data) => (prodId = data)),
mergeMap((prodIds) =>
- forkJoin(prodIds.map((id) => this.eiSvc.getJobsForProducer(id)))
+ forkJoin(prodIds.map((id) => this.producerService.getJobsForProducer(id)))
),
- 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((_) => jobs$),
+ map((response) => this.extractJobs(prodId, response))
)
)
+ );
+
+ return this.polling$
+ .pipe(
+ switchMap((value) => {
+ let pollCondition = value == 0 || this.checked;
+ return pollCondition ? refreshedJobs$ : EMPTY;
+ })
+ )
.subscribe();
}
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);
}
return data.toLowerCase().includes(transformedFilter);
}
- getJobTypeId(eiJob: Job): string {
- if (eiJob.typeId) {
- return eiJob.typeId;
+ 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(prodId: number[], res: JobInfo[][]) {
+ 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);
+ this.jobsSubject$.next(jobList);
+ if (this.firstTime && jobList.length > 0) {
+ this.polling$.next(jobList.length);
+ this.firstTime = false;
+ }
return jobList;
}
- createJobList(prodId: any[], result: EIJob[][]) {
+ createJobList(prodId: any[], result: JobInfo[][]) {
let jobList = [];
prodId.forEach((element, index) => {
let jobs = result[index];
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;
+ createJob(element: any, job: JobInfo): any {
+ let infoJob = <Job>{};
+ infoJob.jobId = job.info_job_identity;
+ infoJob.typeId = job.info_type_identity;
+ infoJob.owner = job.owner;
+ infoJob.targetUri = job.target_uri;
+ infoJob.prodId = element;
+ return infoJob;
}
refreshDataClick() {