mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
Rename remaining all-caps constants in AST layer.
Change-Id: Ibe97e30981969056ffcdb89e63c1468ea1ffa140
This commit is contained in:
parent
895523ad14
commit
cc27fb8aab
112
rules/ast/ast.go
112
rules/ast/ast.go
|
@ -72,10 +72,10 @@ type ExprType int
|
||||||
// because sometimes we need to pass around just the type without an object of
|
// because sometimes we need to pass around just the type without an object of
|
||||||
// that type.
|
// that type.
|
||||||
const (
|
const (
|
||||||
SCALAR ExprType = iota
|
ScalarType ExprType = iota
|
||||||
VECTOR
|
VectorType
|
||||||
MATRIX
|
MatrixType
|
||||||
STRING
|
StringType
|
||||||
)
|
)
|
||||||
|
|
||||||
// BinOpType is an enum for binary operator types.
|
// BinOpType is an enum for binary operator types.
|
||||||
|
@ -83,26 +83,26 @@ type BinOpType int
|
||||||
|
|
||||||
// Possible binary operator types.
|
// Possible binary operator types.
|
||||||
const (
|
const (
|
||||||
ADD BinOpType = iota
|
Add BinOpType = iota
|
||||||
SUB
|
Sub
|
||||||
MUL
|
Mul
|
||||||
DIV
|
Div
|
||||||
MOD
|
Mod
|
||||||
NE
|
NE
|
||||||
EQ
|
EQ
|
||||||
GT
|
GT
|
||||||
LT
|
LT
|
||||||
GE
|
GE
|
||||||
LE
|
LE
|
||||||
AND
|
And
|
||||||
OR
|
Or
|
||||||
)
|
)
|
||||||
|
|
||||||
// shouldDropMetric indicates whether the metric name should be dropped after
|
// shouldDropMetric indicates whether the metric name should be dropped after
|
||||||
// applying this operator to a vector.
|
// applying this operator to a vector.
|
||||||
func (opType BinOpType) shouldDropMetric() bool {
|
func (opType BinOpType) shouldDropMetric() bool {
|
||||||
switch opType {
|
switch opType {
|
||||||
case ADD, SUB, MUL, DIV, MOD:
|
case Add, Sub, Mul, Div, Mod:
|
||||||
return true
|
return true
|
||||||
default:
|
default:
|
||||||
return false
|
return false
|
||||||
|
@ -114,11 +114,11 @@ type AggrType int
|
||||||
|
|
||||||
// Possible aggregation types.
|
// Possible aggregation types.
|
||||||
const (
|
const (
|
||||||
SUM AggrType = iota
|
Sum AggrType = iota
|
||||||
AVG
|
Avg
|
||||||
MIN
|
Min
|
||||||
MAX
|
Max
|
||||||
COUNT
|
Count
|
||||||
)
|
)
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
@ -274,34 +274,34 @@ type (
|
||||||
// Implementations.
|
// Implementations.
|
||||||
|
|
||||||
// Type implements the Node interface.
|
// Type implements the Node interface.
|
||||||
func (node ScalarLiteral) Type() ExprType { return SCALAR }
|
func (node ScalarLiteral) Type() ExprType { return ScalarType }
|
||||||
|
|
||||||
// Type implements the Node interface.
|
// Type implements the Node interface.
|
||||||
func (node ScalarFunctionCall) Type() ExprType { return SCALAR }
|
func (node ScalarFunctionCall) Type() ExprType { return ScalarType }
|
||||||
|
|
||||||
// Type implements the Node interface.
|
// Type implements the Node interface.
|
||||||
func (node ScalarArithExpr) Type() ExprType { return SCALAR }
|
func (node ScalarArithExpr) Type() ExprType { return ScalarType }
|
||||||
|
|
||||||
// Type implements the Node interface.
|
// Type implements the Node interface.
|
||||||
func (node VectorSelector) Type() ExprType { return VECTOR }
|
func (node VectorSelector) Type() ExprType { return VectorType }
|
||||||
|
|
||||||
// Type implements the Node interface.
|
// Type implements the Node interface.
|
||||||
func (node VectorFunctionCall) Type() ExprType { return VECTOR }
|
func (node VectorFunctionCall) Type() ExprType { return VectorType }
|
||||||
|
|
||||||
// Type implements the Node interface.
|
// Type implements the Node interface.
|
||||||
func (node VectorAggregation) Type() ExprType { return VECTOR }
|
func (node VectorAggregation) Type() ExprType { return VectorType }
|
||||||
|
|
||||||
// Type implements the Node interface.
|
// Type implements the Node interface.
|
||||||
func (node VectorArithExpr) Type() ExprType { return VECTOR }
|
func (node VectorArithExpr) Type() ExprType { return VectorType }
|
||||||
|
|
||||||
// Type implements the Node interface.
|
// Type implements the Node interface.
|
||||||
func (node MatrixSelector) Type() ExprType { return MATRIX }
|
func (node MatrixSelector) Type() ExprType { return MatrixType }
|
||||||
|
|
||||||
// Type implements the Node interface.
|
// Type implements the Node interface.
|
||||||
func (node StringLiteral) Type() ExprType { return STRING }
|
func (node StringLiteral) Type() ExprType { return StringType }
|
||||||
|
|
||||||
// Type implements the Node interface.
|
// Type implements the Node interface.
|
||||||
func (node StringFunctionCall) Type() ExprType { return STRING }
|
func (node StringFunctionCall) Type() ExprType { return StringType }
|
||||||
|
|
||||||
// Children implements the Node interface and returns an empty slice.
|
// Children implements the Node interface and returns an empty slice.
|
||||||
func (node ScalarLiteral) Children() Nodes { return Nodes{} }
|
func (node ScalarLiteral) Children() Nodes { return Nodes{} }
|
||||||
|
@ -458,9 +458,9 @@ func (node *VectorAggregation) groupedAggregationsToVector(aggregations map[uint
|
||||||
vector := Vector{}
|
vector := Vector{}
|
||||||
for _, aggregation := range aggregations {
|
for _, aggregation := range aggregations {
|
||||||
switch node.aggrType {
|
switch node.aggrType {
|
||||||
case AVG:
|
case Avg:
|
||||||
aggregation.value = aggregation.value / clientmodel.SampleValue(aggregation.groupCount)
|
aggregation.value = aggregation.value / clientmodel.SampleValue(aggregation.groupCount)
|
||||||
case COUNT:
|
case Count:
|
||||||
aggregation.value = clientmodel.SampleValue(aggregation.groupCount)
|
aggregation.value = clientmodel.SampleValue(aggregation.groupCount)
|
||||||
default:
|
default:
|
||||||
// For other aggregations, we already have the right value.
|
// For other aggregations, we already have the right value.
|
||||||
|
@ -488,20 +488,20 @@ func (node *VectorAggregation) Eval(timestamp clientmodel.Timestamp) Vector {
|
||||||
}
|
}
|
||||||
|
|
||||||
switch node.aggrType {
|
switch node.aggrType {
|
||||||
case SUM:
|
case Sum:
|
||||||
groupedResult.value += sample.Value
|
groupedResult.value += sample.Value
|
||||||
case AVG:
|
case Avg:
|
||||||
groupedResult.value += sample.Value
|
groupedResult.value += sample.Value
|
||||||
groupedResult.groupCount++
|
groupedResult.groupCount++
|
||||||
case MAX:
|
case Max:
|
||||||
if groupedResult.value < sample.Value {
|
if groupedResult.value < sample.Value {
|
||||||
groupedResult.value = sample.Value
|
groupedResult.value = sample.Value
|
||||||
}
|
}
|
||||||
case MIN:
|
case Min:
|
||||||
if groupedResult.value > sample.Value {
|
if groupedResult.value > sample.Value {
|
||||||
groupedResult.value = sample.Value
|
groupedResult.value = sample.Value
|
||||||
}
|
}
|
||||||
case COUNT:
|
case Count:
|
||||||
groupedResult.groupCount++
|
groupedResult.groupCount++
|
||||||
default:
|
default:
|
||||||
panic("Unknown aggregation type")
|
panic("Unknown aggregation type")
|
||||||
|
@ -626,18 +626,18 @@ func evalScalarBinop(opType BinOpType,
|
||||||
lhs clientmodel.SampleValue,
|
lhs clientmodel.SampleValue,
|
||||||
rhs clientmodel.SampleValue) clientmodel.SampleValue {
|
rhs clientmodel.SampleValue) clientmodel.SampleValue {
|
||||||
switch opType {
|
switch opType {
|
||||||
case ADD:
|
case Add:
|
||||||
return lhs + rhs
|
return lhs + rhs
|
||||||
case SUB:
|
case Sub:
|
||||||
return lhs - rhs
|
return lhs - rhs
|
||||||
case MUL:
|
case Mul:
|
||||||
return lhs * rhs
|
return lhs * rhs
|
||||||
case DIV:
|
case Div:
|
||||||
if rhs != 0 {
|
if rhs != 0 {
|
||||||
return lhs / rhs
|
return lhs / rhs
|
||||||
}
|
}
|
||||||
return clientmodel.SampleValue(math.Inf(int(rhs)))
|
return clientmodel.SampleValue(math.Inf(int(rhs)))
|
||||||
case MOD:
|
case Mod:
|
||||||
if rhs != 0 {
|
if rhs != 0 {
|
||||||
return clientmodel.SampleValue(int(lhs) % int(rhs))
|
return clientmodel.SampleValue(int(lhs) % int(rhs))
|
||||||
}
|
}
|
||||||
|
@ -680,18 +680,18 @@ func evalVectorBinop(opType BinOpType,
|
||||||
lhs clientmodel.SampleValue,
|
lhs clientmodel.SampleValue,
|
||||||
rhs clientmodel.SampleValue) (clientmodel.SampleValue, bool) {
|
rhs clientmodel.SampleValue) (clientmodel.SampleValue, bool) {
|
||||||
switch opType {
|
switch opType {
|
||||||
case ADD:
|
case Add:
|
||||||
return lhs + rhs, true
|
return lhs + rhs, true
|
||||||
case SUB:
|
case Sub:
|
||||||
return lhs - rhs, true
|
return lhs - rhs, true
|
||||||
case MUL:
|
case Mul:
|
||||||
return lhs * rhs, true
|
return lhs * rhs, true
|
||||||
case DIV:
|
case Div:
|
||||||
if rhs != 0 {
|
if rhs != 0 {
|
||||||
return lhs / rhs, true
|
return lhs / rhs, true
|
||||||
}
|
}
|
||||||
return clientmodel.SampleValue(math.Inf(int(rhs))), true
|
return clientmodel.SampleValue(math.Inf(int(rhs))), true
|
||||||
case MOD:
|
case Mod:
|
||||||
if rhs != 0 {
|
if rhs != 0 {
|
||||||
return clientmodel.SampleValue(int(lhs) % int(rhs)), true
|
return clientmodel.SampleValue(int(lhs) % int(rhs)), true
|
||||||
}
|
}
|
||||||
|
@ -726,9 +726,9 @@ func evalVectorBinop(opType BinOpType,
|
||||||
return lhs, true
|
return lhs, true
|
||||||
}
|
}
|
||||||
return 0, false
|
return 0, false
|
||||||
case AND:
|
case And:
|
||||||
return lhs, true
|
return lhs, true
|
||||||
case OR:
|
case Or:
|
||||||
return lhs, true // TODO: implement OR
|
return lhs, true // TODO: implement OR
|
||||||
}
|
}
|
||||||
panic("Not all enum values enumerated in switch")
|
panic("Not all enum values enumerated in switch")
|
||||||
|
@ -747,7 +747,7 @@ func labelsEqual(labels1, labels2 clientmodel.Metric) bool {
|
||||||
// the expression.
|
// the expression.
|
||||||
func (node *VectorArithExpr) Eval(timestamp clientmodel.Timestamp) Vector {
|
func (node *VectorArithExpr) Eval(timestamp clientmodel.Timestamp) Vector {
|
||||||
result := Vector{}
|
result := Vector{}
|
||||||
if node.lhs.Type() == SCALAR && node.rhs.Type() == VECTOR {
|
if node.lhs.Type() == ScalarType && node.rhs.Type() == VectorType {
|
||||||
lhs := node.lhs.(ScalarNode).Eval(timestamp)
|
lhs := node.lhs.(ScalarNode).Eval(timestamp)
|
||||||
rhs := node.rhs.(VectorNode).Eval(timestamp)
|
rhs := node.rhs.(VectorNode).Eval(timestamp)
|
||||||
for _, rhsSample := range rhs {
|
for _, rhsSample := range rhs {
|
||||||
|
@ -761,7 +761,7 @@ func (node *VectorArithExpr) Eval(timestamp clientmodel.Timestamp) Vector {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
} else if node.lhs.Type() == VECTOR && node.rhs.Type() == SCALAR {
|
} else if node.lhs.Type() == VectorType && node.rhs.Type() == ScalarType {
|
||||||
lhs := node.lhs.(VectorNode).Eval(timestamp)
|
lhs := node.lhs.(VectorNode).Eval(timestamp)
|
||||||
rhs := node.rhs.(ScalarNode).Eval(timestamp)
|
rhs := node.rhs.(ScalarNode).Eval(timestamp)
|
||||||
for _, lhsSample := range lhs {
|
for _, lhsSample := range lhs {
|
||||||
|
@ -775,7 +775,7 @@ func (node *VectorArithExpr) Eval(timestamp clientmodel.Timestamp) Vector {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
} else if node.lhs.Type() == VECTOR && node.rhs.Type() == VECTOR {
|
} else if node.lhs.Type() == VectorType && node.rhs.Type() == VectorType {
|
||||||
lhs := node.lhs.(VectorNode).Eval(timestamp)
|
lhs := node.lhs.(VectorNode).Eval(timestamp)
|
||||||
rhs := node.rhs.(VectorNode).Eval(timestamp)
|
rhs := node.rhs.(VectorNode).Eval(timestamp)
|
||||||
for _, lhsSample := range lhs {
|
for _, lhsSample := range lhs {
|
||||||
|
@ -916,17 +916,17 @@ func NewFunctionCall(function *Function, args Nodes) (Node, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
switch function.returnType {
|
switch function.returnType {
|
||||||
case SCALAR:
|
case ScalarType:
|
||||||
return &ScalarFunctionCall{
|
return &ScalarFunctionCall{
|
||||||
function: function,
|
function: function,
|
||||||
args: args,
|
args: args,
|
||||||
}, nil
|
}, nil
|
||||||
case VECTOR:
|
case VectorType:
|
||||||
return &VectorFunctionCall{
|
return &VectorFunctionCall{
|
||||||
function: function,
|
function: function,
|
||||||
args: args,
|
args: args,
|
||||||
}, nil
|
}, nil
|
||||||
case STRING:
|
case StringType:
|
||||||
return &StringFunctionCall{
|
return &StringFunctionCall{
|
||||||
function: function,
|
function: function,
|
||||||
args: args,
|
args: args,
|
||||||
|
@ -953,17 +953,17 @@ func nodesHaveTypes(nodes Nodes, exprTypes []ExprType) bool {
|
||||||
// NewArithExpr returns a (not yet evaluated) expression node (of type
|
// NewArithExpr returns a (not yet evaluated) expression node (of type
|
||||||
// VectorArithExpr or ScalarArithExpr).
|
// VectorArithExpr or ScalarArithExpr).
|
||||||
func NewArithExpr(opType BinOpType, lhs Node, rhs Node) (Node, error) {
|
func NewArithExpr(opType BinOpType, lhs Node, rhs Node) (Node, error) {
|
||||||
if !nodesHaveTypes(Nodes{lhs, rhs}, []ExprType{SCALAR, VECTOR}) {
|
if !nodesHaveTypes(Nodes{lhs, rhs}, []ExprType{ScalarType, VectorType}) {
|
||||||
return nil, errors.New("binary operands must be of vector or scalar type")
|
return nil, errors.New("binary operands must be of vector or scalar type")
|
||||||
}
|
}
|
||||||
|
|
||||||
if opType == AND || opType == OR {
|
if opType == And || opType == Or {
|
||||||
if lhs.Type() == SCALAR || rhs.Type() == SCALAR {
|
if lhs.Type() == ScalarType || rhs.Type() == ScalarType {
|
||||||
return nil, errors.New("AND and OR operators may only be used between vectors")
|
return nil, errors.New("AND and OR operators may only be used between vectors")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if lhs.Type() == VECTOR || rhs.Type() == VECTOR {
|
if lhs.Type() == VectorType || rhs.Type() == VectorType {
|
||||||
return &VectorArithExpr{
|
return &VectorArithExpr{
|
||||||
opType: opType,
|
opType: opType,
|
||||||
lhs: lhs,
|
lhs: lhs,
|
||||||
|
|
|
@ -46,19 +46,19 @@ func (function *Function) CheckArgTypes(args []Node) error {
|
||||||
for idx, argType := range function.argTypes {
|
for idx, argType := range function.argTypes {
|
||||||
invalidType := false
|
invalidType := false
|
||||||
var expectedType string
|
var expectedType string
|
||||||
if _, ok := args[idx].(ScalarNode); argType == SCALAR && !ok {
|
if _, ok := args[idx].(ScalarNode); argType == ScalarType && !ok {
|
||||||
invalidType = true
|
invalidType = true
|
||||||
expectedType = "scalar"
|
expectedType = "scalar"
|
||||||
}
|
}
|
||||||
if _, ok := args[idx].(VectorNode); argType == VECTOR && !ok {
|
if _, ok := args[idx].(VectorNode); argType == VectorType && !ok {
|
||||||
invalidType = true
|
invalidType = true
|
||||||
expectedType = "vector"
|
expectedType = "vector"
|
||||||
}
|
}
|
||||||
if _, ok := args[idx].(MatrixNode); argType == MATRIX && !ok {
|
if _, ok := args[idx].(MatrixNode); argType == MatrixType && !ok {
|
||||||
invalidType = true
|
invalidType = true
|
||||||
expectedType = "matrix"
|
expectedType = "matrix"
|
||||||
}
|
}
|
||||||
if _, ok := args[idx].(StringNode); argType == STRING && !ok {
|
if _, ok := args[idx].(StringNode); argType == StringType && !ok {
|
||||||
invalidType = true
|
invalidType = true
|
||||||
expectedType = "string"
|
expectedType = "string"
|
||||||
}
|
}
|
||||||
|
@ -409,104 +409,104 @@ func absentImpl(timestamp clientmodel.Timestamp, args []Node) interface{} {
|
||||||
var functions = map[string]*Function{
|
var functions = map[string]*Function{
|
||||||
"abs": {
|
"abs": {
|
||||||
name: "abs",
|
name: "abs",
|
||||||
argTypes: []ExprType{VECTOR},
|
argTypes: []ExprType{VectorType},
|
||||||
returnType: VECTOR,
|
returnType: VectorType,
|
||||||
callFn: absImpl,
|
callFn: absImpl,
|
||||||
},
|
},
|
||||||
"absent": {
|
"absent": {
|
||||||
name: "absent",
|
name: "absent",
|
||||||
argTypes: []ExprType{VECTOR},
|
argTypes: []ExprType{VectorType},
|
||||||
returnType: VECTOR,
|
returnType: VectorType,
|
||||||
callFn: absentImpl,
|
callFn: absentImpl,
|
||||||
},
|
},
|
||||||
"avg_over_time": {
|
"avg_over_time": {
|
||||||
name: "avg_over_time",
|
name: "avg_over_time",
|
||||||
argTypes: []ExprType{MATRIX},
|
argTypes: []ExprType{MatrixType},
|
||||||
returnType: VECTOR,
|
returnType: VectorType,
|
||||||
callFn: avgOverTimeImpl,
|
callFn: avgOverTimeImpl,
|
||||||
},
|
},
|
||||||
"bottomk": {
|
"bottomk": {
|
||||||
name: "bottomk",
|
name: "bottomk",
|
||||||
argTypes: []ExprType{SCALAR, VECTOR},
|
argTypes: []ExprType{ScalarType, VectorType},
|
||||||
returnType: VECTOR,
|
returnType: VectorType,
|
||||||
callFn: bottomkImpl,
|
callFn: bottomkImpl,
|
||||||
},
|
},
|
||||||
"count_over_time": {
|
"count_over_time": {
|
||||||
name: "count_over_time",
|
name: "count_over_time",
|
||||||
argTypes: []ExprType{MATRIX},
|
argTypes: []ExprType{MatrixType},
|
||||||
returnType: VECTOR,
|
returnType: VectorType,
|
||||||
callFn: countOverTimeImpl,
|
callFn: countOverTimeImpl,
|
||||||
},
|
},
|
||||||
"count_scalar": {
|
"count_scalar": {
|
||||||
name: "count_scalar",
|
name: "count_scalar",
|
||||||
argTypes: []ExprType{VECTOR},
|
argTypes: []ExprType{VectorType},
|
||||||
returnType: SCALAR,
|
returnType: ScalarType,
|
||||||
callFn: countScalarImpl,
|
callFn: countScalarImpl,
|
||||||
},
|
},
|
||||||
"delta": {
|
"delta": {
|
||||||
name: "delta",
|
name: "delta",
|
||||||
argTypes: []ExprType{MATRIX, SCALAR},
|
argTypes: []ExprType{MatrixType, ScalarType},
|
||||||
returnType: VECTOR,
|
returnType: VectorType,
|
||||||
callFn: deltaImpl,
|
callFn: deltaImpl,
|
||||||
},
|
},
|
||||||
"drop_common_labels": {
|
"drop_common_labels": {
|
||||||
name: "drop_common_labels",
|
name: "drop_common_labels",
|
||||||
argTypes: []ExprType{VECTOR},
|
argTypes: []ExprType{VectorType},
|
||||||
returnType: VECTOR,
|
returnType: VectorType,
|
||||||
callFn: dropCommonLabelsImpl,
|
callFn: dropCommonLabelsImpl,
|
||||||
},
|
},
|
||||||
"max_over_time": {
|
"max_over_time": {
|
||||||
name: "max_over_time",
|
name: "max_over_time",
|
||||||
argTypes: []ExprType{MATRIX},
|
argTypes: []ExprType{MatrixType},
|
||||||
returnType: VECTOR,
|
returnType: VectorType,
|
||||||
callFn: maxOverTimeImpl,
|
callFn: maxOverTimeImpl,
|
||||||
},
|
},
|
||||||
"min_over_time": {
|
"min_over_time": {
|
||||||
name: "min_over_time",
|
name: "min_over_time",
|
||||||
argTypes: []ExprType{MATRIX},
|
argTypes: []ExprType{MatrixType},
|
||||||
returnType: VECTOR,
|
returnType: VectorType,
|
||||||
callFn: minOverTimeImpl,
|
callFn: minOverTimeImpl,
|
||||||
},
|
},
|
||||||
"rate": {
|
"rate": {
|
||||||
name: "rate",
|
name: "rate",
|
||||||
argTypes: []ExprType{MATRIX},
|
argTypes: []ExprType{MatrixType},
|
||||||
returnType: VECTOR,
|
returnType: VectorType,
|
||||||
callFn: rateImpl,
|
callFn: rateImpl,
|
||||||
},
|
},
|
||||||
"scalar": {
|
"scalar": {
|
||||||
name: "scalar",
|
name: "scalar",
|
||||||
argTypes: []ExprType{VECTOR},
|
argTypes: []ExprType{VectorType},
|
||||||
returnType: SCALAR,
|
returnType: ScalarType,
|
||||||
callFn: scalarImpl,
|
callFn: scalarImpl,
|
||||||
},
|
},
|
||||||
"sort": {
|
"sort": {
|
||||||
name: "sort",
|
name: "sort",
|
||||||
argTypes: []ExprType{VECTOR},
|
argTypes: []ExprType{VectorType},
|
||||||
returnType: VECTOR,
|
returnType: VectorType,
|
||||||
callFn: sortImpl,
|
callFn: sortImpl,
|
||||||
},
|
},
|
||||||
"sort_desc": {
|
"sort_desc": {
|
||||||
name: "sort_desc",
|
name: "sort_desc",
|
||||||
argTypes: []ExprType{VECTOR},
|
argTypes: []ExprType{VectorType},
|
||||||
returnType: VECTOR,
|
returnType: VectorType,
|
||||||
callFn: sortDescImpl,
|
callFn: sortDescImpl,
|
||||||
},
|
},
|
||||||
"sum_over_time": {
|
"sum_over_time": {
|
||||||
name: "sum_over_time",
|
name: "sum_over_time",
|
||||||
argTypes: []ExprType{MATRIX},
|
argTypes: []ExprType{MatrixType},
|
||||||
returnType: VECTOR,
|
returnType: VectorType,
|
||||||
callFn: sumOverTimeImpl,
|
callFn: sumOverTimeImpl,
|
||||||
},
|
},
|
||||||
"time": {
|
"time": {
|
||||||
name: "time",
|
name: "time",
|
||||||
argTypes: []ExprType{},
|
argTypes: []ExprType{},
|
||||||
returnType: SCALAR,
|
returnType: ScalarType,
|
||||||
callFn: timeImpl,
|
callFn: timeImpl,
|
||||||
},
|
},
|
||||||
"topk": {
|
"topk": {
|
||||||
name: "topk",
|
name: "topk",
|
||||||
argTypes: []ExprType{SCALAR, VECTOR},
|
argTypes: []ExprType{ScalarType, VectorType},
|
||||||
returnType: VECTOR,
|
returnType: VectorType,
|
||||||
callFn: topkImpl,
|
callFn: topkImpl,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ import (
|
||||||
|
|
||||||
type emptyRangeNode struct{}
|
type emptyRangeNode struct{}
|
||||||
|
|
||||||
func (node emptyRangeNode) Type() ExprType { return MATRIX }
|
func (node emptyRangeNode) Type() ExprType { return MatrixType }
|
||||||
func (node emptyRangeNode) NodeTreeToDotGraph() string { return "" }
|
func (node emptyRangeNode) NodeTreeToDotGraph() string { return "" }
|
||||||
func (node emptyRangeNode) String() string { return "" }
|
func (node emptyRangeNode) String() string { return "" }
|
||||||
func (node emptyRangeNode) Children() Nodes { return Nodes{} }
|
func (node emptyRangeNode) Children() Nodes { return Nodes{} }
|
||||||
|
|
|
@ -33,46 +33,46 @@ type OutputFormat int
|
||||||
|
|
||||||
// Possible output formats.
|
// Possible output formats.
|
||||||
const (
|
const (
|
||||||
TEXT OutputFormat = iota
|
Text OutputFormat = iota
|
||||||
JSON
|
JSON
|
||||||
)
|
)
|
||||||
|
|
||||||
func (opType BinOpType) String() string {
|
func (opType BinOpType) String() string {
|
||||||
opTypeMap := map[BinOpType]string{
|
opTypeMap := map[BinOpType]string{
|
||||||
ADD: "+",
|
Add: "+",
|
||||||
SUB: "-",
|
Sub: "-",
|
||||||
MUL: "*",
|
Mul: "*",
|
||||||
DIV: "/",
|
Div: "/",
|
||||||
MOD: "%",
|
Mod: "%",
|
||||||
GT: ">",
|
GT: ">",
|
||||||
LT: "<",
|
LT: "<",
|
||||||
EQ: "==",
|
EQ: "==",
|
||||||
NE: "!=",
|
NE: "!=",
|
||||||
GE: ">=",
|
GE: ">=",
|
||||||
LE: "<=",
|
LE: "<=",
|
||||||
AND: "AND",
|
And: "AND",
|
||||||
OR: "OR",
|
Or: "OR",
|
||||||
}
|
}
|
||||||
return opTypeMap[opType]
|
return opTypeMap[opType]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (aggrType AggrType) String() string {
|
func (aggrType AggrType) String() string {
|
||||||
aggrTypeMap := map[AggrType]string{
|
aggrTypeMap := map[AggrType]string{
|
||||||
SUM: "SUM",
|
Sum: "SUM",
|
||||||
AVG: "AVG",
|
Avg: "AVG",
|
||||||
MIN: "MIN",
|
Min: "MIN",
|
||||||
MAX: "MAX",
|
Max: "MAX",
|
||||||
COUNT: "COUNT",
|
Count: "COUNT",
|
||||||
}
|
}
|
||||||
return aggrTypeMap[aggrType]
|
return aggrTypeMap[aggrType]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (exprType ExprType) String() string {
|
func (exprType ExprType) String() string {
|
||||||
exprTypeMap := map[ExprType]string{
|
exprTypeMap := map[ExprType]string{
|
||||||
SCALAR: "scalar",
|
ScalarType: "scalar",
|
||||||
VECTOR: "vector",
|
VectorType: "vector",
|
||||||
MATRIX: "matrix",
|
MatrixType: "matrix",
|
||||||
STRING: "string",
|
StringType: "string",
|
||||||
}
|
}
|
||||||
return exprTypeMap[exprType]
|
return exprTypeMap[exprType]
|
||||||
}
|
}
|
||||||
|
@ -164,38 +164,38 @@ func EvalToString(node Node, timestamp clientmodel.Timestamp, format OutputForma
|
||||||
|
|
||||||
evalTimer := queryStats.GetTimer(stats.InnerEvalTime).Start()
|
evalTimer := queryStats.GetTimer(stats.InnerEvalTime).Start()
|
||||||
switch node.Type() {
|
switch node.Type() {
|
||||||
case SCALAR:
|
case ScalarType:
|
||||||
scalar := node.(ScalarNode).Eval(timestamp)
|
scalar := node.(ScalarNode).Eval(timestamp)
|
||||||
evalTimer.Stop()
|
evalTimer.Stop()
|
||||||
switch format {
|
switch format {
|
||||||
case TEXT:
|
case Text:
|
||||||
return fmt.Sprintf("scalar: %v @[%v]", scalar, timestamp)
|
return fmt.Sprintf("scalar: %v @[%v]", scalar, timestamp)
|
||||||
case JSON:
|
case JSON:
|
||||||
return TypedValueToJSON(scalar, "scalar")
|
return TypedValueToJSON(scalar, "scalar")
|
||||||
}
|
}
|
||||||
case VECTOR:
|
case VectorType:
|
||||||
vector := node.(VectorNode).Eval(timestamp)
|
vector := node.(VectorNode).Eval(timestamp)
|
||||||
evalTimer.Stop()
|
evalTimer.Stop()
|
||||||
switch format {
|
switch format {
|
||||||
case TEXT:
|
case Text:
|
||||||
return vector.String()
|
return vector.String()
|
||||||
case JSON:
|
case JSON:
|
||||||
return TypedValueToJSON(vector, "vector")
|
return TypedValueToJSON(vector, "vector")
|
||||||
}
|
}
|
||||||
case MATRIX:
|
case MatrixType:
|
||||||
matrix := node.(MatrixNode).Eval(timestamp)
|
matrix := node.(MatrixNode).Eval(timestamp)
|
||||||
evalTimer.Stop()
|
evalTimer.Stop()
|
||||||
switch format {
|
switch format {
|
||||||
case TEXT:
|
case Text:
|
||||||
return matrix.String()
|
return matrix.String()
|
||||||
case JSON:
|
case JSON:
|
||||||
return TypedValueToJSON(matrix, "matrix")
|
return TypedValueToJSON(matrix, "matrix")
|
||||||
}
|
}
|
||||||
case STRING:
|
case StringType:
|
||||||
str := node.(StringNode).Eval(timestamp)
|
str := node.(StringNode).Eval(timestamp)
|
||||||
evalTimer.Stop()
|
evalTimer.Stop()
|
||||||
switch format {
|
switch format {
|
||||||
case TEXT:
|
case Text:
|
||||||
return str
|
return str
|
||||||
case JSON:
|
case JSON:
|
||||||
return TypedValueToJSON(str, "string")
|
return TypedValueToJSON(str, "string")
|
||||||
|
@ -216,17 +216,17 @@ func EvalToVector(node Node, timestamp clientmodel.Timestamp, storage local.Stor
|
||||||
|
|
||||||
evalTimer := queryStats.GetTimer(stats.InnerEvalTime).Start()
|
evalTimer := queryStats.GetTimer(stats.InnerEvalTime).Start()
|
||||||
switch node.Type() {
|
switch node.Type() {
|
||||||
case SCALAR:
|
case ScalarType:
|
||||||
scalar := node.(ScalarNode).Eval(timestamp)
|
scalar := node.(ScalarNode).Eval(timestamp)
|
||||||
evalTimer.Stop()
|
evalTimer.Stop()
|
||||||
return Vector{&Sample{Value: scalar}}, nil
|
return Vector{&Sample{Value: scalar}}, nil
|
||||||
case VECTOR:
|
case VectorType:
|
||||||
vector := node.(VectorNode).Eval(timestamp)
|
vector := node.(VectorNode).Eval(timestamp)
|
||||||
evalTimer.Stop()
|
evalTimer.Stop()
|
||||||
return vector, nil
|
return vector, nil
|
||||||
case MATRIX:
|
case MatrixType:
|
||||||
return nil, errors.New("matrices not supported by EvalToVector")
|
return nil, errors.New("matrices not supported by EvalToVector")
|
||||||
case STRING:
|
case StringType:
|
||||||
str := node.(StringNode).Eval(timestamp)
|
str := node.(StringNode).Eval(timestamp)
|
||||||
evalTimer.Stop()
|
evalTimer.Stop()
|
||||||
return Vector{
|
return Vector{
|
||||||
|
|
|
@ -69,11 +69,11 @@ func NewVectorAggregation(aggrTypeStr string, vector ast.Node, groupBy clientmod
|
||||||
return nil, fmt.Errorf("operand of %v aggregation must be of vector type", aggrTypeStr)
|
return nil, fmt.Errorf("operand of %v aggregation must be of vector type", aggrTypeStr)
|
||||||
}
|
}
|
||||||
var aggrTypes = map[string]ast.AggrType{
|
var aggrTypes = map[string]ast.AggrType{
|
||||||
"SUM": ast.SUM,
|
"SUM": ast.Sum,
|
||||||
"MAX": ast.MAX,
|
"MAX": ast.Max,
|
||||||
"MIN": ast.MIN,
|
"MIN": ast.Min,
|
||||||
"AVG": ast.AVG,
|
"AVG": ast.Avg,
|
||||||
"COUNT": ast.COUNT,
|
"COUNT": ast.Count,
|
||||||
}
|
}
|
||||||
aggrType, ok := aggrTypes[aggrTypeStr]
|
aggrType, ok := aggrTypes[aggrTypeStr]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -85,19 +85,19 @@ func NewVectorAggregation(aggrTypeStr string, vector ast.Node, groupBy clientmod
|
||||||
// NewArithExpr is a convenience function to create a new AST arithmetic expression.
|
// NewArithExpr is a convenience function to create a new AST arithmetic expression.
|
||||||
func NewArithExpr(opTypeStr string, lhs ast.Node, rhs ast.Node) (ast.Node, error) {
|
func NewArithExpr(opTypeStr string, lhs ast.Node, rhs ast.Node) (ast.Node, error) {
|
||||||
var opTypes = map[string]ast.BinOpType{
|
var opTypes = map[string]ast.BinOpType{
|
||||||
"+": ast.ADD,
|
"+": ast.Add,
|
||||||
"-": ast.SUB,
|
"-": ast.Sub,
|
||||||
"*": ast.MUL,
|
"*": ast.Mul,
|
||||||
"/": ast.DIV,
|
"/": ast.Div,
|
||||||
"%": ast.MOD,
|
"%": ast.Mod,
|
||||||
">": ast.GT,
|
">": ast.GT,
|
||||||
"<": ast.LT,
|
"<": ast.LT,
|
||||||
"==": ast.EQ,
|
"==": ast.EQ,
|
||||||
"!=": ast.NE,
|
"!=": ast.NE,
|
||||||
">=": ast.GE,
|
">=": ast.GE,
|
||||||
"<=": ast.LE,
|
"<=": ast.LE,
|
||||||
"AND": ast.AND,
|
"AND": ast.And,
|
||||||
"OR": ast.OR,
|
"OR": ast.Or,
|
||||||
}
|
}
|
||||||
opType, ok := opTypes[opTypeStr]
|
opType, ok := opTypes[opTypeStr]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
3532
rules/lexer.l.go
3532
rules/lexer.l.go
File diff suppressed because it is too large
Load diff
|
@ -67,7 +67,7 @@ var yyToknames = []string{
|
||||||
"WITH",
|
"WITH",
|
||||||
"SUMMARY",
|
"SUMMARY",
|
||||||
"DESCRIPTION",
|
"DESCRIPTION",
|
||||||
" =",
|
"'='",
|
||||||
}
|
}
|
||||||
var yyStatenames = []string{}
|
var yyStatenames = []string{}
|
||||||
|
|
||||||
|
|
|
@ -658,7 +658,7 @@ func TestExpressions(t *testing.T) {
|
||||||
t.Errorf("%d. Test should fail, but didn't", i)
|
t.Errorf("%d. Test should fail, but didn't", i)
|
||||||
}
|
}
|
||||||
failed := false
|
failed := false
|
||||||
resultStr := ast.EvalToString(testExpr, testEvalTime, ast.TEXT, storage, stats.NewTimerGroup())
|
resultStr := ast.EvalToString(testExpr, testEvalTime, ast.Text, storage, stats.NewTimerGroup())
|
||||||
resultLines := strings.Split(resultStr, "\n")
|
resultLines := strings.Split(resultStr, "\n")
|
||||||
|
|
||||||
if len(exprTest.output) != len(resultLines) {
|
if len(exprTest.output) != len(resultLines) {
|
||||||
|
|
|
@ -54,7 +54,7 @@ func (serv MetricsService) Query(w http.ResponseWriter, r *http.Request) {
|
||||||
format = ast.JSON
|
format = ast.JSON
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
} else {
|
} else {
|
||||||
format = ast.TEXT
|
format = ast.Text
|
||||||
w.Header().Set("Content-Type", "text/plain")
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,7 +94,7 @@ func (serv MetricsService) QueryRange(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprint(w, ast.ErrorToJSON(err))
|
fmt.Fprint(w, ast.ErrorToJSON(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if exprNode.Type() != ast.VECTOR {
|
if exprNode.Type() != ast.VectorType {
|
||||||
fmt.Fprint(w, ast.ErrorToJSON(errors.New("expression does not evaluate to vector type")))
|
fmt.Fprint(w, ast.ErrorToJSON(errors.New("expression does not evaluate to vector type")))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue