Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lens] Add bit formatter #141372

Merged
merged 4 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const formatColumnFn: FormatColumnExpressionFunction['fn'] = (
if (!parentFormat) {
if (supportedFormats[format]) {
const serializedFormat: SerializedFieldFormat = {
id: format,
id: supportedFormats[format].formatId,
params: { pattern: supportedFormats[format].decimalsToPattern(decimals) },
};
return withParams(col, serializedFormat as Record<string, unknown>);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

export const supportedFormats: Record<
string,
{ decimalsToPattern: (decimals?: number) => string }
{ decimalsToPattern: (decimals?: number) => string; formatId: string }
> = {
number: {
formatId: 'number',
decimalsToPattern: (decimals = 2) => {
if (decimals === 0) {
return `0,0`;
Expand All @@ -18,6 +19,7 @@ export const supportedFormats: Record<
},
},
percent: {
formatId: 'percent',
decimalsToPattern: (decimals = 2) => {
if (decimals === 0) {
return `0,0%`;
Expand All @@ -26,11 +28,21 @@ export const supportedFormats: Record<
},
},
bytes: {
formatId: 'bytes',
decimalsToPattern: (decimals = 2) => {
if (decimals === 0) {
return `0,0b`;
}
return `0,0.${'0'.repeat(decimals)}b`;
},
},
bits: {
formatId: 'number',
decimalsToPattern: (decimals = 2) => {
if (decimals === 0) {
return `0,0bitd`;
}
return `0,0.${'0'.repeat(decimals)}bitd`;
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { GenericIndexPatternColumn } from '../indexpattern';
import { isColumnFormatted } from '../operations/definitions/helpers';
import { useDebouncedValue } from '../../shared_components';

const supportedFormats: Record<string, { title: string }> = {
const supportedFormats: Record<string, { title: string; defaultDecimals?: number }> = {
number: {
title: i18n.translate('xpack.lens.indexPattern.numberFormatLabel', {
defaultMessage: 'Number',
Expand All @@ -28,6 +28,12 @@ const supportedFormats: Record<string, { title: string }> = {
defaultMessage: 'Bytes (1024)',
}),
},
bits: {
title: i18n.translate('xpack.lens.indexPattern.bitsFormatLabel', {
defaultMessage: 'Bits (1000)',
}),
defaultDecimals: 0,
},
};

const defaultOption = {
Expand Down Expand Up @@ -118,10 +124,13 @@ export function FormatSelector(props: FormatSelectorProps) {
onChange();
return;
}
const id = choices[0].value;
const defaultDecimals = supportedFormats[id].defaultDecimals;
onChange({
id: choices[0].value,
params: { decimals },
params: { decimals: defaultDecimals ?? decimals },
});
setDecimals(defaultDecimals ?? decimals);
},
[onChange, decimals]
);
Expand Down