mirror of
https://github.com/grafana/grafana.git
synced 2025-09-24 23:34:13 +08:00
rename executor into tsdbqueryendpoint
This commit is contained in:
@ -21,7 +21,7 @@ func newBatch(dsId int64, queries []*Query) *Batch {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (bg *Batch) process(ctx context.Context, resultChan chan *BatchResult, tsdbQuery *TsdbQuery) {
|
func (bg *Batch) process(ctx context.Context, resultChan chan *BatchResult, tsdbQuery *TsdbQuery) {
|
||||||
executor, err := getExecutorFor(bg.Queries[0].DataSource)
|
executor, err := getTsdbQueryEndpointFor(bg.Queries[0].DataSource)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
bg.Done = true
|
bg.Done = true
|
||||||
@ -36,7 +36,7 @@ func (bg *Batch) process(ctx context.Context, resultChan chan *BatchResult, tsdb
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
res := executor.Execute(ctx, &TsdbQuery{
|
res := executor.Query(ctx, &TsdbQuery{
|
||||||
Queries: bg.Queries,
|
Queries: bg.Queries,
|
||||||
TimeRange: tsdbQuery.TimeRange,
|
TimeRange: tsdbQuery.TimeRange,
|
||||||
})
|
})
|
||||||
|
@ -1,36 +0,0 @@
|
|||||||
package tsdb
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/models"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Executor interface {
|
|
||||||
Execute(ctx context.Context, query *TsdbQuery) *BatchResult
|
|
||||||
}
|
|
||||||
|
|
||||||
var registry map[string]GetExecutorFn
|
|
||||||
|
|
||||||
type GetExecutorFn func(dsInfo *models.DataSource) (Executor, error)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
registry = make(map[string]GetExecutorFn)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getExecutorFor(dsInfo *models.DataSource) (Executor, error) {
|
|
||||||
if fn, exists := registry[dsInfo.Type]; exists {
|
|
||||||
executor, err := fn(dsInfo)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return executor, nil
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("Could not find executor for data source type: %s", dsInfo.Type)
|
|
||||||
}
|
|
||||||
|
|
||||||
func RegisterExecutor(pluginId string, fn GetExecutorFn) {
|
|
||||||
registry[pluginId] = fn
|
|
||||||
}
|
|
@ -20,7 +20,7 @@ func NewFakeExecutor(dsInfo *models.DataSource) (*FakeExecutor, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *FakeExecutor) Execute(ctx context.Context, context *TsdbQuery) *BatchResult {
|
func (e *FakeExecutor) Query(ctx context.Context, context *TsdbQuery) *BatchResult {
|
||||||
result := &BatchResult{QueryResults: make(map[string]*QueryResult)}
|
result := &BatchResult{QueryResults: make(map[string]*QueryResult)}
|
||||||
for _, query := range context.Queries {
|
for _, query := range context.Queries {
|
||||||
if results, has := e.results[query.RefId]; has {
|
if results, has := e.results[query.RefId]; has {
|
||||||
|
@ -25,7 +25,7 @@ type GraphiteExecutor struct {
|
|||||||
HttpClient *http.Client
|
HttpClient *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGraphiteExecutor(datasource *models.DataSource) (tsdb.Executor, error) {
|
func NewGraphiteExecutor(datasource *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
|
||||||
httpClient, err := datasource.GetHttpClient()
|
httpClient, err := datasource.GetHttpClient()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -44,10 +44,10 @@ var (
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
glog = log.New("tsdb.graphite")
|
glog = log.New("tsdb.graphite")
|
||||||
tsdb.RegisterExecutor("graphite", NewGraphiteExecutor)
|
tsdb.RegisterTsdbQueryEndpoint("graphite", NewGraphiteExecutor)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *GraphiteExecutor) Execute(ctx context.Context, context *tsdb.TsdbQuery) *tsdb.BatchResult {
|
func (e *GraphiteExecutor) Query(ctx context.Context, context *tsdb.TsdbQuery) *tsdb.BatchResult {
|
||||||
result := &tsdb.BatchResult{}
|
result := &tsdb.BatchResult{}
|
||||||
|
|
||||||
from := "-" + formatTimeRange(context.TimeRange.From)
|
from := "-" + formatTimeRange(context.TimeRange.From)
|
||||||
|
@ -23,7 +23,7 @@ type InfluxDBExecutor struct {
|
|||||||
HttpClient *http.Client
|
HttpClient *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewInfluxDBExecutor(datasource *models.DataSource) (tsdb.Executor, error) {
|
func NewInfluxDBExecutor(datasource *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
|
||||||
httpClient, err := datasource.GetHttpClient()
|
httpClient, err := datasource.GetHttpClient()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -44,10 +44,10 @@ var (
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
glog = log.New("tsdb.influxdb")
|
glog = log.New("tsdb.influxdb")
|
||||||
tsdb.RegisterExecutor("influxdb", NewInfluxDBExecutor)
|
tsdb.RegisterTsdbQueryEndpoint("influxdb", NewInfluxDBExecutor)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *InfluxDBExecutor) Execute(ctx context.Context, context *tsdb.TsdbQuery) *tsdb.BatchResult {
|
func (e *InfluxDBExecutor) Query(ctx context.Context, context *tsdb.TsdbQuery) *tsdb.BatchResult {
|
||||||
result := &tsdb.BatchResult{}
|
result := &tsdb.BatchResult{}
|
||||||
|
|
||||||
query, err := e.getQuery(context.Queries, context)
|
query, err := e.getQuery(context.Queries, context)
|
||||||
|
@ -38,10 +38,10 @@ var engineCache = engineCacheType{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
tsdb.RegisterExecutor("mysql", NewMysqlExecutor)
|
tsdb.RegisterTsdbQueryEndpoint("mysql", NewMysqlExecutor)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMysqlExecutor(datasource *models.DataSource) (tsdb.Executor, error) {
|
func NewMysqlExecutor(datasource *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
|
||||||
executor := &MysqlExecutor{
|
executor := &MysqlExecutor{
|
||||||
datasource: datasource,
|
datasource: datasource,
|
||||||
log: log.New("tsdb.mysql"),
|
log: log.New("tsdb.mysql"),
|
||||||
@ -81,7 +81,7 @@ func (e *MysqlExecutor) initEngine() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *MysqlExecutor) Execute(ctx context.Context, context *tsdb.TsdbQuery) *tsdb.BatchResult {
|
func (e *MysqlExecutor) Query(ctx context.Context, context *tsdb.TsdbQuery) *tsdb.BatchResult {
|
||||||
result := &tsdb.BatchResult{
|
result := &tsdb.BatchResult{
|
||||||
QueryResults: make(map[string]*tsdb.QueryResult),
|
QueryResults: make(map[string]*tsdb.QueryResult),
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ type OpenTsdbExecutor struct {
|
|||||||
httpClient *http.Client
|
httpClient *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewOpenTsdbExecutor(datasource *models.DataSource) (tsdb.Executor, error) {
|
func NewOpenTsdbExecutor(datasource *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
|
||||||
httpClient, err := datasource.GetHttpClient()
|
httpClient, err := datasource.GetHttpClient()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -45,10 +45,10 @@ var (
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
plog = log.New("tsdb.opentsdb")
|
plog = log.New("tsdb.opentsdb")
|
||||||
tsdb.RegisterExecutor("opentsdb", NewOpenTsdbExecutor)
|
tsdb.RegisterTsdbQueryEndpoint("opentsdb", NewOpenTsdbExecutor)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *OpenTsdbExecutor) Execute(ctx context.Context, queryContext *tsdb.TsdbQuery) *tsdb.BatchResult {
|
func (e *OpenTsdbExecutor) Query(ctx context.Context, queryContext *tsdb.TsdbQuery) *tsdb.BatchResult {
|
||||||
result := &tsdb.BatchResult{}
|
result := &tsdb.BatchResult{}
|
||||||
|
|
||||||
var tsdbQuery OpenTsdbQuery
|
var tsdbQuery OpenTsdbQuery
|
||||||
|
@ -39,7 +39,7 @@ func (bat basicAuthTransport) RoundTrip(req *http.Request) (*http.Response, erro
|
|||||||
return bat.Transport.RoundTrip(req)
|
return bat.Transport.RoundTrip(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPrometheusExecutor(dsInfo *models.DataSource) (tsdb.Executor, error) {
|
func NewPrometheusExecutor(dsInfo *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
|
||||||
transport, err := dsInfo.GetHttpTransport()
|
transport, err := dsInfo.GetHttpTransport()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -58,7 +58,7 @@ var (
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
plog = log.New("tsdb.prometheus")
|
plog = log.New("tsdb.prometheus")
|
||||||
tsdb.RegisterExecutor("prometheus", NewPrometheusExecutor)
|
tsdb.RegisterTsdbQueryEndpoint("prometheus", NewPrometheusExecutor)
|
||||||
legendFormat = regexp.MustCompile(`\{\{\s*(.+?)\s*\}\}`)
|
legendFormat = regexp.MustCompile(`\{\{\s*(.+?)\s*\}\}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,7 +84,7 @@ func (e *PrometheusExecutor) getClient() (apiv1.API, error) {
|
|||||||
return apiv1.NewAPI(client), nil
|
return apiv1.NewAPI(client), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *PrometheusExecutor) Execute(ctx context.Context, queryContext *tsdb.TsdbQuery) *tsdb.BatchResult {
|
func (e *PrometheusExecutor) Query(ctx context.Context, queryContext *tsdb.TsdbQuery) *tsdb.BatchResult {
|
||||||
result := &tsdb.BatchResult{}
|
result := &tsdb.BatchResult{}
|
||||||
|
|
||||||
client, err := e.getClient()
|
client, err := e.getClient()
|
||||||
|
36
pkg/tsdb/query_endpoint.go
Normal file
36
pkg/tsdb/query_endpoint.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package tsdb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TsdbQueryEndpoint interface {
|
||||||
|
Query(ctx context.Context, query *TsdbQuery) *BatchResult
|
||||||
|
}
|
||||||
|
|
||||||
|
var registry map[string]GetTsdbQueryEndpointFn
|
||||||
|
|
||||||
|
type GetTsdbQueryEndpointFn func(dsInfo *models.DataSource) (TsdbQueryEndpoint, error)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
registry = make(map[string]GetTsdbQueryEndpointFn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getTsdbQueryEndpointFor(dsInfo *models.DataSource) (TsdbQueryEndpoint, error) {
|
||||||
|
if fn, exists := registry[dsInfo.Type]; exists {
|
||||||
|
executor, err := fn(dsInfo)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return executor, nil
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("Could not find executor for data source type: %s", dsInfo.Type)
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterTsdbQueryEndpoint(pluginId string, fn GetTsdbQueryEndpointFn) {
|
||||||
|
registry[pluginId] = fn
|
||||||
|
}
|
6
pkg/tsdb/testdata/testdata.go
vendored
6
pkg/tsdb/testdata/testdata.go
vendored
@ -13,7 +13,7 @@ type TestDataExecutor struct {
|
|||||||
log log.Logger
|
log log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTestDataExecutor(dsInfo *models.DataSource) (tsdb.Executor, error) {
|
func NewTestDataExecutor(dsInfo *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
|
||||||
return &TestDataExecutor{
|
return &TestDataExecutor{
|
||||||
DataSource: dsInfo,
|
DataSource: dsInfo,
|
||||||
log: log.New("tsdb.testdata"),
|
log: log.New("tsdb.testdata"),
|
||||||
@ -21,10 +21,10 @@ func NewTestDataExecutor(dsInfo *models.DataSource) (tsdb.Executor, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
tsdb.RegisterExecutor("grafana-testdata-datasource", NewTestDataExecutor)
|
tsdb.RegisterTsdbQueryEndpoint("grafana-testdata-datasource", NewTestDataExecutor)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *TestDataExecutor) Execute(ctx context.Context, context *tsdb.TsdbQuery) *tsdb.BatchResult {
|
func (e *TestDataExecutor) Query(ctx context.Context, context *tsdb.TsdbQuery) *tsdb.BatchResult {
|
||||||
result := &tsdb.BatchResult{}
|
result := &tsdb.BatchResult{}
|
||||||
result.QueryResults = make(map[string]*tsdb.QueryResult)
|
result.QueryResults = make(map[string]*tsdb.QueryResult)
|
||||||
|
|
||||||
|
@ -169,7 +169,7 @@ func TestMetricQuery(t *testing.T) {
|
|||||||
|
|
||||||
func registerFakeExecutor() *FakeExecutor {
|
func registerFakeExecutor() *FakeExecutor {
|
||||||
executor, _ := NewFakeExecutor(nil)
|
executor, _ := NewFakeExecutor(nil)
|
||||||
RegisterExecutor("test", func(dsInfo *models.DataSource) (Executor, error) {
|
RegisterTsdbQueryEndpoint("test", func(dsInfo *models.DataSource) (TsdbQueryEndpoint, error) {
|
||||||
return executor, nil
|
return executor, nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user