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')
44 name: 'osd_recovery_max_active',
45 text: this.i18n('Recovery Max Active')
48 name: 'osd_recovery_max_single_start',
49 text: this.i18n('Recovery Max Single Start')
52 name: 'osd_recovery_sleep',
53 text: this.i18n('Recovery Sleep')
57 this.priorityAttrs.forEach((attr) => {
58 this.osdRecvSpeedForm.addControl(
60 new FormControl(null, { validators: [Validators.required] })
63 this.configService.get(attr.name).subscribe((data: any) => {
64 if (data.desc !== '') {
65 attr['desc'] = data.desc;
72 this.getStoredPriority((priority) => {
73 this.setPriority(priority);
77 setPriority(priority: any) {
78 const customPriority = _.find(this.priorities, (p) => {
79 return p.name === 'custom';
82 if (priority.name === 'custom') {
83 if (!customPriority) {
84 this.priorities.push(priority);
88 this.priorities.splice(this.priorities.indexOf(customPriority), 1);
92 this.osdRecvSpeedForm.controls.priority.setValue(priority.name);
93 Object.entries(priority.values).forEach(([name, value]) => {
94 this.osdRecvSpeedForm.controls[name].setValue(value);
98 onCustomizePriorityChange() {
99 if (this.osdRecvSpeedForm.getValue('customizePriority')) {
101 this.priorityAttrs.forEach((attr) => {
102 values[attr.name] = this.osdRecvSpeedForm.getValue(attr.name);
104 const customPriority = {
106 text: this.i18n('Custom'),
109 this.setPriority(customPriority);
111 this.setPriority(this.priorities[0]);
115 getStoredPriority(callbackFn: Function) {
116 const observables = [];
117 this.priorityAttrs.forEach((configName) => {
118 observables.push(this.configService.get(configName.name));
121 observableForkJoin(observables)
123 mergeMap((configOptions) => {
125 configOptions.forEach((configOption) => {
126 if (configOption && 'value' in configOption) {
127 configOption.value.forEach((value) => {
128 if (value['section'] === 'osd') {
129 result[configOption.name] = Number(value.value);
137 .subscribe((resp) => {
138 const priority = _.find(this.priorities, (p) => {
139 return _.isEqual(p.values, resp);
142 this.osdRecvSpeedForm.controls.customizePriority.setValue(false);
145 return callbackFn(priority);
148 if (Object.entries(resp).length === 4) {
149 this.osdRecvSpeedForm.controls.customizePriority.setValue(true);
150 return callbackFn(Object({ name: 'custom', text: this.i18n('Custom'), values: resp }));
153 return callbackFn(this.priorities[0]);
157 onPriorityChange(selectedPriorityName) {
158 const selectedPriority =
159 _.find(this.priorities, (p) => {
160 return p.name === selectedPriorityName;
161 }) || this.priorities[0];
163 this.setPriority(selectedPriority);
168 this.priorityAttrs.forEach((attr) => {
169 options[attr.name] = { section: 'osd', value: this.osdRecvSpeedForm.getValue(attr.name) };
172 this.configService.bulkCreate({ options: options }).subscribe(
174 this.notificationService.show(
175 NotificationType.success,
176 this.i18n('OSD recovery speed priority "{{value}}" was set successfully.', {
177 value: this.osdRecvSpeedForm.getValue('priority')
179 this.i18n('OSD recovery speed priority')
181 this.bsModalRef.hide();
184 this.bsModalRef.hide();