All files / src/app/ceph/cluster/osd/osd-recv-speed-modal osd-recv-speed-modal.component.ts

79.31% Statements 92/116
72.41% Branches 42/58
66.67% Functions 18/27
79.25% Lines 84/106

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 2397x 7x   7x 7x 7x   7x 7x 7x 7x 7x   7x 7x             7x       17x 17x   17x 17x 17x 17x 17x 17x 17x   17x 17x 17x       17x                                                             17x 68x             20x 20x 3x 3x 3x   3x 3x       7x 8x 26x     8x   8x 6x     2x 1x 1x         1x     7x 7x 7x 28x   28x 7x 7x 7x     21x 20x     7x     7x 4x 16x 4x         7x 5x 20x     5x 1x 1x     4x         5x 20x 20x       7x 4x 16x 16x 16x   16x       16x 12x     16x 16x             7x                                       7x                     7x                                               7x  
import { Component, OnInit } from '@angular/core';
import { FormControl, Validators } from '@angular/forms';
 
import { I18n } from '@ngx-translate/i18n-polyfill';
import * as _ from 'lodash';
import { BsModalRef } from 'ngx-bootstrap/modal';
 
import { ConfigurationService } from '../../../../shared/api/configuration.service';
import { OsdService } from '../../../../shared/api/osd.service';
import { ConfigOptionTypes } from '../../../../shared/components/config-option/config-option.types';
import { NotificationType } from '../../../../shared/enum/notification-type.enum';
import { CdFormGroup } from '../../../../shared/forms/cd-form-group';
import { Permissions } from '../../../../shared/models/permissions';
import { AuthStorageService } from '../../../../shared/services/auth-storage.service';
import { NotificationService } from '../../../../shared/services/notification.service';
 
@Component({
  selector: 'cd-osd-recv-speed-modal',
  template: require('./osd-recv-speed-modal.component.html'),
  styles: []
})
export class OsdRecvSpeedModalComponent implements OnInit {
  osdRecvSpeedForm: CdFormGroup;
  permissions: Permissions;
 
  priorities = [];
  priorityAttrs = {};
 
  constructor(
    public bsModalRef: BsModalRef,
    private authStorageService: AuthStorageService,
    private configService: ConfigurationService,
    private notificationService: NotificationService,
    private i18n: I18n,
    private osdService: OsdService
  ) {
    this.permissions = this.authStorageService.getPermissions();
    this.priorities = this.osdService.osdRecvSpeedModalPriorities.KNOWN_PRIORITIES;
    this.osdRecvSpeedForm = new CdFormGroup({
      priority: new FormControl(null, { validators: [Validators.required] }),
      customizePriority: new FormControl(false)
    });
    this.priorityAttrs = {
      osd_max_backfills: {
        text: this.i18n('Max Backfills'),
        desc: '',
        patternHelpText: '',
        maxValue: undefined,
        minValue: undefined
      },
      osd_recovery_max_active: {
        text: this.i18n('Recovery Max Active'),
        desc: '',
        patternHelpText: '',
        maxValue: undefined,
        minValue: undefined
      },
      osd_recovery_max_single_start: {
        text: this.i18n('Recovery Max Single Start'),
        desc: '',
        patternHelpText: '',
        maxValue: undefined,
        minValue: undefined
      },
      osd_recovery_sleep: {
        text: this.i18n('Recovery Sleep'),
        desc: '',
        patternHelpText: '',
        maxValue: undefined,
        minValue: undefined
      }
    };
 
    Object.keys(this.priorityAttrs).forEach((configOptionName) => {
      this.osdRecvSpeedForm.addControl(
        configOptionName,
        new FormControl(null, { validators: [Validators.required] })
      );
    });
  }
 
  ngOnInit() {
    this.configService.filter(Object.keys(this.priorityAttrs)).subscribe((data: any) => {
      const config_option_values = this.getCurrentValues(data);
      this.detectPriority(config_option_values.values, (priority) => {
        this.setPriority(priority);
      });
      this.setDescription(config_option_values.configOptions);
      this.setValidators(config_option_values.configOptions);
    });
  }
 
  detectPriority(configOptionValues: any, callbackFn: Function) {
    const priority = _.find(this.priorities, (p) => {
      return _.isEqual(p.values, configOptionValues);
    });
 
    this.osdRecvSpeedForm.controls.customizePriority.setValue(false);
 
    if (priority) {
      return callbackFn(priority);
    }
 
    if (Object.entries(configOptionValues).length === 4) {
      this.osdRecvSpeedForm.controls.customizePriority.setValue(true);
      return callbackFn(
        Object({ name: 'custom', text: this.i18n('Custom'), values: configOptionValues })
      );
    }
 
    return callbackFn(this.priorities[0]);
  }
 
  getCurrentValues(configOptions: any) {
    const currentValues = { values: {}, configOptions: [] };
    configOptions.forEach((configOption) => {
      currentValues.configOptions.push(configOption);
 
      if ('value' in configOption) {
        configOption.value.forEach((value) => {
          Eif (value.section === 'osd') {
            currentValues.values[configOption.name] = Number(value.value);
          }
        });
      } else if ('default' in configOption && configOption.default !== null) {
        currentValues.values[configOption.name] = Number(configOption.default);
      }
    });
    return currentValues;
  }
 
  setDescription(configOptions: Array<any>) {
    configOptions.forEach((configOption) => {
      if (configOption.desc !== '') {
        this.priorityAttrs[configOption.name].desc = configOption.desc;
      }
    });
  }
 
  setPriority(priority: any) {
    const customPriority = _.find(this.priorities, (p) => {
      return p.name === 'custom';
    });
 
    if (priority.name === 'custom') {
      Eif (!customPriority) {
        this.priorities.push(priority);
      }
    } else {
      Iif (customPriority) {
        this.priorities.splice(this.priorities.indexOf(customPriority), 1);
      }
    }
 
    this.osdRecvSpeedForm.controls.priority.setValue(priority.name);
    Object.entries(priority.values).forEach(([name, value]) => {
      this.osdRecvSpeedForm.controls[name].setValue(value);
    });
  }
 
  setValidators(configOptions: Array<any>) {
    configOptions.forEach((configOption) => {
      const typeValidators = ConfigOptionTypes.getTypeValidators(configOption);
      Eif (typeValidators) {
        typeValidators.validators.push(Validators.required);
 
        Iif ('max' in typeValidators && typeValidators.max !== '') {
          this.priorityAttrs[configOption.name].maxValue = typeValidators.max;
        }
 
        if ('min' in typeValidators && typeValidators.min !== '') {
          this.priorityAttrs[configOption.name].minValue = typeValidators.min;
        }
 
        this.priorityAttrs[configOption.name].patternHelpText = typeValidators.patternHelpText;
        this.osdRecvSpeedForm.controls[configOption.name].setValidators(typeValidators.validators);
      } else {
        this.osdRecvSpeedForm.controls[configOption.name].setValidators(Validators.required);
      }
    });
  }
 
  onCustomizePriorityChange() {
    const values = {};
    Object.keys(this.priorityAttrs).forEach((configOptionName) => {
      values[configOptionName] = this.osdRecvSpeedForm.getValue(configOptionName);
    });
 
    if (this.osdRecvSpeedForm.getValue('customizePriority')) {
      const customPriority = {
        name: 'custom',
        text: this.i18n('Custom'),
        values: values
      };
      this.setPriority(customPriority);
    } else {
      this.detectPriority(values, (priority) => {
        this.setPriority(priority);
      });
    }
  }
 
  onPriorityChange(selectedPriorityName) {
    const selectedPriority =
      _.find(this.priorities, (p) => {
        return p.name === selectedPriorityName;
      }) || this.priorities[0];
    // Uncheck the 'Customize priority values' checkbox.
    this.osdRecvSpeedForm.get('customizePriority').setValue(false);
    // Set the priority profile values.
    this.setPriority(selectedPriority);
  }
 
  submitAction() {
    const options = {};
    Object.keys(this.priorityAttrs).forEach((configOptionName) => {
      options[configOptionName] = {
        section: 'osd',
        value: this.osdRecvSpeedForm.getValue(configOptionName)
      };
    });
 
    this.configService.bulkCreate({ options: options }).subscribe(
      () => {
        this.notificationService.show(
          NotificationType.success,
          this.i18n('Updated OSD recovery speed priority "{{value}}"', {
            value: this.osdRecvSpeedForm.getValue('priority')
          })
        );
        this.bsModalRef.hide();
      },
      () => {
        this.bsModalRef.hide();
      }
    );
  }
}