diff options
Diffstat (limited to 'src/pybind/mgr/dashboard/frontend/src/app/ceph/shared/pg-category.service.ts')
-rw-r--r-- | src/pybind/mgr/dashboard/frontend/src/app/ceph/shared/pg-category.service.ts | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/shared/pg-category.service.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/shared/pg-category.service.ts new file mode 100644 index 000000000..ae178ded2 --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/shared/pg-category.service.ts @@ -0,0 +1,63 @@ +import { Injectable } from '@angular/core'; + +import _ 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: string) { + const pgStates = pgStatesText + .replace(/[^a-z_]+/g, ' ') + .trim() + .split(' '); + + return _.uniq(pgStates); + } +} |