scenarios.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package testdata
  2. import (
  3. "math/rand"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "github.com/grafana/grafana/pkg/log"
  8. "github.com/grafana/grafana/pkg/tsdb"
  9. )
  10. type ScenarioHandler func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult
  11. type Scenario struct {
  12. Id string `json:"id"`
  13. Name string `json:"name"`
  14. StringInput string `json:"stringOption"`
  15. Description string `json:"description"`
  16. Handler ScenarioHandler `json:"-"`
  17. }
  18. var ScenarioRegistry map[string]*Scenario
  19. func init() {
  20. ScenarioRegistry = make(map[string]*Scenario)
  21. logger := log.New("tsdb.testdata")
  22. logger.Debug("Initializing TestData Scenario")
  23. registerScenario(&Scenario{
  24. Id: "random_walk",
  25. Name: "Random Walk",
  26. Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult {
  27. timeWalkerMs := context.TimeRange.GetFromAsMsEpoch()
  28. to := context.TimeRange.GetToAsMsEpoch()
  29. series := newSeriesForQuery(query)
  30. points := make(tsdb.TimeSeriesPoints, 0)
  31. walker := rand.Float64() * 100
  32. for i := int64(0); i < 10000 && timeWalkerMs < to; i++ {
  33. points = append(points, tsdb.NewTimePoint(walker, float64(timeWalkerMs)))
  34. walker += rand.Float64() - 0.5
  35. timeWalkerMs += query.IntervalMs
  36. }
  37. series.Points = points
  38. queryRes := tsdb.NewQueryResult()
  39. queryRes.Series = append(queryRes.Series, series)
  40. return queryRes
  41. },
  42. })
  43. registerScenario(&Scenario{
  44. Id: "no_data_points",
  45. Name: "No Data Points",
  46. Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult {
  47. return tsdb.NewQueryResult()
  48. },
  49. })
  50. registerScenario(&Scenario{
  51. Id: "datapoints_outside_range",
  52. Name: "Datapoints Outside Range",
  53. Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult {
  54. queryRes := tsdb.NewQueryResult()
  55. series := newSeriesForQuery(query)
  56. outsideTime := context.TimeRange.MustGetFrom().Add(-1*time.Hour).Unix() * 1000
  57. series.Points = append(series.Points, tsdb.NewTimePoint(10, float64(outsideTime)))
  58. queryRes.Series = append(queryRes.Series, series)
  59. return queryRes
  60. },
  61. })
  62. registerScenario(&Scenario{
  63. Id: "csv_metric_values",
  64. Name: "CSV Metric Values",
  65. StringInput: "1,20,90,30,5,0",
  66. Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult {
  67. queryRes := tsdb.NewQueryResult()
  68. stringInput := query.Model.Get("stringInput").MustString()
  69. values := []float64{}
  70. for _, strVal := range strings.Split(stringInput, ",") {
  71. if val, err := strconv.ParseFloat(strVal, 64); err == nil {
  72. values = append(values, val)
  73. }
  74. }
  75. if len(values) == 0 {
  76. return queryRes
  77. }
  78. series := newSeriesForQuery(query)
  79. startTime := context.TimeRange.GetFromAsMsEpoch()
  80. endTime := context.TimeRange.GetToAsMsEpoch()
  81. step := (endTime - startTime) / int64(len(values)-1)
  82. for _, val := range values {
  83. series.Points = append(series.Points, tsdb.NewTimePoint(val, float64(startTime)))
  84. startTime += step
  85. }
  86. queryRes.Series = append(queryRes.Series, series)
  87. return queryRes
  88. },
  89. })
  90. }
  91. func registerScenario(scenario *Scenario) {
  92. ScenarioRegistry[scenario.Id] = scenario
  93. }
  94. func newSeriesForQuery(query *tsdb.Query) *tsdb.TimeSeries {
  95. alias := query.Model.Get("alias").MustString("")
  96. if alias == "" {
  97. alias = query.RefId + "-series"
  98. }
  99. return &tsdb.TimeSeries{Name: alias}
  100. }