model_parser_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package mqe
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. "github.com/grafana/grafana/pkg/models"
  6. "github.com/grafana/grafana/pkg/tsdb"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestMQEQueryParser(t *testing.T) {
  10. Convey("MQE query parser", t, func() {
  11. parser := &QueryParser{}
  12. dsInfo := &models.DataSource{JsonData: simplejson.New()}
  13. queryContext := &tsdb.TsdbQuery{}
  14. Convey("can parse simple mqe model", func() {
  15. json := `
  16. {
  17. "cluster": [],
  18. "hosts": [
  19. "staples-lab-1"
  20. ],
  21. "metrics": [
  22. {
  23. "metric": "os.cpu.all*"
  24. }
  25. ],
  26. "rawQuery": "",
  27. "refId": "A"
  28. }
  29. `
  30. modelJson, err := simplejson.NewJson([]byte(json))
  31. So(err, ShouldBeNil)
  32. query, err := parser.Parse(modelJson, dsInfo, queryContext)
  33. So(err, ShouldBeNil)
  34. So(query.UseRawQuery, ShouldBeFalse)
  35. So(len(query.Cluster), ShouldEqual, 0)
  36. So(query.Hosts[0], ShouldEqual, "staples-lab-1")
  37. So(query.Metrics[0].Metric, ShouldEqual, "os.cpu.all*")
  38. })
  39. Convey("can parse multi serie mqe model", func() {
  40. json := `
  41. {
  42. "cluster": [
  43. "demoapp"
  44. ],
  45. "hosts": [
  46. "staples-lab-1"
  47. ],
  48. "metrics": [
  49. {
  50. "metric": "os.cpu.all.active_percentage"
  51. },
  52. {
  53. "metric": "os.disk.sda.io_time"
  54. }
  55. ],
  56. "functionList": [
  57. {
  58. "func": "aggregate.min"
  59. },
  60. {
  61. "func": "aggregate.max"
  62. }
  63. ],
  64. "rawQuery": "",
  65. "refId": "A",
  66. "addClusterToAlias": true,
  67. "addHostToAlias": true
  68. }
  69. `
  70. modelJson, err := simplejson.NewJson([]byte(json))
  71. So(err, ShouldBeNil)
  72. query, err := parser.Parse(modelJson, dsInfo, queryContext)
  73. So(err, ShouldBeNil)
  74. So(query.UseRawQuery, ShouldBeFalse)
  75. So(query.Cluster[0], ShouldEqual, "demoapp")
  76. So(query.Metrics[0].Metric, ShouldEqual, "os.cpu.all.active_percentage")
  77. So(query.Metrics[1].Metric, ShouldEqual, "os.disk.sda.io_time")
  78. So(query.FunctionList[0].Func, ShouldEqual, "aggregate.min")
  79. So(query.FunctionList[1].Func, ShouldEqual, "aggregate.max")
  80. })
  81. Convey("can parse raw query", func() {
  82. json := `
  83. {
  84. "addClusterToAlias": true,
  85. "addHostToAlias": true,
  86. "cluster": [],
  87. "hosts": [
  88. "staples-lab-1"
  89. ],
  90. "metrics": [
  91. {
  92. "alias": "cpu active",
  93. "metric": "os.cpu.all.active_percentage"
  94. },
  95. {
  96. "alias": "disk sda time",
  97. "metric": "os.disk.sda.io_time"
  98. }
  99. ],
  100. "rawQuery": true,
  101. "query": "raw-query",
  102. "refId": "A"
  103. }
  104. `
  105. modelJson, err := simplejson.NewJson([]byte(json))
  106. So(err, ShouldBeNil)
  107. query, err := parser.Parse(modelJson, dsInfo, queryContext)
  108. So(err, ShouldBeNil)
  109. So(query.UseRawQuery, ShouldBeTrue)
  110. So(query.RawQuery, ShouldEqual, "raw-query")
  111. So(query.AddClusterToAlias, ShouldBeTrue)
  112. So(query.AddHostToAlias, ShouldBeTrue)
  113. })
  114. })
  115. }