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

[Index Patterns Field Formatter] Added human readable precise formatter for duration #100540

Merged
merged 23 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
174 changes: 173 additions & 1 deletion src/plugins/data/common/field_formats/converters/duration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,182 @@ describe('Duration Format', () => {
],
});

testCase({
inputFormat: 'nanoseconds',
outputFormat: 'humanizePrecise',
outputPrecision: 2,
showSuffix: true,
fixtures: [
{
input: 1988,
output: '0.00 Milliseconds',
},
{
input: 658,
output: '0.00 Milliseconds',
},
{
input: 3857,
output: '0.00 Milliseconds',
},
],
});

testCase({
inputFormat: 'microseconds',
outputFormat: 'humanizePrecise',
outputPrecision: 2,
showSuffix: true,
fixtures: [
{
input: 1988,
output: '1.99 Milliseconds',
},
{
input: 658,
output: '0.66 Milliseconds',
},
{
input: 3857,
output: '3.86 Milliseconds',
},
],
});

testCase({
inputFormat: 'microseconds',
outputFormat: 'humanizePrecise',
outputPrecision: 1,
showSuffix: true,
fixtures: [
{
input: 1988,
output: '2.0 Milliseconds',
},
{
input: 0,
output: '0.0 Milliseconds',
},
{
input: 658,
output: '0.7 Milliseconds',
},
{
input: 3857,
output: '3.9 Milliseconds',
},
],
});

testCase({
inputFormat: 'seconds',
outputFormat: 'humanizePrecise',
outputPrecision: 0,
showSuffix: true,
fixtures: [
{
input: 600,
output: '10 Minutes',
},
{
input: 30,
output: '30 Seconds',
},
{
input: 3000,
output: '50 Minutes',
},
],
});

testCase({
inputFormat: 'milliseconds',
outputFormat: 'humanizePrecise',
outputPrecision: 0,
showSuffix: true,
useShortSuffix: true,
fixtures: [
{
input: -123,
output: '-123 ms',
},
{
input: 1,
output: '1 ms',
},
{
input: 600,
output: '600 ms',
},
{
input: 30,
output: '30 ms',
},
{
input: 3000,
output: '3 s',
},
{
input: 300000,
output: '5 min',
},
{
input: 30000000,
output: '8 h',
},
{
input: 90000000,
output: '1 d',
},
{
input: 9000000000,
output: '3 mon',
},
{
input: 99999999999,
output: '3 y',
},
],
});

testCase({
inputFormat: 'milliseconds',
outputFormat: 'humanizePrecise',
outputPrecision: 0,
showSuffix: true,
useShortSuffix: true,
includeSpaceWithSuffix: false,
fixtures: [
{
input: -123,
output: '-123ms',
},
{
input: 1,
output: '1ms',
},
{
input: 600,
output: '600ms',
},
],
});

function testCase({
inputFormat,
outputFormat,
outputPrecision,
showSuffix,
useShortSuffix,
includeSpaceWithSuffix,
fixtures,
}: {
inputFormat: string;
outputFormat: string;
outputPrecision: number | undefined;
showSuffix: boolean | undefined;
useShortSuffix?: boolean;
includeSpaceWithSuffix?: boolean;
fixtures: any[];
}) {
fixtures.forEach((fixture: Record<string, any>) => {
Expand All @@ -160,7 +325,14 @@ describe('Duration Format', () => {
outputPrecision ? `, ${outputPrecision} decimals` : ''
}`, () => {
const duration = new DurationFormat(
{ inputFormat, outputFormat, outputPrecision, showSuffix },
{
inputFormat,
outputFormat,
outputPrecision,
showSuffix,
useShortSuffix,
includeSpaceWithSuffix,
},
jest.fn()
);
expect(duration.convert(input)).toBe(output);
Expand Down
Loading