summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/ceph/cephfs/cephfs-tabs/cephfs-tabs.component.ts
blob: 404ec20aac2829ed9b732cdad357e5d004bdedb7 (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
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
import { Component, Input, NgZone, OnChanges, OnDestroy } from '@angular/core';

import _ from 'lodash';
import { Subscription, timer } from 'rxjs';

import { CephfsService } from '~/app/shared/api/cephfs.service';
import { TableStatusViewCache } from '~/app/shared/classes/table-status-view-cache';
import { ViewCacheStatus } from '~/app/shared/enum/view-cache-status.enum';
import { Permission } from '~/app/shared/models/permissions';
import { AuthStorageService } from '~/app/shared/services/auth-storage.service';

@Component({
  selector: 'cd-cephfs-tabs',
  templateUrl: './cephfs-tabs.component.html',
  styleUrls: ['./cephfs-tabs.component.scss']
})
export class CephfsTabsComponent implements OnChanges, OnDestroy {
  @Input()
  selection: any;

  // Grafana tab
  grafanaId: any;
  grafanaPermission: Permission;

  // Client tab
  id: number;
  clients: Record<string, any> = {
    data: [],
    status: new TableStatusViewCache(ViewCacheStatus.ValueNone)
  };

  // Details tab
  details: Record<string, any> = {
    standbys: '',
    pools: [],
    ranks: [],
    mdsCounters: {},
    name: ''
  };

  private data: any;
  private reloadSubscriber: Subscription;

  constructor(
    private ngZone: NgZone,
    private authStorageService: AuthStorageService,
    private cephfsService: CephfsService
  ) {
    this.grafanaPermission = this.authStorageService.getPermissions().grafana;
  }

  ngOnChanges() {
    if (!this.selection) {
      this.unsubscribeInterval();
      return;
    }
    if (this.selection.id !== this.id) {
      this.setupSelected(this.selection.id, this.selection.mdsmap.info);
    }
  }

  private setupSelected(id: number, mdsInfo: any) {
    this.id = id;
    const firstMds: any = _.first(Object.values(mdsInfo));
    this.grafanaId = firstMds && firstMds['name'];
    this.details = {
      standbys: '',
      pools: [],
      ranks: [],
      mdsCounters: {},
      name: ''
    };
    this.clients = {
      data: [],
      status: new TableStatusViewCache(ViewCacheStatus.ValueNone)
    };
    this.updateInterval();
  }

  private updateInterval() {
    this.unsubscribeInterval();
    this.subscribeInterval();
  }

  private unsubscribeInterval() {
    if (this.reloadSubscriber) {
      this.reloadSubscriber.unsubscribe();
    }
  }

  private subscribeInterval() {
    this.ngZone.runOutsideAngular(
      () =>
        (this.reloadSubscriber = timer(0, 5000).subscribe(() =>
          this.ngZone.run(() => this.refresh())
        ))
    );
  }

  refresh() {
    this.cephfsService.getTabs(this.id).subscribe(
      (data: any) => {
        this.data = data;
        this.softRefresh();
      },
      () => {
        this.clients.status = new TableStatusViewCache(ViewCacheStatus.ValueException);
      }
    );
  }

  softRefresh() {
    const data = _.cloneDeep(this.data); // Forces update of tab tables on tab switch
    // Clients tab
    this.clients = data.clients;
    this.clients.status = new TableStatusViewCache(this.clients.status);
    // Details tab
    this.details = {
      standbys: data.standbys,
      pools: data.pools,
      ranks: data.ranks,
      mdsCounters: data.mds_counters,
      name: data.name
    };
  }

  ngOnDestroy() {
    this.unsubscribeInterval();
  }
}