Skip to content

Commit 4aa3920

Browse files
[Index Patterns Field Formatter] Added human readable precise formatter for duration (#100540)
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
1 parent 19b8aa7 commit 4aa3920

File tree

7 files changed

+628
-14
lines changed

7 files changed

+628
-14
lines changed

src/plugins/data/common/field_formats/converters/duration.test.ts

Lines changed: 173 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,182 @@ describe('Duration Format', () => {
139139
],
140140
});
141141

142+
testCase({
143+
inputFormat: 'nanoseconds',
144+
outputFormat: 'humanizePrecise',
145+
outputPrecision: 2,
146+
showSuffix: true,
147+
fixtures: [
148+
{
149+
input: 1988,
150+
output: '0.00 Milliseconds',
151+
},
152+
{
153+
input: 658,
154+
output: '0.00 Milliseconds',
155+
},
156+
{
157+
input: 3857,
158+
output: '0.00 Milliseconds',
159+
},
160+
],
161+
});
162+
163+
testCase({
164+
inputFormat: 'microseconds',
165+
outputFormat: 'humanizePrecise',
166+
outputPrecision: 2,
167+
showSuffix: true,
168+
fixtures: [
169+
{
170+
input: 1988,
171+
output: '1.99 Milliseconds',
172+
},
173+
{
174+
input: 658,
175+
output: '0.66 Milliseconds',
176+
},
177+
{
178+
input: 3857,
179+
output: '3.86 Milliseconds',
180+
},
181+
],
182+
});
183+
184+
testCase({
185+
inputFormat: 'microseconds',
186+
outputFormat: 'humanizePrecise',
187+
outputPrecision: 1,
188+
showSuffix: true,
189+
fixtures: [
190+
{
191+
input: 1988,
192+
output: '2.0 Milliseconds',
193+
},
194+
{
195+
input: 0,
196+
output: '0.0 Milliseconds',
197+
},
198+
{
199+
input: 658,
200+
output: '0.7 Milliseconds',
201+
},
202+
{
203+
input: 3857,
204+
output: '3.9 Milliseconds',
205+
},
206+
],
207+
});
208+
209+
testCase({
210+
inputFormat: 'seconds',
211+
outputFormat: 'humanizePrecise',
212+
outputPrecision: 0,
213+
showSuffix: true,
214+
fixtures: [
215+
{
216+
input: 600,
217+
output: '10 Minutes',
218+
},
219+
{
220+
input: 30,
221+
output: '30 Seconds',
222+
},
223+
{
224+
input: 3000,
225+
output: '50 Minutes',
226+
},
227+
],
228+
});
229+
230+
testCase({
231+
inputFormat: 'milliseconds',
232+
outputFormat: 'humanizePrecise',
233+
outputPrecision: 0,
234+
showSuffix: true,
235+
useShortSuffix: true,
236+
fixtures: [
237+
{
238+
input: -123,
239+
output: '-123 ms',
240+
},
241+
{
242+
input: 1,
243+
output: '1 ms',
244+
},
245+
{
246+
input: 600,
247+
output: '600 ms',
248+
},
249+
{
250+
input: 30,
251+
output: '30 ms',
252+
},
253+
{
254+
input: 3000,
255+
output: '3 s',
256+
},
257+
{
258+
input: 300000,
259+
output: '5 min',
260+
},
261+
{
262+
input: 30000000,
263+
output: '8 h',
264+
},
265+
{
266+
input: 90000000,
267+
output: '1 d',
268+
},
269+
{
270+
input: 9000000000,
271+
output: '3 mon',
272+
},
273+
{
274+
input: 99999999999,
275+
output: '3 y',
276+
},
277+
],
278+
});
279+
280+
testCase({
281+
inputFormat: 'milliseconds',
282+
outputFormat: 'humanizePrecise',
283+
outputPrecision: 0,
284+
showSuffix: true,
285+
useShortSuffix: true,
286+
includeSpaceWithSuffix: false,
287+
fixtures: [
288+
{
289+
input: -123,
290+
output: '-123ms',
291+
},
292+
{
293+
input: 1,
294+
output: '1ms',
295+
},
296+
{
297+
input: 600,
298+
output: '600ms',
299+
},
300+
],
301+
});
302+
142303
function testCase({
143304
inputFormat,
144305
outputFormat,
145306
outputPrecision,
146307
showSuffix,
308+
useShortSuffix,
309+
includeSpaceWithSuffix,
147310
fixtures,
148311
}: {
149312
inputFormat: string;
150313
outputFormat: string;
151314
outputPrecision: number | undefined;
152315
showSuffix: boolean | undefined;
316+
useShortSuffix?: boolean;
317+
includeSpaceWithSuffix?: boolean;
153318
fixtures: any[];
154319
}) {
155320
fixtures.forEach((fixture: Record<string, any>) => {
@@ -160,7 +325,14 @@ describe('Duration Format', () => {
160325
outputPrecision ? `, ${outputPrecision} decimals` : ''
161326
}`, () => {
162327
const duration = new DurationFormat(
163-
{ inputFormat, outputFormat, outputPrecision, showSuffix },
328+
{
329+
inputFormat,
330+
outputFormat,
331+
outputPrecision,
332+
showSuffix,
333+
useShortSuffix,
334+
includeSpaceWithSuffix,
335+
},
164336
jest.fn()
165337
);
166338
expect(duration.convert(input)).toBe(output);

0 commit comments

Comments
 (0)