# All files / src/app/ceph/shared pg-category.service.ts

96.3% Statements 26/27
100% Branches 2/2
100% Functions 7/7
95.83% Lines 23/24

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
    649x

    9x

    9x
    9x

    9x

    47x

    9x
    19x

    34x
    34x

    34x

    136x

    5x

    29x

    22x

    7x

    9x
    47x

    188x

    9x
    34x

    34x

    9x
     import { Injectable } from '@angular/core';

    import * as _ from 'lodash';

    import { CephSharedModule } from './ceph-shared.module';
    import { PgCategory } from './pg-category.model';

    @Injectable({
      providedIn: CephSharedModule
    })
    export class PgCategoryService {
      private categories: object;

      constructor() {
        this.categories = this.createCategories();
      }

      getAllTypes() {
        return PgCategory.VALID_CATEGORIES;
      }

      getTypeByStates(pgStatesText: string): string {
        const pgStates = this.getPgStatesFromText(pgStatesText);

        if (pgStates.length === 0) {
          return PgCategory.CATEGORY_UNKNOWN;
        }

        const intersections = _.zipObject(
          PgCategory.VALID_CATEGORIES,
          PgCategory.VALID_CATEGORIES.map(
            (category) => _.intersection(this.categories[category].states, pgStates).length
          )
        );

        if (intersections[PgCategory.CATEGORY_WARNING] > 0) {
          return PgCategory.CATEGORY_WARNING;
        }

        const pgWorkingStates = intersections[PgCategory.CATEGORY_WORKING];
        if (pgStates.length > intersections[PgCategory.CATEGORY_CLEAN] + pgWorkingStates) {
          return PgCategory.CATEGORY_UNKNOWN;
        }

        return pgWorkingStates ? PgCategory.CATEGORY_WORKING : PgCategory.CATEGORY_CLEAN;
      }

      private createCategories() {
        return _.zipObject(
          PgCategory.VALID_CATEGORIES,
          PgCategory.VALID_CATEGORIES.map((category) => new PgCategory(category))
        );
      }

      private getPgStatesFromText(pgStatesText) {
        const pgStates = pgStatesText
          .replace(/[^a-z]+/g, ' ')
          .trim()
          .split(' ');

        return _.uniq(pgStates);
      }
    }