All files / src/app/ceph/dashboard/health health.component.ts

89.41% Statements 76/85
79.07% Branches 34/43
93.33% Functions 14/15
88.75% Lines 71/80

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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 2265x   5x 5x 5x   5x   5x 5x 5x 5x       5x 5x 5x             5x   21x       21x         21x                             21x                           21x 21x 21x 21x 21x 21x 21x 21x   21x 21x     12x 12x 12x 12x       5x 20x     24x 24x 24x       5x                                                   5x 18x       18x         18x   18x                 18x         20x 20x 20x   20x 4x   4x 4x   4x 4x     20x   80x   20x               5x 18x   18x         18x                               18x             18x       18x     5x 64x 64x   64x     5x 196x 189x     7x   5x  
import { Component, OnDestroy, OnInit } from '@angular/core';
 
import { I18n } from '@ngx-translate/i18n-polyfill';
import * as _ from 'lodash';
import { Subscription } from 'rxjs/Subscription';
 
import { HealthService } from '../../../shared/api/health.service';
import { Permissions } from '../../../shared/models/permissions';
import { DimlessBinaryPipe } from '../../../shared/pipes/dimless-binary.pipe';
import { DimlessPipe } from '../../../shared/pipes/dimless.pipe';
import { AuthStorageService } from '../../../shared/services/auth-storage.service';
import {
  FeatureTogglesMap$,
  FeatureTogglesService
} from '../../../shared/services/feature-toggles.service';
import { RefreshIntervalService } from '../../../shared/services/refresh-interval.service';
import { PgCategoryService } from '../../shared/pg-category.service';
import { HealthPieColor } from '../health-pie/health-pie-color.enum';
 
@Component({
  selector: 'cd-health',
  template: require('./health.component.html'),
  styles: []
})
export class HealthComponent implements OnInit, OnDestroy {
  healthData: any;
  interval = new Subscription();
  permissions: Permissions;
  enabledFeature$: FeatureTogglesMap$;
 
  rawCapacityChartConfig = {
    options: {
      title: { display: true, position: 'bottom' }
    }
  };
  objectsChartConfig = {
    options: {
      title: { display: true, position: 'bottom' }
    },
    colors: [
      {
        backgroundColor: [
          HealthPieColor.DEFAULT_GREEN,
          HealthPieColor.DEFAULT_MAGENTA,
          HealthPieColor.DEFAULT_ORANGE,
          HealthPieColor.DEFAULT_RED
        ]
      }
    ]
  };
  pgStatusChartConfig = {
    colors: [
      {
        backgroundColor: [
          HealthPieColor.DEFAULT_GREEN,
          HealthPieColor.DEFAULT_BLUE,
          HealthPieColor.DEFAULT_ORANGE,
          HealthPieColor.DEFAULT_RED
        ]
      }
    ]
  };
 
  constructor(
    private healthService: HealthService,
    private i18n: I18n,
    private authStorageService: AuthStorageService,
    private pgCategoryService: PgCategoryService,
    private featureToggles: FeatureTogglesService,
    private refreshIntervalService: RefreshIntervalService,
    private dimlessBinary: DimlessBinaryPipe,
    private dimless: DimlessPipe
  ) {
    this.permissions = this.authStorageService.getPermissions();
    this.enabledFeature$ = this.featureToggles.get();
  }
 
  ngOnInit() {
    this.getHealth();
    this.interval = this.refreshIntervalService.intervalData$.subscribe(() => {
      this.getHealth();
    });
  }
 
  ngOnDestroy() {
    this.interval.unsubscribe();
  }
 
  getHealth() {
    this.healthService.getMinimalHealth().subscribe((data: any) => {
      this.healthData = data;
    });
  }
 
  prepareReadWriteRatio(chart) {
    const ratioLabels = [];
    const ratioData = [];
 
    const total =
      this.healthData.client_perf.write_op_per_sec + this.healthData.client_perf.read_op_per_sec;
 
    ratioLabels.push(
      `${this.i18n('Writes')} (${this.calcPercentage(
        this.healthData.client_perf.write_op_per_sec,
        total
      )}%)`
    );
    ratioData.push(this.healthData.client_perf.write_op_per_sec);
    ratioLabels.push(
      `${this.i18n('Reads')} (${this.calcPercentage(
        this.healthData.client_perf.read_op_per_sec,
        total
      )}%)`
    );
    ratioData.push(this.healthData.client_perf.read_op_per_sec);
 
    chart.dataset[0].data = ratioData;
    chart.labels = ratioLabels;
  }
 
  prepareRawUsage(chart, data) {
    const percentAvailable = this.calcPercentage(
      data.df.stats.total_bytes - data.df.stats.total_used_raw_bytes,
      data.df.stats.total_bytes
    );
    const percentUsed = this.calcPercentage(
      data.df.stats.total_used_raw_bytes,
      data.df.stats.total_bytes
    );
 
    chart.dataset[0].data = [data.df.stats.total_used_raw_bytes, data.df.stats.total_avail_bytes];
 
    chart.labels = [
      `${this.dimlessBinary.transform(data.df.stats.total_used_raw_bytes)} ${this.i18n(
        'Used'
      )} (${percentUsed}%)`,
      `${this.dimlessBinary.transform(
        data.df.stats.total_bytes - data.df.stats.total_used_raw_bytes
      )} ${this.i18n('Avail.')} (${percentAvailable}%)`
    ];
 
    chart.options.title.text = `${this.dimlessBinary.transform(
      data.df.stats.total_bytes
    )} ${this.i18n('total')}`;
  }
 
  preparePgStatus(chart, data) {
    const categoryPgAmount = {};
    let totalPgs = 0;
 
    _.forEach(data.pg_info.statuses, (pgAmount, pgStatesText) => {
      const categoryType = this.pgCategoryService.getTypeByStates(pgStatesText);
 
      Eif (_.isUndefined(categoryPgAmount[categoryType])) {
        categoryPgAmount[categoryType] = 0;
      }
      categoryPgAmount[categoryType] += pgAmount;
      totalPgs += pgAmount;
    });
 
    chart.dataset[0].data = this.pgCategoryService
      .getAllTypes()
      .map((categoryType) => categoryPgAmount[categoryType]);
 
    chart.labels = [
      `${this.i18n('Clean')} (${this.calcPercentage(categoryPgAmount['clean'], totalPgs)}%)`,
      `${this.i18n('Working')} (${this.calcPercentage(categoryPgAmount['working'], totalPgs)}%)`,
      `${this.i18n('Warning')} (${this.calcPercentage(categoryPgAmount['warning'], totalPgs)}%)`,
      `${this.i18n('Unknown')} (${this.calcPercentage(categoryPgAmount['unknown'], totalPgs)}%)`
    ];
  }
 
  prepareObjects(chart, data) {
    const totalReplicas = data.pg_info.object_stats.num_object_copies;
    const healthy =
      totalReplicas -
      data.pg_info.object_stats.num_objects_misplaced -
      data.pg_info.object_stats.num_objects_degraded -
      data.pg_info.object_stats.num_objects_unfound;
 
    chart.labels = [
      `${this.i18n('Healthy')} (${this.calcPercentage(healthy, totalReplicas)}%)`,
      `${this.i18n('Misplaced')} (${this.calcPercentage(
        data.pg_info.object_stats.num_objects_misplaced,
        totalReplicas
      )}%)`,
      `${this.i18n('Degraded')} (${this.calcPercentage(
        data.pg_info.object_stats.num_objects_degraded,
        totalReplicas
      )}%)`,
      `${this.i18n('Unfound')} (${this.calcPercentage(
        data.pg_info.object_stats.num_objects_unfound,
        totalReplicas
      )}%)`
    ];
 
    chart.dataset[0].data = [
      healthy,
      data.pg_info.object_stats.num_objects_misplaced,
      data.pg_info.object_stats.num_objects_degraded,
      data.pg_info.object_stats.num_objects_unfound
    ];
 
    chart.options.title.text = `${this.dimless.transform(
      data.pg_info.object_stats.num_objects
    )} ${this.i18n('total')} (${this.dimless.transform(totalReplicas)} ${this.i18n('replicas')})`;
 
    chart.options.maintainAspectRatio = window.innerWidth >= 375;
  }
 
  isClientReadWriteChartShowable() {
    const readOps = this.healthData.client_perf.read_op_per_sec || 0;
    const writeOps = this.healthData.client_perf.write_op_per_sec || 0;
 
    return readOps + writeOps > 0;
  }
 
  private calcPercentage(dividend: number, divisor: number) {
    if (!_.isNumber(dividend) || !_.isNumber(divisor) || divisor === 0) {
      return 0;
    }
 
    return Math.round((dividend / divisor) * 100);
  }
}