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';
7 import { forkJoin as observableForkJoin, of } from 'rxjs';
8 import { mergeMap } from 'rxjs/operators';
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';
17 selector: 'cd-osd-recv-speed-modal',
18 templateUrl: './osd-recv-speed-modal.component.html',
19 styleUrls: ['./osd-recv-speed-modal.component.scss']
21 export class OsdRecvSpeedModalComponent implements OnInit {
22 osdRecvSpeedForm: CdFormGroup;
27 public bsModalRef: BsModalRef,
28 private configService: ConfigurationService,
29 private notificationService: NotificationService,
31 private osdService: OsdService
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)
38 this.priorityAttrs = [
40 name: 'osd_max_backfills',
41 text: this.i18n('Max Backfills'),
45 name: 'osd_recovery_max_active',
46 text: this.i18n('Recovery Max Active'),
50 name: 'osd_recovery_max_single_start',
51 text: this.i18n('Recovery Max Single Start'),
55 name: 'osd_recovery_sleep',
56 text: this.i18n('Recovery Sleep'),
61 this.priorityAttrs.forEach((attr) => {
62 this.osdRecvSpeedForm.addControl(
64 new FormControl(null, { validators: [Validators.required] })
70 const observables = [];
71 this.priorityAttrs.forEach((configName) => {
72 observables.push(this.configService.get(configName.name));
75 observableForkJoin(observables)
77 mergeMap((configOptions) => {
78 const result = { values: {}, configOptions: [] };
79 configOptions.forEach((configOption) => {
80 result.configOptions.push(configOption);
82 if (configOption && 'value' in configOption) {
83 configOption.value.forEach((value) => {
84 if (value['section'] === 'osd') {
85 result.values[configOption.name] = Number(value.value);
93 .subscribe((resp) => {
94 this.getStoredPriority(resp.values, (priority) => {
95 this.setPriority(priority);
97 this.setDescription(resp.configOptions);
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;
113 setPriority(priority: any) {
114 const customPriority = _.find(this.priorities, (p) => {
115 return p.name === 'custom';
118 if (priority.name === 'custom') {
119 if (!customPriority) {
120 this.priorities.push(priority);
123 if (customPriority) {
124 this.priorities.splice(this.priorities.indexOf(customPriority), 1);
128 this.osdRecvSpeedForm.controls.priority.setValue(priority.name);
129 Object.entries(priority.values).forEach(([name, value]) => {
130 this.osdRecvSpeedForm.controls[name].setValue(value);
134 onCustomizePriorityChange() {
135 if (this.osdRecvSpeedForm.getValue('customizePriority')) {
137 this.priorityAttrs.forEach((attr) => {
138 values[attr.name] = this.osdRecvSpeedForm.getValue(attr.name);
140 const customPriority = {
142 text: this.i18n('Custom'),
145 this.setPriority(customPriority);
147 this.setPriority(this.priorities[0]);
151 getStoredPriority(configOptionValues: any, callbackFn: Function) {
152 const priority = _.find(this.priorities, (p) => {
153 return _.isEqual(p.values, configOptionValues);
156 this.osdRecvSpeedForm.controls.customizePriority.setValue(false);
159 return callbackFn(priority);
162 if (Object.entries(configOptionValues).length === 4) {
163 this.osdRecvSpeedForm.controls.customizePriority.setValue(true);
165 Object({ name: 'custom', text: this.i18n('Custom'), values: configOptionValues })
169 return callbackFn(this.priorities[0]);
172 onPriorityChange(selectedPriorityName) {
173 const selectedPriority =
174 _.find(this.priorities, (p) => {
175 return p.name === selectedPriorityName;
176 }) || this.priorities[0];
178 this.setPriority(selectedPriority);
183 this.priorityAttrs.forEach((attr) => {
184 options[attr.name] = { section: 'osd', value: this.osdRecvSpeedForm.getValue(attr.name) };
187 this.configService.bulkCreate({ options: options }).subscribe(
189 this.notificationService.show(
190 NotificationType.success,
191 this.i18n('OSD recovery speed priority "{{value}}" was set successfully.', {
192 value: this.osdRecvSpeedForm.getValue('priority')
194 this.i18n('OSD recovery speed priority')
196 this.bsModalRef.hide();
199 this.bsModalRef.hide();