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] Introduce new duration formatter #162246

Merged
merged 9 commits into from
Jul 24, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,18 @@ describe('MetricVisComponent', function () {
});
});

it('does not override duration configuration at visualization level when set', () => {
getFormattedMetrics(394.2393, 983123.984, {
id: 'duration',
params: { formatOverride: true, outputFormat: 'asSeconds' },
});
expect(mockDeserialize).toHaveBeenCalledTimes(2);
expect(mockDeserialize).toHaveBeenCalledWith({
id: 'duration',
params: { formatOverride: true, outputFormat: 'asSeconds' },
});
});

it('does not tweak bytes format when passed', () => {
getFormattedMetrics(394.2393, 983123.984, {
id: 'bytes',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const defaultColor = euiThemeVars.euiColorLightestShade;

function enhanceFieldFormat(serializedFieldFormat: SerializedFieldFormat | undefined) {
const formatId = serializedFieldFormat?.id || 'number';
if (formatId === 'duration') {
if (formatId === 'duration' && !serializedFieldFormat?.params?.formatOverride) {
return {
...serializedFieldFormat,
params: {
Expand Down
171 changes: 171 additions & 0 deletions src/plugins/field_formats/common/constants/duration_formats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { i18n } from '@kbn/i18n';

const DEFAULT_INPUT_FORMAT = {
text: i18n.translate('fieldFormats.duration.inputFormats.seconds', {
defaultMessage: 'Seconds',
}),
kind: 'seconds',
};
const inputFormats = [
{
text: i18n.translate('fieldFormats.duration.inputFormats.picoseconds', {
defaultMessage: 'Picoseconds',
}),
kind: 'picoseconds',
},
{
text: i18n.translate('fieldFormats.duration.inputFormats.nanoseconds', {
defaultMessage: 'Nanoseconds',
}),
kind: 'nanoseconds',
},
{
text: i18n.translate('fieldFormats.duration.inputFormats.microseconds', {
defaultMessage: 'Microseconds',
}),
kind: 'microseconds',
},
{
text: i18n.translate('fieldFormats.duration.inputFormats.milliseconds', {
defaultMessage: 'Milliseconds',
}),
kind: 'milliseconds',
},
{ ...DEFAULT_INPUT_FORMAT },
{
text: i18n.translate('fieldFormats.duration.inputFormats.minutes', {
defaultMessage: 'Minutes',
}),
kind: 'minutes',
},
{
text: i18n.translate('fieldFormats.duration.inputFormats.hours', {
defaultMessage: 'Hours',
}),
kind: 'hours',
},
{
text: i18n.translate('fieldFormats.duration.inputFormats.days', {
defaultMessage: 'Days',
}),
kind: 'days',
},
{
text: i18n.translate('fieldFormats.duration.inputFormats.weeks', {
defaultMessage: 'Weeks',
}),
kind: 'weeks',
},
{
text: i18n.translate('fieldFormats.duration.inputFormats.months', {
defaultMessage: 'Months',
}),
kind: 'months',
},
{
text: i18n.translate('fieldFormats.duration.inputFormats.years', {
defaultMessage: 'Years',
}),
kind: 'years',
},
];
const DEFAULT_OUTPUT_FORMAT = {
text: i18n.translate('fieldFormats.duration.outputFormats.humanize.approximate', {
defaultMessage: 'Human-readable (approximate)',
}),
method: 'humanize',
};
const outputFormats = [
{ ...DEFAULT_OUTPUT_FORMAT },
{
text: i18n.translate('fieldFormats.duration.outputFormats.humanize.precise', {
defaultMessage: 'Human-readable (precise)',
}),
method: 'humanizePrecise',
},
{
text: i18n.translate('fieldFormats.duration.outputFormats.asMilliseconds', {
defaultMessage: 'Milliseconds',
}),
shortText: i18n.translate('fieldFormats.duration.outputFormats.asMilliseconds.short', {
defaultMessage: 'ms',
}),
method: 'asMilliseconds',
},
{
text: i18n.translate('fieldFormats.duration.outputFormats.asSeconds', {
defaultMessage: 'Seconds',
}),
shortText: i18n.translate('fieldFormats.duration.outputFormats.asSeconds.short', {
defaultMessage: 's',
}),
method: 'asSeconds',
},
{
text: i18n.translate('fieldFormats.duration.outputFormats.asMinutes', {
defaultMessage: 'Minutes',
}),
shortText: i18n.translate('fieldFormats.duration.outputFormats.asMinutes.short', {
defaultMessage: 'min',
}),
method: 'asMinutes',
},
{
text: i18n.translate('fieldFormats.duration.outputFormats.asHours', {
defaultMessage: 'Hours',
}),
shortText: i18n.translate('fieldFormats.duration.outputFormats.asHours.short', {
defaultMessage: 'h',
}),
method: 'asHours',
},
{
text: i18n.translate('fieldFormats.duration.outputFormats.asDays', {
defaultMessage: 'Days',
}),
shortText: i18n.translate('fieldFormats.duration.outputFormats.asDays.short', {
defaultMessage: 'd',
}),
method: 'asDays',
},
{
text: i18n.translate('fieldFormats.duration.outputFormats.asWeeks', {
defaultMessage: 'Weeks',
}),
shortText: i18n.translate('fieldFormats.duration.outputFormats.asWeeks.short', {
defaultMessage: 'w',
}),
method: 'asWeeks',
},
{
text: i18n.translate('fieldFormats.duration.outputFormats.asMonths', {
defaultMessage: 'Months',
}),
shortText: i18n.translate('fieldFormats.duration.outputFormats.asMonths.short', {
defaultMessage: 'mon',
}),
method: 'asMonths',
},
{
text: i18n.translate('fieldFormats.duration.outputFormats.asYears', {
defaultMessage: 'Years',
}),
shortText: i18n.translate('fieldFormats.duration.outputFormats.asYears.short', {
defaultMessage: 'y',
}),
method: 'asYears',
},
];

export const DEFAULT_DURATION_INPUT_FORMAT = DEFAULT_INPUT_FORMAT;
export const DEFAULT_DURATION_OUTPUT_FORMAT = DEFAULT_OUTPUT_FORMAT;
export const DURATION_INPUT_FORMATS = inputFormats;
export const DURATION_OUTPUT_FORMATS = outputFormats;
Loading