Expressions: expose ConvertDataFramesToResults (#83805)

This commit is contained in:
Ryan McKinley
2024-03-04 18:22:56 +02:00
committed by GitHub
parent 9bd84b30e9
commit 3036b50df3
13 changed files with 545 additions and 483 deletions
+32 -16
View File
@@ -14,38 +14,52 @@ import (
// Once we are comfortable with the parsing logic, this struct will
// be merged/replace the existing Query struct in grafana/pkg/expr/transform.go
type ExpressionQuery struct {
RefID string
Command Command
GraphID int64 `json:"id,omitempty"`
RefID string `json:"refId"`
QueryType QueryType `json:"queryType"`
// The typed query parameters
Properties any `json:"properties"`
// Hidden in debug JSON
Command Command `json:"-"`
}
// ID is used to identify nodes in the directed graph
func (q ExpressionQuery) ID() int64 {
return q.GraphID
}
type ExpressionQueryReader struct {
features featuremgmt.FeatureToggles
}
func NewExpressionQueryReader(features featuremgmt.FeatureToggles) (*ExpressionQueryReader, error) {
h := &ExpressionQueryReader{
func NewExpressionQueryReader(features featuremgmt.FeatureToggles) *ExpressionQueryReader {
return &ExpressionQueryReader{
features: features,
}
return h, nil
}
// ReadQuery implements query.TypedQueryHandler.
// nolint:gocyclo
func (h *ExpressionQueryReader) ReadQuery(
// Properties that have been parsed off the same node
common *rawNode, // common query.CommonQueryProperties
common *rawNode,
// An iterator with context for the full node (include common values)
iter *jsoniter.Iterator,
) (eq ExpressionQuery, err error) {
referenceVar := ""
eq.RefID = common.RefID
qt := QueryType(common.QueryType)
switch qt {
if common.QueryType == "" {
return eq, fmt.Errorf("missing queryType")
}
eq.QueryType = QueryType(common.QueryType)
switch eq.QueryType {
case QueryTypeMath:
q := &MathQuery{}
err = iter.ReadVal(q)
if err == nil {
eq.Command, err = NewMathCommand(common.RefID, q.Expression)
eq.Properties = q
}
case QueryTypeReduce:
@@ -54,6 +68,7 @@ func (h *ExpressionQueryReader) ReadQuery(
err = iter.ReadVal(q)
if err == nil {
referenceVar, err = getReferenceVar(q.Expression, common.RefID)
eq.Properties = q
}
if err == nil && q.Settings != nil {
switch q.Settings.Mode {
@@ -69,6 +84,7 @@ func (h *ExpressionQueryReader) ReadQuery(
}
}
if err == nil {
eq.Properties = q
eq.Command, err = NewReduceCommand(common.RefID,
q.Reducer, referenceVar, mapper)
}
@@ -83,23 +99,21 @@ func (h *ExpressionQueryReader) ReadQuery(
referenceVar, err = getReferenceVar(q.Expression, common.RefID)
}
if err == nil {
// tr := legacydata.NewDataTimeRange(common.TimeRange.From, common.TimeRange.To)
// AbsoluteTimeRange{
// From: tr.GetFromAsTimeUTC(),
// To: tr.GetToAsTimeUTC(),
// })
eq.Properties = q
eq.Command, err = NewResampleCommand(common.RefID,
q.Window,
referenceVar,
q.Downsampler,
q.Upsampler,
common.TimeRange)
common.TimeRange,
)
}
case QueryTypeClassic:
q := &ClassicQuery{}
err = iter.ReadVal(q)
if err == nil {
eq.Properties = q
eq.Command, err = classic.NewConditionCmd(common.RefID, q.Conditions)
}
@@ -107,7 +121,8 @@ func (h *ExpressionQueryReader) ReadQuery(
q := &SQLExpression{}
err = iter.ReadVal(q)
if err == nil {
eq.Command, err = NewSQLCommand(common.RefID, q.Expression, common.TimeRange)
eq.Properties = q
eq.Command, err = NewSQLCommand(common.RefID, q.Expression)
}
case QueryTypeThreshold:
@@ -128,6 +143,7 @@ func (h *ExpressionQueryReader) ReadQuery(
return eq, fmt.Errorf("invalid condition: %w", err)
}
eq.Command = threshold
eq.Properties = q
if firstCondition.UnloadEvaluator != nil && h.features.IsEnabledGlobally(featuremgmt.FlagRecoveryThreshold) {
unloading, err := NewThresholdCommand(common.RefID, referenceVar, firstCondition.UnloadEvaluator.Type, firstCondition.UnloadEvaluator.Params)