grafana/pkg/tsdb/testdatasource/testdata.go
Marcus Efraimsson 00e7c7c4b7
Testdata: Rename package to circumvent convention in go (#19409)
Before this change the tests for test datasource was not run.
2019-09-27 16:18:24 +02:00

43 lines
1.1 KiB
Go

package testdatasource
import (
"context"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/tsdb"
)
type TestDataExecutor struct {
*models.DataSource
log log.Logger
}
func NewTestDataExecutor(dsInfo *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
return &TestDataExecutor{
DataSource: dsInfo,
log: log.New("tsdb.testdata"),
}, nil
}
func init() {
tsdb.RegisterTsdbQueryEndpoint("testdata", NewTestDataExecutor)
}
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)
for _, query := range tsdbQuery.Queries {
scenarioId := query.Model.Get("scenarioId").MustString("random_walk")
if scenario, exist := ScenarioRegistry[scenarioId]; exist {
result.Results[query.RefId] = scenario.Handler(query, tsdbQuery)
result.Results[query.RefId].RefId = query.RefId
} else {
e.log.Error("Scenario not found", "scenarioId", scenarioId)
}
}
return result, nil
}