From 3cace870318f1b216348cae68a9ee81489adf79b Mon Sep 17 00:00:00 2001 From: Tiago Melo Date: Thu, 14 May 2020 16:01:57 +0000 Subject: [PATCH] mgr/dashboard: Improve RbdMirroringService's subscribe methods Signed-off-by: Tiago Melo (cherry picked from commit b599a5db7b5c162e6e1c4a68df5efea725b6d381) Modified nfs-list.component.spec.ts:134 to be able to pass the unit tests. --- .../bootstrap-create-modal.component.ts | 6 +---- .../bootstrap-import-modal.component.ts | 6 +---- .../daemon-list/daemon-list.component.ts | 5 +---- .../image-list/image-list.component.ts | 5 +---- .../mirroring/overview/overview.component.ts | 5 +---- .../pool-edit-mode-modal.component.ts | 6 +---- .../pool-list/pool-list.component.ts | 5 +---- .../nfs/nfs-list/nfs-list.component.spec.ts | 2 +- .../shared/api/rbd-mirroring.service.spec.ts | 16 ++------------ .../app/shared/api/rbd-mirroring.service.ts | 22 +++++++++---------- .../app/shared/models/mirroring-summary.ts | 4 ++++ 11 files changed, 24 insertions(+), 58 deletions(-) create mode 100644 src/pybind/mgr/dashboard/frontend/src/app/shared/models/mirroring-summary.ts diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/bootstrap-create-modal/bootstrap-create-modal.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/bootstrap-create-modal/bootstrap-create-modal.component.ts index b2130f8b515..63ccdc71175 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/bootstrap-create-modal/bootstrap-create-modal.component.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/bootstrap-create-modal/bootstrap-create-modal.component.ts @@ -55,11 +55,7 @@ export class BootstrapCreateModalComponent implements OnDestroy, OnInit { this.createBootstrapForm.get('siteName').setValue(response.site_name); }); - this.subs = this.rbdMirroringService.subscribeSummary((data: any) => { - if (!data) { - return; - } - + this.subs = this.rbdMirroringService.subscribeSummary((data) => { const pools = data.content_data.pools; this.pools = pools.reduce((acc: any[], pool: Pool) => { acc.push({ diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/bootstrap-import-modal/bootstrap-import-modal.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/bootstrap-import-modal/bootstrap-import-modal.component.ts index 0e7d56d4087..ca6c8336312 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/bootstrap-import-modal/bootstrap-import-modal.component.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/bootstrap-import-modal/bootstrap-import-modal.component.ts @@ -62,11 +62,7 @@ export class BootstrapImportModalComponent implements OnInit, OnDestroy { this.importBootstrapForm.get('siteName').setValue(response.site_name); }); - this.subs = this.rbdMirroringService.subscribeSummary((data: any) => { - if (!data) { - return; - } - + this.subs = this.rbdMirroringService.subscribeSummary((data) => { const pools = data.content_data.pools; this.pools = pools.reduce((acc: any[], pool: Pool) => { acc.push({ diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/daemon-list/daemon-list.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/daemon-list/daemon-list.component.ts index 62826028d75..7eaae7aa024 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/daemon-list/daemon-list.component.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/daemon-list/daemon-list.component.ts @@ -45,10 +45,7 @@ export class DaemonListComponent implements OnInit, OnDestroy { } ]; - this.subs = this.rbdMirroringService.subscribeSummary((data: any) => { - if (!data) { - return; - } + this.subs = this.rbdMirroringService.subscribeSummary((data) => { this.data = data.content_data.daemons; }); } diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/image-list/image-list.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/image-list/image-list.component.ts index 5acdd1ea29d..3867da7db92 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/image-list/image-list.component.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/image-list/image-list.component.ts @@ -77,10 +77,7 @@ export class ImageListComponent implements OnInit, OnDestroy { } ]; - this.subs = this.rbdMirroringService.subscribeSummary((data: any) => { - if (!data) { - return; - } + this.subs = this.rbdMirroringService.subscribeSummary((data) => { this.image_error.data = data.content_data.image_error; this.image_syncing.data = data.content_data.image_syncing; this.image_ready.data = data.content_data.image_ready; diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/overview/overview.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/overview/overview.component.ts index 682ed68c8bc..a73db2f62fa 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/overview/overview.component.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/overview/overview.component.ts @@ -67,10 +67,7 @@ export class OverviewComponent implements OnInit, OnDestroy { ngOnInit() { this.subs.add(this.rbdMirroringService.startPolling()); this.subs.add( - this.rbdMirroringService.subscribeSummary((data: any) => { - if (!data) { - return; - } + this.rbdMirroringService.subscribeSummary((data) => { this.status = data.content_data.status; this.siteName = data.site_name; diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/pool-edit-mode-modal/pool-edit-mode-modal.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/pool-edit-mode-modal/pool-edit-mode-modal.component.ts index 3a393170b87..64054aabb08 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/pool-edit-mode-modal/pool-edit-mode-modal.component.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/pool-edit-mode-modal/pool-edit-mode-modal.component.ts @@ -59,12 +59,8 @@ export class PoolEditModeModalComponent implements OnInit, OnDestroy { this.setResponse(resp); }); - this.subs = this.rbdMirroringService.subscribeSummary((data: any) => { + this.subs = this.rbdMirroringService.subscribeSummary((data) => { this.peerExists = false; - if (!data) { - return; - } - const poolData = data.content_data.pools; const pool = poolData.find((o: any) => this.poolName === o['name']); this.peerExists = pool && pool['peer_uuids'].length; diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/pool-list/pool-list.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/pool-list/pool-list.component.ts index efa0beba8c7..2cb0bfd46a6 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/pool-list/pool-list.component.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/pool-list/pool-list.component.ts @@ -94,10 +94,7 @@ export class PoolListComponent implements OnInit, OnDestroy { } ]; - this.subs = this.rbdMirroringService.subscribeSummary((data: any) => { - if (!data) { - return; - } + this.subs = this.rbdMirroringService.subscribeSummary((data) => { this.data = data.content_data.pools; }); } diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/nfs/nfs-list/nfs-list.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/nfs/nfs-list/nfs-list.component.spec.ts index 8ad7e0d4e24..a62f28a19f5 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/nfs/nfs-list/nfs-list.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/nfs/nfs-list/nfs-list.component.spec.ts @@ -131,7 +131,7 @@ describe('NfsListComponent', () => { addExport('b'); addExport('c'); component.exports = exports; - refresh(new Summary()); + refresh({ executing_tasks: [] }); spyOn(nfsService, 'list').and.callFake(() => of(exports)); fixture.detectChanges(); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd-mirroring.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd-mirroring.service.spec.ts index 37be86346dd..34eb6282526 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd-mirroring.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd-mirroring.service.spec.ts @@ -62,7 +62,7 @@ describe('RbdMirroringService', () => { const subs = service.startPolling(); tick(); const calledWith: any[] = []; - service.subscribeSummary((data: any) => { + service.subscribeSummary((data) => { calledWith.push(data); }); tick(service.REFRESH_INTERVAL * 2); @@ -70,23 +70,11 @@ describe('RbdMirroringService', () => { expect(calls.length).toEqual(3); calls.forEach((call: TestRequest) => flushCalls(call)); - expect(calledWith).toEqual([null, summary]); + expect(calledWith).toEqual([summary]); subs.unsubscribe(); })); - it('should get current summary', () => { - service.refresh(); - const calledWith: any[] = []; - service.subscribeSummary((data: any) => { - calledWith.push(data); - }); - const calls = getMirroringSummaryCalls(); - calls.forEach((call: TestRequest) => flushCalls(call)); - - expect(service.getCurrentSummary()).toEqual(summary); - }); - it('should get pool config', () => { service.getPool('poolName').subscribe(); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd-mirroring.service.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd-mirroring.service.ts index c1f069323be..6a378b35997 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd-mirroring.service.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd-mirroring.service.ts @@ -2,8 +2,10 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { BehaviorSubject, Observable, Subscription } from 'rxjs'; +import { filter } from 'rxjs/operators'; import { cdEncode, cdEncodeNot } from '../decorators/cd-encode'; +import { MirroringSummary } from '../models/mirroring-summary'; import { TimerService } from '../services/timer.service'; import { ApiModule } from './api.module'; @@ -14,7 +16,7 @@ import { ApiModule } from './api.module'; export class RbdMirroringService { readonly REFRESH_INTERVAL = 30000; // Observable sources - private summaryDataSource = new BehaviorSubject(null); + private summaryDataSource = new BehaviorSubject(null); // Observable streams summaryData$ = this.summaryDataSource.asObservable(); @@ -30,29 +32,25 @@ export class RbdMirroringService { return this.retrieveSummaryObservable().subscribe(this.retrieveSummaryObserver()); } - private retrieveSummaryObservable(): Observable { + private retrieveSummaryObservable(): Observable { return this.http.get('api/block/mirroring/summary'); } - private retrieveSummaryObserver(): (data: any) => void { + private retrieveSummaryObserver(): (data: MirroringSummary) => void { return (data: any) => { this.summaryDataSource.next(data); }; } - /** - * Returns the current value of summaryData - */ - getCurrentSummary(): { [key: string]: any; executing_tasks: object[] } { - return this.summaryDataSource.getValue(); - } - /** * Subscribes to the summaryData, * which is updated periodically or when a new task is created. */ - subscribeSummary(next: (summary: any) => void, error?: (error: any) => void): Subscription { - return this.summaryData$.subscribe(next, error); + subscribeSummary( + next: (summary: MirroringSummary) => void, + error?: (error: any) => void + ): Subscription { + return this.summaryData$.pipe(filter((value) => !!value)).subscribe(next, error); } getPool(poolName: string) { diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/models/mirroring-summary.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/models/mirroring-summary.ts new file mode 100644 index 00000000000..95327ed779a --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/models/mirroring-summary.ts @@ -0,0 +1,4 @@ +export interface MirroringSummary { + content_data?: any; + site_name?: any; +} -- 2.47.3