response_parser_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package mqe
  2. import (
  3. "testing"
  4. "net/http"
  5. "strings"
  6. "io/ioutil"
  7. "github.com/grafana/grafana/pkg/components/null"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. var (
  11. testJson string
  12. )
  13. func TestMQEResponseParser(t *testing.T) {
  14. Convey("MQE response parser", t, func() {
  15. parser := NewResponseParser()
  16. Convey("Can parse response", func() {
  17. queryRef := QueryToSend{
  18. QueryRef: &Query{
  19. AddClusterToAlias: true,
  20. AddHostToAlias: true,
  21. },
  22. Metric: Metric{Alias: ""},
  23. }
  24. response := &http.Response{
  25. StatusCode: 200,
  26. Body: ioutil.NopCloser(strings.NewReader(testJson)),
  27. }
  28. res, err := parser.Parse(response, queryRef)
  29. So(err, ShouldBeNil)
  30. So(len(res), ShouldEqual, 2)
  31. So(len(res[0].Points), ShouldEqual, 14)
  32. So(res[0].Name, ShouldEqual, "demoapp staples-lab-1 os.disk.sda3.weighted_io_time")
  33. startTime := 1479287280000
  34. for i := 0; i < 11; i++ {
  35. So(res[0].Points[i][0].Float64, ShouldEqual, i+1)
  36. So(res[0].Points[i][1].Float64, ShouldEqual, startTime+(i*30000))
  37. }
  38. })
  39. Convey("Can format legend", func() {
  40. mqeSerie := MQESerie{
  41. Tagset: map[string]string{
  42. "cluster": "demoapp",
  43. "host": "staples-lab-1",
  44. },
  45. Values: []null.Float{null.NewFloat(3, true)},
  46. }
  47. Convey("with empty alias", func() {
  48. serie := MQEResponseSerie{Name: "os.disk.sda3.weighted_io_time"}
  49. queryRef := QueryToSend{
  50. QueryRef: &Query{
  51. AddClusterToAlias: true,
  52. AddHostToAlias: true,
  53. },
  54. Metric: Metric{Alias: ""},
  55. }
  56. legend := parser.formatLegend(serie, mqeSerie, queryRef)
  57. So(legend, ShouldEqual, "demoapp staples-lab-1 os.disk.sda3.weighted_io_time")
  58. })
  59. Convey("with index alias (ex $2 $3)", func() {
  60. serie := MQEResponseSerie{Name: "os.disk.sda3.weighted_io_time"}
  61. queryRef := QueryToSend{
  62. QueryRef: &Query{
  63. AddClusterToAlias: true,
  64. AddHostToAlias: true,
  65. },
  66. Metric: Metric{Alias: "$2 $3", Metric: "os.disk.sda3.weighted_io_time"},
  67. }
  68. legend := parser.formatLegend(serie, mqeSerie, queryRef)
  69. So(legend, ShouldEqual, "demoapp staples-lab-1 disk.sda3")
  70. })
  71. Convey("with wildcard alias", func() {
  72. serie := MQEResponseSerie{Name: "os.disk.sda3.weighted_io_time", Query: "os.disk.*"}
  73. queryRef := QueryToSend{
  74. QueryRef: &Query{
  75. AddClusterToAlias: true,
  76. AddHostToAlias: true,
  77. },
  78. RawQuery: "os.disk.sda3.weighted_io_time",
  79. Metric: Metric{Alias: "*", Metric: "os.disk.*.weighted_io_time"},
  80. }
  81. legend := parser.formatLegend(serie, mqeSerie, queryRef)
  82. So(legend, ShouldEqual, "demoapp staples-lab-1 sda3")
  83. })
  84. })
  85. })
  86. }
  87. func init() {
  88. testJson = `{
  89. "success": true,
  90. "name": "select",
  91. "body": [
  92. {
  93. "query": "os.disk.sda3.weighted_io_time",
  94. "name": "os.disk.sda3.weighted_io_time",
  95. "type": "series",
  96. "series": [
  97. {
  98. "tagset": {
  99. "cluster": "demoapp",
  100. "host": "staples-lab-1"
  101. },
  102. "values": [1,2,3,4,5,6,7,8,9,10,11, null, null, null]
  103. },
  104. {
  105. "tagset": {
  106. "cluster": "demoapp",
  107. "host": "staples-lab-2"
  108. },
  109. "values": [11,10,9,8,7,6,5,4,3,2,1]
  110. }
  111. ],
  112. "timerange": {
  113. "start": 1479287280000,
  114. "end": 1479287580000,
  115. "resolution": 30000
  116. }
  117. }
  118. ],
  119. "metadata": {
  120. "description": {
  121. "cluster": [
  122. "demoapp"
  123. ],
  124. "host": [
  125. "staples-lab-1",
  126. "staples-lab-2"
  127. ]
  128. },
  129. "notes": null,
  130. "profile": [
  131. {
  132. "name": "Parsing Query",
  133. "start": "2016-11-16T04:16:21.874354721-05:00",
  134. "finish": "2016-11-16T04:16:21.874762291-05:00"
  135. },
  136. {
  137. "name": "Cassandra GetAllTags",
  138. "start": "2016-11-16T04:16:21.874907171-05:00",
  139. "finish": "2016-11-16T04:16:21.876401922-05:00"
  140. },
  141. {
  142. "name": "CachedMetricMetadataAPI_GetAllTags_Expired",
  143. "start": "2016-11-16T04:16:21.874904751-05:00",
  144. "finish": "2016-11-16T04:16:21.876407852-05:00"
  145. },
  146. {
  147. "name": "CachedMetricMetadataAPI_GetAllTags",
  148. "start": "2016-11-16T04:16:21.874899491-05:00",
  149. "finish": "2016-11-16T04:16:21.876410382-05:00"
  150. },
  151. {
  152. "name": "Blueflood FetchSingleTimeseries Resolution",
  153. "description": "os.disk.sda3.weighted_io_time [app=demoapp,host=staples-lab-1]\n at 30s",
  154. "start": "2016-11-16T04:16:21.876623312-05:00",
  155. "finish": "2016-11-16T04:16:21.881763444-05:00"
  156. },
  157. {
  158. "name": "Blueflood FetchSingleTimeseries Resolution",
  159. "description": "os.disk.sda3.weighted_io_time [app=demoapp,host=staples-lab-2]\n at 30s",
  160. "start": "2016-11-16T04:16:21.876642682-05:00",
  161. "finish": "2016-11-16T04:16:21.881895914-05:00"
  162. },
  163. {
  164. "name": "Blueflood FetchMultipleTimeseries",
  165. "start": "2016-11-16T04:16:21.876418022-05:00",
  166. "finish": "2016-11-16T04:16:21.881921474-05:00"
  167. }
  168. ]
  169. }
  170. }
  171. `
  172. }