reducer_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package conditions
  2. import (
  3. "testing"
  4. . "github.com/smartystreets/goconvey/convey"
  5. "github.com/grafana/grafana/pkg/components/null"
  6. "github.com/grafana/grafana/pkg/tsdb"
  7. )
  8. func TestSimpleReducer(t *testing.T) {
  9. Convey("Test simple reducer by calculating", t, func() {
  10. Convey("sum", func() {
  11. result := testReducer("sum", 1, 2, 3)
  12. So(result, ShouldEqual, float64(6))
  13. })
  14. Convey("min", func() {
  15. result := testReducer("min", 3, 2, 1)
  16. So(result, ShouldEqual, float64(1))
  17. })
  18. Convey("max", func() {
  19. result := testReducer("max", 1, 2, 3)
  20. So(result, ShouldEqual, float64(3))
  21. })
  22. Convey("count", func() {
  23. result := testReducer("count", 1, 2, 3000)
  24. So(result, ShouldEqual, float64(3))
  25. })
  26. Convey("last", func() {
  27. result := testReducer("last", 1, 2, 3000)
  28. So(result, ShouldEqual, float64(3000))
  29. })
  30. Convey("median odd amount of numbers", func() {
  31. result := testReducer("median", 1, 2, 3000)
  32. So(result, ShouldEqual, float64(2))
  33. })
  34. Convey("median even amount of numbers", func() {
  35. result := testReducer("median", 1, 2, 4, 3000)
  36. So(result, ShouldEqual, float64(3))
  37. })
  38. Convey("median with one values", func() {
  39. result := testReducer("median", 1)
  40. So(result, ShouldEqual, float64(1))
  41. })
  42. Convey("avg", func() {
  43. result := testReducer("avg", 1, 2, 3)
  44. So(result, ShouldEqual, float64(2))
  45. })
  46. Convey("avg with only nulls", func() {
  47. reducer := NewSimpleReducer("avg")
  48. series := &tsdb.TimeSeries{
  49. Name: "test time serie",
  50. }
  51. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFromPtr(nil), 1))
  52. So(reducer.Reduce(series).Valid, ShouldEqual, false)
  53. })
  54. Convey("count_non_null", func() {
  55. Convey("with null values and real values", func() {
  56. reducer := NewSimpleReducer("count_non_null")
  57. series := &tsdb.TimeSeries{
  58. Name: "test time serie",
  59. }
  60. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFromPtr(nil), 1))
  61. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFromPtr(nil), 2))
  62. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFrom(3), 3))
  63. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFrom(3), 4))
  64. So(reducer.Reduce(series).Valid, ShouldEqual, true)
  65. So(reducer.Reduce(series).Float64, ShouldEqual, 2)
  66. })
  67. Convey("with null values", func() {
  68. reducer := NewSimpleReducer("count_non_null")
  69. series := &tsdb.TimeSeries{
  70. Name: "test time serie",
  71. }
  72. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFromPtr(nil), 1))
  73. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFromPtr(nil), 2))
  74. So(reducer.Reduce(series).Valid, ShouldEqual, false)
  75. })
  76. })
  77. Convey("avg of number values and null values should ignore nulls", func() {
  78. reducer := NewSimpleReducer("avg")
  79. series := &tsdb.TimeSeries{
  80. Name: "test time serie",
  81. }
  82. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFrom(3), 1))
  83. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFromPtr(nil), 2))
  84. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFromPtr(nil), 3))
  85. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFrom(3), 4))
  86. So(reducer.Reduce(series).Float64, ShouldEqual, float64(3))
  87. })
  88. Convey("diff", func() {
  89. result := testReducer("diff", 30, 40)
  90. So(result, ShouldEqual, float64(10))
  91. })
  92. Convey("percent_diff", func() {
  93. result := testReducer("percent_diff", 30, 40)
  94. So(result, ShouldEqual, float64(33.33333333333333))
  95. })
  96. })
  97. }
  98. func testReducer(typ string, datapoints ...float64) float64 {
  99. reducer := NewSimpleReducer(typ)
  100. series := &tsdb.TimeSeries{
  101. Name: "test time serie",
  102. }
  103. for idx := range datapoints {
  104. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFrom(datapoints[idx]), 1234134))
  105. }
  106. return reducer.Reduce(series).Float64
  107. }