]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
251314c3e86a80c918994a49aaa8edee37afc264
[ceph.git] /
1 import { Component, Input, OnChanges, OnInit, SimpleChanges } from '@angular/core';
2 import { BehaviorSubject, Observable, forkJoin, of } from 'rxjs';
3 import { catchError, shareReplay, switchMap, tap } from 'rxjs/operators';
4 import { CephfsSubvolumeGroupService } from '~/app/shared/api/cephfs-subvolume-group.service';
5 import { CephfsSubvolumeService } from '~/app/shared/api/cephfs-subvolume.service';
6 import { CellTemplate } from '~/app/shared/enum/cell-template.enum';
7 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
8 import { CdTableFetchDataContext } from '~/app/shared/models/cd-table-fetch-data-context';
9 import { CephfsSubvolume, SubvolumeSnapshot } from '~/app/shared/models/cephfs-subvolume.model';
10
11 @Component({
12   selector: 'cd-cephfs-subvolume-snapshots-list',
13   templateUrl: './cephfs-subvolume-snapshots-list.component.html',
14   styleUrls: ['./cephfs-subvolume-snapshots-list.component.scss']
15 })
16 export class CephfsSubvolumeSnapshotsListComponent implements OnInit, OnChanges {
17   @Input() fsName: string;
18
19   context: CdTableFetchDataContext;
20   columns: CdTableColumn[] = [];
21
22   subVolumes$: Observable<CephfsSubvolume[]>;
23   snapshots$: Observable<any[]>;
24   snapshotSubject = new BehaviorSubject<SubvolumeSnapshot[]>([]);
25   subVolumeSubject = new BehaviorSubject<CephfsSubvolume[]>([]);
26
27   subvolumeGroupList: string[] = [];
28   subVolumesList: string[];
29
30   activeGroupName = '';
31   activeSubVolumeName = '';
32
33   isSubVolumesAvailable = false;
34   isLoading = true;
35
36   observables: any = [];
37
38   constructor(
39     private cephfsSubvolumeGroupService: CephfsSubvolumeGroupService,
40     private cephfsSubvolumeService: CephfsSubvolumeService
41   ) {}
42
43   ngOnInit(): void {
44     this.columns = [
45       {
46         name: $localize`Name`,
47         prop: 'name',
48         flexGrow: 1
49       },
50       {
51         name: $localize`Created`,
52         prop: 'info.created_at',
53         flexGrow: 1,
54         cellTransformation: CellTemplate.timeAgo
55       },
56       {
57         name: $localize`Pending Clones`,
58         prop: 'info.has_pending_clones',
59         flexGrow: 0.5,
60         cellTransformation: CellTemplate.badge,
61         customTemplateConfig: {
62           map: {
63             no: { class: 'badge-success' },
64             yes: { class: 'badge-info' }
65           }
66         }
67       }
68     ];
69
70     this.cephfsSubvolumeGroupService
71       .get(this.fsName)
72       .pipe(
73         switchMap((groups) => {
74           // manually adding the group '_nogroup' to the list.
75           groups.unshift({ name: '' });
76
77           const observables = groups.map((group) =>
78             this.cephfsSubvolumeService.existsInFs(this.fsName, group.name).pipe(
79               switchMap((resp) => {
80                 if (resp) {
81                   this.subvolumeGroupList.push(group.name);
82                 }
83                 return of(resp); // Emit the response
84               })
85             )
86           );
87
88           return forkJoin(observables);
89         })
90       )
91       .subscribe(() => {
92         if (this.subvolumeGroupList.length) {
93           this.isSubVolumesAvailable = true;
94         }
95         this.isLoading = false;
96       });
97   }
98
99   ngOnChanges(changes: SimpleChanges): void {
100     if (changes.fsName) {
101       this.subVolumeSubject.next([]);
102     }
103   }
104
105   selectSubVolumeGroup(subVolumeGroupName: string) {
106     this.activeGroupName = subVolumeGroupName;
107     this.getSubVolumes();
108   }
109
110   selectSubVolume(subVolumeName: string) {
111     this.activeSubVolumeName = subVolumeName;
112     this.getSubVolumesSnapshot();
113   }
114
115   getSubVolumes() {
116     this.subVolumes$ = this.subVolumeSubject.pipe(
117       switchMap(() =>
118         this.cephfsSubvolumeService.get(this.fsName, this.activeGroupName, false).pipe(
119           tap((resp) => {
120             this.subVolumesList = resp.map((subVolume) => subVolume.name);
121             this.activeSubVolumeName = resp[0].name;
122             this.getSubVolumesSnapshot();
123           })
124         )
125       )
126     );
127   }
128
129   getSubVolumesSnapshot() {
130     this.snapshots$ = this.snapshotSubject.pipe(
131       switchMap(() =>
132         this.cephfsSubvolumeService
133           .getSnapshots(this.fsName, this.activeSubVolumeName, this.activeGroupName)
134           .pipe(
135             catchError(() => {
136               this.context.error();
137               return of(null);
138             })
139           )
140       ),
141       shareReplay(1)
142     );
143   }
144
145   fetchData() {
146     this.snapshotSubject.next([]);
147   }
148 }