]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { Injectable } from '@angular/core'; |
2 | ||
3 | import * as _ from 'lodash'; | |
4 | ||
5 | import { CephSharedModule } from './ceph-shared.module'; | |
6 | import { PgCategory } from './pg-category.model'; | |
7 | ||
8 | @Injectable({ | |
9 | providedIn: CephSharedModule | |
10 | }) | |
11 | export class PgCategoryService { | |
12 | private categories: object; | |
13 | ||
14 | constructor() { | |
15 | this.categories = this.createCategories(); | |
16 | } | |
17 | ||
18 | getAllTypes() { | |
19 | return PgCategory.VALID_CATEGORIES; | |
20 | } | |
21 | ||
22 | getTypeByStates(pgStatesText: string): string { | |
23 | const pgStates = this.getPgStatesFromText(pgStatesText); | |
24 | ||
25 | if (pgStates.length === 0) { | |
26 | return PgCategory.CATEGORY_UNKNOWN; | |
27 | } | |
28 | ||
29 | const intersections = _.zipObject( | |
30 | PgCategory.VALID_CATEGORIES, | |
31 | PgCategory.VALID_CATEGORIES.map( | |
32 | (category) => _.intersection(this.categories[category].states, pgStates).length | |
33 | ) | |
34 | ); | |
35 | ||
36 | if (intersections[PgCategory.CATEGORY_WARNING] > 0) { | |
37 | return PgCategory.CATEGORY_WARNING; | |
38 | } | |
39 | ||
40 | const pgWorkingStates = intersections[PgCategory.CATEGORY_WORKING]; | |
41 | if (pgStates.length > intersections[PgCategory.CATEGORY_CLEAN] + pgWorkingStates) { | |
42 | return PgCategory.CATEGORY_UNKNOWN; | |
43 | } | |
44 | ||
45 | return pgWorkingStates ? PgCategory.CATEGORY_WORKING : PgCategory.CATEGORY_CLEAN; | |
46 | } | |
47 | ||
48 | private createCategories() { | |
49 | return _.zipObject( | |
50 | PgCategory.VALID_CATEGORIES, | |
51 | PgCategory.VALID_CATEGORIES.map((category) => new PgCategory(category)) | |
52 | ); | |
53 | } | |
54 | ||
9f95a23c | 55 | private getPgStatesFromText(pgStatesText: string) { |
11fdf7f2 TL |
56 | const pgStates = pgStatesText |
57 | .replace(/[^a-z]+/g, ' ') | |
58 | .trim() | |
59 | .split(' '); | |
60 | ||
61 | return _.uniq(pgStates); | |
62 | } | |
63 | } |