| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- package elasticsearch
- import (
- "encoding/json"
- "fmt"
- "github.com/grafana/grafana/pkg/components/simplejson"
- "github.com/grafana/grafana/pkg/models"
- "github.com/grafana/grafana/pkg/tsdb"
- . "github.com/smartystreets/goconvey/convey"
- "reflect"
- "strconv"
- "strings"
- "testing"
- )
- func testElasticSearchResponse(query Query, expectedElasticSearchRequestJSON string) {
- var queryExpectedJSONInterface, queryJSONInterface interface{}
- jsonDate, _ := simplejson.NewJson([]byte(`{"esVersion":2}`))
- dsInfo := &models.DataSource{
- Database: "grafana-test",
- JsonData: jsonDate,
- }
- testTimeRange := tsdb.NewTimeRange("5m", "now")
- s, err := query.Build(&tsdb.TsdbQuery{TimeRange: testTimeRange}, dsInfo)
- So(err, ShouldBeNil)
- queryJSON := strings.Split(s, "\n")[1]
- err = json.Unmarshal([]byte(queryJSON), &queryJSONInterface)
- So(err, ShouldBeNil)
- expectedElasticSearchRequestJSON = strings.Replace(
- expectedElasticSearchRequestJSON,
- "<FROM_TIMESTAMP>",
- strconv.FormatInt(testTimeRange.GetFromAsMsEpoch(), 10),
- -1,
- )
- expectedElasticSearchRequestJSON = strings.Replace(
- expectedElasticSearchRequestJSON,
- "<TO_TIMESTAMP>",
- strconv.FormatInt(testTimeRange.GetToAsMsEpoch(), 10),
- -1,
- )
- err = json.Unmarshal([]byte(expectedElasticSearchRequestJSON), &queryExpectedJSONInterface)
- So(err, ShouldBeNil)
- result := reflect.DeepEqual(queryExpectedJSONInterface, queryJSONInterface)
- if !result {
- fmt.Printf("ERROR: %s \n != \n %s", expectedElasticSearchRequestJSON, queryJSON)
- }
- So(result, ShouldBeTrue)
- }
- func TestElasticSearchQueryBuilder(t *testing.T) {
- Convey("Elasticsearch QueryBuilder query testing", t, func() {
- Convey("Build test average metric with moving average", func() {
- var expectedElasticsearchQueryJSON = `
- {
- "size": 0,
- "query": {
- "bool": {
- "filter": [
- {
- "range": {
- "timestamp": {
- "gte": "<FROM_TIMESTAMP>",
- "lte": "<TO_TIMESTAMP>",
- "format": "epoch_millis"
- }
- }
- },
- {
- "query_string": {
- "analyze_wildcard": true,
- "query": "(test:query) AND (name:sample)"
- }
- }
- ]
- }
- },
- "aggs": {
- "2": {
- "date_histogram": {
- "interval": "200ms",
- "field": "timestamp",
- "min_doc_count": 0,
- "extended_bounds": {
- "min": "<FROM_TIMESTAMP>",
- "max": "<TO_TIMESTAMP>"
- },
- "format": "epoch_millis"
- },
- "aggs": {
- "1": {
- "avg": {
- "field": "value",
- "script": {
- "inline": "_value * 2"
- }
- }
- },
- "3": {
- "moving_avg": {
- "buckets_path": "1",
- "window": 5,
- "model": "simple",
- "minimize": false
- }
- }
- }
- }
- }
- }`
- testElasticSearchResponse(avgWithMovingAvg, expectedElasticsearchQueryJSON)
- })
- Convey("Test Wildcards and Quotes", func() {
- expectedElasticsearchQueryJSON := `
- {
- "size": 0,
- "query": {
- "bool": {
- "filter": [
- {
- "range": {
- "timestamp": {
- "gte": "<FROM_TIMESTAMP>",
- "lte": "<TO_TIMESTAMP>",
- "format": "epoch_millis"
- }
- }
- },
- {
- "query_string": {
- "analyze_wildcard": true,
- "query": "scope:$location.leagueconnect.api AND name:*CreateRegistration AND name:\"*.201-responses.rate\""
- }
- }
- ]
- }
- },
- "aggs": {
- "2": {
- "aggs": {
- "1": {
- "sum": {
- "field": "value"
- }
- }
- },
- "date_histogram": {
- "extended_bounds": {
- "max": "<TO_TIMESTAMP>",
- "min": "<FROM_TIMESTAMP>"
- },
- "field": "timestamp",
- "format": "epoch_millis",
- "min_doc_count": 0
- }
- }
- }
- }`
- testElasticSearchResponse(wildcardsAndQuotes, expectedElasticsearchQueryJSON)
- })
- Convey("Test Term Aggregates", func() {
- expectedElasticsearchQueryJSON := `
- {
- "size": 0,
- "query": {
- "bool": {
- "filter": [
- {
- "range": {
- "timestamp": {
- "gte": "<FROM_TIMESTAMP>",
- "lte": "<TO_TIMESTAMP>",
- "format": "epoch_millis"
- }
- }
- },
- {
- "query_string": {
- "analyze_wildcard": true,
- "query": "(scope:*.hmp.metricsd) AND (name_raw:builtin.general.*_instance_count)"
- }
- }
- ]
- }
- },
- "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":"200ms","min_doc_count":0}}},"terms":{"field":"name_raw","order":{"_term":"desc"},"size":10}}}
- }`
- testElasticSearchResponse(termAggs, expectedElasticsearchQueryJSON)
- })
- Convey("Test Filters Aggregates", func() {
- 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(filtersAggs, expectedElasticsearchQueryJSON)
- })
- })
- }
|