feat(testdata): lots of work on new test data data source and scenarios
This commit is contained in:
Vendored
+98
@@ -0,0 +1,98 @@
|
||||
package testdata
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/log"
|
||||
"github.com/grafana/grafana/pkg/tsdb"
|
||||
)
|
||||
|
||||
type ScenarioHandler func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult
|
||||
|
||||
type Scenario struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Handler ScenarioHandler `json:"-"`
|
||||
}
|
||||
|
||||
var ScenarioRegistry map[string]*Scenario
|
||||
|
||||
func init() {
|
||||
ScenarioRegistry = make(map[string]*Scenario)
|
||||
logger := log.New("tsdb.testdata")
|
||||
|
||||
registerScenario(&Scenario{
|
||||
Id: "random_walk",
|
||||
Name: "Random Walk",
|
||||
|
||||
Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult {
|
||||
timeWalkerMs := context.TimeRange.MustGetFrom().Unix() * 1000
|
||||
to := context.TimeRange.MustGetTo().Unix() * 1000
|
||||
|
||||
series := newSeriesForQuery(query)
|
||||
|
||||
points := make([][2]*float64, 0)
|
||||
walker := rand.Float64() * 100
|
||||
|
||||
for i := int64(0); i < 10000 && timeWalkerMs < to; i++ {
|
||||
timestamp := float64(timeWalkerMs)
|
||||
val := float64(walker)
|
||||
points = append(points, [2]*float64{&val, ×tamp})
|
||||
|
||||
walker += rand.Float64() - 0.5
|
||||
timeWalkerMs += query.IntervalMs
|
||||
}
|
||||
|
||||
series.Points = points
|
||||
|
||||
queryRes := &tsdb.QueryResult{}
|
||||
queryRes.Series = append(queryRes.Series, series)
|
||||
return queryRes
|
||||
},
|
||||
})
|
||||
|
||||
registerScenario(&Scenario{
|
||||
Id: "no_data_points",
|
||||
Name: "No Data Points",
|
||||
Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult {
|
||||
return &tsdb.QueryResult{
|
||||
Series: make(tsdb.TimeSeriesSlice, 0),
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
registerScenario(&Scenario{
|
||||
Id: "datapoints_outside_range",
|
||||
Name: "Datapoints Outside Range",
|
||||
Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult {
|
||||
queryRes := &tsdb.QueryResult{}
|
||||
|
||||
series := newSeriesForQuery(query)
|
||||
outsideTime := context.TimeRange.MustGetFrom().Add(-1*time.Hour).Unix() * 1000
|
||||
|
||||
timestamp := float64(outsideTime)
|
||||
logger.Info("time", "from", timestamp)
|
||||
val := float64(10)
|
||||
|
||||
series.Points = append(series.Points, [2]*float64{&val, ×tamp})
|
||||
queryRes.Series = append(queryRes.Series, series)
|
||||
return queryRes
|
||||
},
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func registerScenario(scenario *Scenario) {
|
||||
ScenarioRegistry[scenario.Id] = scenario
|
||||
}
|
||||
|
||||
func newSeriesForQuery(query *tsdb.Query) *tsdb.TimeSeries {
|
||||
alias := query.Model.Get("alias").MustString("")
|
||||
if alias == "" {
|
||||
alias = query.RefId + "-series"
|
||||
}
|
||||
|
||||
return &tsdb.TimeSeries{Name: alias}
|
||||
}
|
||||
Vendored
+12
-27
@@ -1,17 +1,20 @@
|
||||
package testdata
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
|
||||
"github.com/grafana/grafana/pkg/log"
|
||||
"github.com/grafana/grafana/pkg/tsdb"
|
||||
)
|
||||
|
||||
type TestDataExecutor struct {
|
||||
*tsdb.DataSourceInfo
|
||||
log log.Logger
|
||||
}
|
||||
|
||||
func NewTestDataExecutor(dsInfo *tsdb.DataSourceInfo) tsdb.Executor {
|
||||
return &TestDataExecutor{dsInfo}
|
||||
return &TestDataExecutor{
|
||||
DataSourceInfo: dsInfo,
|
||||
log: log.New("tsdb.testdata"),
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -22,33 +25,15 @@ func (e *TestDataExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryC
|
||||
result := &tsdb.BatchResult{}
|
||||
result.QueryResults = make(map[string]*tsdb.QueryResult)
|
||||
|
||||
from, _ := context.TimeRange.FromTime()
|
||||
to, _ := context.TimeRange.ToTime()
|
||||
|
||||
queryRes := &tsdb.QueryResult{}
|
||||
|
||||
for _, query := range queries {
|
||||
// scenario := query.Model.Get("scenario").MustString("random_walk")
|
||||
series := &tsdb.TimeSeries{Name: "test-series-0"}
|
||||
|
||||
stepInSeconds := (to.Unix() - from.Unix()) / query.MaxDataPoints
|
||||
points := make([][2]*float64, 0)
|
||||
walker := rand.Float64() * 100
|
||||
time := from.Unix()
|
||||
|
||||
for i := int64(0); i < query.MaxDataPoints; i++ {
|
||||
timestamp := float64(time)
|
||||
val := float64(walker)
|
||||
points = append(points, [2]*float64{&val, ×tamp})
|
||||
|
||||
walker += rand.Float64() - 0.5
|
||||
time += stepInSeconds
|
||||
scenarioId := query.Model.Get("scenarioId").MustString("random_walk")
|
||||
if scenario, exist := ScenarioRegistry[scenarioId]; exist {
|
||||
result.QueryResults[query.RefId] = scenario.Handler(query, context)
|
||||
result.QueryResults[query.RefId].RefId = query.RefId
|
||||
} else {
|
||||
e.log.Error("Scenario not found", "scenarioId", scenarioId)
|
||||
}
|
||||
|
||||
series.Points = points
|
||||
queryRes.Series = append(queryRes.Series, series)
|
||||
}
|
||||
|
||||
result.QueryResults["A"] = queryRes
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user