1 import { Injectable } from '@angular/core';
3 import * as _ from 'lodash';
5 import { CephSharedModule } from './ceph-shared.module';
6 import { PgCategory } from './pg-category.model';
9 providedIn: CephSharedModule
11 export class PgCategoryService {
12 private categories: object;
15 this.categories = this.createCategories();
19 return PgCategory.VALID_CATEGORIES;
22 getTypeByStates(pgStatesText: string): string {
23 const pgStates = this.getPgStatesFromText(pgStatesText);
25 if (pgStates.length === 0) {
26 return PgCategory.CATEGORY_UNKNOWN;
29 const intersections = _.zipObject(
30 PgCategory.VALID_CATEGORIES,
31 PgCategory.VALID_CATEGORIES.map(
32 (category) => _.intersection(this.categories[category].states, pgStates).length
36 if (intersections[PgCategory.CATEGORY_WARNING] > 0) {
37 return PgCategory.CATEGORY_WARNING;
40 const pgWorkingStates = intersections[PgCategory.CATEGORY_WORKING];
41 if (pgStates.length > intersections[PgCategory.CATEGORY_CLEAN] + pgWorkingStates) {
42 return PgCategory.CATEGORY_UNKNOWN;
45 return pgWorkingStates ? PgCategory.CATEGORY_WORKING : PgCategory.CATEGORY_CLEAN;
48 private createCategories() {
50 PgCategory.VALID_CATEGORIES,
51 PgCategory.VALID_CATEGORIES.map((category) => new PgCategory(category))
55 private getPgStatesFromText(pgStatesText) {
56 const pgStates = pgStatesText
57 .replace(/[^a-z]+/g, ' ')
61 return _.uniq(pgStates);