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
|
|
|
|
}
|
|
|
|
|
|
|
|
type ResultsFn func(context *QueryContext) *QueryResult
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-10-03 02:38:03 -05:00
|
|
|
func (e *FakeExecutor) Execute(ctx context.Context, queries QuerySlice, context *QueryContext) *BatchResult {
|
2016-06-06 10:11:46 -05:00
|
|
|
result := &BatchResult{QueryResults: make(map[string]*QueryResult)}
|
|
|
|
for _, query := range queries {
|
|
|
|
if results, has := e.results[query.RefId]; has {
|
|
|
|
result.QueryResults[query.RefId] = results
|
|
|
|
}
|
|
|
|
if testFunc, has := e.resultsFn[query.RefId]; has {
|
|
|
|
result.QueryResults[query.RefId] = testFunc(context)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|