Skip to content

Commit

Permalink
Visualize: Bad request when working with histogram aggregation (#77684)…
Browse files Browse the repository at this point in the history
… (#77850)

* Visualize: Bad request when working with histogram aggregation

Closes: #77023

* ✏️ Add some more fix context

* ✅ Add test

Co-authored-by: dej611 <dej611@gmail.com>
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

Co-authored-by: dej611 <dej611@gmail.com>
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 18, 2020
1 parent ebb2d54 commit 7e01905
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ describe('calculateHistogramInterval', () => {
})
).toEqual(0.02);
});

test('should correctly fallback to the default value for empty string', () => {
expect(
calculateHistogramInterval({
...params,
maxBucketsUserInput: '',
values: {
min: 0.1,
max: 0.9,
},
})
).toBe(0.01);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface IntervalValuesRange {
export interface CalculateHistogramIntervalParams {
interval: string;
maxBucketsUiSettings: number;
maxBucketsUserInput?: number;
maxBucketsUserInput?: number | '';
intervalBase?: number;
values?: IntervalValuesRange;
}
Expand Down Expand Up @@ -77,12 +77,7 @@ const calculateForGivenInterval = (
- The lower power of 10, times 2
- The lower power of 10, times 5
**/
const calculateAutoInterval = (
diff: number,
maxBucketsUiSettings: CalculateHistogramIntervalParams['maxBucketsUiSettings'],
maxBucketsUserInput: CalculateHistogramIntervalParams['maxBucketsUserInput']
) => {
const maxBars = Math.min(maxBucketsUiSettings, maxBucketsUserInput ?? maxBucketsUiSettings);
const calculateAutoInterval = (diff: number, maxBars: number) => {
const exactInterval = diff / maxBars;

const lowerPower = Math.pow(10, Math.floor(Math.log10(exactInterval)));
Expand Down Expand Up @@ -122,7 +117,11 @@ export const calculateHistogramInterval = ({

if (diff) {
calculatedInterval = isAuto
? calculateAutoInterval(diff, maxBucketsUiSettings, maxBucketsUserInput)
? calculateAutoInterval(
diff,
// Mind maxBucketsUserInput can be an empty string, hence we need to ensure it here
Math.min(maxBucketsUiSettings, maxBucketsUserInput || maxBucketsUiSettings)
)
: calculateForGivenInterval(diff, calculatedInterval, maxBucketsUiSettings);
}
}
Expand Down

0 comments on commit 7e01905

Please sign in to comment.