fake_test.go 1005 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package tsdb
  2. type FakeExecutor struct {
  3. results map[string]*QueryResult
  4. resultsFn map[string]ResultsFn
  5. }
  6. type ResultsFn func(context *QueryContext) *QueryResult
  7. func NewFakeExecutor(dsInfo *DataSourceInfo) *FakeExecutor {
  8. return &FakeExecutor{
  9. results: make(map[string]*QueryResult),
  10. resultsFn: make(map[string]ResultsFn),
  11. }
  12. }
  13. func (e *FakeExecutor) Execute(queries QuerySlice, context *QueryContext) *BatchResult {
  14. result := &BatchResult{QueryResults: make(map[string]*QueryResult)}
  15. for _, query := range queries {
  16. if results, has := e.results[query.RefId]; has {
  17. result.QueryResults[query.RefId] = results
  18. }
  19. if testFunc, has := e.resultsFn[query.RefId]; has {
  20. result.QueryResults[query.RefId] = testFunc(context)
  21. }
  22. }
  23. return result
  24. }
  25. func (e *FakeExecutor) Return(refId string, series TimeSeriesSlice) {
  26. e.results[refId] = &QueryResult{
  27. RefId: refId, Series: series,
  28. }
  29. }
  30. func (e *FakeExecutor) HandleQuery(refId string, fn ResultsFn) {
  31. e.resultsFn[refId] = fn
  32. }