# All files / src/app/shared/components/grafana grafana.component.ts

87.04% Statements 47/54
73.68% Branches 14/19
70% Functions 7/10
86% Lines 43/50

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
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    23891x
    91x

    91x

    91x
    91x
    91x

    91x

    9x
    9x
    9x
    9x
    9x

    9x

    91x

    91x

    91x

    9x
    9x
    9x
    9x
    9x

    9x

    91x
    8x
    8x

    8x

    8x

    8x
    3x
    3x
    3x
    3x

    8x

    91x
    4x

    4x

    4x

    91x

    91x
    1x

    1x

    91x

    91x
     import { Component, Input, OnChanges, OnInit } from '@angular/core';
    import { DomSanitizer, SafeUrl } from '@angular/platform-browser';

    import { I18n } from '@ngx-translate/i18n-polyfill';

    import { CephReleaseNamePipe } from '../../../shared/pipes/ceph-release-name.pipe';
    import { SummaryService } from '../../../shared/services/summary.service';
    import { SettingsService } from '../../api/settings.service';

    @Component({
      selector: 'cd-grafana',
      template: require('./grafana.component.html'),
      styles: []
    })
    export class GrafanaComponent implements OnInit, OnChanges {
      grafanaSrc: SafeUrl;
      url: string;
      protocol: string;
      host: string;
      port: number;
      baseUrl: any;
      panelStyle: any;
      grafanaExist = false;
      mode = '&kiosk';
      loading = true;
      styles = {};
      dashboardExist = true;
      time: string;
      grafanaTimes: any;
      readonly DEFAULT_TIME: string = 'from=now-1h&to=now';

      @Input()
      grafanaPath: string;
      @Input()
      grafanaStyle: string;
      @Input()
      uid: string;

      docsUrl: string;

      constructor(
        private summaryService: SummaryService,
        private sanitizer: DomSanitizer,
        private settingsService: SettingsService,
        private cephReleaseNamePipe: CephReleaseNamePipe,
        private i18n: I18n
      ) {
        this.grafanaTimes = [
          {
            name: this.i18n('Last 5 minutes'),
            value: 'from=now-5m&to=now'
          },
          {
            name: this.i18n('Last 15 minutes'),
            value: 'from=now-15m&to=now'
          },
          {
            name: this.i18n('Last 30 minutes'),
            value: 'from=now-30m&to=now'
          },
          {
            name: this.i18n('Last 1 hour (Default)'),
            value: 'from=now-1h&to=now'
          },
          {
            name: this.i18n('Last 3 hours'),
            value: 'from=now-3h&to=now'
          },
          {
            name: this.i18n('Last 6 hours'),
            value: 'from=now-6h&to=now'
          },
          {
            name: this.i18n('Last 12 hours'),
            value: 'from=now-12h&to=now'
          },
          {
            name: this.i18n('Last 24 hours'),
            value: 'from=now-24h&to=now'
          },
          {
            name: this.i18n('Yesterday'),
            value: 'from=now-1d%2Fd&to=now-1d%2Fd'
          },
          {
            name: this.i18n('Today'),
            value: 'from=now%2Fd&to=now%2Fd'
          },
          {
            name: this.i18n('Today so far'),
            value: 'from=now%2Fd&to=now'
          },
          {
            name: this.i18n('Day before yesterday'),
            value: 'from=now-2d%2Fd&to=now-2d%2Fd'
          },
          {
            name: this.i18n('Last 2 days'),
            value: 'from=now-2d&to=now'
          },
          {
            name: this.i18n('This day last week'),
            value: 'from=now-7d%2Fd&to=now-7d%2Fd'
          },
          {
            name: this.i18n('Previous week'),
            value: 'from=now-1w%2Fw&to=now-1w%2Fw'
          },
          {
            name: this.i18n('This week'),
            value: 'from=now%2Fw&to=now%2Fw'
          },
          {
            name: this.i18n('This week so far'),
            value: 'from=now%2Fw&to=now'
          },
          {
            name: this.i18n('Last 7 days'),
            value: 'from=now-7d&to=now'
          },
          {
            name: this.i18n('Previous month'),
            value: 'from=now-1M%2FM&to=now-1M%2FM'
          },
          {
            name: this.i18n('This month'),
            value: 'from=now%2FM&to=now%2FM'
          },
          {
            name: this.i18n('This month so far'),
            value: 'from=now%2FM&to=now'
          },
          {
            name: this.i18n('Last 30 days'),
            value: 'from=now-30d&to=now'
          },
          {
            name: this.i18n('Last 90 days'),
            value: 'from=now-90d&to=now'
          },
          {
            name: this.i18n('Last 6 months'),
            value: 'from=now-6M&to=now'
          },
          {
            name: this.i18n('Last 1 year'),
            value: 'from=now-1y&to=now'
          },
          {
            name: this.i18n('Previous year'),
            value: 'from=now-1y%2Fy&to=now-1y%2Fy'
          },
          {
            name: this.i18n('This year'),
            value: 'from=now%2Fy&to=now%2Fy'
          },
          {
            name: this.i18n('This year so far'),
            value: 'from=now%2Fy&to=now'
          },
          {
            name: this.i18n('Last 2 years'),
            value: 'from=now-2y&to=now'
          },
          {
            name: this.i18n('Last 5 years'),
            value: 'from=now-5y&to=now'
          }
        ];
      }

      ngOnInit() {
        this.time = this.DEFAULT_TIME;
        this.styles = {
          one: 'grafana_one',
          two: 'grafana_two',
          three: 'grafana_three'
        };

        const subs = this.summaryService.subscribe((summary: any) => {
          if (!summary) {
            return;
          }

          const releaseName = this.cephReleaseNamePipe.transform(summary.version);
          this.docsUrl =
            `http://docs.ceph.com/docs/${releaseName}/mgr/dashboard/` +
            `#enabling-the-embedding-of-grafana-dashboards`;

          setTimeout(() => {
            subs.unsubscribe();
          }, 0);
        });
        this.settingsService.ifSettingConfigured('api/grafana/url', (url) => {
          this.grafanaExist = true;
          this.loading = false;
          this.baseUrl = url + '/d/';
          this.getFrame();
        });
        this.panelStyle = this.styles[this.grafanaStyle];
      }

      getFrame() {
        this.settingsService
          .validateGrafanaDashboardUrl(this.uid)
          .subscribe((data: any) => (this.dashboardExist = data === 200));
        this.url =
          this.baseUrl +
          this.uid +
          '/' +
          this.grafanaPath +
          '&refresh=2s' +
          this.mode +
          '&' +
          this.time;
        this.grafanaSrc = this.sanitizer.bypassSecurityTrustResourceUrl(this.url);
      }

      onTimepickerChange() {
        if (this.grafanaExist) {
          this.getFrame();
        }
      }

      reset() {
        this.time = this.DEFAULT_TIME;
        if (this.grafanaExist) {
          this.getFrame();
        }
      }

      ngOnChanges() {
        if (this.grafanaExist) {
          this.getFrame();
        }
      }
    }