Expressions: Field names from refId (#29755)
so one can see which expression data came from when visualizing
This commit is contained in:
+31
-30
@@ -24,6 +24,7 @@ type State struct {
|
||||
// Could hold more properties that change behavior around:
|
||||
// - Unions (How many result A and many Result B in case A + B are joined)
|
||||
// - NaN/Null behavior
|
||||
RefID string
|
||||
}
|
||||
|
||||
// Vars holds the results of datasource queries or other expression commands.
|
||||
@@ -43,10 +44,11 @@ func New(expr string, funcs ...map[string]parse.Func) (*Expr, error) {
|
||||
}
|
||||
|
||||
// Execute applies a parse expression to the context and executes it
|
||||
func (e *Expr) Execute(vars Vars) (r Results, err error) {
|
||||
func (e *Expr) Execute(refID string, vars Vars) (r Results, err error) {
|
||||
s := &State{
|
||||
Expr: e,
|
||||
Vars: vars,
|
||||
Expr: e,
|
||||
Vars: vars,
|
||||
RefID: refID,
|
||||
}
|
||||
return e.executeState(s)
|
||||
}
|
||||
@@ -78,7 +80,7 @@ func errRecover(errp *error, s *State) {
|
||||
func (e *State) walk(node parse.Node) (res Results, err error) {
|
||||
switch node := node.(type) {
|
||||
case *parse.ScalarNode:
|
||||
res = NewScalarResults(&node.Float64)
|
||||
res = NewScalarResults(e.RefID, &node.Float64)
|
||||
case *parse.VarNode:
|
||||
res = e.Vars[node.Name]
|
||||
case *parse.BinaryNode:
|
||||
@@ -103,19 +105,19 @@ func (e *State) walkUnary(node *parse.UnaryNode) (Results, error) {
|
||||
var newVal Value
|
||||
switch rt := val.(type) {
|
||||
case Scalar:
|
||||
newVal = NewScalar(nil)
|
||||
newVal = NewScalar(e.RefID, nil)
|
||||
f := rt.GetFloat64Value()
|
||||
if f != nil {
|
||||
newF, err := unaryOp(node.OpStr, *f)
|
||||
if err != nil {
|
||||
return newResults, err
|
||||
}
|
||||
newVal = NewScalar(&newF)
|
||||
newVal = NewScalar(e.RefID, &newF)
|
||||
}
|
||||
case Number:
|
||||
newVal, err = unaryNumber(rt, node.OpStr)
|
||||
newVal, err = e.unaryNumber(rt, node.OpStr)
|
||||
case Series:
|
||||
newVal, err = unarySeries(rt, node.OpStr)
|
||||
newVal, err = e.unarySeries(rt, node.OpStr)
|
||||
default:
|
||||
return newResults, fmt.Errorf("can not perform a unary operation on type %v", rt.Type())
|
||||
}
|
||||
@@ -127,8 +129,8 @@ func (e *State) walkUnary(node *parse.UnaryNode) (Results, error) {
|
||||
return newResults, nil
|
||||
}
|
||||
|
||||
func unarySeries(s Series, op string) (Series, error) {
|
||||
newSeries := NewSeries(s.GetName(), s.GetLabels(), s.TimeIdx, s.TimeIsNullable, s.ValueIdx, s.ValueIsNullabe, s.Len())
|
||||
func (e *State) unarySeries(s Series, op string) (Series, error) {
|
||||
newSeries := NewSeries(e.RefID, s.GetLabels(), s.TimeIdx, s.TimeIsNullable, s.ValueIdx, s.ValueIsNullabe, s.Len())
|
||||
for i := 0; i < s.Len(); i++ {
|
||||
t, f := s.GetPoint(i)
|
||||
if f == nil {
|
||||
@@ -148,8 +150,8 @@ func unarySeries(s Series, op string) (Series, error) {
|
||||
return newSeries, nil
|
||||
}
|
||||
|
||||
func unaryNumber(n Number, op string) (Number, error) {
|
||||
newNumber := NewNumber(n.GetName(), n.GetLabels())
|
||||
func (e *State) unaryNumber(n Number, op string) (Number, error) {
|
||||
newNumber := NewNumber(e.RefID, n.GetLabels())
|
||||
|
||||
f := n.GetFloat64Value()
|
||||
if f != nil {
|
||||
@@ -253,7 +255,6 @@ func (e *State) walkBinary(node *parse.BinaryNode) (Results, error) {
|
||||
}
|
||||
unions := union(ar, br)
|
||||
for _, uni := range unions {
|
||||
name := uni.Labels.String()
|
||||
var value Value
|
||||
switch at := uni.A.(type) {
|
||||
case Scalar:
|
||||
@@ -263,7 +264,7 @@ func (e *State) walkBinary(node *parse.BinaryNode) (Results, error) {
|
||||
case Scalar:
|
||||
bFloat := bt.GetFloat64Value()
|
||||
if aFloat == nil || bFloat == nil {
|
||||
value = NewScalar(nil)
|
||||
value = NewScalar(e.RefID, nil)
|
||||
break
|
||||
}
|
||||
f := math.NaN()
|
||||
@@ -273,13 +274,13 @@ func (e *State) walkBinary(node *parse.BinaryNode) (Results, error) {
|
||||
return res, err
|
||||
}
|
||||
}
|
||||
value = NewScalar(&f)
|
||||
value = NewScalar(e.RefID, &f)
|
||||
// Scalar op Scalar
|
||||
case Number:
|
||||
value, err = biScalarNumber(name, uni.Labels, node.OpStr, bt, aFloat, false)
|
||||
value, err = e.biScalarNumber(uni.Labels, node.OpStr, bt, aFloat, false)
|
||||
// Scalar op Series
|
||||
case Series:
|
||||
value, err = biSeriesNumber(name, uni.Labels, node.OpStr, bt, aFloat, false)
|
||||
value, err = e.biSeriesNumber(uni.Labels, node.OpStr, bt, aFloat, false)
|
||||
default:
|
||||
return res, fmt.Errorf("not implemented: binary %v on %T and %T", node.OpStr, uni.A, uni.B)
|
||||
}
|
||||
@@ -288,14 +289,14 @@ func (e *State) walkBinary(node *parse.BinaryNode) (Results, error) {
|
||||
// Series Op Scalar
|
||||
case Scalar:
|
||||
bFloat := bt.GetFloat64Value()
|
||||
value, err = biSeriesNumber(name, uni.Labels, node.OpStr, at, bFloat, true)
|
||||
value, err = e.biSeriesNumber(uni.Labels, node.OpStr, at, bFloat, true)
|
||||
// case Series Op Number
|
||||
case Number:
|
||||
bFloat := bt.GetFloat64Value()
|
||||
value, err = biSeriesNumber(name, uni.Labels, node.OpStr, at, bFloat, true)
|
||||
value, err = e.biSeriesNumber(uni.Labels, node.OpStr, at, bFloat, true)
|
||||
// case Series op Series
|
||||
case Series:
|
||||
value, err = biSeriesSeries(name, uni.Labels, node.OpStr, at, bt)
|
||||
value, err = e.biSeriesSeries(uni.Labels, node.OpStr, at, bt)
|
||||
default:
|
||||
return res, fmt.Errorf("not implemented: binary %v on %T and %T", node.OpStr, uni.A, uni.B)
|
||||
}
|
||||
@@ -304,12 +305,12 @@ func (e *State) walkBinary(node *parse.BinaryNode) (Results, error) {
|
||||
switch bt := uni.B.(type) {
|
||||
case Scalar:
|
||||
bFloat := bt.GetFloat64Value()
|
||||
value, err = biScalarNumber(name, uni.Labels, node.OpStr, at, bFloat, true)
|
||||
value, err = e.biScalarNumber(uni.Labels, node.OpStr, at, bFloat, true)
|
||||
case Number:
|
||||
bFloat := bt.GetFloat64Value()
|
||||
value, err = biScalarNumber(name, uni.Labels, node.OpStr, at, bFloat, true)
|
||||
value, err = e.biScalarNumber(uni.Labels, node.OpStr, at, bFloat, true)
|
||||
case Series:
|
||||
value, err = biSeriesNumber(name, uni.Labels, node.OpStr, bt, aFloat, false)
|
||||
value, err = e.biSeriesNumber(uni.Labels, node.OpStr, bt, aFloat, false)
|
||||
default:
|
||||
return res, fmt.Errorf("not implemented: binary %v on %T and %T", node.OpStr, uni.A, uni.B)
|
||||
}
|
||||
@@ -409,8 +410,8 @@ func binaryOp(op string, a, b float64) (r float64, err error) {
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func biScalarNumber(name string, labels data.Labels, op string, number Number, scalarVal *float64, numberFirst bool) (Number, error) {
|
||||
newNumber := NewNumber(name, labels)
|
||||
func (e *State) biScalarNumber(labels data.Labels, op string, number Number, scalarVal *float64, numberFirst bool) (Number, error) {
|
||||
newNumber := NewNumber(e.RefID, labels)
|
||||
f := number.GetFloat64Value()
|
||||
if f == nil || scalarVal == nil {
|
||||
newNumber.SetValue(nil)
|
||||
@@ -430,8 +431,8 @@ func biScalarNumber(name string, labels data.Labels, op string, number Number, s
|
||||
return newNumber, nil
|
||||
}
|
||||
|
||||
func biSeriesNumber(name string, labels data.Labels, op string, s Series, scalarVal *float64, seriesFirst bool) (Series, error) {
|
||||
newSeries := NewSeries(name, labels, s.TimeIdx, s.TimeIsNullable, s.ValueIdx, s.ValueIsNullabe, s.Len())
|
||||
func (e *State) biSeriesNumber(labels data.Labels, op string, s Series, scalarVal *float64, seriesFirst bool) (Series, error) {
|
||||
newSeries := NewSeries(e.RefID, labels, s.TimeIdx, s.TimeIsNullable, s.ValueIdx, s.ValueIsNullabe, s.Len())
|
||||
var err error
|
||||
for i := 0; i < s.Len(); i++ {
|
||||
nF := math.NaN()
|
||||
@@ -460,7 +461,7 @@ func biSeriesNumber(name string, labels data.Labels, op string, s Series, scalar
|
||||
// ... if would you like some series with your series and then get some series, or is that enough series?
|
||||
// biSeriesSeries performs a the binary operation for each value in the two series where the times
|
||||
// are equal. If there are datapoints in A or B that do not share a time, they will be dropped.
|
||||
func biSeriesSeries(name string, labels data.Labels, op string, aSeries, bSeries Series) (Series, error) {
|
||||
func (e *State) biSeriesSeries(labels data.Labels, op string, aSeries, bSeries Series) (Series, error) {
|
||||
bPoints := make(map[time.Time]*float64)
|
||||
for i := 0; i < bSeries.Len(); i++ {
|
||||
t, f := bSeries.GetPoint(i)
|
||||
@@ -469,7 +470,7 @@ func biSeriesSeries(name string, labels data.Labels, op string, aSeries, bSeries
|
||||
}
|
||||
}
|
||||
|
||||
newSeries := NewSeries(name, labels, aSeries.TimeIdx, aSeries.TimeIsNullable || bSeries.TimeIsNullable, aSeries.ValueIdx, aSeries.ValueIsNullabe || bSeries.ValueIsNullabe, 0)
|
||||
newSeries := NewSeries(e.RefID, labels, aSeries.TimeIdx, aSeries.TimeIsNullable || bSeries.TimeIsNullable, aSeries.ValueIdx, aSeries.ValueIsNullabe || bSeries.ValueIsNullabe, 0)
|
||||
for aIdx := 0; aIdx < aSeries.Len(); aIdx++ {
|
||||
aTime, aF := aSeries.GetPoint(aIdx)
|
||||
bF, ok := bPoints[*aTime]
|
||||
@@ -505,7 +506,7 @@ func (e *State) walkFunc(node *parse.FuncNode) (Results, error) {
|
||||
case *parse.VarNode:
|
||||
v = e.Vars[t.Name]
|
||||
case *parse.ScalarNode:
|
||||
v = NewScalarResults(&t.Float64)
|
||||
v = NewScalarResults(e.RefID, &t.Float64)
|
||||
case *parse.FuncNode:
|
||||
v, err = e.walkFunc(t)
|
||||
case *parse.UnaryNode:
|
||||
|
||||
Reference in New Issue
Block a user