1 import { Component, OnInit } from '@angular/core';
2 import { FormControl, Validators } from '@angular/forms';
4 import { I18n } from '@ngx-translate/i18n-polyfill';
5 import * as _ from 'lodash';
6 import { BsModalRef } from 'ngx-bootstrap/modal';
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';
16 selector: 'cd-osd-recv-speed-modal',
17 templateUrl: './osd-recv-speed-modal.component.html',
18 styleUrls: ['./osd-recv-speed-modal.component.scss']
20 export class OsdRecvSpeedModalComponent implements OnInit {
21 osdRecvSpeedForm: CdFormGroup;
26 public bsModalRef: BsModalRef,
27 private configService: ConfigurationService,
28 private notificationService: NotificationService,
30 private osdService: OsdService
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)
37 this.priorityAttrs = {
39 text: this.i18n('Max Backfills'),
45 osd_recovery_max_active: {
46 text: this.i18n('Recovery Max Active'),
52 osd_recovery_max_single_start: {
53 text: this.i18n('Recovery Max Single Start'),
60 text: this.i18n('Recovery Sleep'),
68 Object.keys(this.priorityAttrs).forEach((configOptionName) => {
69 this.osdRecvSpeedForm.addControl(
71 new FormControl(null, { validators: [Validators.required] })
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);
82 this.setDescription(config_option_values.configOptions);
83 this.setValidators(config_option_values.configOptions);
87 detectPriority(configOptionValues: any, callbackFn: Function) {
88 const priority = _.find(this.priorities, (p) => {
89 return _.isEqual(p.values, configOptionValues);
92 this.osdRecvSpeedForm.controls.customizePriority.setValue(false);
95 return callbackFn(priority);
98 if (Object.entries(configOptionValues).length === 4) {
99 this.osdRecvSpeedForm.controls.customizePriority.setValue(true);
101 Object({ name: 'custom', text: this.i18n('Custom'), values: configOptionValues })
105 return callbackFn(this.priorities[0]);
108 getCurrentValues(configOptions: any) {
109 const currentValues = { values: {}, configOptions: [] };
110 configOptions.forEach((configOption) => {
111 currentValues.configOptions.push(configOption);
113 if ('value' in configOption) {
114 configOption.value.forEach((value) => {
115 if (value.section === 'osd') {
116 currentValues.values[configOption.name] = Number(value.value);
119 } else if ('default' in configOption && configOption.default !== null) {
120 currentValues.values[configOption.name] = Number(configOption.default);
123 return currentValues;
126 setDescription(configOptions: Array<any>) {
127 configOptions.forEach((configOption) => {
128 if (configOption.desc !== '') {
129 this.priorityAttrs[configOption.name].desc = configOption.desc;
134 setPriority(priority: any) {
135 const customPriority = _.find(this.priorities, (p) => {
136 return p.name === 'custom';
139 if (priority.name === 'custom') {
140 if (!customPriority) {
141 this.priorities.push(priority);
144 if (customPriority) {
145 this.priorities.splice(this.priorities.indexOf(customPriority), 1);
149 this.osdRecvSpeedForm.controls.priority.setValue(priority.name);
150 Object.entries(priority.values).forEach(([name, value]) => {
151 this.osdRecvSpeedForm.controls[name].setValue(value);
155 setValidators(configOptions: Array<any>) {
156 configOptions.forEach((configOption) => {
157 const typeValidators = ConfigOptionTypes.getTypeValidators(configOption);
158 if (typeValidators) {
159 typeValidators.validators.push(Validators.required);
161 if ('max' in typeValidators && typeValidators.max !== '') {
162 this.priorityAttrs[configOption.name].maxValue = typeValidators.max;
165 if ('min' in typeValidators && typeValidators.min !== '') {
166 this.priorityAttrs[configOption.name].minValue = typeValidators.min;
169 this.priorityAttrs[configOption.name].patternHelpText = typeValidators.patternHelpText;
170 this.osdRecvSpeedForm.controls[configOption.name].setValidators(typeValidators.validators);
172 this.osdRecvSpeedForm.controls[configOption.name].setValidators(Validators.required);
177 onCustomizePriorityChange() {
179 Object.keys(this.priorityAttrs).forEach((configOptionName) => {
180 values[configOptionName] = this.osdRecvSpeedForm.getValue(configOptionName);
183 if (this.osdRecvSpeedForm.getValue('customizePriority')) {
184 const customPriority = {
186 text: this.i18n('Custom'),
189 this.setPriority(customPriority);
191 this.detectPriority(values, (priority) => {
192 this.setPriority(priority);
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);
210 Object.keys(this.priorityAttrs).forEach((configOptionName) => {
211 options[configOptionName] = {
213 value: this.osdRecvSpeedForm.getValue(configOptionName)
217 this.configService.bulkCreate({ options: options }).subscribe(
219 this.notificationService.show(
220 NotificationType.success,
221 this.i18n('Updated OSD recovery speed priority "{{value}}"', {
222 value: this.osdRecvSpeedForm.getValue('priority')
225 this.bsModalRef.hide();
228 this.bsModalRef.hide();