From 97069e34638f927ce5c168a2f290d996f3616532 Mon Sep 17 00:00:00 2001 From: Drew Corlin Date: Sat, 30 Nov 2024 15:26:50 -0500 Subject: [PATCH] Add padStart formatting function Signed-off-by: Drew Corlin --- .../src/utils/link-formatting.test.js | 45 +++++++++++++++---- .../jaeger-ui/src/utils/link-formatting.tsx | 35 +++++++++++++-- 2 files changed, 68 insertions(+), 12 deletions(-) diff --git a/packages/jaeger-ui/src/utils/link-formatting.test.js b/packages/jaeger-ui/src/utils/link-formatting.test.js index d77cb3ecc3..149ca934d1 100644 --- a/packages/jaeger-ui/src/utils/link-formatting.test.js +++ b/packages/jaeger-ui/src/utils/link-formatting.test.js @@ -15,16 +15,45 @@ import { getParameterAndFormatter } from './link-formatting'; describe('getParameterAndFormatter()', () => { - test('epoch_micros_to_date_iso', () => { - const result = getParameterAndFormatter('startTime | epoch_micros_to_date_iso'); - expect(result).toEqual({ - parameterName: 'startTime', - formatFunction: expect.any(Function), + describe('epoch_micros_to_date_iso', () => { + test('epoch_micros_to_date_iso', () => { + const result = getParameterAndFormatter('startTime | epoch_micros_to_date_iso'); + expect(result).toEqual({ + parameterName: 'startTime', + formatFunction: expect.any(Function), + }); + + expect(result.formatFunction(new Date('2020-01-01').getTime() * 1000)).toEqual( + '2020-01-01T00:00:00.000Z' + ); + }); + + test('Non date', () => { + const result = getParameterAndFormatter('startTime | epoch_micros_to_date_iso'); + expect(result.formatFunction('Not a date value')).toEqual('Not a date value'); + }); + }); + + describe('pad_start', () => { + test('Valid desired length', () => { + const result = getParameterAndFormatter('traceID | pad_start 10 0'); + expect(result).toEqual({ + parameterName: 'traceID', + formatFunction: expect.any(Function), + }); + + expect(result.formatFunction('12345')).toEqual('0000012345'); }); - expect(result.formatFunction(new Date('2020-01-01').getTime() * 1000)).toEqual( - '2020-01-01T00:00:00.000Z' - ); + test('Invalid desired length', () => { + const result = getParameterAndFormatter('traceID | pad_start invalid 0'); + expect(result.formatFunction('12345')).toEqual('12345'); + }); + + test('Invalid input', () => { + const result = getParameterAndFormatter('traceID | pad_start 32 0'); + expect(result.formatFunction(12345)).toEqual(12345); + }); }); test('No function', () => { diff --git a/packages/jaeger-ui/src/utils/link-formatting.tsx b/packages/jaeger-ui/src/utils/link-formatting.tsx index fd40a4becc..299e6005db 100644 --- a/packages/jaeger-ui/src/utils/link-formatting.tsx +++ b/packages/jaeger-ui/src/utils/link-formatting.tsx @@ -14,7 +14,10 @@ import { Trace } from '../types/trace'; -function getFormatFunctions(): Record string | T> { +function getFormatFunctions(): Record< + string, + (value: T, ...args: string[]) => string | T +> { return { epoch_micros_to_date_iso: microsSinceEpoch => { if (typeof microsSinceEpoch !== 'number') { @@ -26,6 +29,26 @@ function getFormatFunctions(): Record { + if (typeof value !== 'string') { + console.error('pad_start can only operate on strings, ignoring formatting', { + value, + desiredLength: desiredLengthString, + padCharacter, + }); + return value; + } + const desiredLength = parseInt(desiredLengthString, 10); + if (Number.isNaN(desiredLength)) { + console.error('pad_start needs a desired length as second argument, ignoring formatting', { + value, + desiredLength: desiredLengthString, + padCharacter, + }); + } + + return value.padStart(desiredLength, padCharacter); + }, }; } @@ -35,8 +58,12 @@ export function getParameterAndFormatter( parameterName: string; formatFunction: ((value: T) => T | string) | null; } { - const [parameterName, formatFunctionName] = parameter.split('|').map(part => part.trim()); - if (!formatFunctionName) return { parameterName, formatFunction: null }; + const parts = parameter.split('|').map(part => part.trim()); + const parameterName = parts[0]; + if (parts.length === 1) return { parameterName, formatFunction: null }; + + const [formatFunctionName, ...args] = parts[1].split(' '); + const formatFunctions = getFormatFunctions(); const formatFunction = formatFunctions[formatFunctionName]; @@ -48,5 +75,5 @@ export function getParameterAndFormatter( }); } - return { parameterName, formatFunction: formatFunction ?? null }; + return { parameterName, formatFunction: formatFunction ? val => formatFunction(val, ...args) : null }; }