]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/task-list.service.ts
import ceph quincy 17.2.4
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / services / task-list.service.ts
CommitLineData
11fdf7f2
TL
1import { Injectable, OnDestroy } from '@angular/core';
2
3import { Observable, Subscription } from 'rxjs';
4
5import { ExecutingTask } from '../models/executing-task';
f67539c2 6import { Summary } from '../models/summary.model';
11fdf7f2
TL
7import { SummaryService } from './summary.service';
8import { TaskMessageService } from './task-message.service';
9
10@Injectable()
11export class TaskListService implements OnDestroy {
12 summaryDataSubscription: Subscription;
13
2a845540 14 getUpdate: (context?: any) => Observable<object>;
11fdf7f2
TL
15 preProcessing: (_: any) => any[];
16 setList: (_: any[]) => void;
17 onFetchError: (error: any) => void;
18 taskFilter: (task: ExecutingTask) => boolean;
9f95a23c 19 itemFilter: (item: any, task: ExecutingTask) => boolean;
11fdf7f2 20 builders: object;
f67539c2 21 summary: Summary;
11fdf7f2
TL
22
23 constructor(
24 private taskMessageService: TaskMessageService,
25 private summaryService: SummaryService
26 ) {}
27
28 /**
29 * @param {() => Observable<object>} getUpdate Method that calls the api and
30 * returns that without subscribing.
31 * @param {(_: any) => any[]} preProcessing Method executed before merging
32 * Tasks with Items
33 * @param {(_: any[]) => void} setList Method used to update array of item in the component.
34 * @param {(error: any) => void} onFetchError Method called when there were
35 * problems while fetching data.
36 * @param {(task: ExecutingTask) => boolean} taskFilter callback used in tasks_array.filter()
37 * @param {(item, task: ExecutingTask) => boolean} itemFilter callback used in
38 * items_array.filter()
39 * @param {object} builders
40 * object with builders for each type of task.
41 * You can also use a 'default' one.
42 * @memberof TaskListService
43 */
44 init(
2a845540 45 getUpdate: (context?: any) => Observable<object>,
11fdf7f2
TL
46 preProcessing: (_: any) => any[],
47 setList: (_: any[]) => void,
48 onFetchError: (error: any) => void,
49 taskFilter: (task: ExecutingTask) => boolean,
9f95a23c 50 itemFilter: (item: any, task: ExecutingTask) => boolean,
11fdf7f2
TL
51 builders: object
52 ) {
53 this.getUpdate = getUpdate;
54 this.preProcessing = preProcessing;
55 this.setList = setList;
56 this.onFetchError = onFetchError;
57 this.taskFilter = taskFilter;
58 this.itemFilter = itemFilter;
59 this.builders = builders || {};
60
f6b5b4d7 61 this.summaryDataSubscription = this.summaryService.subscribe((summary) => {
f67539c2
TL
62 this.summary = summary;
63 this.fetch();
64 }, this.onFetchError);
65 }
66
2a845540
TL
67 fetch(context: any = null) {
68 this.getUpdate(context).subscribe((resp: any) => {
69 this.updateData(resp, this.summary?.['executing_tasks'].filter(this.taskFilter));
11fdf7f2
TL
70 }, this.onFetchError);
71 }
72
73 private updateData(resp: any, tasks: ExecutingTask[]) {
74 const data: any[] = this.preProcessing ? this.preProcessing(resp) : resp;
75 this.addMissing(data, tasks);
76 data.forEach((item) => {
77 const executingTasks = tasks.filter((task) => this.itemFilter(item, task));
78 item.cdExecuting = this.getTaskAction(executingTasks);
79 });
80 this.setList(data);
81 }
82
83 private addMissing(data: any[], tasks: ExecutingTask[]) {
9f95a23c 84 const defaultBuilder = this.builders['default'];
2a845540 85 tasks?.forEach((task) => {
11fdf7f2
TL
86 const existing = data.find((item) => this.itemFilter(item, task));
87 const builder = this.builders[task.name];
88 if (!existing && (builder || defaultBuilder)) {
9f95a23c 89 data.push(builder ? builder(task.metadata) : defaultBuilder(task.metadata));
11fdf7f2
TL
90 }
91 });
92 }
93
94 private getTaskAction(tasks: ExecutingTask[]): string {
95 if (tasks.length === 0) {
9f95a23c 96 return undefined;
11fdf7f2 97 }
eafe8130
TL
98 return tasks
99 .map((task) => {
100 const progress = task.progress ? ` ${task.progress}%` : '';
101 return this.taskMessageService.getRunningText(task) + '...' + progress;
102 })
103 .join(', ');
11fdf7f2
TL
104 }
105
106 ngOnDestroy() {
107 if (this.summaryDataSubscription) {
108 this.summaryDataSubscription.unsubscribe();
109 }
110 }
111}