summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/ceph/shared/pg-category.service.ts
blob: ae178ded2769d31692f3c6ef537f55223905287f (plain)
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
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);
  }
}