]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/summary.service.ts
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / services / summary.service.ts
1 import { HttpClient } from '@angular/common/http';
2 import { Injectable, NgZone } from '@angular/core';
3 import { Router } from '@angular/router';
4
5 import * as _ from 'lodash';
6 import { BehaviorSubject, Subscription } from 'rxjs';
7
8 import { ExecutingTask } from '../models/executing-task';
9 import { ServicesModule } from './services.module';
10
11 @Injectable({
12 providedIn: ServicesModule
13 })
14 export class SummaryService {
15 // Observable sources
16 private summaryDataSource = new BehaviorSubject(null);
17
18 // Observable streams
19 summaryData$ = this.summaryDataSource.asObservable();
20
21 polling: number;
22
23 constructor(private http: HttpClient, private router: Router, private ngZone: NgZone) {
24 this.enablePolling();
25 }
26
27 enablePolling() {
28 this.refresh();
29
30 this.ngZone.runOutsideAngular(() => {
31 this.polling = window.setInterval(() => {
32 this.ngZone.run(() => {
33 this.refresh();
34 });
35 }, 5000);
36 });
37 }
38
39 refresh() {
40 if (this.router.url !== '/login') {
41 this.http.get('api/summary').subscribe((data) => {
42 this.summaryDataSource.next(data);
43 });
44 }
45 }
46
47 /**
48 * Returns the current value of summaryData
49 */
50 getCurrentSummary(): { [key: string]: any; executing_tasks: object[] } {
51 return this.summaryDataSource.getValue();
52 }
53
54 /**
55 * Subscribes to the summaryData,
56 * which is updated once every 5 seconds or when a new task is created.
57 */
58 subscribe(next: (summary: any) => void, error?: (error: any) => void): Subscription {
59 return this.summaryData$.subscribe(next, error);
60 }
61
62 /**
63 * Inserts a newly created task to the local list of executing tasks.
64 * After that, it will automatically push that new information
65 * to all subscribers.
66 */
67 addRunningTask(task: ExecutingTask) {
68 const current = this.summaryDataSource.getValue();
69 if (!current) {
70 return;
71 }
72
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);
76 });
77 if (!exists) {
78 current.executing_tasks.push(task);
79 }
80 } else {
81 current.executing_tasks = [task];
82 }
83
84 this.summaryDataSource.next(current);
85 }
86 }