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"
|
|
|
|
)
|
|
|
|
|
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) {
|
2021-04-23 09:52:32 -05:00
|
|
|
req := Request{
|
|
|
|
OrgId: query.User.OrgId,
|
|
|
|
Queries: []Query{},
|
2020-11-19 06:17:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2021-04-23 09:52:32 -05:00
|
|
|
req.Queries = append(req.Queries, Query{
|
2020-11-19 06:17:00 -06:00
|
|
|
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,
|
2021-04-23 09:52:32 -05:00
|
|
|
TimeRange: TimeRange{
|
2020-11-19 06:17:00 -06:00
|
|
|
From: query.TimeRange.GetFromAsTimeUTC(),
|
|
|
|
To: query.TimeRange.GetToAsTimeUTC(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2021-04-23 09:52:32 -05:00
|
|
|
return s.TransformData(ctx, &req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request is similar to plugins.DataQuery but with the Time Ranges is per Query.
|
|
|
|
type Request struct {
|
|
|
|
Headers map[string]string
|
|
|
|
Debug bool
|
|
|
|
OrgId int64
|
|
|
|
Queries []Query
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query is like plugins.DataSubQuery, but with a a time range, and only the UID
|
|
|
|
// for the data source. Also interval is a time.Duration.
|
|
|
|
type Query struct {
|
|
|
|
RefID string
|
|
|
|
TimeRange TimeRange
|
|
|
|
DatasourceUID string
|
|
|
|
JSON json.RawMessage
|
|
|
|
Interval time.Duration
|
|
|
|
QueryType string
|
|
|
|
MaxDataPoints int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// TimeRange is a time.Time based TimeRange.
|
|
|
|
type TimeRange struct {
|
|
|
|
From time.Time
|
|
|
|
To time.Time
|
2020-11-19 06:17:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// TransformData takes Queries which are either expressions nodes
|
|
|
|
// or are datasource requests.
|
2021-04-23 09:52:32 -05:00
|
|
|
func (s *Service) TransformData(ctx context.Context, req *Request) (r *backend.QueryDataResponse, err error) {
|
2021-01-22 11:27:33 -06:00
|
|
|
if s.isDisabled() {
|
2021-05-26 08:15:21 -05:00
|
|
|
return nil, fmt.Errorf("server side expressions are disabled")
|
2021-01-22 11:27:33 -06:00
|
|
|
}
|
|
|
|
|
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 {
|
2021-05-26 08:15:21 -05:00
|
|
|
return nil, err
|
2020-11-19 06:17:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2021-05-26 08:15:21 -05:00
|
|
|
return nil, err
|
2020-11-19 06:17:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2021-05-26 08:15:21 -05:00
|
|
|
return nil, err
|
2020-11-19 06:17:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-23 09:52:32 -05:00
|
|
|
func hiddenRefIDs(queries []Query) (map[string]struct{}, error) {
|
2020-11-19 06:17:00 -06:00
|
|
|
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,
|
2021-07-09 06:43:22 -05:00
|
|
|
Headers: req.Headers,
|
2020-11-19 06:17:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|