From: Tiago Melo Date: Thu, 14 May 2020 16:01:57 +0000 (+0000) Subject: mgr/dashboard: Improve RbdMirroringService's subscribe methods X-Git-Tag: v16.1.0~2160^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=b599a5db7b5c162e6e1c4a68df5efea725b6d381;p=ceph.git mgr/dashboard: Improve RbdMirroringService's subscribe methods Signed-off-by: Tiago Melo --- 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 b2130f8b5157..63ccdc71175c 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 0e7d56d40873..ca6c83363120 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 62826028d75d..7eaae7aa0243 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 5acdd1ea29d2..3867da7db92a 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 682ed68c8bc3..a73db2f62faf 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 3a393170b87f..64054aabb083 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 efa0beba8c75..2cb0bfd46a67 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/shared/api/rbd-mirroring.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd-mirroring.service.spec.ts index 5aacb19d9cf7..2efa5a799851 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 @@ -59,7 +59,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); @@ -67,23 +67,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 c1f069323be2..6a378b359971 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 000000000000..95327ed779af --- /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; +}