2019-09-27 09:18:24 -05:00
|
|
|
package testdatasource
|
2016-09-27 07:39:51 -05:00
|
|
|
|
|
|
|
import (
|
2016-10-03 02:38:03 -05:00
|
|
|
"context"
|
|
|
|
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2016-12-07 04:10:42 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2016-09-27 07:39:51 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestDataExecutor struct {
|
2016-12-07 04:10:42 -06:00
|
|
|
*models.DataSource
|
2016-09-27 11:17:39 -05:00
|
|
|
log log.Logger
|
2016-09-27 07:39:51 -05:00
|
|
|
}
|
|
|
|
|
2017-09-21 03:44:25 -05:00
|
|
|
func NewTestDataExecutor(dsInfo *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
|
2016-09-27 11:17:39 -05:00
|
|
|
return &TestDataExecutor{
|
2016-12-07 04:10:42 -06:00
|
|
|
DataSource: dsInfo,
|
|
|
|
log: log.New("tsdb.testdata"),
|
|
|
|
}, nil
|
2016-09-27 07:39:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2018-07-16 05:12:13 -05:00
|
|
|
tsdb.RegisterTsdbQueryEndpoint("testdata", NewTestDataExecutor)
|
2016-09-27 07:39:51 -05:00
|
|
|
}
|
|
|
|
|
2017-09-21 11:04:06 -05:00
|
|
|
func (e *TestDataExecutor) Query(ctx context.Context, dsInfo *models.DataSource, tsdbQuery *tsdb.TsdbQuery) (*tsdb.Response, error) {
|
|
|
|
result := &tsdb.Response{}
|
|
|
|
result.Results = make(map[string]*tsdb.QueryResult)
|
2016-09-27 07:39:51 -05:00
|
|
|
|
2017-09-21 08:23:34 -05:00
|
|
|
for _, query := range tsdbQuery.Queries {
|
2016-09-27 11:17:39 -05:00
|
|
|
scenarioId := query.Model.Get("scenarioId").MustString("random_walk")
|
|
|
|
if scenario, exist := ScenarioRegistry[scenarioId]; exist {
|
2017-09-21 11:04:06 -05:00
|
|
|
result.Results[query.RefId] = scenario.Handler(query, tsdbQuery)
|
|
|
|
result.Results[query.RefId].RefId = query.RefId
|
2016-09-27 11:17:39 -05:00
|
|
|
} else {
|
|
|
|
e.log.Error("Scenario not found", "scenarioId", scenarioId)
|
2016-09-27 07:39:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-21 11:04:06 -05:00
|
|
|
return result, nil
|
2016-09-27 07:39:51 -05:00
|
|
|
}
|