mirror of
https://github.com/grafana/grafana.git
synced 2025-08-06 02:29:33 +08:00
Expressions: use datasource model from the query (#41376)
* refactor datasource loading * refactor datasource loading * pass uid * use dscache in alerting to get DS * remove expr/translate pacakge * remove dup injection entry * fix DS type on metrics endpoint, remove SQL DS lookup inside SSE * update test and adapter * comment fix * Make eval run as admin when getting datasource info Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com> * fmt and comment * remove unncessary/redundant code Co-authored-by: Kyle Brandt <kyle@grafana.com> Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com> Co-authored-by: Santiago <santiagohernandez.1997@gmail.com>
This commit is contained in:
@ -6,10 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins/adapters"
|
||||
"github.com/grafana/grafana/pkg/util/errutil"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
@ -44,30 +41,13 @@ type Request struct {
|
||||
type Query struct {
|
||||
RefID string
|
||||
TimeRange TimeRange
|
||||
DatasourceUID string // deprecated, value -100 when expressions
|
||||
Datasource DataSourceRef `json:"datasource"`
|
||||
DataSource *models.DataSource `json:"datasource"`
|
||||
JSON json.RawMessage
|
||||
Interval time.Duration
|
||||
QueryType string
|
||||
MaxDataPoints int64
|
||||
}
|
||||
|
||||
type DataSourceRef struct {
|
||||
Type string `json:"type"` // value should be __expr__
|
||||
UID string `json:"uid"` // value should be __expr__
|
||||
}
|
||||
|
||||
func (q *Query) GetDatasourceUID() string {
|
||||
if q.DatasourceUID != "" {
|
||||
return q.DatasourceUID // backwards compatibility gets precedence
|
||||
}
|
||||
|
||||
if q.Datasource.UID != "" {
|
||||
return q.Datasource.UID
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// TimeRange is a time.Time based TimeRange.
|
||||
type TimeRange struct {
|
||||
From time.Time
|
||||
@ -146,42 +126,6 @@ func hiddenRefIDs(queries []Query) (map[string]struct{}, error) {
|
||||
return hidden, nil
|
||||
}
|
||||
|
||||
// queryData is called used to query datasources that are not expression commands, but are used
|
||||
// alongside expressions and/or are the input of an expression command.
|
||||
func (s *Service) queryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
if len(req.Queries) == 0 {
|
||||
return nil, fmt.Errorf("zero queries found in datasource request")
|
||||
}
|
||||
|
||||
datasourceID := int64(0)
|
||||
var datasourceUID string
|
||||
|
||||
if req.PluginContext.DataSourceInstanceSettings != nil {
|
||||
datasourceID = req.PluginContext.DataSourceInstanceSettings.ID
|
||||
datasourceUID = req.PluginContext.DataSourceInstanceSettings.UID
|
||||
}
|
||||
|
||||
getDsInfo := &models.GetDataSourceQuery{
|
||||
OrgId: req.PluginContext.OrgID,
|
||||
Id: datasourceID,
|
||||
Uid: datasourceUID,
|
||||
}
|
||||
|
||||
if err := bus.DispatchCtx(ctx, getDsInfo); err != nil {
|
||||
return nil, fmt.Errorf("could not find datasource: %w", err)
|
||||
}
|
||||
|
||||
dsInstanceSettings, err := adapters.ModelToInstanceSettings(getDsInfo.Result, s.decryptSecureJsonDataFn(ctx))
|
||||
if err != nil {
|
||||
return nil, errutil.Wrap("failed to convert datasource instance settings", err)
|
||||
}
|
||||
|
||||
req.PluginContext.DataSourceInstanceSettings = dsInstanceSettings
|
||||
req.PluginContext.PluginID = getDsInfo.Result.Type
|
||||
|
||||
return s.dataService.QueryData(ctx, req)
|
||||
}
|
||||
|
||||
func (s *Service) decryptSecureJsonDataFn(ctx context.Context) func(map[string][]byte) map[string]string {
|
||||
return func(m map[string][]byte) map[string]string {
|
||||
decryptedJsonData, err := s.secretsService.DecryptJsonData(ctx, m)
|
||||
|
Reference in New Issue
Block a user