response_parser_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package elasticsearch
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/grafana/grafana/pkg/tsdb"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func testElasticsearchResponse(body string, target Query) *tsdb.QueryResult {
  9. var responses Responses
  10. err := json.Unmarshal([]byte(body), &responses)
  11. So(err, ShouldBeNil)
  12. responseParser := ElasticsearchResponseParser{responses.Responses, []*Query{&target}}
  13. return responseParser.getTimeSeries()
  14. }
  15. func TestElasticSearchResponseParser(t *testing.T) {
  16. Convey("Elasticsearch Response query testing", t, func() {
  17. Convey("Build test average metric with moving average", func() {
  18. responses := `{
  19. "responses": [
  20. {
  21. "took": 1,
  22. "timed_out": false,
  23. "_shards": {
  24. "total": 5,
  25. "successful": 5,
  26. "skipped": 0,
  27. "failed": 0
  28. },
  29. "hits": {
  30. "total": 4500,
  31. "max_score": 0,
  32. "hits": []
  33. },
  34. "aggregations": {
  35. "2": {
  36. "buckets": [
  37. {
  38. "1": {
  39. "value": null
  40. },
  41. "key_as_string": "1522205880000",
  42. "key": 1522205880000,
  43. "doc_count": 0
  44. },
  45. {
  46. "1": {
  47. "value": 10
  48. },
  49. "key_as_string": "1522205940000",
  50. "key": 1522205940000,
  51. "doc_count": 300
  52. },
  53. {
  54. "1": {
  55. "value": 10
  56. },
  57. "3": {
  58. "value": 20
  59. },
  60. "key_as_string": "1522206000000",
  61. "key": 1522206000000,
  62. "doc_count": 300
  63. },
  64. {
  65. "1": {
  66. "value": 10
  67. },
  68. "3": {
  69. "value": 20
  70. },
  71. "key_as_string": "1522206060000",
  72. "key": 1522206060000,
  73. "doc_count": 300
  74. }
  75. ]
  76. }
  77. },
  78. "status": 200
  79. }
  80. ]
  81. }
  82. `
  83. res := testElasticsearchResponse(responses, avgWithMovingAvg)
  84. So(len(res.Series), ShouldEqual, 2)
  85. So(res.Series[0].Name, ShouldEqual, "Average value")
  86. So(len(res.Series[0].Points), ShouldEqual, 4)
  87. for i, p := range res.Series[0].Points {
  88. if i == 0 {
  89. So(p[0].Valid, ShouldBeFalse)
  90. } else {
  91. So(p[0].Float64, ShouldEqual, 10)
  92. }
  93. So(p[1].Float64, ShouldEqual, 1522205880000+60000*i)
  94. }
  95. So(res.Series[1].Name, ShouldEqual, "Moving Average Average 1")
  96. So(len(res.Series[1].Points), ShouldEqual, 2)
  97. for _, p := range res.Series[1].Points {
  98. So(p[0].Float64, ShouldEqual, 20)
  99. }
  100. })
  101. })
  102. }