ElasticSearch: Parse histogram interval as float instead int (#99270)
This commit is contained in:
@@ -207,10 +207,10 @@ func newAggDef(key string, aggregation *aggContainer) *aggDef {
|
||||
|
||||
// HistogramAgg represents a histogram aggregation
|
||||
type HistogramAgg struct {
|
||||
Interval int `json:"interval,omitempty"`
|
||||
Field string `json:"field"`
|
||||
MinDocCount int `json:"min_doc_count"`
|
||||
Missing *int `json:"missing,omitempty"`
|
||||
Interval float64 `json:"interval,omitempty"`
|
||||
Field string `json:"field"`
|
||||
MinDocCount int `json:"min_doc_count"`
|
||||
Missing *int `json:"missing,omitempty"`
|
||||
}
|
||||
|
||||
// DateHistogramAgg represents a date histogram aggregation
|
||||
|
||||
@@ -225,7 +225,7 @@ func addDateHistogramAgg(aggBuilder es.AggBuilder, bucketAgg *BucketAgg, timeFro
|
||||
|
||||
func addHistogramAgg(aggBuilder es.AggBuilder, bucketAgg *BucketAgg) es.AggBuilder {
|
||||
aggBuilder.Histogram(bucketAgg.ID, bucketAgg.Field, func(a *es.HistogramAgg, b es.AggBuilder) {
|
||||
a.Interval = stringToIntWithDefaultValue(bucketAgg.Settings.Get("interval").MustString(), 1000)
|
||||
a.Interval = stringToFloatWithDefaultValue(bucketAgg.Settings.Get("interval").MustString(), 1000)
|
||||
a.MinDocCount = bucketAgg.Settings.Get("min_doc_count").MustInt(0)
|
||||
|
||||
if missing, err := bucketAgg.Settings.Get("missing").Int(); err == nil {
|
||||
@@ -519,3 +519,15 @@ func stringToIntWithDefaultValue(valueStr string, defaultValue int) int {
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func stringToFloatWithDefaultValue(valueStr string, defaultValue float64) float64 {
|
||||
value, err := strconv.ParseFloat(valueStr, 64)
|
||||
if err != nil {
|
||||
value = defaultValue
|
||||
}
|
||||
// In our case, 0 is not a valid value and in this case we default to defaultValue
|
||||
if value == 0 {
|
||||
value = defaultValue
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
@@ -576,10 +576,51 @@ func TestExecuteElasticsearchDataQuery(t *testing.T) {
|
||||
require.Equal(t, firstLevel.Aggregation.Type, "histogram")
|
||||
hAgg := firstLevel.Aggregation.Aggregation.(*es.HistogramAgg)
|
||||
require.Equal(t, hAgg.Field, "bytes")
|
||||
require.Equal(t, hAgg.Interval, 10)
|
||||
require.Equal(t, hAgg.Interval, float64(10))
|
||||
require.Equal(t, hAgg.MinDocCount, 2)
|
||||
require.Equal(t, *hAgg.Missing, 5)
|
||||
})
|
||||
t.Run("With histogram agg with decimal interval", func(t *testing.T) {
|
||||
c := newFakeClient()
|
||||
_, err := executeElasticsearchDataQuery(c, `{
|
||||
"bucketAggs": [
|
||||
{
|
||||
"id": "3",
|
||||
"type": "histogram",
|
||||
"field": "bytes",
|
||||
"settings": { "interval": "5.5", "min_doc_count": 2, "missing": 5 }
|
||||
}
|
||||
],
|
||||
"metrics": [{"type": "count", "id": "1" }]
|
||||
}`, from, to)
|
||||
require.NoError(t, err)
|
||||
sr := c.multisearchRequests[0].Requests[0]
|
||||
|
||||
firstLevel := sr.Aggs[0]
|
||||
hAgg := firstLevel.Aggregation.Aggregation.(*es.HistogramAgg)
|
||||
require.Equal(t, hAgg.Interval, 5.5)
|
||||
})
|
||||
|
||||
t.Run("With histogram agg with invalid interval", func(t *testing.T) {
|
||||
c := newFakeClient()
|
||||
_, err := executeElasticsearchDataQuery(c, `{
|
||||
"bucketAggs": [
|
||||
{
|
||||
"id": "3",
|
||||
"type": "histogram",
|
||||
"field": "bytes",
|
||||
"settings": { "interval": "5,5", "min_doc_count": 2, "missing": 5 }
|
||||
}
|
||||
],
|
||||
"metrics": [{"type": "count", "id": "1" }]
|
||||
}`, from, to)
|
||||
require.NoError(t, err)
|
||||
sr := c.multisearchRequests[0].Requests[0]
|
||||
|
||||
firstLevel := sr.Aggs[0]
|
||||
hAgg := firstLevel.Aggregation.Aggregation.(*es.HistogramAgg)
|
||||
require.Equal(t, hAgg.Interval, float64(1000))
|
||||
})
|
||||
|
||||
t.Run("With histogram (from frontend tests)", func(t *testing.T) {
|
||||
c := newFakeClient()
|
||||
@@ -602,7 +643,7 @@ func TestExecuteElasticsearchDataQuery(t *testing.T) {
|
||||
require.Equal(t, firstLevel.Aggregation.Type, "histogram")
|
||||
hAgg := firstLevel.Aggregation.Aggregation.(*es.HistogramAgg)
|
||||
require.Equal(t, hAgg.Field, "bytes")
|
||||
require.Equal(t, hAgg.Interval, 10)
|
||||
require.Equal(t, hAgg.Interval, float64(10))
|
||||
require.Equal(t, hAgg.MinDocCount, 2)
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user