Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with long values in TSVB static metric #40256

Merged
merged 5 commits into from
Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ export const bucketTransform = {
},
static: bucket => {
checkMetric(bucket, ['value']);
const isDecimalValue = !Number.isInteger(Number(bucket.value));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can move it into helper 'method' to reuse it in future.
Also looks like it returns 'true' if bucket.value is string.
!Number.isInteger(Number('stringValue')); /// => true

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this line would justify a utility method, especially as you mentioned it's not very generic, since it works because we know that this will be a valid number in this place, generated by TSVB.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about to use RegExp here instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would you prefer using regex, if we can use a native Number method to check the same?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that we "sure" that the number in this place is a valid number, but I never trust code, so I'd also like to have a small check for NaN values here. It doesn't hurt the code having one more check

return {
bucket_script: {
buckets_path: { count: '_count' },
script: {
source: bucket.value,
source: isDecimalValue ? bucket.value : `${bucket.value.replace(/\.\d*$/, '')}L`,
lang: 'painless',
},
gap_policy: 'skip',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,28 @@ 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'
);
});

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'
);
});
});
});
});