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