]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/blob
12dc31a0dcc88c1a3cd7f00becd799cd7698bbef
[ceph.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 import { ConfigOptionTypes } from '../../configuration/configuration-form/configuration-form.types';
16
17 @Component({
18   selector: 'cd-osd-recv-speed-modal',
19   templateUrl: './osd-recv-speed-modal.component.html',
20   styleUrls: ['./osd-recv-speed-modal.component.scss']
21 })
22 export class OsdRecvSpeedModalComponent implements OnInit {
23   osdRecvSpeedForm: CdFormGroup;
24   priorities = [];
25   priorityAttrs = {};
26
27   constructor(
28     public bsModalRef: BsModalRef,
29     private configService: ConfigurationService,
30     private notificationService: NotificationService,
31     private i18n: I18n,
32     private osdService: OsdService
33   ) {
34     this.priorities = this.osdService.osdRecvSpeedModalPriorities.KNOWN_PRIORITIES;
35     this.osdRecvSpeedForm = new CdFormGroup({
36       priority: new FormControl(null, { validators: [Validators.required] }),
37       customizePriority: new FormControl(false)
38     });
39     this.priorityAttrs = {
40       osd_max_backfills: {
41         text: this.i18n('Max Backfills'),
42         desc: '',
43         patternHelpText: '',
44         maxValue: undefined,
45         minValue: undefined
46       },
47       osd_recovery_max_active: {
48         text: this.i18n('Recovery Max Active'),
49         desc: '',
50         patternHelpText: '',
51         maxValue: undefined,
52         minValue: undefined
53       },
54       osd_recovery_max_single_start: {
55         text: this.i18n('Recovery Max Single Start'),
56         desc: '',
57         patternHelpText: '',
58         maxValue: undefined,
59         minValue: undefined
60       },
61       osd_recovery_sleep: {
62         text: this.i18n('Recovery Sleep'),
63         desc: '',
64         patternHelpText: '',
65         maxValue: undefined,
66         minValue: undefined
67       }
68     };
69
70     Object.keys(this.priorityAttrs).forEach((configOptionName) => {
71       this.osdRecvSpeedForm.addControl(
72         configOptionName,
73         new FormControl(null, { validators: [Validators.required] })
74       );
75     });
76   }
77
78   ngOnInit() {
79     const observables = [];
80     Object.keys(this.priorityAttrs).forEach((configOptionName) => {
81       observables.push(this.configService.get(configOptionName));
82     });
83
84     observableForkJoin(observables)
85       .pipe(
86         mergeMap((configOptions) => {
87           const result = { values: {}, configOptions: [] };
88           configOptions.forEach((configOption) => {
89             result.configOptions.push(configOption);
90
91             if (configOption && 'value' in configOption) {
92               configOption.value.forEach((value) => {
93                 if (value['section'] === 'osd') {
94                   result.values[configOption.name] = Number(value.value);
95                 }
96               });
97             }
98           });
99           return of(result);
100         })
101       )
102       .subscribe((resp) => {
103         this.getStoredPriority(resp.values, (priority) => {
104           this.setPriority(priority);
105         });
106         this.setDescription(resp.configOptions);
107         this.setValidators(resp.configOptions);
108       });
109   }
110
111   getStoredPriority(configOptionValues: any, callbackFn: Function) {
112     const priority = _.find(this.priorities, (p) => {
113       return _.isEqual(p.values, configOptionValues);
114     });
115
116     this.osdRecvSpeedForm.controls.customizePriority.setValue(false);
117
118     if (priority) {
119       return callbackFn(priority);
120     }
121
122     if (Object.entries(configOptionValues).length === 4) {
123       this.osdRecvSpeedForm.controls.customizePriority.setValue(true);
124       return callbackFn(
125         Object({ name: 'custom', text: this.i18n('Custom'), values: configOptionValues })
126       );
127     }
128
129     return callbackFn(this.priorities[0]);
130   }
131
132   setDescription(configOptions: Array<any>) {
133     configOptions.forEach((configOption) => {
134       if (configOption.desc !== '') {
135         this.priorityAttrs[configOption.name].desc = configOption.desc;
136       }
137     });
138   }
139
140   setPriority(priority: any) {
141     const customPriority = _.find(this.priorities, (p) => {
142       return p.name === 'custom';
143     });
144
145     if (priority.name === 'custom') {
146       if (!customPriority) {
147         this.priorities.push(priority);
148       }
149     } else {
150       if (customPriority) {
151         this.priorities.splice(this.priorities.indexOf(customPriority), 1);
152       }
153     }
154
155     this.osdRecvSpeedForm.controls.priority.setValue(priority.name);
156     Object.entries(priority.values).forEach(([name, value]) => {
157       this.osdRecvSpeedForm.controls[name].setValue(value);
158     });
159   }
160
161   setValidators(configOptions: Array<any>) {
162     configOptions.forEach((configOption) => {
163       const typeValidators = ConfigOptionTypes.getTypeValidators(configOption);
164       if (typeValidators) {
165         typeValidators.validators.push(Validators.required);
166
167         if ('max' in typeValidators && typeValidators.max !== '') {
168           this.priorityAttrs[configOption.name].maxValue = typeValidators.max;
169         }
170
171         if ('min' in typeValidators && typeValidators.min !== '') {
172           this.priorityAttrs[configOption.name].minValue = typeValidators.min;
173         }
174
175         this.priorityAttrs[configOption.name].patternHelpText = typeValidators.patternHelpText;
176         this.osdRecvSpeedForm.controls[configOption.name].setValidators(typeValidators.validators);
177       } else {
178         this.osdRecvSpeedForm.controls[configOption.name].setValidators(Validators.required);
179       }
180     });
181   }
182
183   onCustomizePriorityChange() {
184     if (this.osdRecvSpeedForm.getValue('customizePriority')) {
185       const values = {};
186       Object.keys(this.priorityAttrs).forEach((configOptionName) => {
187         values[configOptionName] = this.osdRecvSpeedForm.getValue(configOptionName);
188       });
189       const customPriority = {
190         name: 'custom',
191         text: this.i18n('Custom'),
192         values: values
193       };
194       this.setPriority(customPriority);
195     } else {
196       Object.keys(this.priorityAttrs).forEach((configOptionName) => {
197         this.osdRecvSpeedForm.get(configOptionName).reset();
198       });
199       this.setPriority(this.priorities[0]);
200     }
201   }
202
203   onPriorityChange(selectedPriorityName) {
204     const selectedPriority =
205       _.find(this.priorities, (p) => {
206         return p.name === selectedPriorityName;
207       }) || this.priorities[0];
208     // Uncheck the 'Customize priority values' checkbox.
209     this.osdRecvSpeedForm.get('customizePriority').setValue(false);
210     // Set the priority profile values.
211     this.setPriority(selectedPriority);
212   }
213
214   submitAction() {
215     const options = {};
216     Object.keys(this.priorityAttrs).forEach((configOptionName) => {
217       options[configOptionName] = {
218         section: 'osd',
219         value: this.osdRecvSpeedForm.getValue(configOptionName)
220       };
221     });
222
223     this.configService.bulkCreate({ options: options }).subscribe(
224       () => {
225         this.notificationService.show(
226           NotificationType.success,
227           this.i18n('OSD recovery speed priority "{{value}}" was set successfully.', {
228             value: this.osdRecvSpeedForm.getValue('priority')
229           }),
230           this.i18n('OSD recovery speed priority')
231         );
232         this.bsModalRef.hide();
233       },
234       () => {
235         this.bsModalRef.hide();
236       }
237     );
238   }
239 }