2016-06-06 10:11:46 -05:00
|
|
|
package tsdb
|
|
|
|
|
2016-12-07 04:10:42 -06:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
)
|
2016-10-03 02:38:03 -05:00
|
|
|
|
2016-06-06 10:11:46 -05:00
|
|
|
type FakeExecutor struct {
|
|
|
|
results map[string]*QueryResult
|
|
|
|
resultsFn map[string]ResultsFn
|
|
|
|
}
|
|
|
|
|
2017-09-20 11:31:34 -05:00
|
|
|
type ResultsFn func(context *TsdbQuery) *QueryResult
|
2016-06-06 10:11:46 -05:00
|
|
|
|
2016-12-07 04:10:42 -06:00
|
|
|
func NewFakeExecutor(dsInfo *models.DataSource) (*FakeExecutor, error) {
|
2016-06-06 10:11:46 -05:00
|
|
|
return &FakeExecutor{
|
|
|
|
results: make(map[string]*QueryResult),
|
|
|
|
resultsFn: make(map[string]ResultsFn),
|
2016-12-07 04:10:42 -06:00
|
|
|
}, nil
|
2016-06-06 10:11:46 -05:00
|
|
|
}
|
|
|
|
|
2017-09-21 11:04:06 -05:00
|
|
|
func (e *FakeExecutor) Query(ctx context.Context, dsInfo *models.DataSource, context *TsdbQuery) (*Response, error) {
|
|
|
|
result := &Response{Results: make(map[string]*QueryResult)}
|
2017-09-20 11:56:33 -05:00
|
|
|
for _, query := range context.Queries {
|
2016-06-06 10:11:46 -05:00
|
|
|
if results, has := e.results[query.RefId]; has {
|
2017-09-21 11:04:06 -05:00
|
|
|
result.Results[query.RefId] = results
|
2016-06-06 10:11:46 -05:00
|
|
|
}
|
|
|
|
if testFunc, has := e.resultsFn[query.RefId]; has {
|
2017-09-21 11:04:06 -05:00
|
|
|
result.Results[query.RefId] = testFunc(context)
|
2016-06-06 10:11:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-21 11:04:06 -05:00
|
|
|
return result, nil
|
2016-06-06 10:11:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *FakeExecutor) Return(refId string, series TimeSeriesSlice) {
|
|
|
|
e.results[refId] = &QueryResult{
|
|
|
|
RefId: refId, Series: series,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *FakeExecutor) HandleQuery(refId string, fn ResultsFn) {
|
|
|
|
e.resultsFn[refId] = fn
|
|
|
|
}
|