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, URLVerbs } from '~/app/shared/constants/app.constants';
30 import { CephfsSnapshotscheduleFormComponent } from '../cephfs-snapshotschedule-form/cephfs-snapshotschedule-form.component';
31 import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
32 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
33 import { FinishedTask } from '~/app/shared/models/finished-task';
36 selector: 'cd-cephfs-snapshotschedule-list',
37 templateUrl: './cephfs-snapshotschedule-list.component.html',
38 styleUrls: ['./cephfs-snapshotschedule-list.component.scss']
40 export class CephfsSnapshotscheduleListComponent
42 implements OnInit, OnChanges, OnDestroy {
43 @Input() fsName!: string;
46 @ViewChild('pathTpl', { static: true })
49 @ViewChild('retentionTpl', { static: true })
52 @ViewChild('subvolTpl', { static: true })
58 snapshotSchedules$!: Observable<SnapshotSchedule[]>;
59 subject$ = new BehaviorSubject<SnapshotSchedule[]>([]);
60 snapScheduleModuleStatus$ = new BehaviorSubject<boolean>(false);
61 moduleServiceListSub!: Subscription;
62 columns: CdTableColumn[] = [];
63 tableActions$ = new BehaviorSubject<CdTableAction[]>([]);
64 context!: CdTableFetchDataContext;
65 selection = new CdTableSelection();
66 permissions!: Permissions;
67 modalRef!: NgbModalRef;
68 errorMessage: string = '';
69 selectedName: string = '';
71 tableActions: CdTableAction[] = [
73 name: this.actionLabels.CREATE,
76 click: () => this.openModal(false)
79 name: this.actionLabels.EDIT,
82 click: () => this.openModal(true)
85 name: this.actionLabels.DELETE,
88 click: () => this.deleteSnapshotSchedule()
92 MODULE_NAME = 'snap_schedule';
93 ENABLE_MODULE_TIMER = 2 * 1000;
96 private snapshotScheduleService: CephfsSnapshotScheduleService,
97 private authStorageService: AuthStorageService,
98 private modalService: ModalService,
99 private mgrModuleService: MgrModuleService,
100 private notificationService: NotificationService,
101 private actionLabels: ActionLabelsI18n,
102 private taskWrapper: TaskWrapperService
105 this.permissions = this.authStorageService.getPermissions();
108 ngOnChanges(changes: SimpleChanges): void {
109 if (changes.fsName) {
110 this.subject$.next([]);
115 this.moduleServiceListSub = this.mgrModuleService
118 map((modules: any[]) => modules.find((module) => module?.['name'] === this.MODULE_NAME))
121 next: (module: any) => this.snapScheduleModuleStatus$.next(module?.enabled)
124 this.snapshotSchedules$ = this.subject$.pipe(
126 this.snapScheduleModuleStatus$.pipe(
127 switchMap((status) => {
131 return this.snapshotScheduleService
132 .getSnapshotScheduleList('/', this.fsName)
135 list.map((l) => ({ ...l, pathForSelection: `${l.path}@${l.schedule}` }))
145 { prop: 'pathForSelection', name: $localize`Path`, flexGrow: 3, cellTemplate: this.pathTpl },
146 { prop: 'path', isHidden: true, isInvisible: true },
147 { prop: 'subvol', name: $localize`Subvolume`, cellTemplate: this.subvolTpl },
148 { prop: 'scheduleCopy', name: $localize`Repeat interval` },
149 { prop: 'schedule', isHidden: true, isInvisible: true },
150 { prop: 'retentionCopy', name: $localize`Retention policy`, cellTemplate: this.retentionTpl },
151 { prop: 'retention', isHidden: true, isInvisible: true },
152 { prop: 'created_count', name: $localize`Created Count` },
153 { prop: 'pruned_count', name: $localize`Deleted Count` },
154 { prop: 'start', name: $localize`Start time`, cellTransformation: CellTemplate.timeAgo },
155 { prop: 'created', name: $localize`Created`, cellTransformation: CellTemplate.timeAgo }
158 this.tableActions$.next(this.tableActions);
161 ngOnDestroy(): void {
162 this.moduleServiceListSub.unsubscribe();
166 this.subject$.next([]);
169 updateSelection(selection: CdTableSelection) {
170 this.selection = selection;
171 if (!this.selection.hasSelection) return;
172 const isActive = this.selection.first()?.active;
174 this.tableActions$.next([
175 ...this.tableActions,
177 name: isActive ? this.actionLabels.DEACTIVATE : this.actionLabels.ACTIVATE,
178 permission: 'update',
179 icon: isActive ? Icons.warning : Icons.success,
181 isActive ? this.deactivateSnapshotSchedule() : this.activateSnapshotSchedule()
186 openModal(edit = false) {
187 this.modalService.show(
188 CephfsSnapshotscheduleFormComponent,
192 path: this.selection?.first()?.path,
193 schedule: this.selection?.first()?.schedule,
194 retention: this.selection?.first()?.retention,
195 start: this.selection?.first()?.start,
196 status: this.selection?.first()?.status,
203 enableSnapshotSchedule() {
205 const fnWaitUntilReconnected = () => {
206 timer(this.ENABLE_MODULE_TIMER).subscribe(() => {
207 // Trigger an API request to check if the connection is
209 this.mgrModuleService.list().subscribe(
211 // Resume showing the notification toasties.
212 this.notificationService.suspendToasties(false);
213 // Unblock the whole UI.
215 // Reload the data table content.
216 this.notificationService.show(
217 NotificationType.success,
218 $localize`Enabled Snapshot Schedule Module`
220 // Reload the data table content.
223 fnWaitUntilReconnected();
229 if (!this.snapScheduleModuleStatus$.value) {
230 $obs = this.mgrModuleService
231 .enable(this.MODULE_NAME)
232 .pipe(finalize(() => this.snapScheduleModuleStatus$.next(true)));
237 // Suspend showing the notification toasties.
238 this.notificationService.suspendToasties(true);
239 // Block the whole UI to prevent user interactions until
240 // the connection to the backend is reestablished
241 this.blockUI.start($localize`Reconnecting, please wait ...`);
242 fnWaitUntilReconnected();
247 deactivateSnapshotSchedule() {
248 const { path, start, fs, schedule, subvol, group } = this.selection.first();
250 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
251 itemDescription: $localize`snapshot schedule`,
252 actionDescription: this.actionLabels.DEACTIVATE,
253 submitActionObservable: () =>
254 this.taskWrapper.wrapTaskAroundCall({
255 task: new FinishedTask('cephfs/snapshot/schedule/deactivate', {
258 call: this.snapshotScheduleService.deactivate({
270 activateSnapshotSchedule() {
271 const { path, start, fs, schedule, subvol, group } = this.selection.first();
273 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
274 itemDescription: $localize`snapshot schedule`,
275 actionDescription: this.actionLabels.ACTIVATE,
276 submitActionObservable: () =>
277 this.taskWrapper.wrapTaskAroundCall({
278 task: new FinishedTask('cephfs/snapshot/schedule/activate', {
281 call: this.snapshotScheduleService.activate({
293 deleteSnapshotSchedule() {
294 const { path, start, fs, schedule, subvol, group, retention } = this.selection.first();
295 const retentionPolicy = retention
297 ?.filter((r: string) => !!r)
298 ?.map((r: string) => {
299 const frequency = r.substring(r.length - 1);
300 const interval = r.substring(0, r.length - 1);
301 return `${interval}-${frequency}`;
304 ?.toLocaleLowerCase();
306 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
307 itemDescription: $localize`snapshot schedule`,
308 submitActionObservable: () =>
309 this.taskWrapper.wrapTaskAroundCall({
310 task: new FinishedTask('cephfs/snapshot/schedule/' + URLVerbs.DELETE, {
313 call: this.snapshotScheduleService.delete({