]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
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'; | |
11fdf7f2 TL |
7 | |
8 | import { ConfigurationService } from '../../../../shared/api/configuration.service'; | |
9 | import { OsdService } from '../../../../shared/api/osd.service'; | |
81eedcae | 10 | import { ConfigOptionTypes } from '../../../../shared/components/config-option/config-option.types'; |
11fdf7f2 TL |
11 | import { NotificationType } from '../../../../shared/enum/notification-type.enum'; |
12 | import { CdFormGroup } from '../../../../shared/forms/cd-form-group'; | |
81eedcae TL |
13 | import { Permissions } from '../../../../shared/models/permissions'; |
14 | import { AuthStorageService } from '../../../../shared/services/auth-storage.service'; | |
11fdf7f2 | 15 | import { NotificationService } from '../../../../shared/services/notification.service'; |
11fdf7f2 TL |
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; | |
81eedcae TL |
24 | permissions: Permissions; |
25 | ||
11fdf7f2 TL |
26 | priorities = []; |
27 | priorityAttrs = {}; | |
28 | ||
29 | constructor( | |
30 | public bsModalRef: BsModalRef, | |
81eedcae | 31 | private authStorageService: AuthStorageService, |
11fdf7f2 TL |
32 | private configService: ConfigurationService, |
33 | private notificationService: NotificationService, | |
34 | private i18n: I18n, | |
35 | private osdService: OsdService | |
36 | ) { | |
81eedcae | 37 | this.permissions = this.authStorageService.getPermissions(); |
11fdf7f2 TL |
38 | this.priorities = this.osdService.osdRecvSpeedModalPriorities.KNOWN_PRIORITIES; |
39 | this.osdRecvSpeedForm = new CdFormGroup({ | |
40 | priority: new FormControl(null, { validators: [Validators.required] }), | |
41 | customizePriority: new FormControl(false) | |
42 | }); | |
43 | this.priorityAttrs = { | |
44 | osd_max_backfills: { | |
45 | text: this.i18n('Max Backfills'), | |
46 | desc: '', | |
47 | patternHelpText: '', | |
48 | maxValue: undefined, | |
49 | minValue: undefined | |
50 | }, | |
51 | osd_recovery_max_active: { | |
52 | text: this.i18n('Recovery Max Active'), | |
53 | desc: '', | |
54 | patternHelpText: '', | |
55 | maxValue: undefined, | |
56 | minValue: undefined | |
57 | }, | |
58 | osd_recovery_max_single_start: { | |
59 | text: this.i18n('Recovery Max Single Start'), | |
60 | desc: '', | |
61 | patternHelpText: '', | |
62 | maxValue: undefined, | |
63 | minValue: undefined | |
64 | }, | |
65 | osd_recovery_sleep: { | |
66 | text: this.i18n('Recovery Sleep'), | |
67 | desc: '', | |
68 | patternHelpText: '', | |
69 | maxValue: undefined, | |
70 | minValue: undefined | |
71 | } | |
72 | }; | |
73 | ||
74 | Object.keys(this.priorityAttrs).forEach((configOptionName) => { | |
75 | this.osdRecvSpeedForm.addControl( | |
76 | configOptionName, | |
77 | new FormControl(null, { validators: [Validators.required] }) | |
78 | ); | |
79 | }); | |
80 | } | |
81 | ||
82 | ngOnInit() { | |
81eedcae TL |
83 | this.configService.filter(Object.keys(this.priorityAttrs)).subscribe((data: any) => { |
84 | const config_option_values = this.getCurrentValues(data); | |
85 | this.detectPriority(config_option_values.values, (priority) => { | |
86 | this.setPriority(priority); | |
11fdf7f2 | 87 | }); |
81eedcae TL |
88 | this.setDescription(config_option_values.configOptions); |
89 | this.setValidators(config_option_values.configOptions); | |
90 | }); | |
11fdf7f2 TL |
91 | } |
92 | ||
93 | detectPriority(configOptionValues: any, callbackFn: Function) { | |
94 | const priority = _.find(this.priorities, (p) => { | |
95 | return _.isEqual(p.values, configOptionValues); | |
96 | }); | |
97 | ||
98 | this.osdRecvSpeedForm.controls.customizePriority.setValue(false); | |
99 | ||
100 | if (priority) { | |
101 | return callbackFn(priority); | |
102 | } | |
103 | ||
104 | if (Object.entries(configOptionValues).length === 4) { | |
105 | this.osdRecvSpeedForm.controls.customizePriority.setValue(true); | |
106 | return callbackFn( | |
107 | Object({ name: 'custom', text: this.i18n('Custom'), values: configOptionValues }) | |
108 | ); | |
109 | } | |
110 | ||
111 | return callbackFn(this.priorities[0]); | |
112 | } | |
113 | ||
114 | getCurrentValues(configOptions: any) { | |
115 | const currentValues = { values: {}, configOptions: [] }; | |
116 | configOptions.forEach((configOption) => { | |
117 | currentValues.configOptions.push(configOption); | |
118 | ||
119 | if ('value' in configOption) { | |
120 | configOption.value.forEach((value) => { | |
121 | if (value.section === 'osd') { | |
122 | currentValues.values[configOption.name] = Number(value.value); | |
123 | } | |
124 | }); | |
125 | } else if ('default' in configOption && configOption.default !== null) { | |
126 | currentValues.values[configOption.name] = Number(configOption.default); | |
127 | } | |
128 | }); | |
129 | return currentValues; | |
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 | const values = {}; | |
185 | Object.keys(this.priorityAttrs).forEach((configOptionName) => { | |
186 | values[configOptionName] = this.osdRecvSpeedForm.getValue(configOptionName); | |
187 | }); | |
188 | ||
189 | if (this.osdRecvSpeedForm.getValue('customizePriority')) { | |
190 | const customPriority = { | |
191 | name: 'custom', | |
192 | text: this.i18n('Custom'), | |
193 | values: values | |
194 | }; | |
195 | this.setPriority(customPriority); | |
196 | } else { | |
197 | this.detectPriority(values, (priority) => { | |
198 | this.setPriority(priority); | |
199 | }); | |
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('Updated OSD recovery speed priority "{{value}}"', { | |
228 | value: this.osdRecvSpeedForm.getValue('priority') | |
229 | }) | |
230 | ); | |
231 | this.bsModalRef.hide(); | |
232 | }, | |
233 | () => { | |
234 | this.bsModalRef.hide(); | |
235 | } | |
236 | ); | |
237 | } | |
238 | } |