Elasticsearch: Fix processing of duplicated metric types and field (#66973)

* Elasticsearch: Fix processing of duplicated metric types and field

* Fix lint

* Fix linting
This commit is contained in:
Ivana Huckova 2023-04-24 17:26:04 +02:00 committed by GitHub
parent 3c655821e8
commit e2e243d1b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 2 deletions

View File

@ -1255,13 +1255,24 @@ func addOtherMetricsToFields(fields *[]*data.Field, bucket *simplejson.Json, met
otherMetrics := make([]*MetricAgg, 0)
for _, m := range target.Metrics {
if m.Type == metric.Type {
// To other metrics we add metric of the same type that are not the current metric
if m.ID != metric.ID && m.Type == metric.Type {
otherMetrics = append(otherMetrics, m)
}
}
if len(otherMetrics) > 1 {
if len(otherMetrics) > 0 {
metricName += " " + metric.Field
// We check if we have metric with the same type and same field name
// If so, append metric.ID to the metric name
for _, m := range otherMetrics {
if m.Field == metric.Field {
metricName += " " + metric.ID
break
}
}
if metric.Type == "bucket_script" {
// Use the formula in the column name
metricName = metric.Settings.Get("script").MustString("")

View File

@ -124,6 +124,54 @@ func TestResponseParser(t *testing.T) {
assert.Equal(t, frame.Fields[1].Config.DisplayNameFromDS, "Average value")
})
t.Run("Query with duplicated avg metric creates unique field name", func(t *testing.T) {
targets := map[string]string{
"A": `{
"metrics": [{"type": "avg", "field": "value", "id": "1" }, {"type": "avg", "field": "value", "id": "4" }],
"bucketAggs": [{ "type": "terms", "field": "label", "id": "3" }]
}`,
}
response := `{
"responses": [
{
"aggregations": {
"3": {
"buckets": [
{
"1": { "value": 88 },
"4": { "value": 88 },
"doc_count": 10,
"key": "val1"
},
{
"1": { "value": 99 },
"4": { "value": 99 },
"doc_count": 15,
"key": "val2"
}
]
}
}
}
]
}`
result, err := parseTestResponse(targets, response)
require.NoError(t, err)
require.Len(t, result.Responses, 1)
queryRes := result.Responses["A"]
require.NotNil(t, queryRes)
dataframes := queryRes.Frames
require.NoError(t, err)
require.Len(t, dataframes, 1)
frame := dataframes[0]
require.Len(t, frame.Fields, 3)
require.Equal(t, frame.Fields[0].Name, "label")
require.Equal(t, frame.Fields[1].Name, "Average value 1")
require.Equal(t, frame.Fields[2].Name, "Average value 4")
})
t.Run("Single group by query one metric", func(t *testing.T) {
targets := map[string]string{
"A": `{