Skip to content

Commit 1dde0c4

Browse files
[7.x] [ML] Fix job list crashing due to undefined processed records (#71966) (#72112)
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent 05dc674 commit 1dde0c4

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

x-pack/plugins/ml/public/application/util/string_utils.test.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ describe('ML - string utils', () => {
3838
initial_record_score: 0,
3939
};
4040

41-
test('returns correct values without URI encoding', () => {
41+
test('should return correct values without URI encoding', () => {
4242
const result = replaceStringTokens('user=$user$,time=$timestamp$', testRecord, false);
4343
expect(result).toBe("user=Des O'Connor,time=1454890500000");
4444
});
4545

46-
test('returns correct values for missing token without URI encoding', () => {
46+
test('should return correct values for missing token without URI encoding', () => {
4747
const result = replaceStringTokens('user=$username$,time=$timestamp$', testRecord, false);
4848
expect(result).toBe('user=$username$,time=1454890500000');
4949
});
5050

51-
test('returns correct values with URI encoding', () => {
51+
test('should return correct values with URI encoding', () => {
5252
const testString1 = 'https://www.google.co.uk/webhp#q=$testfield1$';
5353
const testString2 = 'https://www.google.co.uk/webhp#q=$testfield2$';
5454
const testString3 = 'https://www.google.co.uk/webhp#q=$testfield3$';
@@ -65,15 +65,15 @@ describe('ML - string utils', () => {
6565
expect(result4).toBe("https://www.google.co.uk/webhp#q=Des%20O'Connor");
6666
});
6767

68-
test('returns correct values for missing token with URI encoding', () => {
68+
test('should return correct values for missing token with URI encoding', () => {
6969
const testString = 'https://www.google.co.uk/webhp#q=$username$&time=$timestamp$';
7070
const result = replaceStringTokens(testString, testRecord, true);
7171
expect(result).toBe('https://www.google.co.uk/webhp#q=$username$&time=1454890500000');
7272
});
7373
});
7474

7575
describe('detectorToString', () => {
76-
test('returns the correct descriptions for detectors', () => {
76+
test('should return the correct descriptions for detectors', () => {
7777
const detector1: Detector = {
7878
function: 'count',
7979
};
@@ -102,7 +102,7 @@ describe('ML - string utils', () => {
102102
});
103103

104104
describe('toLocaleString', () => {
105-
test('returns correct comma placement for large numbers', () => {
105+
test('should return correct comma placement for large numbers', () => {
106106
expect(toLocaleString(1)).toBe('1');
107107
expect(toLocaleString(10)).toBe('10');
108108
expect(toLocaleString(100)).toBe('100');
@@ -114,10 +114,14 @@ describe('ML - string utils', () => {
114114
expect(toLocaleString(100000000)).toBe('100,000,000');
115115
expect(toLocaleString(1000000000)).toBe('1,000,000,000');
116116
});
117+
test('should return empty string for undefined or null', () => {
118+
expect(toLocaleString(undefined)).toBe('');
119+
expect(toLocaleString(null)).toBe('');
120+
});
117121
});
118122

119123
describe('mlEscape', () => {
120-
test('returns correct escaping of characters', () => {
124+
test('should return correct escaping of characters', () => {
121125
expect(mlEscape('foo&bar')).toBe('foo&amp;bar');
122126
expect(mlEscape('foo<bar')).toBe('foo&lt;bar');
123127
expect(mlEscape('foo>bar')).toBe('foo&gt;bar');
@@ -128,7 +132,7 @@ describe('ML - string utils', () => {
128132
});
129133

130134
describe('escapeForElasticsearchQuery', () => {
131-
test('returns correct escaping of reserved elasticsearch characters', () => {
135+
test('should return correct escaping of reserved elasticsearch characters', () => {
132136
expect(escapeForElasticsearchQuery('foo+bar')).toBe('foo\\+bar');
133137
expect(escapeForElasticsearchQuery('foo-bar')).toBe('foo\\-bar');
134138
expect(escapeForElasticsearchQuery('foo=bar')).toBe('foo\\=bar');

x-pack/plugins/ml/public/application/util/string_utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ function quoteField(field: string): string {
9090

9191
// add commas to large numbers
9292
// Number.toLocaleString is not supported on safari
93-
export function toLocaleString(x: number): string {
93+
export function toLocaleString(x: number | undefined | null): string {
94+
if (x === undefined || x === null) {
95+
return '';
96+
}
9497
let result = x.toString();
9598
if (x && typeof x === 'number') {
9699
const parts = x.toString().split('.');

0 commit comments

Comments
 (0)