All files / src/app/ceph/cephfs/cephfs-chart cephfs-chart.component.ts

80% Statements 48/60
57.14% Branches 8/14
64.29% Functions 9/14
80.7% Lines 46/57

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 2025x     5x 5x   5x             5x   5x   5x     5x   3x 3x   3x                                                                                                                                                                 5x 3x     3x 3x     5x 1x     1x     5x 3x           3x 3x 3x               3x     5x 4x               4x     4x                 5x 4x 4x 16x                     4x   4x     5x   4x 4x 4x 12x 12x 12x 12x   12x         12x   4x   5x  
import { Component, ElementRef, Input, OnChanges, OnInit, ViewChild } from '@angular/core';
 
import { ChartDataSets, ChartOptions, ChartPoint, ChartType } from 'chart.js';
import * as _ from 'lodash';
import * as moment from 'moment';
 
import { ChartTooltip } from '../../../shared/models/chart-tooltip';
 
@Component({
  selector: 'cd-cephfs-chart',
  template: require('./cephfs-chart.component.html'),
  styles: []
})
export class CephfsChartComponent implements OnChanges, OnInit {
  @ViewChild('chartCanvas')
  chartCanvas: ElementRef;
  @ViewChild('chartTooltip')
  chartTooltip: ElementRef;
 
  @Input()
  mdsCounter: any;
 
  lhsCounter = 'mds_mem.ino';
  rhsCounter = 'mds_server.handle_client_request';
 
  chart: {
    datasets: ChartDataSets[];
    options: ChartOptions;
    chartType: ChartType;
  } = {
    datasets: [
      {
        label: this.lhsCounter,
        yAxisID: 'LHS',
        data: [],
        lineTension: 0.1
      },
      {
        label: this.rhsCounter,
        yAxisID: 'RHS',
        data: [],
        lineTension: 0.1
      }
    ],
    options: {
      title: {
        text: '',
        display: true
      },
      responsive: true,
      maintainAspectRatio: false,
      legend: {
        position: 'top'
      },
      scales: {
        xAxes: [
          {
            position: 'top',
            type: 'time',
            time: {
              displayFormats: {
                quarter: 'MMM YYYY'
              }
            },
            ticks: {
              maxRotation: 0
            }
          }
        ],
        yAxes: [
          {
            id: 'LHS',
            type: 'linear',
            position: 'left'
          },
          {
            id: 'RHS',
            type: 'linear',
            position: 'right'
          }
        ]
      },
      tooltips: {
        enabled: false,
        mode: 'index',
        intersect: false,
        position: 'nearest',
        callbacks: {
          // Pick the Unix timestamp of the first tooltip item.
          title: (tooltipItems, data): string => {
            let ts = 0;
            if (tooltipItems.length > 0) {
              const item = tooltipItems[0];
              const point = data.datasets[item.datasetIndex].data[item.index] as ChartPoint;
              ts = point.x as number;
            }
            return ts.toString();
          }
        }
      }
    },
    chartType: 'line'
  };
 
  constructor() {}
 
  ngOnInit() {
    Iif (_.isUndefined(this.mdsCounter)) {
      return;
    }
    this.setChartTooltip();
    this.updateChart();
  }
 
  ngOnChanges() {
    Iif (_.isUndefined(this.mdsCounter)) {
      return;
    }
    this.updateChart();
  }
 
  private setChartTooltip() {
    const chartTooltip = new ChartTooltip(
      this.chartCanvas,
      this.chartTooltip,
      (tooltip) => tooltip.caretX + 'px',
      (tooltip) => tooltip.caretY - tooltip.height - 23 + 'px'
    );
    chartTooltip.getTitle = (ts) => moment(ts, 'x').format('LTS');
    chartTooltip.checkOffset = true;
    const chartOptions: ChartOptions = {
      title: {
        text: this.mdsCounter.name
      },
      tooltips: {
        custom: (tooltip) => chartTooltip.customTooltips(tooltip)
      }
    };
    _.merge(this.chart, { options: chartOptions });
  }
 
  private updateChart() {
    const chartDataSets: ChartDataSets[] = [
      {
        data: this.convertTimeSeries(this.mdsCounter[this.lhsCounter])
      },
      {
        data: this.deltaTimeSeries(this.mdsCounter[this.rhsCounter])
      }
    ];
    _.merge(this.chart, {
      datasets: chartDataSets
    });
    this.chart.datasets = [...this.chart.datasets]; // Force angular to update
  }
 
  /**
   * Convert ceph-mgr's time series format (list of 2-tuples
   * with seconds-since-epoch timestamps) into what chart.js
   * can handle (list of objects with millisecs-since-epoch
   * timestamps)
   */
  private convertTimeSeries(sourceSeries) {
    const data = [];
    _.each(sourceSeries, (dp) => {
      data.push({
        x: dp[0] * 1000,
        y: dp[1]
      });
    });
 
    /**
     * MDS performance counters chart is expecting the same number of items
     * from each data series. Since in deltaTimeSeries we are ignoring the first
     * element, we will do the same here.
     */
    data.shift();
 
    return data;
  }
 
  private deltaTimeSeries(sourceSeries) {
    let i;
    let prev = sourceSeries[0];
    const result = [];
    for (i = 1; i < sourceSeries.length; i++) {
      const cur = sourceSeries[i];
      const tdelta = cur[0] - prev[0];
      const vdelta = cur[1] - prev[1];
      const rate = vdelta / tdelta;
 
      result.push({
        x: cur[0] * 1000,
        y: rate
      });
 
      prev = cur;
    }
    return result;
  }
}