2020-11-19 06:17:00 -06:00
|
|
|
package expr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-03-08 00:02:49 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2021-02-01 07:48:02 -06:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2020-11-19 06:17:00 -06:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
2021-02-01 07:48:02 -06:00
|
|
|
var (
|
|
|
|
expressionsQuerySummary *prometheus.SummaryVec
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
expressionsQuerySummary = prometheus.NewSummaryVec(
|
|
|
|
prometheus.SummaryOpts{
|
|
|
|
Name: "expressions_queries_duration_milliseconds",
|
|
|
|
Help: "Expressions query summary",
|
|
|
|
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
|
|
|
|
},
|
|
|
|
[]string{"status"},
|
|
|
|
)
|
|
|
|
|
|
|
|
prometheus.MustRegister(expressionsQuerySummary)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WrapTransformData creates and executes transform requests
|
2021-03-31 10:35:03 -05:00
|
|
|
func (s *Service) WrapTransformData(ctx context.Context, query plugins.DataQuery) (*backend.QueryDataResponse, error) {
|
2020-11-19 06:17:00 -06:00
|
|
|
sdkReq := &backend.QueryDataRequest{
|
|
|
|
PluginContext: backend.PluginContext{
|
|
|
|
OrgID: query.User.OrgId,
|
|
|
|
},
|
|
|
|
Queries: []backend.DataQuery{},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, q := range query.Queries {
|
|
|
|
modelJSON, err := q.Model.MarshalJSON()
|
|
|
|
if err != nil {
|
2021-03-31 10:35:03 -05:00
|
|
|
return nil, err
|
2020-11-19 06:17:00 -06:00
|
|
|
}
|
|
|
|
sdkReq.Queries = append(sdkReq.Queries, backend.DataQuery{
|
|
|
|
JSON: modelJSON,
|
2021-03-08 00:02:49 -06:00
|
|
|
Interval: time.Duration(q.IntervalMS) * time.Millisecond,
|
|
|
|
RefID: q.RefID,
|
2020-11-19 06:17:00 -06:00
|
|
|
MaxDataPoints: q.MaxDataPoints,
|
|
|
|
QueryType: q.QueryType,
|
|
|
|
TimeRange: backend.TimeRange{
|
|
|
|
From: query.TimeRange.GetFromAsTimeUTC(),
|
|
|
|
To: query.TimeRange.GetToAsTimeUTC(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2021-03-31 10:35:03 -05:00
|
|
|
return s.TransformData(ctx, sdkReq)
|
2020-11-19 06:17:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// TransformData takes Queries which are either expressions nodes
|
|
|
|
// or are datasource requests.
|
2021-02-01 07:48:02 -06:00
|
|
|
func (s *Service) TransformData(ctx context.Context, req *backend.QueryDataRequest) (r *backend.QueryDataResponse, err error) {
|
2021-01-22 11:27:33 -06:00
|
|
|
if s.isDisabled() {
|
|
|
|
return nil, status.Error(codes.PermissionDenied, "Expressions are disabled")
|
|
|
|
}
|
|
|
|
|
2021-02-01 07:48:02 -06:00
|
|
|
start := time.Now()
|
|
|
|
defer func() {
|
|
|
|
var respStatus string
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
respStatus = "success"
|
|
|
|
default:
|
|
|
|
respStatus = "failure"
|
|
|
|
}
|
|
|
|
duration := float64(time.Since(start).Nanoseconds()) / float64(time.Millisecond)
|
|
|
|
expressionsQuerySummary.WithLabelValues(respStatus).Observe(duration)
|
|
|
|
}()
|
|
|
|
|
2020-11-19 06:17:00 -06:00
|
|
|
// Build the pipeline from the request, checking for ordering issues (e.g. loops)
|
|
|
|
// and parsing graph nodes from the queries.
|
2021-01-22 11:27:33 -06:00
|
|
|
pipeline, err := s.BuildPipeline(req)
|
2020-11-19 06:17:00 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the pipeline
|
2021-01-22 11:27:33 -06:00
|
|
|
responses, err := s.ExecutePipeline(ctx, pipeline)
|
2020-11-19 06:17:00 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Unknown, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get which queries have the Hide property so they those queries' results
|
|
|
|
// can be excluded from the response.
|
|
|
|
hidden, err := hiddenRefIDs(req.Queries)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error((codes.Internal), err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(hidden) != 0 {
|
|
|
|
filteredRes := backend.NewQueryDataResponse()
|
|
|
|
for refID, res := range responses.Responses {
|
|
|
|
if _, ok := hidden[refID]; !ok {
|
|
|
|
filteredRes.Responses[refID] = res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
responses = filteredRes
|
|
|
|
}
|
|
|
|
|
|
|
|
return responses, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func hiddenRefIDs(queries []backend.DataQuery) (map[string]struct{}, error) {
|
|
|
|
hidden := make(map[string]struct{})
|
|
|
|
|
|
|
|
for _, query := range queries {
|
|
|
|
hide := struct {
|
|
|
|
Hide bool `json:"hide"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(query.JSON, &hide); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if hide.Hide {
|
|
|
|
hidden[query.RefID] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hidden, nil
|
|
|
|
}
|
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
// queryData is called used to query datasources that are not expression commands, but are used
|
2020-11-19 06:17:00 -06:00
|
|
|
// alongside expressions and/or are the input of an expression command.
|
2021-03-08 00:02:49 -06:00
|
|
|
func (s *Service) queryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
2020-11-19 06:17:00 -06:00
|
|
|
if len(req.Queries) == 0 {
|
|
|
|
return nil, fmt.Errorf("zero queries found in datasource request")
|
|
|
|
}
|
|
|
|
|
|
|
|
datasourceID := int64(0)
|
2021-01-15 10:33:50 -06:00
|
|
|
var datasourceUID string
|
2020-11-19 06:17:00 -06:00
|
|
|
|
|
|
|
if req.PluginContext.DataSourceInstanceSettings != nil {
|
|
|
|
datasourceID = req.PluginContext.DataSourceInstanceSettings.ID
|
2021-01-15 10:33:50 -06:00
|
|
|
datasourceUID = req.PluginContext.DataSourceInstanceSettings.UID
|
2020-11-19 06:17:00 -06:00
|
|
|
}
|
|
|
|
|
2021-01-13 12:16:27 -06:00
|
|
|
getDsInfo := &models.GetDataSourceQuery{
|
2020-11-19 06:17:00 -06:00
|
|
|
OrgId: req.PluginContext.OrgID,
|
|
|
|
Id: datasourceID,
|
2021-01-15 10:33:50 -06:00
|
|
|
Uid: datasourceUID,
|
2020-11-19 06:17:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(getDsInfo); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not find datasource: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert plugin-model (datasource) queries to tsdb queries
|
2021-03-08 00:02:49 -06:00
|
|
|
queries := make([]plugins.DataSubQuery, len(req.Queries))
|
2020-11-19 06:17:00 -06:00
|
|
|
for i, query := range req.Queries {
|
|
|
|
sj, err := simplejson.NewJson(query.JSON)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-08 00:02:49 -06:00
|
|
|
queries[i] = plugins.DataSubQuery{
|
|
|
|
RefID: query.RefID,
|
|
|
|
IntervalMS: query.Interval.Milliseconds(),
|
2020-11-19 06:17:00 -06:00
|
|
|
MaxDataPoints: query.MaxDataPoints,
|
|
|
|
QueryType: query.QueryType,
|
|
|
|
DataSource: getDsInfo.Result,
|
|
|
|
Model: sj,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For now take Time Range from first query.
|
2021-03-08 00:02:49 -06:00
|
|
|
timeRange := plugins.NewDataTimeRange(strconv.FormatInt(req.Queries[0].TimeRange.From.Unix()*1000, 10),
|
|
|
|
strconv.FormatInt(req.Queries[0].TimeRange.To.Unix()*1000, 10))
|
2020-11-19 06:17:00 -06:00
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
tQ := plugins.DataQuery{
|
|
|
|
TimeRange: &timeRange,
|
2020-11-19 06:17:00 -06:00
|
|
|
Queries: queries,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the converted queries
|
2021-03-08 00:02:49 -06:00
|
|
|
tsdbRes, err := s.DataService.HandleRequest(ctx, getDsInfo.Result, tQ)
|
2020-11-19 06:17:00 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-31 10:35:03 -05:00
|
|
|
return tsdbRes.ToBackendDataResponse()
|
2020-11-19 06:17:00 -06:00
|
|
|
}
|