scenarios.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package testdata
  2. import (
  3. "encoding/json"
  4. "math/rand"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "github.com/grafana/grafana/pkg/components/null"
  9. "github.com/grafana/grafana/pkg/log"
  10. "github.com/grafana/grafana/pkg/tsdb"
  11. )
  12. type ScenarioHandler func(query *tsdb.Query, context *tsdb.TsdbQuery) *tsdb.QueryResult
  13. type Scenario struct {
  14. Id string `json:"id"`
  15. Name string `json:"name"`
  16. StringInput string `json:"stringOption"`
  17. Description string `json:"description"`
  18. Handler ScenarioHandler `json:"-"`
  19. }
  20. var ScenarioRegistry map[string]*Scenario
  21. func init() {
  22. ScenarioRegistry = make(map[string]*Scenario)
  23. logger := log.New("tsdb.testdata")
  24. logger.Debug("Initializing TestData Scenario")
  25. registerScenario(&Scenario{
  26. Id: "exponential_heatmap_bucket_data",
  27. Name: "Exponential heatmap bucket data",
  28. Handler: func(query *tsdb.Query, context *tsdb.TsdbQuery) *tsdb.QueryResult {
  29. to := context.TimeRange.GetToAsMsEpoch()
  30. var series []*tsdb.TimeSeries
  31. start := 1
  32. factor := 2
  33. for i := 0; i < 10; i++ {
  34. timeWalkerMs := context.TimeRange.GetFromAsMsEpoch()
  35. serie := &tsdb.TimeSeries{Name: strconv.Itoa(start)}
  36. start *= factor
  37. points := make(tsdb.TimeSeriesPoints, 0)
  38. for j := int64(0); j < 100 && timeWalkerMs < to; j++ {
  39. v := float64(rand.Int63n(100))
  40. points = append(points, tsdb.NewTimePoint(null.FloatFrom(v), float64(timeWalkerMs)))
  41. timeWalkerMs += query.IntervalMs * 50
  42. }
  43. serie.Points = points
  44. series = append(series, serie)
  45. }
  46. queryRes := tsdb.NewQueryResult()
  47. queryRes.Series = append(queryRes.Series, series...)
  48. return queryRes
  49. },
  50. })
  51. registerScenario(&Scenario{
  52. Id: "linear_heatmap_bucket_data",
  53. Name: "Linear heatmap bucket data",
  54. Handler: func(query *tsdb.Query, context *tsdb.TsdbQuery) *tsdb.QueryResult {
  55. to := context.TimeRange.GetToAsMsEpoch()
  56. var series []*tsdb.TimeSeries
  57. for i := 0; i < 10; i++ {
  58. timeWalkerMs := context.TimeRange.GetFromAsMsEpoch()
  59. serie := &tsdb.TimeSeries{Name: strconv.Itoa(i * 10)}
  60. points := make(tsdb.TimeSeriesPoints, 0)
  61. for j := int64(0); j < 100 && timeWalkerMs < to; j++ {
  62. v := float64(rand.Int63n(100))
  63. points = append(points, tsdb.NewTimePoint(null.FloatFrom(v), float64(timeWalkerMs)))
  64. timeWalkerMs += query.IntervalMs * 50
  65. }
  66. serie.Points = points
  67. series = append(series, serie)
  68. }
  69. queryRes := tsdb.NewQueryResult()
  70. queryRes.Series = append(queryRes.Series, series...)
  71. return queryRes
  72. },
  73. })
  74. registerScenario(&Scenario{
  75. Id: "random_walk",
  76. Name: "Random Walk",
  77. Handler: func(query *tsdb.Query, tsdbQuery *tsdb.TsdbQuery) *tsdb.QueryResult {
  78. timeWalkerMs := tsdbQuery.TimeRange.GetFromAsMsEpoch()
  79. to := tsdbQuery.TimeRange.GetToAsMsEpoch()
  80. series := newSeriesForQuery(query)
  81. points := make(tsdb.TimeSeriesPoints, 0)
  82. walker := rand.Float64() * 100
  83. for i := int64(0); i < 10000 && timeWalkerMs < to; i++ {
  84. points = append(points, tsdb.NewTimePoint(null.FloatFrom(walker), float64(timeWalkerMs)))
  85. walker += rand.Float64() - 0.5
  86. timeWalkerMs += query.IntervalMs
  87. }
  88. series.Points = points
  89. queryRes := tsdb.NewQueryResult()
  90. queryRes.Series = append(queryRes.Series, series)
  91. return queryRes
  92. },
  93. })
  94. registerScenario(&Scenario{
  95. Id: "no_data_points",
  96. Name: "No Data Points",
  97. Handler: func(query *tsdb.Query, context *tsdb.TsdbQuery) *tsdb.QueryResult {
  98. return tsdb.NewQueryResult()
  99. },
  100. })
  101. registerScenario(&Scenario{
  102. Id: "datapoints_outside_range",
  103. Name: "Datapoints Outside Range",
  104. Handler: func(query *tsdb.Query, context *tsdb.TsdbQuery) *tsdb.QueryResult {
  105. queryRes := tsdb.NewQueryResult()
  106. series := newSeriesForQuery(query)
  107. outsideTime := context.TimeRange.MustGetFrom().Add(-1*time.Hour).Unix() * 1000
  108. series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFrom(10), float64(outsideTime)))
  109. queryRes.Series = append(queryRes.Series, series)
  110. return queryRes
  111. },
  112. })
  113. registerScenario(&Scenario{
  114. Id: "manual_entry",
  115. Name: "Manual Entry",
  116. Handler: func(query *tsdb.Query, context *tsdb.TsdbQuery) *tsdb.QueryResult {
  117. queryRes := tsdb.NewQueryResult()
  118. points := query.Model.Get("points").MustArray()
  119. series := newSeriesForQuery(query)
  120. startTime := context.TimeRange.GetFromAsMsEpoch()
  121. endTime := context.TimeRange.GetToAsMsEpoch()
  122. for _, val := range points {
  123. pointValues := val.([]interface{})
  124. var value null.Float
  125. var time int64
  126. if valueFloat, err := strconv.ParseFloat(string(pointValues[0].(json.Number)), 64); err == nil {
  127. value = null.FloatFrom(valueFloat)
  128. }
  129. if timeInt, err := strconv.ParseInt(string(pointValues[1].(json.Number)), 10, 64); err != nil {
  130. continue
  131. } else {
  132. time = timeInt
  133. }
  134. if time >= startTime && time <= endTime {
  135. series.Points = append(series.Points, tsdb.NewTimePoint(value, float64(time)))
  136. }
  137. }
  138. queryRes.Series = append(queryRes.Series, series)
  139. return queryRes
  140. },
  141. })
  142. registerScenario(&Scenario{
  143. Id: "csv_metric_values",
  144. Name: "CSV Metric Values",
  145. StringInput: "1,20,90,30,5,0",
  146. Handler: func(query *tsdb.Query, context *tsdb.TsdbQuery) *tsdb.QueryResult {
  147. queryRes := tsdb.NewQueryResult()
  148. stringInput := query.Model.Get("stringInput").MustString()
  149. stringInput = strings.Replace(stringInput, " ", "", -1)
  150. values := []null.Float{}
  151. for _, strVal := range strings.Split(stringInput, ",") {
  152. if strVal == "null" {
  153. values = append(values, null.FloatFromPtr(nil))
  154. }
  155. if val, err := strconv.ParseFloat(strVal, 64); err == nil {
  156. values = append(values, null.FloatFrom(val))
  157. }
  158. }
  159. if len(values) == 0 {
  160. return queryRes
  161. }
  162. series := newSeriesForQuery(query)
  163. startTime := context.TimeRange.GetFromAsMsEpoch()
  164. endTime := context.TimeRange.GetToAsMsEpoch()
  165. step := (endTime - startTime) / int64(len(values)-1)
  166. for _, val := range values {
  167. series.Points = append(series.Points, tsdb.TimePoint{val, null.FloatFrom(float64(startTime))})
  168. startTime += step
  169. }
  170. queryRes.Series = append(queryRes.Series, series)
  171. return queryRes
  172. },
  173. })
  174. }
  175. func registerScenario(scenario *Scenario) {
  176. ScenarioRegistry[scenario.Id] = scenario
  177. }
  178. func newSeriesForQuery(query *tsdb.Query) *tsdb.TimeSeries {
  179. alias := query.Model.Get("alias").MustString("")
  180. if alias == "" {
  181. alias = query.RefId + "-series"
  182. }
  183. return &tsdb.TimeSeries{Name: alias}
  184. }