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

33.96% Statements 18/53
66.67% Branches 4/6
18.18% Functions 2/11
31.37% Lines 16/51

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
    1825x

    5x
    5x

    5x

    5x

    5x

    5x

    5x

    1x
    1x

    5x

    1x

    5x

    5x

    5x

    5x
     import { Component, ElementRef, Input, OnChanges, OnInit, ViewChild } from '@angular/core';

    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.inodes';
      rhsCounter = 'mds_server.handle_client_request';

      chart: any;

      constructor() {}

      ngOnInit() {
        if (_.isUndefined(this.mdsCounter)) {
          return;
        }

        const getTitle = (ts) => {
          return moment(ts, 'x').format('LTS');
        };

        const getStyleTop = (tooltip) => {
          return tooltip.caretY - tooltip.height - 15 + 'px';
        };

        const getStyleLeft = (tooltip) => {
          return tooltip.caretX + 'px';
        };

        const chartTooltip = new ChartTooltip(
          this.chartCanvas,
          this.chartTooltip,
          getStyleLeft,
          getStyleTop
        );
        chartTooltip.getTitle = getTitle;
        chartTooltip.checkOffset = true;

        const lhsData = this.convert_timeseries(this.mdsCounter[this.lhsCounter]);
        const rhsData = this.delta_timeseries(this.mdsCounter[this.rhsCounter]);

        this.chart = {
          datasets: [
            {
              label: this.lhsCounter,
              yAxisID: 'LHS',
              data: lhsData,
              tension: 0.1
            },
            {
              label: this.rhsCounter,
              yAxisID: 'RHS',
              data: rhsData,
              tension: 0.1
            }
          ],
          options: {
            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',
                  min: 0
                },
                {
                  id: 'RHS',
                  type: 'linear',
                  position: 'right',
                  min: 0
                }
              ]
            },
            tooltips: {
              enabled: false,
              mode: 'index',
              intersect: false,
              position: 'nearest',
              callbacks: {
                // Pick the Unix timestamp of the first tooltip item.
                title: function(tooltipItems, data) {
                  let ts = 0;
                  if (tooltipItems.length > 0) {
                    const item = tooltipItems[0];
                    ts = data.datasets[item.datasetIndex].data[item.index].x;
                  }
                  return ts;
                }
              },
              custom: (tooltip) => {
                chartTooltip.customTooltips(tooltip);
              }
            }
          },
          chartType: 'line'
        };
      }

      ngOnChanges() {
        if (!this.chart) {
          return;
        }

        const lhsData = this.convert_timeseries(this.mdsCounter[this.lhsCounter]);
        const rhsData = this.delta_timeseries(this.mdsCounter[this.rhsCounter]);

        this.chart.datasets[0].data = lhsData;
        this.chart.datasets[1].data = rhsData;
      }

      // 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)
      convert_timeseries(sourceSeries) {
        const data = [];
        _.each(sourceSeries, (dp) => {
          data.push({
            x: dp[0] * 1000,
            y: dp[1]
          });
        });

        return data;
      }

      delta_timeseries(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;
      }
    }