mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
@@ -1255,13 +1255,24 @@ func addOtherMetricsToFields(fields *[]*data.Field, bucket *simplejson.Json, met
|
|||||||
otherMetrics := make([]*MetricAgg, 0)
|
otherMetrics := make([]*MetricAgg, 0)
|
||||||
|
|
||||||
for _, m := range target.Metrics {
|
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)
|
otherMetrics = append(otherMetrics, m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(otherMetrics) > 1 {
|
if len(otherMetrics) > 0 {
|
||||||
metricName += " " + metric.Field
|
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" {
|
if metric.Type == "bucket_script" {
|
||||||
// Use the formula in the column name
|
// Use the formula in the column name
|
||||||
metricName = metric.Settings.Get("script").MustString("")
|
metricName = metric.Settings.Get("script").MustString("")
|
||||||
|
|||||||
@@ -124,6 +124,54 @@ func TestResponseParser(t *testing.T) {
|
|||||||
assert.Equal(t, frame.Fields[1].Config.DisplayNameFromDS, "Average value")
|
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) {
|
t.Run("Single group by query one metric", func(t *testing.T) {
|
||||||
targets := map[string]string{
|
targets := map[string]string{
|
||||||
"A": `{
|
"A": `{
|
||||||
|
|||||||
Reference in New Issue
Block a user