mirror of
https://github.com/grafana/grafana.git
synced 2026-08-14 07:04:57 -05:00
Elasticsearch: Fix ignoring precision when running queries trough backend
This commit is contained in:
@@ -269,7 +269,7 @@ func addFiltersAgg(aggBuilder es.AggBuilder, bucketAgg *BucketAgg) es.AggBuilder
|
|||||||
|
|
||||||
func addGeoHashGridAgg(aggBuilder es.AggBuilder, bucketAgg *BucketAgg) es.AggBuilder {
|
func addGeoHashGridAgg(aggBuilder es.AggBuilder, bucketAgg *BucketAgg) es.AggBuilder {
|
||||||
aggBuilder.GeoHashGrid(bucketAgg.ID, bucketAgg.Field, func(a *es.GeoHashGridAggregation, b es.AggBuilder) {
|
aggBuilder.GeoHashGrid(bucketAgg.ID, bucketAgg.Field, func(a *es.GeoHashGridAggregation, b es.AggBuilder) {
|
||||||
a.Precision = bucketAgg.Settings.Get("precision").MustInt(3)
|
a.Precision = stringToIntWithDefaultValue(bucketAgg.Settings.Get("precision").MustString(), 3)
|
||||||
aggBuilder = b
|
aggBuilder = b
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -612,7 +612,7 @@ func TestExecuteElasticsearchDataQuery(t *testing.T) {
|
|||||||
"id": "3",
|
"id": "3",
|
||||||
"type": "geohash_grid",
|
"type": "geohash_grid",
|
||||||
"field": "@location",
|
"field": "@location",
|
||||||
"settings": { "precision": 3 }
|
"settings": { "precision": "6" }
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metrics": [{"type": "count", "id": "1" }]
|
"metrics": [{"type": "count", "id": "1" }]
|
||||||
@@ -625,6 +625,56 @@ func TestExecuteElasticsearchDataQuery(t *testing.T) {
|
|||||||
require.Equal(t, firstLevel.Aggregation.Type, "geohash_grid")
|
require.Equal(t, firstLevel.Aggregation.Type, "geohash_grid")
|
||||||
ghGridAgg := firstLevel.Aggregation.Aggregation.(*es.GeoHashGridAggregation)
|
ghGridAgg := firstLevel.Aggregation.Aggregation.(*es.GeoHashGridAggregation)
|
||||||
require.Equal(t, ghGridAgg.Field, "@location")
|
require.Equal(t, ghGridAgg.Field, "@location")
|
||||||
|
require.Equal(t, ghGridAgg.Precision, 6)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("With geo hash grid agg with invalid int precision", func(t *testing.T) {
|
||||||
|
c := newFakeClient()
|
||||||
|
_, err := executeElasticsearchDataQuery(c, `{
|
||||||
|
"bucketAggs": [
|
||||||
|
{
|
||||||
|
"id": "3",
|
||||||
|
"type": "geohash_grid",
|
||||||
|
"field": "@location",
|
||||||
|
"settings": { "precision": 7 }
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metrics": [{"type": "count", "id": "1" }]
|
||||||
|
}`, from, to)
|
||||||
|
require.NoError(t, err)
|
||||||
|
sr := c.multisearchRequests[0].Requests[0]
|
||||||
|
|
||||||
|
firstLevel := sr.Aggs[0]
|
||||||
|
require.Equal(t, firstLevel.Key, "3")
|
||||||
|
require.Equal(t, firstLevel.Aggregation.Type, "geohash_grid")
|
||||||
|
ghGridAgg := firstLevel.Aggregation.Aggregation.(*es.GeoHashGridAggregation)
|
||||||
|
require.Equal(t, ghGridAgg.Field, "@location")
|
||||||
|
// It should default to 3
|
||||||
|
require.Equal(t, ghGridAgg.Precision, 3)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("With geo hash grid agg with no precision", func(t *testing.T) {
|
||||||
|
c := newFakeClient()
|
||||||
|
_, err := executeElasticsearchDataQuery(c, `{
|
||||||
|
"bucketAggs": [
|
||||||
|
{
|
||||||
|
"id": "3",
|
||||||
|
"type": "geohash_grid",
|
||||||
|
"field": "@location",
|
||||||
|
"settings": { "precision": 7 }
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metrics": [{"type": "count", "id": "1" }]
|
||||||
|
}`, from, to)
|
||||||
|
require.NoError(t, err)
|
||||||
|
sr := c.multisearchRequests[0].Requests[0]
|
||||||
|
|
||||||
|
firstLevel := sr.Aggs[0]
|
||||||
|
require.Equal(t, firstLevel.Key, "3")
|
||||||
|
require.Equal(t, firstLevel.Aggregation.Type, "geohash_grid")
|
||||||
|
ghGridAgg := firstLevel.Aggregation.Aggregation.(*es.GeoHashGridAggregation)
|
||||||
|
require.Equal(t, ghGridAgg.Field, "@location")
|
||||||
|
// It should default to 3
|
||||||
require.Equal(t, ghGridAgg.Precision, 3)
|
require.Equal(t, ghGridAgg.Precision, 3)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user