1- import { Component , Input , OnChanges , OnInit , SimpleChanges , ViewChild } from '@angular/core' ;
1+ import {
2+ Component ,
3+ Input ,
4+ OnChanges ,
5+ OnDestroy ,
6+ OnInit ,
7+ SimpleChanges ,
8+ ViewChild
9+ } from '@angular/core' ;
210import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap' ;
3- import { BehaviorSubject , Observable } from 'rxjs' ;
4- import { finalize , shareReplay , switchMap } from 'rxjs/operators' ;
11+ import { BehaviorSubject , Observable , Subscription , of , timer } from 'rxjs' ;
12+ import { finalize , map , shareReplay , switchMap } from 'rxjs/operators' ;
513import { CephfsSnapshotScheduleService } from '~/app/shared/api/cephfs-snapshot-schedule.service' ;
614import { CdForm } from '~/app/shared/forms/cd-form' ;
715import { CdTableAction } from '~/app/shared/models/cd-table-action' ;
@@ -14,21 +22,31 @@ import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
1422import { ModalService } from '~/app/shared/services/modal.service' ;
1523import { Icons } from '~/app/shared/enum/icons.enum' ;
1624import { 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' ;
1729
1830@Component ( {
1931 selector : 'cd-cephfs-snapshotschedule-list' ,
2032 templateUrl : './cephfs-snapshotschedule-list.component.html' ,
2133 styleUrls : [ './cephfs-snapshotschedule-list.component.scss' ]
2234} )
23- export class CephfsSnapshotscheduleListComponent extends CdForm implements OnInit , OnChanges {
35+ export class CephfsSnapshotscheduleListComponent
36+ extends CdForm
37+ implements OnInit , OnChanges , OnDestroy {
2438 @Input ( ) fsName ! : string ;
2539
2640 @ViewChild ( 'pathTpl' , { static : true } )
2741 pathTpl : any ;
2842
43+ @BlockUI ( )
44+ blockUI : NgBlockUI ;
45+
2946 snapshotSchedules$ ! : Observable < SnapshotSchedule [ ] > ;
3047 subject$ = new BehaviorSubject < SnapshotSchedule [ ] > ( [ ] ) ;
31- isLoading$ = new BehaviorSubject < boolean > ( true ) ;
48+ snapScheduleModuleStatus$ = new BehaviorSubject < boolean > ( false ) ;
49+ moduleServiceListSub ! : Subscription ;
3250 columns : CdTableColumn [ ] = [ ] ;
3351 tableActions : CdTableAction [ ] = [ ] ;
3452 context ! : CdTableFetchDataContext ;
@@ -39,10 +57,15 @@ export class CephfsSnapshotscheduleListComponent extends CdForm implements OnIni
3957 selectedName : string = '' ;
4058 icons = Icons ;
4159
60+ MODULE_NAME = 'snap_schedule' ;
61+ ENABLE_MODULE_TIMER = 2 * 1000 ;
62+
4263 constructor (
4364 private snapshotScheduleService : CephfsSnapshotScheduleService ,
4465 private authStorageService : AuthStorageService ,
45- private modalService : ModalService
66+ private modalService : ModalService ,
67+ private mgrModuleService : MgrModuleService ,
68+ private notificationService : NotificationService
4669 ) {
4770 super ( ) ;
4871 this . permissions = this . authStorageService . getPermissions ( ) ;
@@ -55,13 +78,27 @@ export class CephfsSnapshotscheduleListComponent extends CdForm implements OnIni
5578 }
5679
5780 ngOnInit ( ) : void {
81+ this . moduleServiceListSub = this . mgrModuleService
82+ . list ( )
83+ . pipe (
84+ map ( ( modules : any [ ] ) => modules . find ( ( module ) => module ?. [ 'name' ] === this . MODULE_NAME ) )
85+ )
86+ . subscribe ( {
87+ next : ( module : any ) => this . snapScheduleModuleStatus$ . next ( module ?. enabled )
88+ } ) ;
89+
5890 this . snapshotSchedules$ = this . subject$ . pipe (
5991 switchMap ( ( ) =>
60- this . snapshotScheduleService
61- . getSnapshotScheduleList ( '/' , this . fsName )
62- . pipe ( finalize ( ( ) => this . isLoading$ . next ( false ) ) )
63- ) ,
64- shareReplay ( 1 )
92+ this . snapScheduleModuleStatus$ . pipe (
93+ switchMap ( ( status ) => {
94+ if ( ! status ) {
95+ return of ( [ ] ) ;
96+ }
97+ return this . snapshotScheduleService . getSnapshotScheduleList ( '/' , this . fsName ) ;
98+ } ) ,
99+ shareReplay ( 1 )
100+ )
101+ )
65102 ) ;
66103
67104 this . columns = [
@@ -78,6 +115,10 @@ export class CephfsSnapshotscheduleListComponent extends CdForm implements OnIni
78115 this . tableActions = [ ] ;
79116 }
80117
118+ ngOnDestroy ( ) : void {
119+ this . moduleServiceListSub . unsubscribe ( ) ;
120+ }
121+
81122 fetchData ( ) {
82123 this . subject$ . next ( [ ] ) ;
83124 }
@@ -96,4 +137,48 @@ export class CephfsSnapshotscheduleListComponent extends CdForm implements OnIni
96137 { size : 'lg' }
97138 ) ;
98139 }
140+
141+ enableSnapshotSchedule ( ) {
142+ let $obs ;
143+ const fnWaitUntilReconnected = ( ) => {
144+ timer ( this . ENABLE_MODULE_TIMER ) . subscribe ( ( ) => {
145+ // Trigger an API request to check if the connection is
146+ // re-established.
147+ this . mgrModuleService . list ( ) . subscribe (
148+ ( ) => {
149+ // Resume showing the notification toasties.
150+ this . notificationService . suspendToasties ( false ) ;
151+ // Unblock the whole UI.
152+ this . blockUI . stop ( ) ;
153+ // Reload the data table content.
154+ this . notificationService . show (
155+ NotificationType . success ,
156+ $localize `Enabled Snapshot Schedule Module`
157+ ) ;
158+ // Reload the data table content.
159+ } ,
160+ ( ) => {
161+ fnWaitUntilReconnected ( ) ;
162+ }
163+ ) ;
164+ } ) ;
165+ } ;
166+
167+ if ( ! this . snapScheduleModuleStatus$ . value ) {
168+ $obs = this . mgrModuleService
169+ . enable ( this . MODULE_NAME )
170+ . pipe ( finalize ( ( ) => this . snapScheduleModuleStatus$ . next ( true ) ) ) ;
171+ }
172+ $obs . subscribe (
173+ ( ) => undefined ,
174+ ( ) => {
175+ // Suspend showing the notification toasties.
176+ this . notificationService . suspendToasties ( true ) ;
177+ // Block the whole UI to prevent user interactions until
178+ // the connection to the backend is reestablished
179+ this . blockUI . start ( $localize `Reconnecting, please wait ...` ) ;
180+ fnWaitUntilReconnected ( ) ;
181+ }
182+ ) ;
183+ }
99184}
0 commit comments