1 import { HttpClient } from '@angular/common/http';
2 import { Injectable, NgZone } from '@angular/core';
3 import { Router } from '@angular/router';
5 import * as _ from 'lodash';
6 import { BehaviorSubject, Subscription } from 'rxjs';
8 import { ExecutingTask } from '../models/executing-task';
9 import { ServicesModule } from './services.module';
12 providedIn: ServicesModule
14 export class SummaryService {
16 private summaryDataSource = new BehaviorSubject(null);
19 summaryData$ = this.summaryDataSource.asObservable();
23 constructor(private http: HttpClient, private router: Router, private ngZone: NgZone) {
30 this.ngZone.runOutsideAngular(() => {
31 this.polling = window.setInterval(() => {
32 this.ngZone.run(() => {
40 if (this.router.url !== '/login') {
41 this.http.get('api/summary').subscribe((data) => {
42 this.summaryDataSource.next(data);
48 * Returns the current value of summaryData
50 getCurrentSummary(): { [key: string]: any; executing_tasks: object[] } {
51 return this.summaryDataSource.getValue();
55 * Subscribes to the summaryData,
56 * which is updated once every 5 seconds or when a new task is created.
58 subscribe(next: (summary: any) => void, error?: (error: any) => void): Subscription {
59 return this.summaryData$.subscribe(next, error);
63 * Inserts a newly created task to the local list of executing tasks.
64 * After that, it will automatically push that new information
67 addRunningTask(task: ExecutingTask) {
68 const current = this.summaryDataSource.getValue();
73 if (_.isArray(current.executing_tasks)) {
74 const exists = current.executing_tasks.find((element) => {
75 return element.name === task.name && _.isEqual(element.metadata, task.metadata);
78 current.executing_tasks.push(task);
81 current.executing_tasks = [task];
84 this.summaryDataSource.next(current);