]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { Component, OnInit } from '@angular/core'; |
2 | import { FormControl, Validators } from '@angular/forms'; | |
3 | ||
f67539c2 TL |
4 | import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; |
5 | import _ from 'lodash'; | |
6 | ||
7 | import { ConfigurationService } from '~/app/shared/api/configuration.service'; | |
8 | import { OsdService } from '~/app/shared/api/osd.service'; | |
9 | import { ConfigOptionTypes } from '~/app/shared/components/config-option/config-option.types'; | |
10 | import { ActionLabelsI18n } from '~/app/shared/constants/app.constants'; | |
11 | import { NotificationType } from '~/app/shared/enum/notification-type.enum'; | |
12 | import { CdFormGroup } from '~/app/shared/forms/cd-form-group'; | |
13 | import { Permissions } from '~/app/shared/models/permissions'; | |
14 | import { AuthStorageService } from '~/app/shared/services/auth-storage.service'; | |
15 | import { NotificationService } from '~/app/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 | ||
9f95a23c | 26 | priorities: any[] = []; |
11fdf7f2 TL |
27 | priorityAttrs = {}; |
28 | ||
29 | constructor( | |
f67539c2 TL |
30 | public activeModal: NgbActiveModal, |
31 | public actionLabels: ActionLabelsI18n, | |
81eedcae | 32 | private authStorageService: AuthStorageService, |
11fdf7f2 TL |
33 | private configService: ConfigurationService, |
34 | private notificationService: NotificationService, | |
11fdf7f2 TL |
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: { | |
f67539c2 | 45 | text: $localize`Max Backfills`, |
11fdf7f2 TL |
46 | desc: '', |
47 | patternHelpText: '', | |
48 | maxValue: undefined, | |
49 | minValue: undefined | |
50 | }, | |
51 | osd_recovery_max_active: { | |
f67539c2 | 52 | text: $localize`Recovery Max Active`, |
11fdf7f2 TL |
53 | desc: '', |
54 | patternHelpText: '', | |
55 | maxValue: undefined, | |
56 | minValue: undefined | |
57 | }, | |
58 | osd_recovery_max_single_start: { | |
f67539c2 | 59 | text: $localize`Recovery Max Single Start`, |
11fdf7f2 TL |
60 | desc: '', |
61 | patternHelpText: '', | |
62 | maxValue: undefined, | |
63 | minValue: undefined | |
64 | }, | |
65 | osd_recovery_sleep: { | |
f67539c2 | 66 | text: $localize`Recovery Sleep`, |
11fdf7f2 TL |
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); | |
9f95a23c | 85 | this.detectPriority(config_option_values.values, (priority: any) => { |
81eedcae | 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( | |
f67539c2 | 107 | Object({ name: 'custom', text: $localize`Custom`, values: configOptionValues }) |
11fdf7f2 TL |
108 | ); |
109 | } | |
110 | ||
111 | return callbackFn(this.priorities[0]); | |
112 | } | |
113 | ||
114 | getCurrentValues(configOptions: any) { | |
9f95a23c TL |
115 | const currentValues: Record<string, any> = { values: {}, configOptions: [] }; |
116 | configOptions.forEach((configOption: any) => { | |
11fdf7f2 TL |
117 | currentValues.configOptions.push(configOption); |
118 | ||
119 | if ('value' in configOption) { | |
9f95a23c | 120 | configOption.value.forEach((value: any) => { |
11fdf7f2 TL |
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', | |
f67539c2 | 192 | text: $localize`Custom`, |
11fdf7f2 TL |
193 | values: values |
194 | }; | |
195 | this.setPriority(customPriority); | |
196 | } else { | |
9f95a23c | 197 | this.detectPriority(values, (priority: any) => { |
11fdf7f2 TL |
198 | this.setPriority(priority); |
199 | }); | |
200 | } | |
201 | } | |
202 | ||
9f95a23c | 203 | onPriorityChange(selectedPriorityName: string) { |
11fdf7f2 TL |
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, | |
f67539c2 TL |
227 | $localize`Updated OSD recovery speed priority '${this.osdRecvSpeedForm.getValue( |
228 | 'priority' | |
229 | )}'` | |
11fdf7f2 | 230 | ); |
f67539c2 | 231 | this.activeModal.close(); |
11fdf7f2 TL |
232 | }, |
233 | () => { | |
f67539c2 | 234 | this.activeModal.close(); |
11fdf7f2 TL |
235 | } |
236 | ); | |
237 | } | |
238 | } |