From: Volker Theile Date: Tue, 20 Mar 2018 16:23:22 +0000 (+0100) Subject: mgr/dashboard: Add toBytes() method to FormatterService. X-Git-Tag: v13.1.0~523^2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=e71a33e7f87fb6eabed7035c6b9062edaf053343;p=ceph.git mgr/dashboard: Add toBytes() method to FormatterService. Signed-off-by: Volker Theile --- diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.spec.ts index f3a99b595488d..c79c8b22ce76e 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.spec.ts @@ -9,7 +9,28 @@ describe('FormatterService', () => { }); }); - it('should be created', inject([FormatterService], (service: FormatterService) => { - expect(service).toBeTruthy(); - })); + 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); + })); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.ts index 3986408798fd2..82b53db337f71 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.ts @@ -1,5 +1,7 @@ import { Injectable } from '@angular/core'; +import * as _ from 'lodash'; + @Injectable() export class FormatterService { constructor() {} @@ -48,4 +50,51 @@ export class FormatterService { return truncatedFloat === '' ? '-' : (truncatedFloat + units[unit]); } + + /** + * Convert the given value into bytes. + * @param {string} value The value to be converted, e.g. 1024B, 10M, 300KiB or 1ZB. + * @returns Returns the given value in bytes without any appended unit or null in case + * of an error. + */ + toBytes(value: string): number | null { + const base = 1024; + const units = { + 'b': 1, + 'k': Math.pow(base, 1), + 'kb': Math.pow(base, 1), + 'kib': Math.pow(base, 1), + 'm': Math.pow(base, 2), + 'mb': Math.pow(base, 2), + 'mib': Math.pow(base, 2), + 'g': Math.pow(base, 3), + 'gb': Math.pow(base, 3), + 'gib': Math.pow(base, 3), + 't': Math.pow(base, 4), + 'tb': Math.pow(base, 4), + 'tib': Math.pow(base, 4), + 'p': Math.pow(base, 5), + 'pb': Math.pow(base, 5), + 'pib': Math.pow(base, 5), + 'e': Math.pow(base, 6), + 'eb': Math.pow(base, 6), + 'eib': Math.pow(base, 6), + 'z': Math.pow(base, 7), + 'zb': Math.pow(base, 7), + 'zib': Math.pow(base, 7), + 'y': Math.pow(base, 8), + '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)?|' + + '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()]; + } + return bytes; + } }