SSE: Add functions that determine NodeType by UID and construct a data source struct from NodeType (#70106)

* add NodeTypeFromDatasourceUID and DataSourceModelFromNodeType()
* deprecate expr.DataSourceModel
* replace usages of IsDataSource to NodeTypeFromDatasourceUID 
* replace usages of DataSourceModel to DataSourceModelFromNodeType()
This commit is contained in:
Yuri Tseretyan
2023-06-16 13:05:06 -04:00
committed by GitHub
parent 3d15d54a71
commit 842f33580e
7 changed files with 76 additions and 40 deletions
+42 -14
View File
@@ -2,6 +2,8 @@ package expr
import (
"context"
"errors"
"fmt"
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
@@ -39,6 +41,15 @@ func IsDataSource(uid string) bool {
return uid == DatasourceUID || uid == OldDatasourceUID
}
// NodeTypeFromDatasourceUID returns NodeType depending on the UID of the data source: TypeCMDNode if UID is DatasourceUID
// or OldDatasourceUID, and TypeDatasourceNode otherwise.
func NodeTypeFromDatasourceUID(uid string) NodeType {
if IsDataSource(uid) {
return TypeCMDNode
}
return TypeDatasourceNode
}
// Service is service representation for expression handling.
type Service struct {
cfg *setting.Cfg
@@ -46,6 +57,8 @@ type Service struct {
pCtxProvider *plugincontext.Provider
features featuremgmt.FeatureToggles
pluginsClient backend.CallResourceHandler
tracer tracing.Tracer
metrics *metrics
}
@@ -53,12 +66,13 @@ type Service struct {
func ProvideService(cfg *setting.Cfg, pluginClient plugins.Client, pCtxProvider *plugincontext.Provider,
features featuremgmt.FeatureToggles, registerer prometheus.Registerer, tracer tracing.Tracer) *Service {
return &Service{
cfg: cfg,
dataService: pluginClient,
pCtxProvider: pCtxProvider,
features: features,
tracer: tracer,
metrics: newMetrics(registerer),
cfg: cfg,
dataService: pluginClient,
pCtxProvider: pCtxProvider,
features: features,
tracer: tracer,
metrics: newMetrics(registerer),
pluginsClient: pluginClient,
}
}
@@ -91,13 +105,27 @@ func (s *Service) ExecutePipeline(ctx context.Context, now time.Time, pipeline D
return res, nil
}
func DataSourceModel() *datasources.DataSource {
return &datasources.DataSource{
ID: DatasourceID,
UID: DatasourceUID,
Name: DatasourceUID,
Type: DatasourceType,
JsonData: simplejson.New(),
SecureJsonData: make(map[string][]byte),
// Create a datasources.DataSource struct from NodeType. Returns error if kind is TypeDatasourceNode or unknown one.
func DataSourceModelFromNodeType(kind NodeType) (*datasources.DataSource, error) {
switch kind {
case TypeCMDNode:
return &datasources.DataSource{
ID: DatasourceID,
UID: DatasourceUID,
Name: DatasourceUID,
Type: DatasourceType,
JsonData: simplejson.New(),
SecureJsonData: make(map[string][]byte),
}, nil
case TypeDatasourceNode:
return nil, errors.New("cannot create expression data source for data source kind")
default:
return nil, fmt.Errorf("cannot create expression data source for '%s' kind", kind)
}
}
// Deprecated. Use DataSourceModelFromNodeType instead
func DataSourceModel() *datasources.DataSource {
d, _ := DataSourceModelFromNodeType(TypeCMDNode)
return d
}