Skip to content

Commit

Permalink
Add padStart formatting function
Browse files Browse the repository at this point in the history
Signed-off-by: Drew Corlin <[email protected]>
  • Loading branch information
drewcorlin1 committed Nov 30, 2024
1 parent d9315c6 commit a41b7da
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 12 deletions.
60 changes: 52 additions & 8 deletions packages/jaeger-ui/src/utils/link-formatting.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,60 @@
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).toEqual({
parameterName: 'startTime',
formatFunction: expect.any(Function),
});

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(new Date('2020-01-01').getTime() * 1000)).toEqual(
'2020-01-01T00:00:00.000Z'
);
expect(result.formatFunction('12345')).toEqual('0000012345');
});

test('Invalid desired length', () => {
const result = getParameterAndFormatter('traceID | pad_start invalid 0');
expect(result).toEqual({
parameterName: 'traceID',
formatFunction: expect.any(Function),
});

expect(result.formatFunction('12345')).toEqual('12345');
});

test('Invalid input', () => {
const result = getParameterAndFormatter('traceID | pad_start 32 0');
expect(result).toEqual({
parameterName: 'traceID',
formatFunction: expect.any(Function),
});

expect(result.formatFunction(12345)).toEqual(12345);
});
});

test('No function', () => {
Expand Down
35 changes: 31 additions & 4 deletions packages/jaeger-ui/src/utils/link-formatting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

import { Trace } from '../types/trace';

function getFormatFunctions<T = Trace[keyof Trace]>(): Record<string, (value: T) => string | T> {
function getFormatFunctions<T = Trace[keyof Trace]>(): Record<
string,
(value: T, ...args: string[]) => string | T
> {
return {
epoch_micros_to_date_iso: microsSinceEpoch => {
if (typeof microsSinceEpoch !== 'number') {
Expand All @@ -26,6 +29,26 @@ function getFormatFunctions<T = Trace[keyof Trace]>(): Record<string, (value: T)

return new Date(microsSinceEpoch / 1000).toISOString();
},
pad_start: (value, desiredLengthString: string, padCharacter: string) => {
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);
},
};
}

Expand All @@ -35,8 +58,12 @@ export function getParameterAndFormatter<T = Trace[keyof Trace]>(
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<T>();

const formatFunction = formatFunctions[formatFunctionName];
Expand All @@ -48,5 +75,5 @@ export function getParameterAndFormatter<T = Trace[keyof Trace]>(
});
}

return { parameterName, formatFunction: formatFunction ?? null };
return { parameterName, formatFunction: formatFunction ? val => formatFunction(val, ...args) : null };
}

0 comments on commit a41b7da

Please sign in to comment.