]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/dashboard: Convert floating values to bytes
authorStephan Müller <smueller@suse.com>
Thu, 26 Apr 2018 10:20:01 +0000 (12:20 +0200)
committerStephan Müller <smueller@suse.com>
Wed, 2 May 2018 08:39:31 +0000 (10:39 +0200)
Now the 'formatter' service can convert floating values to bytes.

Signed-off-by: Stephan Müller <smueller@suse.com>
src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.ts

index c79c8b22ce76efad3d386c0815106f1c5c4454e6..f32f92889ce68c3f0033e3eef94e20f35586d81d 100644 (file)
@@ -1,36 +1,51 @@
-import { inject, TestBed } from '@angular/core/testing';
+import { TestBed } from '@angular/core/testing';
 
 import { FormatterService } from './formatter.service';
 
 describe('FormatterService', () => {
+  let service: FormatterService;
   beforeEach(() => {
     TestBed.configureTestingModule({
       providers: [FormatterService]
     });
+    service = new FormatterService();
   });
 
-  it('should be created',
-    inject([FormatterService], (service: FormatterService) => {
-      expect(service).toBeTruthy();
-    }));
-
-  it('should not convert 10xyz to bytes (failure)',
-    inject([FormatterService], (service: FormatterService) => {
-      const bytes = service.toBytes('10xyz');
-      expect(bytes).toBeNull();
-    }));
-
-  it('should convert 4815162342 to bytes',
-    inject([FormatterService], (service: FormatterService) => {
-      const bytes = service.toBytes('4815162342');
-      expect(bytes).toEqual(jasmine.any(Number));
-      expect(bytes).toBe(4815162342);
-    }));
-
-  it('should convert 100M to bytes',
-    inject([FormatterService], (service: FormatterService) => {
-      const bytes = service.toBytes('100M');
-      expect(bytes).toEqual(jasmine.any(Number));
-      expect(bytes).toBe(104857600);
-    }));
+  it('should be created', () => {
+    expect(service).toBeTruthy();
+  });
+
+  it('should not convert 10xyz to bytes (failure)', () => {
+    const bytes = service.toBytes('10xyz');
+    expect(bytes).toBeNull();
+  });
+
+  it('should not convert 1.1.1KiB to bytes (failure)', () => {
+    const bytes = service.toBytes('1.1.1KiB');
+    expect(bytes).toBeNull();
+  });
+
+  it('should convert 4815162342 to bytes', () => {
+    const bytes = service.toBytes('4815162342');
+    expect(bytes).toEqual(jasmine.any(Number));
+    expect(bytes).toBe(4815162342);
+  });
+
+  it('should convert 100M to bytes', () => {
+    const bytes = service.toBytes('100M');
+    expect(bytes).toEqual(jasmine.any(Number));
+    expect(bytes).toBe(104857600);
+  });
+
+  it('should convert 1.532KiB to bytes', () => {
+    const bytes = service.toBytes('1.532KiB');
+    expect(bytes).toEqual(jasmine.any(Number));
+    expect(bytes).toBe(Math.floor(1.532 * 1024));
+  });
+
+  it('should convert 0.000000000001TiB to bytes', () => {
+    const bytes = service.toBytes('0.000000000001TiB');
+    expect(bytes).toEqual(jasmine.any(Number));
+    expect(bytes).toBe(1);
+  });
 });
index 82b53db337f71597b79f98673f52d2e91e508716..7c6a95d3048a821810f1397ca79962a8de5f1f66 100644 (file)
@@ -86,15 +86,15 @@ export class FormatterService {
       'yb': Math.pow(base, 8),
       'yib': Math.pow(base, 8)
     };
-    const m = RegExp('^(\\d+)\\s*(B|K(B|iB)?|M(B|iB)?|G(B|iB)?|T(B|iB)?|P(B|iB)?|' +
+    const m = RegExp('^(\\d+(\.\\d+)?)\\s*(B|K(B|iB)?|M(B|iB)?|G(B|iB)?|T(B|iB)?|P(B|iB)?|' +
       'E(B|iB)?|Z(B|iB)?|Y(B|iB)?)?$', 'i').exec(value);
     if (m === null) {
       return null;
     }
-    let bytes = parseInt(m[1], 10);
-    if (_.isString(m[2])) {
-      bytes = bytes * units[m[2].toLowerCase()];
+    let bytes = parseFloat(m[1]);
+    if (_.isString(m[3])) {
+      bytes = bytes * units[m[3].toLowerCase()];
     }
-    return bytes;
+    return Math.floor(bytes);
   }
 }