Elasticsearch: Fix top metrics with terms in backend (#65063)

* Elasticsearch: Fix top metrics with terms in backend

* Refactor to remove cyclomatic complexity
This commit is contained in:
Ivana Huckova
2023-03-22 09:36:43 +01:00
committed by GitHub
parent 895d4cc60b
commit 0740d4e911
2 changed files with 183 additions and 74 deletions
+71 -1
View File
@@ -1484,7 +1484,7 @@ func TestResponseParser(t *testing.T) {
})
})
t.Run("With top_metrics", func(t *testing.T) {
t.Run("With top_metrics and date_histogram agg", func(t *testing.T) {
targets := map[string]string{
"A": `{
"metrics": [
@@ -1571,6 +1571,76 @@ func TestResponseParser(t *testing.T) {
v, _ = frame.FloatAt(1, 1)
assert.Equal(t, 2., v)
})
t.Run("With top_metrics and terms agg", func(t *testing.T) {
targets := map[string]string{
"A": `{
"metrics": [
{
"type": "top_metrics",
"settings": {
"order": "desc",
"orderBy": "@timestamp",
"metrics": ["@value", "@anotherValue"]
},
"id": "1"
}
],
"bucketAggs": [{ "type": "terms", "field": "id", "id": "3" }]
}`,
}
response := `{
"responses": [{
"aggregations": {
"3": {
"buckets": [
{
"key": "id1",
"1": {
"top": [
{ "sort": [10], "metrics": { "@value": 10, "@anotherValue": 2 } }
]
}
},
{
"key": "id2",
"1": {
"top": [
{ "sort": [5], "metrics": { "@value": 5, "@anotherValue": 2 } }
]
}
}
]
}
}
}]
}`
result, err := parseTestResponse(targets, response)
assert.Nil(t, err)
assert.Len(t, result.Responses, 1)
frames := result.Responses["A"].Frames
require.Len(t, frames, 1)
requireFrameLength(t, frames[0], 2)
require.Len(t, frames[0].Fields, 3)
f1 := frames[0].Fields[0]
f2 := frames[0].Fields[1]
f3 := frames[0].Fields[2]
require.Equal(t, "id", f1.Name)
require.Equal(t, "Top Metrics @value", f2.Name)
require.Equal(t, "Top Metrics @anotherValue", f3.Name)
requireStringAt(t, "id1", f1, 0)
requireStringAt(t, "id2", f1, 1)
requireFloatAt(t, 10, f2, 0)
requireFloatAt(t, 5, f2, 1)
requireFloatAt(t, 2, f3, 0)
requireFloatAt(t, 2, f3, 1)
})
}
func parseTestResponse(tsdbQueries map[string]string, responseBody string) (*backend.QueryDataResponse, error) {