mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
fix a terms bug and add test
This commit is contained in:
parent
a21afbe9a6
commit
4042e4b225
@ -41,7 +41,7 @@ type DateHistogramAgg struct {
|
||||
}
|
||||
|
||||
type FiltersAgg struct {
|
||||
Filter map[string]interface{} `json:"filter"`
|
||||
Filters map[string]interface{} `json:"filters"`
|
||||
}
|
||||
|
||||
type TermsAggSetting struct {
|
||||
|
@ -193,15 +193,17 @@ func (q *Query) getHistogramAgg(model *simplejson.Json) *HistogramAgg {
|
||||
|
||||
func (q *Query) getFilters(model *simplejson.Json) *FiltersAgg {
|
||||
agg := &FiltersAgg{}
|
||||
agg.Filters = map[string]interface{}{}
|
||||
settings := simplejson.NewFromAny(model.Get("settings").Interface())
|
||||
for filter := range settings.Get("filters").MustArray() {
|
||||
|
||||
for _, filter := range settings.Get("filters").MustArray() {
|
||||
filterJson := simplejson.NewFromAny(filter)
|
||||
query := filterJson.Get("query").MustString("")
|
||||
label := filterJson.Get("label").MustString("")
|
||||
if label == "" {
|
||||
label = query
|
||||
}
|
||||
agg.Filter[label] = newQueryStringFilter(true, query)
|
||||
agg.Filters[label] = newQueryStringFilter(true, query)
|
||||
}
|
||||
return agg
|
||||
}
|
||||
|
@ -325,6 +325,103 @@ func TestElasticSearchQueryBuilder(t *testing.T) {
|
||||
"aggs": {"4":{"aggs":{"2":{"aggs":{"1":{"sum":{"field":"value"}}},"date_histogram":{"extended_bounds":{"max":"<TO_TIMESTAMP>","min":"<FROM_TIMESTAMP>"},"field":"timestamp","format":"epoch_millis","interval":"1m","min_doc_count":0}}},"terms":{"field":"name_raw","order":{"_term":"desc"},"size":10}}}
|
||||
}`
|
||||
|
||||
testElasticSearchResponse(testElasticsearchModelRequestJSON, expectedElasticsearchQueryJSON)
|
||||
})
|
||||
Convey("Test Filters Aggregates", func() {
|
||||
testElasticsearchModelRequestJSON := `
|
||||
{
|
||||
"bucketAggs": [
|
||||
{
|
||||
"id": "3",
|
||||
"settings": {
|
||||
"filters": [{
|
||||
"label": "hello",
|
||||
"query": "host:\"67.65.185.232\""
|
||||
}]
|
||||
},
|
||||
"type": "filters"
|
||||
},
|
||||
{
|
||||
"field": "time",
|
||||
"id": "2",
|
||||
"settings": {
|
||||
"interval": "auto",
|
||||
"min_doc_count": 0,
|
||||
"trimEdges": 0
|
||||
},
|
||||
"type": "date_histogram"
|
||||
}
|
||||
],
|
||||
"metrics": [
|
||||
{
|
||||
"pipelineAgg": "select metric",
|
||||
"field": "bytesSent",
|
||||
"id": "1",
|
||||
"meta": {},
|
||||
"settings": {},
|
||||
"type": "count"
|
||||
}
|
||||
],
|
||||
"query": "*",
|
||||
"refId": "A",
|
||||
"timeField": "time"
|
||||
}`
|
||||
|
||||
expectedElasticsearchQueryJSON := `{
|
||||
"size": 0,
|
||||
"query": {
|
||||
"bool": {
|
||||
"filter": [
|
||||
{
|
||||
"range": {
|
||||
"time": {
|
||||
"gte": "<FROM_TIMESTAMP>",
|
||||
"lte": "<TO_TIMESTAMP>",
|
||||
"format": "epoch_millis"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"query_string": {
|
||||
"analyze_wildcard": true,
|
||||
"query": "*"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"aggs": {
|
||||
"3": {
|
||||
"filters": {
|
||||
"filters": {
|
||||
"hello": {
|
||||
"query_string": {
|
||||
"query": "host:\"67.65.185.232\"",
|
||||
"analyze_wildcard": true
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"aggs": {
|
||||
"2": {
|
||||
"date_histogram": {
|
||||
"interval": "200ms",
|
||||
"field": "time",
|
||||
"min_doc_count": 0,
|
||||
"extended_bounds": {
|
||||
"min": "<FROM_TIMESTAMP>",
|
||||
"max": "<TO_TIMESTAMP>"
|
||||
},
|
||||
"format": "epoch_millis"
|
||||
},
|
||||
"aggs": {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
testElasticSearchResponse(testElasticsearchModelRequestJSON, expectedElasticsearchQueryJSON)
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user