]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/dashboard: call API only once to get config options infos
authorTatjana Dehler <tdehler@suse.com>
Fri, 7 Dec 2018 14:26:32 +0000 (15:26 +0100)
committerTatjana Dehler <tdehler@suse.com>
Wed, 23 Jan 2019 10:24:55 +0000 (11:24 +0100)
Refactor the component in order to call API only once to get the infos
about the relevant config options and (if set) the current values.

Signed-off-by: Tatjana Dehler <tdehler@suse.com>
src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/osd/osd-recv-speed-modal/osd-recv-speed-modal.component.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/osd/osd-recv-speed-modal/osd-recv-speed-modal.component.ts

index 3336ed8e5c0deb07d0bf35ce9a8c290550fed2f1..f67b084e0d522c8227ae9cce80cd03dd7751b07e 100755 (executable)
@@ -1,21 +1,18 @@
 import { HttpClientTestingModule } from '@angular/common/http/testing';
-import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
+import { ComponentFixture, TestBed } from '@angular/core/testing';
 import { ReactiveFormsModule } from '@angular/forms';
 
 import * as _ from 'lodash';
 import { ToastModule } from 'ng2-toastr';
 import { BsModalRef, ModalModule } from 'ngx-bootstrap/modal';
-import { of } from 'rxjs';
 
 import { configureTestBed, i18nProviders } from '../../../../../testing/unit-test-helper';
-import { ConfigurationService } from '../../../../shared/api/configuration.service';
 import { SharedModule } from '../../../../shared/shared.module';
 import { OsdRecvSpeedModalComponent } from './osd-recv-speed-modal.component';
 
 describe('OsdRecvSpeedModalComponent', () => {
   let component: OsdRecvSpeedModalComponent;
   let fixture: ComponentFixture<OsdRecvSpeedModalComponent>;
-  let configService: ConfigurationService;
 
   configureTestBed({
     imports: [
@@ -32,7 +29,6 @@ describe('OsdRecvSpeedModalComponent', () => {
   beforeEach(() => {
     fixture = TestBed.createComponent(OsdRecvSpeedModalComponent);
     component = fixture.componentInstance;
-    configService = TestBed.get(ConfigurationService);
     fixture.detectChanges();
   });
 
@@ -95,270 +91,105 @@ describe('OsdRecvSpeedModalComponent', () => {
   });
 
   describe('getStoredPriority', () => {
-    const configOptionsLow = [
-      {
-        name: 'osd_max_backfills',
-        value: [
-          {
-            section: 'osd',
-            value: '1'
-          }
-        ]
-      },
-      {
-        name: 'osd_recovery_max_active',
-        value: [
-          {
-            section: 'osd',
-            value: '1'
-          }
-        ]
-      },
-      {
-        name: 'osd_recovery_max_single_start',
-        value: [
-          {
-            section: 'osd',
-            value: '1'
-          }
-        ]
-      },
-      {
-        name: 'osd_recovery_sleep',
-        value: [
-          {
-            section: 'osd',
-            value: '0.5'
-          }
-        ]
-      }
-    ];
-
-    const configOptionsDefault = [
-      {
-        name: 'osd_max_backfills',
-        value: [
-          {
-            section: 'osd',
-            value: '1'
-          }
-        ]
-      },
-      {
-        name: 'osd_recovery_max_active',
-        value: [
-          {
-            section: 'osd',
-            value: '3'
-          }
-        ]
-      },
-      {
-        name: 'osd_recovery_max_single_start',
-        value: [
-          {
-            section: 'osd',
-            value: '1'
-          }
-        ]
-      },
-      {
-        name: 'osd_recovery_sleep',
-        value: [
-          {
-            section: 'osd',
-            value: '0'
-          }
-        ]
-      }
-    ];
-
-    const configOptionsHigh = [
-      {
-        name: 'osd_max_backfills',
-        value: [
-          {
-            section: 'osd',
-            value: '4'
-          }
-        ]
-      },
-      {
-        name: 'osd_recovery_max_active',
-        value: [
-          {
-            section: 'osd',
-            value: '4'
-          }
-        ]
-      },
-      {
-        name: 'osd_recovery_max_single_start',
-        value: [
-          {
-            section: 'osd',
-            value: '4'
-          }
-        ]
-      },
-      {
-        name: 'osd_recovery_sleep',
-        value: [
-          {
-            section: 'osd',
-            value: '0'
-          }
-        ]
-      }
-    ];
-
-    const configOptionsCustom = [
-      {
-        name: 'osd_max_backfills',
-        value: [
-          {
-            section: 'osd',
-            value: '1'
-          }
-        ]
-      },
-      {
-        name: 'osd_recovery_max_active',
-        value: [
-          {
-            section: 'osd',
-            value: '2'
-          }
-        ]
-      },
-      {
-        name: 'osd_recovery_max_single_start',
-        value: [
-          {
-            section: 'osd',
-            value: '1'
-          }
-        ]
-      },
-      {
-        name: 'osd_recovery_sleep',
-        value: [
-          {
-            section: 'osd',
-            value: '0'
-          }
-        ]
-      }
-    ];
-
-    const configOptionsIncomplete = [
-      {
-        name: 'osd_max_backfills',
-        value: [
-          {
-            section: 'osd',
-            value: '1'
-          }
-        ]
-      },
-      {
-        name: 'osd_recovery_max_single_start',
-        value: [
-          {
-            section: 'osd',
-            value: '1'
-          }
-        ]
-      },
-      {
-        name: 'osd_recovery_sleep',
-        value: [
-          {
-            section: 'osd',
-            value: '0'
-          }
-        ]
-      }
-    ];
-
-    it('should return priority "low" if the config option values have been set accordingly', fakeAsync(() => {
-      spyOn(configService, 'get').and.callFake((configOptionName: string) => {
-        const result = _.find(configOptionsLow, (configOption) => {
-          return configOption.name === configOptionName;
-        });
-        return of(result);
-      });
-
-      component.getStoredPriority((priority) => {
+    const configOptionsLow = {
+      osd_max_backfills: 1,
+      osd_recovery_max_active: 1,
+      osd_recovery_max_single_start: 1,
+      osd_recovery_sleep: 0.5
+    };
+
+    const configOptionsDefault = {
+      osd_max_backfills: 1,
+      osd_recovery_max_active: 3,
+      osd_recovery_max_single_start: 1,
+      osd_recovery_sleep: 0
+    };
+
+    const configOptionsHigh = {
+      osd_max_backfills: 4,
+      osd_recovery_max_active: 4,
+      osd_recovery_max_single_start: 4,
+      osd_recovery_sleep: 0
+    };
+
+    const configOptionsCustom = {
+      osd_max_backfills: 1,
+      osd_recovery_max_active: 2,
+      osd_recovery_max_single_start: 1,
+      osd_recovery_sleep: 0
+    };
+
+    const configOptionsIncomplete = {
+      osd_max_backfills: 1,
+      osd_recovery_max_single_start: 1,
+      osd_recovery_sleep: 0
+    };
+
+    it('should return priority "low" if the config option values have been set accordingly', () => {
+      component.getStoredPriority(configOptionsLow, (priority) => {
         expect(priority.name).toBe('low');
       });
-      tick();
-
       expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeFalsy();
-    }));
-
-    it('should return priority "default" if the config option values have been set accordingly', fakeAsync(() => {
-      spyOn(configService, 'get').and.callFake((configOptionName: string) => {
-        const result = _.find(configOptionsDefault, (configOption) => {
-          return configOption.name === configOptionName;
-        });
-        return of(result);
-      });
+    });
 
-      component.getStoredPriority((priority) => {
+    it('should return priority "default" if the config option values have been set accordingly', () => {
+      component.getStoredPriority(configOptionsDefault, (priority) => {
         expect(priority.name).toBe('default');
       });
-      tick();
-
       expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeFalsy();
-    }));
-
-    it('should return priority "high" if the config option values have been set accordingly', fakeAsync(() => {
-      spyOn(configService, 'get').and.callFake((configOptionName: string) => {
-        const result = _.find(configOptionsHigh, (configOption) => {
-          return configOption.name === configOptionName;
-        });
-        return of(result);
-      });
+    });
 
-      component.getStoredPriority((priority) => {
+    it('should return priority "high" if the config option values have been set accordingly', () => {
+      component.getStoredPriority(configOptionsHigh, (priority) => {
         expect(priority.name).toBe('high');
       });
-      tick();
-
       expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeFalsy();
-    }));
-
-    it('should return priority "custom" if the config option values do not match any priority', fakeAsync(() => {
-      spyOn(configService, 'get').and.callFake((configOptionName: string) => {
-        const result = _.find(configOptionsCustom, (configOption) => {
-          return configOption.name === configOptionName;
-        });
-        return of(result);
-      });
+    });
 
-      component.getStoredPriority((priority) => {
+    it('should return priority "custom" if the config option values do not match any priority', () => {
+      component.getStoredPriority(configOptionsCustom, (priority) => {
         expect(priority.name).toBe('custom');
       });
-      tick();
-
       expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeTruthy();
-    }));
-
-    it('should return no priority if the config option values are incomplete', fakeAsync(() => {
-      spyOn(configService, 'get').and.callFake((configOptionName: string) => {
-        const result = _.find(configOptionsIncomplete, (configOption) => {
-          return configOption.name === configOptionName;
-        });
-        return of(result);
-      });
+    });
 
-      component.getStoredPriority((priority) => {
+    it('should return no priority if the config option values are incomplete', () => {
+      component.getStoredPriority(configOptionsIncomplete, (priority) => {
         expect(priority.name).toBeNull();
       });
-      tick();
-
       expect(component.osdRecvSpeedForm.getValue('customizePriority')).toBeFalsy();
-    }));
+    });
+  });
+
+  describe('setDescription', () => {
+    const configOptions = [
+      {
+        name: 'osd_max_backfills',
+        desc: ''
+      },
+      {
+        name: 'osd_recovery_max_active',
+        desc: ''
+      },
+      {
+        name: 'osd_recovery_max_single_start',
+        desc: ''
+      },
+      {
+        name: 'osd_recovery_sleep',
+        desc: 'Time in seconds to sleep before next recovery or backfill op'
+      }
+    ];
+
+    it('should set the description if one is given', () => {
+      component.setDescription(configOptions);
+      component.priorityAttrs.forEach((p) => {
+        if (p.name === 'osd_recovery_sleep') {
+          expect(p.desc).toBe('Time in seconds to sleep before next recovery or backfill op');
+        } else {
+          expect(p.desc).toBe('');
+        }
+      });
+    });
   });
 });
index 26c738f226945ba6ade3d38919e892b658851add..b6e5ffbfa65924044a58459d089a3586d82c50c9 100755 (executable)
@@ -38,19 +38,23 @@ export class OsdRecvSpeedModalComponent implements OnInit {
     this.priorityAttrs = [
       {
         name: 'osd_max_backfills',
-        text: this.i18n('Max Backfills')
+        text: this.i18n('Max Backfills'),
+        desc: ''
       },
       {
         name: 'osd_recovery_max_active',
-        text: this.i18n('Recovery Max Active')
+        text: this.i18n('Recovery Max Active'),
+        desc: ''
       },
       {
         name: 'osd_recovery_max_single_start',
-        text: this.i18n('Recovery Max Single Start')
+        text: this.i18n('Recovery Max Single Start'),
+        desc: ''
       },
       {
         name: 'osd_recovery_sleep',
-        text: this.i18n('Recovery Sleep')
+        text: this.i18n('Recovery Sleep'),
+        desc: ''
       }
     ];
 
@@ -59,18 +63,50 @@ export class OsdRecvSpeedModalComponent implements OnInit {
         attr.name,
         new FormControl(null, { validators: [Validators.required] })
       );
-
-      this.configService.get(attr.name).subscribe((data: any) => {
-        if (data.desc !== '') {
-          attr['desc'] = data.desc;
-        }
-      });
     });
   }
 
   ngOnInit() {
-    this.getStoredPriority((priority) => {
-      this.setPriority(priority);
+    const observables = [];
+    this.priorityAttrs.forEach((configName) => {
+      observables.push(this.configService.get(configName.name));
+    });
+
+    observableForkJoin(observables)
+      .pipe(
+        mergeMap((configOptions) => {
+          const result = { values: {}, configOptions: [] };
+          configOptions.forEach((configOption) => {
+            result.configOptions.push(configOption);
+
+            if (configOption && 'value' in configOption) {
+              configOption.value.forEach((value) => {
+                if (value['section'] === 'osd') {
+                  result.values[configOption.name] = Number(value.value);
+                }
+              });
+            }
+          });
+          return of(result);
+        })
+      )
+      .subscribe((resp) => {
+        this.getStoredPriority(resp.values, (priority) => {
+          this.setPriority(priority);
+        });
+        this.setDescription(resp.configOptions);
+      });
+  }
+
+  setDescription(configOptions: Array<any>) {
+    configOptions.forEach((configOption) => {
+      if (configOption.desc !== '') {
+        this.priorityAttrs.forEach((p) => {
+          if (p.name === configOption.name) {
+            p['desc'] = configOption.desc;
+          }
+        });
+      }
     });
   }
 
@@ -112,46 +148,25 @@ export class OsdRecvSpeedModalComponent implements OnInit {
     }
   }
 
-  getStoredPriority(callbackFn: Function) {
-    const observables = [];
-    this.priorityAttrs.forEach((configName) => {
-      observables.push(this.configService.get(configName.name));
+  getStoredPriority(configOptionValues: any, callbackFn: Function) {
+    const priority = _.find(this.priorities, (p) => {
+      return _.isEqual(p.values, configOptionValues);
     });
 
-    observableForkJoin(observables)
-      .pipe(
-        mergeMap((configOptions) => {
-          const result = {};
-          configOptions.forEach((configOption) => {
-            if (configOption && 'value' in configOption) {
-              configOption.value.forEach((value) => {
-                if (value['section'] === 'osd') {
-                  result[configOption.name] = Number(value.value);
-                }
-              });
-            }
-          });
-          return of(result);
-        })
-      )
-      .subscribe((resp) => {
-        const priority = _.find(this.priorities, (p) => {
-          return _.isEqual(p.values, resp);
-        });
-
-        this.osdRecvSpeedForm.controls.customizePriority.setValue(false);
+    this.osdRecvSpeedForm.controls.customizePriority.setValue(false);
 
-        if (priority) {
-          return callbackFn(priority);
-        }
+    if (priority) {
+      return callbackFn(priority);
+    }
 
-        if (Object.entries(resp).length === 4) {
-          this.osdRecvSpeedForm.controls.customizePriority.setValue(true);
-          return callbackFn(Object({ name: 'custom', text: this.i18n('Custom'), values: resp }));
-        }
+    if (Object.entries(configOptionValues).length === 4) {
+      this.osdRecvSpeedForm.controls.customizePriority.setValue(true);
+      return callbackFn(
+        Object({ name: 'custom', text: this.i18n('Custom'), values: configOptionValues })
+      );
+    }
 
-        return callbackFn(this.priorities[0]);
-      });
+    return callbackFn(this.priorities[0]);
   }
 
   onPriorityChange(selectedPriorityName) {