Skip to content

Commit

Permalink
- Add new memory units adaptive formatter to format bytes units into …
Browse files Browse the repository at this point in the history
…kB, MB, GB, etc.
  • Loading branch information
mkopec87 committed Oct 9, 2024
1 parent a849c29 commit ea4a65e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export const D3_FORMAT_OPTIONS: [string, string][] = [
...d3Formatted,
['DURATION', t('Duration in ms (66000 => 1m 6s)')],
['DURATION_SUB', t('Duration in ms (1.40008 => 1ms 400µs 80ns)')],
['MEMORY_DECIMAL', t('Memory in bytes - decimal (1024B => 1.024kB)')],
['MEMORY_BINARY', t('Memory in bytes - binary (1024B => 1KiB)')],
];

export const D3_TIME_FORMAT_DOCS = t(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import NumberFormatter from '../NumberFormatter';

export default function createMemoryFormatter(config: {
description?: string;
id?: string;
label?: string;
binary?: bool;
}) {
const { description, id, label, binary } = config;

return new NumberFormatter({
description,
formatFunc: value => {
if value == 0 return '0B';

const suffixes = binary ? ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'] : ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB', 'RB', 'QB']
const base = binary ? 1024 : 1000;
const decimals = 2;

const i = Math.floor(Math.log(value) / Math.log(base))
return `${parseFloat((value / Math.pow(base, i)).toFixed(decimals))}${suffixes[i]}`
},
id: id ?? 'memory_format',
label: label ?? `Memory formatter`,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ export {
export { default as NumberFormatterRegistry } from './NumberFormatterRegistry';
export { default as createD3NumberFormatter } from './factories/createD3NumberFormatter';
export { default as createDurationFormatter } from './factories/createDurationFormatter';
export { default as createMemoryFormatter } from './factories/createMemoryFormatter';
export { default as createSiAtMostNDigitFormatter } from './factories/createSiAtMostNDigitFormatter';
export { default as createSmartNumberFormatter } from './factories/createSmartNumberFormatter';
6 changes: 5 additions & 1 deletion superset-frontend/src/setup/setupFormatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
createSmartDateFormatter,
createSmartDateVerboseFormatter,
createSmartDateDetailedFormatter,
createMemoryFormatter,
} from '@superset-ui/core';
import { FormatLocaleDefinition } from 'd3-format';
import { TimeLocaleDefinition } from 'd3-time-format';
Expand Down Expand Up @@ -76,7 +77,10 @@ export default function setupFormatters(
.registerValue(
'DURATION_SUB',
createDurationFormatter({ formatSubMilliseconds: true }),
);
)
.registerValue('MEMORY_DECIMAL', createMemoryFormatter({ binary: false }))
.registerValue('MEMORY_BINARY', createMemoryFormatter({ binary: true }))
);

const timeFormatterRegistry = getTimeFormatterRegistry();

Expand Down

0 comments on commit ea4a65e

Please sign in to comment.