From cf38f1cb03d54dd2511921c1358e177d71167b0d Mon Sep 17 00:00:00 2001 From: Tim Roes Date: Wed, 3 Jul 2019 14:27:50 +0200 Subject: [PATCH 1/4] Fix issue with long values in TSVB static metric --- .../lib/vis_data/helpers/bucket_transform.js | 3 ++- .../vis_data/helpers/bucket_transform.test.js | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.js b/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.js index 95550e2115efb..3c969f4eb79c7 100644 --- a/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.js +++ b/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.js @@ -78,11 +78,12 @@ export const bucketTransform = { }, static: bucket => { checkMetric(bucket, ['value']); + const isDecimalValue = !Number.isInteger(Number(bucket.value)); return { bucket_script: { buckets_path: { count: '_count' }, script: { - source: bucket.value, + source: isDecimalValue ? bucket.value : `${bucket.value}L`, lang: 'painless', }, gap_policy: 'skip', diff --git a/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.test.js b/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.test.js index 186baf73a86c7..3ffd14eba88d8 100644 --- a/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.test.js +++ b/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.test.js @@ -63,5 +63,21 @@ describe('src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_tra expect(bucketTransform.moving_average(bucket, metrics)).toMatchSnapshot(); }); }); + + describe('static', () => { + test('should return a script with a double value when using decimals', () => { + expect(bucketTransform.static({ value: '421.12' })).toHaveProperty( + 'bucket_script.script.source', + '421.12' + ); + }); + + test('should return a long script for integer values', () => { + expect(bucketTransform.static({ value: '1234567890123' })).toHaveProperty( + 'bucket_script.script.source', + '1234567890123L' + ); + }); + }); }); }); From 4489a7ac48616dc3455fc47d48f135d2f587c3f4 Mon Sep 17 00:00:00 2001 From: Tim Roes Date: Wed, 3 Jul 2019 14:35:47 +0200 Subject: [PATCH 2/4] Handle numeric values with decimal zeros --- .../server/lib/vis_data/helpers/bucket_transform.js | 2 +- .../server/lib/vis_data/helpers/bucket_transform.test.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.js b/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.js index 3c969f4eb79c7..f1a7a80a8aa14 100644 --- a/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.js +++ b/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.js @@ -83,7 +83,7 @@ export const bucketTransform = { bucket_script: { buckets_path: { count: '_count' }, script: { - source: isDecimalValue ? bucket.value : `${bucket.value}L`, + source: isDecimalValue ? bucket.value : `${bucket.value.replace(/\.\d*$/, '')}L`, lang: 'painless', }, gap_policy: 'skip', diff --git a/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.test.js b/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.test.js index 3ffd14eba88d8..ad19212b3c85a 100644 --- a/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.test.js +++ b/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.test.js @@ -78,6 +78,13 @@ describe('src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_tra '1234567890123L' ); }); + + test('should also work if the user specifies an integer with decimal 0s', () => { + expect(bucketTransform.static({ value: '1234567890123.0' })).toHaveProperty( + 'bucket_script.script.source', + '1234567890123L' + ); + }); }); }); }); From a8b3cb41e7f9c28676e4cb9539b99d5f43ff173d Mon Sep 17 00:00:00 2001 From: Tim Roes Date: Fri, 5 Jul 2019 11:05:57 +0200 Subject: [PATCH 3/4] Work properly with exponential values --- .../server/lib/vis_data/helpers/bucket_transform.js | 5 +++-- .../lib/vis_data/helpers/bucket_transform.test.js | 13 ++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.js b/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.js index f1a7a80a8aa14..6416f2ac8cb0a 100644 --- a/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.js +++ b/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.js @@ -78,12 +78,13 @@ export const bucketTransform = { }, static: bucket => { checkMetric(bucket, ['value']); - const isDecimalValue = !Number.isInteger(Number(bucket.value)); + // Anything containing a decimal point or an exponent is considered decimal value + const isDecimalValue = bucket.value.match(/[.e]/i); return { bucket_script: { buckets_path: { count: '_count' }, script: { - source: isDecimalValue ? bucket.value : `${bucket.value.replace(/\.\d*$/, '')}L`, + source: isDecimalValue ? bucket.value : `${bucket.value}L`, lang: 'painless', }, gap_policy: 'skip', diff --git a/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.test.js b/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.test.js index ad19212b3c85a..3def18997863e 100644 --- a/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.test.js +++ b/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.test.js @@ -79,10 +79,17 @@ describe('src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_tra ); }); - test('should also work if the user specifies an integer with decimal 0s', () => { - expect(bucketTransform.static({ value: '1234567890123.0' })).toHaveProperty( + test('should not return a long script for exponential values', () => { + expect(bucketTransform.static({ value: '123123123e12' })).toHaveProperty( 'bucket_script.script.source', - '1234567890123L' + '123123123e12' + ); + }); + + test('should return decimal scripts for very large decimals', () => { + expect(bucketTransform.static({ value: '1234178312312381273123123.11123' })).toHaveProperty( + 'bucket_script.script.source', + '1234178312312381273123123.11123' ); }); }); From ac2028b5d67c95a470bd4967e00a81c1b2942953 Mon Sep 17 00:00:00 2001 From: Tim Roes Date: Fri, 5 Jul 2019 11:53:37 +0200 Subject: [PATCH 4/4] Wrap into boolean --- .../metrics/server/lib/vis_data/helpers/bucket_transform.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.js b/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.js index 6416f2ac8cb0a..00e883509cfbe 100644 --- a/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.js +++ b/src/legacy/core_plugins/metrics/server/lib/vis_data/helpers/bucket_transform.js @@ -79,7 +79,7 @@ export const bucketTransform = { static: bucket => { checkMetric(bucket, ['value']); // Anything containing a decimal point or an exponent is considered decimal value - const isDecimalValue = bucket.value.match(/[.e]/i); + const isDecimalValue = Boolean(bucket.value.match(/[.e]/i)); return { bucket_script: { buckets_path: { count: '_count' },