]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/dashboard: Fix summary refresh call stack
authorTiago Melo <tmelo@suse.com>
Fri, 11 Jan 2019 12:08:01 +0000 (11:08 -0100)
committerTiago Melo <tmelo@suse.com>
Wed, 23 Jan 2019 14:31:19 +0000 (14:31 +0000)
Currently each time we created a task it would call the summary refresh method.
Since that method was a recursive method, overtime we would get N calls to the
refresh each 5 seconds.

Signed-off-by: Tiago Melo <tmelo@suse.com>
src/pybind/mgr/dashboard/frontend/src/app/shared/services/summary.service.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/services/summary.service.ts

index 86ea4e108976d0b5eac9a9092c3054ded6222b62..d4439416d987302bf0d05838dc12c9a01a49e682 100644 (file)
@@ -47,6 +47,7 @@ describe('SummaryService', () => {
   });
 
   it('should call refresh', fakeAsync(() => {
+    summaryService.enablePolling();
     authStorageService.set('foobar', undefined, undefined);
     const calledWith = [];
     summaryService.subscribe((data) => {
@@ -57,10 +58,9 @@ describe('SummaryService', () => {
     expect(calledWith).toEqual([summary, summary]);
     tick(10000);
     expect(calledWith.length).toEqual(4);
-    // In order to not trigger setTimeout again,
+    // In order to not trigger setInterval again,
     // which would raise 'Error: 1 timer(s) still in the queue.'
-    spyOn(summaryService, 'refresh').and.callFake(() => true);
-    tick(5000);
+    window.clearInterval(summaryService.polling);
   }));
 
   describe('Should test methods after first refresh', () => {
index a4397f85cba184499f84e42ec8379f42d1b60038..0f81659c7c7b4bee3d07039f18c72937eb991014 100644 (file)
@@ -18,19 +18,17 @@ export class SummaryService {
   // Observable streams
   summaryData$ = this.summaryDataSource.asObservable();
 
+  polling: number;
+
   constructor(private http: HttpClient, private router: Router, private ngZone: NgZone) {
-    this.refresh();
+    this.enablePolling();
   }
 
-  refresh() {
-    if (this.router.url !== '/login') {
-      this.http.get('api/summary').subscribe((data) => {
-        this.summaryDataSource.next(data);
-      });
-    }
+  enablePolling() {
+    this.refresh();
 
     this.ngZone.runOutsideAngular(() => {
-      setTimeout(() => {
+      this.polling = window.setInterval(() => {
         this.ngZone.run(() => {
           this.refresh();
         });
@@ -38,6 +36,14 @@ export class SummaryService {
     });
   }
 
+  refresh() {
+    if (this.router.url !== '/login') {
+      this.http.get('api/summary').subscribe((data) => {
+        this.summaryDataSource.next(data);
+      });
+    }
+  }
+
   /**
    * Returns the current value of summaryData
    */