Skip to content

Commit 89ef12e

Browse files
alexwizpdej611elasticmachine
authored
Visualize: Bad request when working with histogram aggregation (#77684)
* Visualize: Bad request when working with histogram aggregation Closes: #77023 * ✏️ Add some more fix context * ✅ Add test Co-authored-by: dej611 <[email protected]> Co-authored-by: Elastic Machine <[email protected]>
1 parent 3759063 commit 89ef12e

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/plugins/data/common/search/aggs/buckets/lib/histogram_calculate_interval.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,19 @@ describe('calculateHistogramInterval', () => {
139139
})
140140
).toEqual(0.02);
141141
});
142+
143+
test('should correctly fallback to the default value for empty string', () => {
144+
expect(
145+
calculateHistogramInterval({
146+
...params,
147+
maxBucketsUserInput: '',
148+
values: {
149+
min: 0.1,
150+
max: 0.9,
151+
},
152+
})
153+
).toBe(0.01);
154+
});
142155
});
143156
});
144157
});

src/plugins/data/common/search/aggs/buckets/lib/histogram_calculate_interval.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ interface IntervalValuesRange {
2727
export interface CalculateHistogramIntervalParams {
2828
interval: string;
2929
maxBucketsUiSettings: number;
30-
maxBucketsUserInput?: number;
30+
maxBucketsUserInput?: number | '';
3131
intervalBase?: number;
3232
values?: IntervalValuesRange;
3333
}
@@ -77,12 +77,7 @@ const calculateForGivenInterval = (
7777
- The lower power of 10, times 2
7878
- The lower power of 10, times 5
7979
**/
80-
const calculateAutoInterval = (
81-
diff: number,
82-
maxBucketsUiSettings: CalculateHistogramIntervalParams['maxBucketsUiSettings'],
83-
maxBucketsUserInput: CalculateHistogramIntervalParams['maxBucketsUserInput']
84-
) => {
85-
const maxBars = Math.min(maxBucketsUiSettings, maxBucketsUserInput ?? maxBucketsUiSettings);
80+
const calculateAutoInterval = (diff: number, maxBars: number) => {
8681
const exactInterval = diff / maxBars;
8782

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

123118
if (diff) {
124119
calculatedInterval = isAuto
125-
? calculateAutoInterval(diff, maxBucketsUiSettings, maxBucketsUserInput)
120+
? calculateAutoInterval(
121+
diff,
122+
// Mind maxBucketsUserInput can be an empty string, hence we need to ensure it here
123+
Math.min(maxBucketsUiSettings, maxBucketsUserInput || maxBucketsUiSettings)
124+
)
126125
: calculateForGivenInterval(diff, calculatedInterval, maxBucketsUiSettings);
127126
}
128127
}

0 commit comments

Comments
 (0)