]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
6b406cfc1712f7bfff02a0d3d790feea10cf8902
[ceph-ci.git] /
1 import {
2   Component,
3   Input,
4   OnChanges,
5   OnDestroy,
6   OnInit,
7   SimpleChanges,
8   ViewChild
9 } from '@angular/core';
10 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
11 import { BehaviorSubject, Observable, Subscription, of, timer } from 'rxjs';
12 import { finalize, map, shareReplay, switchMap } from 'rxjs/operators';
13 import { CephfsSnapshotScheduleService } from '~/app/shared/api/cephfs-snapshot-schedule.service';
14 import { CdForm } from '~/app/shared/forms/cd-form';
15 import { CdTableAction } from '~/app/shared/models/cd-table-action';
16 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
17 import { CdTableFetchDataContext } from '~/app/shared/models/cd-table-fetch-data-context';
18 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
19 import { Permissions } from '~/app/shared/models/permissions';
20 import { SnapshotSchedule } from '~/app/shared/models/snapshot-schedule';
21 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
22 import { ModalService } from '~/app/shared/services/modal.service';
23 import { Icons } from '~/app/shared/enum/icons.enum';
24 import { CellTemplate } from '~/app/shared/enum/cell-template.enum';
25 import { MgrModuleService } from '~/app/shared/api/mgr-module.service';
26 import { NotificationService } from '~/app/shared/services/notification.service';
27 import { BlockUI, NgBlockUI } from 'ng-block-ui';
28 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
29 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
30 import { CephfsSnapshotscheduleFormComponent } from '../cephfs-snapshotschedule-form/cephfs-snapshotschedule-form.component';
31
32 @Component({
33   selector: 'cd-cephfs-snapshotschedule-list',
34   templateUrl: './cephfs-snapshotschedule-list.component.html',
35   styleUrls: ['./cephfs-snapshotschedule-list.component.scss']
36 })
37 export class CephfsSnapshotscheduleListComponent
38   extends CdForm
39   implements OnInit, OnChanges, OnDestroy {
40   @Input() fsName!: string;
41   @Input() id!: number;
42
43   @ViewChild('pathTpl', { static: true })
44   pathTpl: any;
45
46   @BlockUI()
47   blockUI: NgBlockUI;
48
49   snapshotSchedules$!: Observable<SnapshotSchedule[]>;
50   subject$ = new BehaviorSubject<SnapshotSchedule[]>([]);
51   snapScheduleModuleStatus$ = new BehaviorSubject<boolean>(false);
52   moduleServiceListSub!: Subscription;
53   columns: CdTableColumn[] = [];
54   tableActions: CdTableAction[] = [];
55   context!: CdTableFetchDataContext;
56   selection = new CdTableSelection();
57   permissions!: Permissions;
58   modalRef!: NgbModalRef;
59   errorMessage: string = '';
60   selectedName: string = '';
61   icons = Icons;
62
63   MODULE_NAME = 'snap_schedule';
64   ENABLE_MODULE_TIMER = 2 * 1000;
65
66   constructor(
67     private snapshotScheduleService: CephfsSnapshotScheduleService,
68     private authStorageService: AuthStorageService,
69     private modalService: ModalService,
70     private mgrModuleService: MgrModuleService,
71     private notificationService: NotificationService,
72     private actionLables: ActionLabelsI18n
73   ) {
74     super();
75     this.permissions = this.authStorageService.getPermissions();
76   }
77
78   ngOnChanges(changes: SimpleChanges): void {
79     if (changes.fsName) {
80       this.subject$.next([]);
81     }
82   }
83
84   ngOnInit(): void {
85     this.moduleServiceListSub = this.mgrModuleService
86       .list()
87       .pipe(
88         map((modules: any[]) => modules.find((module) => module?.['name'] === this.MODULE_NAME))
89       )
90       .subscribe({
91         next: (module: any) => this.snapScheduleModuleStatus$.next(module?.enabled)
92       });
93
94     this.snapshotSchedules$ = this.subject$.pipe(
95       switchMap(() =>
96         this.snapScheduleModuleStatus$.pipe(
97           switchMap((status) => {
98             if (!status) {
99               return of([]);
100             }
101             return this.snapshotScheduleService.getSnapshotScheduleList('/', this.fsName);
102           }),
103           shareReplay(1)
104         )
105       )
106     );
107
108     this.columns = [
109       { prop: 'path', name: $localize`Path`, flexGrow: 3, cellTemplate: this.pathTpl },
110       { prop: 'subvol', name: $localize`Subvolume` },
111       { prop: 'schedule', name: $localize`Repeat interval` },
112       { prop: 'retention', name: $localize`Retention policy` },
113       { prop: 'created_count', name: $localize`Created Count` },
114       { prop: 'pruned_count', name: $localize`Deleted Count` },
115       { prop: 'start', name: $localize`Start time`, cellTransformation: CellTemplate.timeAgo },
116       { prop: 'created', name: $localize`Created`, cellTransformation: CellTemplate.timeAgo }
117     ];
118
119     this.tableActions = [
120       {
121         name: this.actionLables.CREATE,
122         permission: 'create',
123         icon: Icons.add,
124         click: () => this.openModal(true)
125       }
126     ];
127   }
128
129   ngOnDestroy(): void {
130     this.moduleServiceListSub.unsubscribe();
131   }
132
133   fetchData() {
134     this.subject$.next([]);
135   }
136
137   updateSelection(selection: CdTableSelection) {
138     this.selection = selection;
139   }
140
141   openModal(edit = false) {
142     this.modalService.show(
143       CephfsSnapshotscheduleFormComponent,
144       {
145         fsName: this.fsName,
146         id: this.id,
147         path: this.selection?.first()?.path,
148         isEdit: edit
149       },
150       { size: 'lg' }
151     );
152   }
153
154   enableSnapshotSchedule() {
155     let $obs;
156     const fnWaitUntilReconnected = () => {
157       timer(this.ENABLE_MODULE_TIMER).subscribe(() => {
158         // Trigger an API request to check if the connection is
159         // re-established.
160         this.mgrModuleService.list().subscribe(
161           () => {
162             // Resume showing the notification toasties.
163             this.notificationService.suspendToasties(false);
164             // Unblock the whole UI.
165             this.blockUI.stop();
166             // Reload the data table content.
167             this.notificationService.show(
168               NotificationType.success,
169               $localize`Enabled Snapshot Schedule Module`
170             );
171             // Reload the data table content.
172           },
173           () => {
174             fnWaitUntilReconnected();
175           }
176         );
177       });
178     };
179
180     if (!this.snapScheduleModuleStatus$.value) {
181       $obs = this.mgrModuleService
182         .enable(this.MODULE_NAME)
183         .pipe(finalize(() => this.snapScheduleModuleStatus$.next(true)));
184     }
185     $obs.subscribe(
186       () => undefined,
187       () => {
188         // Suspend showing the notification toasties.
189         this.notificationService.suspendToasties(true);
190         // Block the whole UI to prevent user interactions until
191         // the connection to the backend is reestablished
192         this.blockUI.start($localize`Reconnecting, please wait ...`);
193         fnWaitUntilReconnected();
194       }
195     );
196   }
197 }