]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
b6e5ffbfa65924044a58459d089a3586d82c50c9
[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         desc: ''
43       },
44       {
45         name: 'osd_recovery_max_active',
46         text: this.i18n('Recovery Max Active'),
47         desc: ''
48       },
49       {
50         name: 'osd_recovery_max_single_start',
51         text: this.i18n('Recovery Max Single Start'),
52         desc: ''
53       },
54       {
55         name: 'osd_recovery_sleep',
56         text: this.i18n('Recovery Sleep'),
57         desc: ''
58       }
59     ];
60
61     this.priorityAttrs.forEach((attr) => {
62       this.osdRecvSpeedForm.addControl(
63         attr.name,
64         new FormControl(null, { validators: [Validators.required] })
65       );
66     });
67   }
68
69   ngOnInit() {
70     const observables = [];
71     this.priorityAttrs.forEach((configName) => {
72       observables.push(this.configService.get(configName.name));
73     });
74
75     observableForkJoin(observables)
76       .pipe(
77         mergeMap((configOptions) => {
78           const result = { values: {}, configOptions: [] };
79           configOptions.forEach((configOption) => {
80             result.configOptions.push(configOption);
81
82             if (configOption && 'value' in configOption) {
83               configOption.value.forEach((value) => {
84                 if (value['section'] === 'osd') {
85                   result.values[configOption.name] = Number(value.value);
86                 }
87               });
88             }
89           });
90           return of(result);
91         })
92       )
93       .subscribe((resp) => {
94         this.getStoredPriority(resp.values, (priority) => {
95           this.setPriority(priority);
96         });
97         this.setDescription(resp.configOptions);
98       });
99   }
100
101   setDescription(configOptions: Array<any>) {
102     configOptions.forEach((configOption) => {
103       if (configOption.desc !== '') {
104         this.priorityAttrs.forEach((p) => {
105           if (p.name === configOption.name) {
106             p['desc'] = configOption.desc;
107           }
108         });
109       }
110     });
111   }
112
113   setPriority(priority: any) {
114     const customPriority = _.find(this.priorities, (p) => {
115       return p.name === 'custom';
116     });
117
118     if (priority.name === 'custom') {
119       if (!customPriority) {
120         this.priorities.push(priority);
121       }
122     } else {
123       if (customPriority) {
124         this.priorities.splice(this.priorities.indexOf(customPriority), 1);
125       }
126     }
127
128     this.osdRecvSpeedForm.controls.priority.setValue(priority.name);
129     Object.entries(priority.values).forEach(([name, value]) => {
130       this.osdRecvSpeedForm.controls[name].setValue(value);
131     });
132   }
133
134   onCustomizePriorityChange() {
135     if (this.osdRecvSpeedForm.getValue('customizePriority')) {
136       const values = {};
137       this.priorityAttrs.forEach((attr) => {
138         values[attr.name] = this.osdRecvSpeedForm.getValue(attr.name);
139       });
140       const customPriority = {
141         name: 'custom',
142         text: this.i18n('Custom'),
143         values: values
144       };
145       this.setPriority(customPriority);
146     } else {
147       this.setPriority(this.priorities[0]);
148     }
149   }
150
151   getStoredPriority(configOptionValues: any, callbackFn: Function) {
152     const priority = _.find(this.priorities, (p) => {
153       return _.isEqual(p.values, configOptionValues);
154     });
155
156     this.osdRecvSpeedForm.controls.customizePriority.setValue(false);
157
158     if (priority) {
159       return callbackFn(priority);
160     }
161
162     if (Object.entries(configOptionValues).length === 4) {
163       this.osdRecvSpeedForm.controls.customizePriority.setValue(true);
164       return callbackFn(
165         Object({ name: 'custom', text: this.i18n('Custom'), values: configOptionValues })
166       );
167     }
168
169     return callbackFn(this.priorities[0]);
170   }
171
172   onPriorityChange(selectedPriorityName) {
173     const selectedPriority =
174       _.find(this.priorities, (p) => {
175         return p.name === selectedPriorityName;
176       }) || this.priorities[0];
177
178     this.setPriority(selectedPriority);
179   }
180
181   submitAction() {
182     const options = {};
183     this.priorityAttrs.forEach((attr) => {
184       options[attr.name] = { section: 'osd', value: this.osdRecvSpeedForm.getValue(attr.name) };
185     });
186
187     this.configService.bulkCreate({ options: options }).subscribe(
188       () => {
189         this.notificationService.show(
190           NotificationType.success,
191           this.i18n('OSD recovery speed priority "{{value}}" was set successfully.', {
192             value: this.osdRecvSpeedForm.getValue('priority')
193           }),
194           this.i18n('OSD recovery speed priority')
195         );
196         this.bsModalRef.hide();
197       },
198       () => {
199         this.bsModalRef.hide();
200       }
201     );
202   }
203 }