All files / src/app/shared/services rbd-configuration.service.ts

97.22% Statements 35/36
62.5% Branches 5/8
93.75% Functions 15/16
100% Lines 29/29

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 14720x   20x   20x                           20x     272x 272x                                                                                                                                                                   20x 717x     20x 5x 60x       20x 2x 2x     20x 792x 9504x 792x       20x 2x     20x 710x     20x 3x     20x 1x     20x 1x   20x  
import { Injectable } from '@angular/core';
 
import { I18n } from '@ngx-translate/i18n-polyfill';
 
import {
  RbdConfigurationExtraField,
  RbdConfigurationSection,
  RbdConfigurationType
} from '../models/configuration';
 
/**
 * Define here which options should be made available under which section heading.
 * The display name and description needs to be added manually as long as Ceph does not provide
 * this information.
 */
@Injectable({
  providedIn: 'root'
})
export class RbdConfigurationService {
  readonly sections: RbdConfigurationSection[];
 
  constructor(private i18n: I18n) {
    this.sections = [
      {
        heading: this.i18n('Quality of Service'),
        class: 'quality-of-service',
        options: [
          {
            name: 'rbd_qos_bps_limit',
            displayName: this.i18n('BPS Limit'),
            description: this.i18n('The desired limit of IO bytes per second.'),
            type: RbdConfigurationType.bps
          },
          {
            name: 'rbd_qos_iops_limit',
            displayName: this.i18n('IOPS Limit'),
            description: this.i18n('The desired limit of IO operations per second.'),
            type: RbdConfigurationType.iops
          },
          {
            name: 'rbd_qos_read_bps_limit',
            displayName: this.i18n('Read BPS Limit'),
            description: this.i18n('The desired limit of read bytes per second.'),
            type: RbdConfigurationType.bps
          },
          {
            name: 'rbd_qos_read_iops_limit',
            displayName: this.i18n('Read IOPS Limit'),
            description: this.i18n('The desired limit of read operations per second.'),
            type: RbdConfigurationType.iops
          },
          {
            name: 'rbd_qos_write_bps_limit',
            displayName: this.i18n('Write BPS Limit'),
            description: this.i18n('The desired limit of write bytes per second.'),
            type: RbdConfigurationType.bps
          },
          {
            name: 'rbd_qos_write_iops_limit',
            displayName: this.i18n('Write IOPS Limit'),
            description: this.i18n('The desired limit of write operations per second.'),
            type: RbdConfigurationType.iops
          },
          {
            name: 'rbd_qos_bps_burst',
            displayName: this.i18n('BPS Burst'),
            description: this.i18n('The desired burst limit of IO bytes.'),
            type: RbdConfigurationType.bps
          },
          {
            name: 'rbd_qos_iops_burst',
            displayName: this.i18n('IOPS Burst'),
            description: this.i18n('The desired burst limit of IO operations.'),
            type: RbdConfigurationType.iops
          },
          {
            name: 'rbd_qos_read_bps_burst',
            displayName: this.i18n('Read BPS Burst'),
            description: this.i18n('The desired burst limit of read bytes.'),
            type: RbdConfigurationType.bps
          },
          {
            name: 'rbd_qos_read_iops_burst',
            displayName: this.i18n('Read IOPS Burst'),
            description: this.i18n('The desired burst limit of read operations.'),
            type: RbdConfigurationType.iops
          },
          {
            name: 'rbd_qos_write_bps_burst',
            displayName: this.i18n('Write BPS Burst'),
            description: this.i18n('The desired burst limit of write bytes.'),
            type: RbdConfigurationType.bps
          },
          {
            name: 'rbd_qos_write_iops_burst',
            displayName: this.i18n('Write IOPS Burst'),
            description: this.i18n('The desired burst limit of write operations.'),
            type: RbdConfigurationType.iops
          }
        ] as RbdConfigurationExtraField[]
      }
    ];
  }
 
  private static getOptionsFromSections(sections: RbdConfigurationSection[]) {
    return sections.map((section) => section.options).reduce((a, b) => a.concat(b));
  }
 
  private filterConfigOptionsByName(configName: string) {
    return RbdConfigurationService.getOptionsFromSections(this.sections).filter(
      (option) => option.name === configName
    );
  }
 
  private getOptionValueByName(configName: string, fieldName: string, EdefaultValue = '') {
    const configOptions = this.filterConfigOptionsByName(configName);
    return configOptions.length === 1 ? configOptions.pop()[fieldName] : defaultValue;
  }
 
  getWritableSections() {
    return this.sections.map((section) => {
      section.options = section.options.filter((o) => !o.readOnly);
      return section;
    });
  }
 
  getOptionFields() {
    return RbdConfigurationService.getOptionsFromSections(this.sections);
  }
 
  getWritableOptionFields() {
    return RbdConfigurationService.getOptionsFromSections(this.getWritableSections());
  }
 
  getOptionByName(optionName: string): RbdConfigurationExtraField {
    return this.filterConfigOptionsByName(optionName).pop();
  }
 
  getDisplayName(configName: string): string {
    return this.getOptionValueByName(configName, 'displayName');
  }
 
  getDescription(configName: string): string {
    return this.getOptionValueByName(configName, 'description');
  }
}