]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
26c738f226945ba6ade3d38919e892b658851add
[ceph-ci.git] /
1 import { Component, OnInit } from '@angular/core';
2 import { FormControl, Validators } from '@angular/forms';
3
4 import { I18n } from '@ngx-translate/i18n-polyfill';
5 import * as _ from 'lodash';
6 import { BsModalRef } from 'ngx-bootstrap/modal';
7 import { forkJoin as observableForkJoin, of } from 'rxjs';
8 import { mergeMap } from 'rxjs/operators';
9
10 import { ConfigurationService } from '../../../../shared/api/configuration.service';
11 import { OsdService } from '../../../../shared/api/osd.service';
12 import { NotificationType } from '../../../../shared/enum/notification-type.enum';
13 import { CdFormGroup } from '../../../../shared/forms/cd-form-group';
14 import { NotificationService } from '../../../../shared/services/notification.service';
15
16 @Component({
17   selector: 'cd-osd-recv-speed-modal',
18   templateUrl: './osd-recv-speed-modal.component.html',
19   styleUrls: ['./osd-recv-speed-modal.component.scss']
20 })
21 export class OsdRecvSpeedModalComponent implements OnInit {
22   osdRecvSpeedForm: CdFormGroup;
23   priorities = [];
24   priorityAttrs = [];
25
26   constructor(
27     public bsModalRef: BsModalRef,
28     private configService: ConfigurationService,
29     private notificationService: NotificationService,
30     private i18n: I18n,
31     private osdService: OsdService
32   ) {
33     this.priorities = this.osdService.osdRecvSpeedModalPriorities.KNOWN_PRIORITIES;
34     this.osdRecvSpeedForm = new CdFormGroup({
35       priority: new FormControl(null, { validators: [Validators.required] }),
36       customizePriority: new FormControl(false)
37     });
38     this.priorityAttrs = [
39       {
40         name: 'osd_max_backfills',
41         text: this.i18n('Max Backfills')
42       },
43       {
44         name: 'osd_recovery_max_active',
45         text: this.i18n('Recovery Max Active')
46       },
47       {
48         name: 'osd_recovery_max_single_start',
49         text: this.i18n('Recovery Max Single Start')
50       },
51       {
52         name: 'osd_recovery_sleep',
53         text: this.i18n('Recovery Sleep')
54       }
55     ];
56
57     this.priorityAttrs.forEach((attr) => {
58       this.osdRecvSpeedForm.addControl(
59         attr.name,
60         new FormControl(null, { validators: [Validators.required] })
61       );
62
63       this.configService.get(attr.name).subscribe((data: any) => {
64         if (data.desc !== '') {
65           attr['desc'] = data.desc;
66         }
67       });
68     });
69   }
70
71   ngOnInit() {
72     this.getStoredPriority((priority) => {
73       this.setPriority(priority);
74     });
75   }
76
77   setPriority(priority: any) {
78     const customPriority = _.find(this.priorities, (p) => {
79       return p.name === 'custom';
80     });
81
82     if (priority.name === 'custom') {
83       if (!customPriority) {
84         this.priorities.push(priority);
85       }
86     } else {
87       if (customPriority) {
88         this.priorities.splice(this.priorities.indexOf(customPriority), 1);
89       }
90     }
91
92     this.osdRecvSpeedForm.controls.priority.setValue(priority.name);
93     Object.entries(priority.values).forEach(([name, value]) => {
94       this.osdRecvSpeedForm.controls[name].setValue(value);
95     });
96   }
97
98   onCustomizePriorityChange() {
99     if (this.osdRecvSpeedForm.getValue('customizePriority')) {
100       const values = {};
101       this.priorityAttrs.forEach((attr) => {
102         values[attr.name] = this.osdRecvSpeedForm.getValue(attr.name);
103       });
104       const customPriority = {
105         name: 'custom',
106         text: this.i18n('Custom'),
107         values: values
108       };
109       this.setPriority(customPriority);
110     } else {
111       this.setPriority(this.priorities[0]);
112     }
113   }
114
115   getStoredPriority(callbackFn: Function) {
116     const observables = [];
117     this.priorityAttrs.forEach((configName) => {
118       observables.push(this.configService.get(configName.name));
119     });
120
121     observableForkJoin(observables)
122       .pipe(
123         mergeMap((configOptions) => {
124           const result = {};
125           configOptions.forEach((configOption) => {
126             if (configOption && 'value' in configOption) {
127               configOption.value.forEach((value) => {
128                 if (value['section'] === 'osd') {
129                   result[configOption.name] = Number(value.value);
130                 }
131               });
132             }
133           });
134           return of(result);
135         })
136       )
137       .subscribe((resp) => {
138         const priority = _.find(this.priorities, (p) => {
139           return _.isEqual(p.values, resp);
140         });
141
142         this.osdRecvSpeedForm.controls.customizePriority.setValue(false);
143
144         if (priority) {
145           return callbackFn(priority);
146         }
147
148         if (Object.entries(resp).length === 4) {
149           this.osdRecvSpeedForm.controls.customizePriority.setValue(true);
150           return callbackFn(Object({ name: 'custom', text: this.i18n('Custom'), values: resp }));
151         }
152
153         return callbackFn(this.priorities[0]);
154       });
155   }
156
157   onPriorityChange(selectedPriorityName) {
158     const selectedPriority =
159       _.find(this.priorities, (p) => {
160         return p.name === selectedPriorityName;
161       }) || this.priorities[0];
162
163     this.setPriority(selectedPriority);
164   }
165
166   submitAction() {
167     const options = {};
168     this.priorityAttrs.forEach((attr) => {
169       options[attr.name] = { section: 'osd', value: this.osdRecvSpeedForm.getValue(attr.name) };
170     });
171
172     this.configService.bulkCreate({ options: options }).subscribe(
173       () => {
174         this.notificationService.show(
175           NotificationType.success,
176           this.i18n('OSD recovery speed priority "{{value}}" was set successfully.', {
177             value: this.osdRecvSpeedForm.getValue('priority')
178           }),
179           this.i18n('OSD recovery speed priority')
180         );
181         this.bsModalRef.hide();
182       },
183       () => {
184         this.bsModalRef.hide();
185       }
186     );
187   }
188 }