| 
									
										
										
										
											2013-02-07 18:49:04 +08:00
										 |  |  | // Copyright 2013 Prometheus Team
 | 
					
						
							|  |  |  | // Licensed under the Apache License, Version 2.0 (the "License");
 | 
					
						
							|  |  |  | // you may not use this file except in compliance with the License.
 | 
					
						
							|  |  |  | // You may obtain a copy of the License at
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Unless required by applicable law or agreed to in writing, software
 | 
					
						
							|  |  |  | // distributed under the License is distributed on an "AS IS" BASIS,
 | 
					
						
							|  |  |  | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
					
						
							|  |  |  | // See the License for the specific language governing permissions and
 | 
					
						
							|  |  |  | // limitations under the License.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | package rules | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2013-01-28 01:49:45 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/model" | 
					
						
							|  |  |  | 	"github.com/prometheus/prometheus/rules/ast" | 
					
						
							| 
									
										
										
										
											2013-02-14 00:41:35 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/utility" | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func CreateRule(name string, labels model.LabelSet, root ast.Node, permanent bool) (*Rule, error) { | 
					
						
							|  |  |  | 	if root.Type() != ast.VECTOR { | 
					
						
							| 
									
										
										
										
											2013-02-14 00:41:35 +08:00
										 |  |  | 		return nil, fmt.Errorf("Rule %v does not evaluate to vector type", name) | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return NewRule(name, labels, root.(ast.VectorNode), permanent), nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewFunctionCall(name string, args []ast.Node) (ast.Node, error) { | 
					
						
							|  |  |  | 	function, err := ast.GetFunction(name) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2013-02-14 00:41:35 +08:00
										 |  |  | 		return nil, fmt.Errorf("Unknown function \"%v\"", name) | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	functionCall, err := ast.NewFunctionCall(function, args) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2013-02-14 00:41:35 +08:00
										 |  |  | 		return nil, fmt.Errorf(err.Error()) | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return functionCall, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewVectorAggregation(aggrTypeStr string, vector ast.Node, groupBy []model.LabelName) (*ast.VectorAggregation, error) { | 
					
						
							|  |  |  | 	if vector.Type() != ast.VECTOR { | 
					
						
							| 
									
										
										
										
											2013-02-14 00:41:35 +08:00
										 |  |  | 		return nil, fmt.Errorf("Operand of %v aggregation must be of vector type", aggrTypeStr) | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	var aggrTypes = map[string]ast.AggrType{ | 
					
						
							|  |  |  | 		"SUM": ast.SUM, | 
					
						
							|  |  |  | 		"MAX": ast.MAX, | 
					
						
							|  |  |  | 		"MIN": ast.MIN, | 
					
						
							|  |  |  | 		"AVG": ast.AVG, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	aggrType, ok := aggrTypes[aggrTypeStr] | 
					
						
							|  |  |  | 	if !ok { | 
					
						
							| 
									
										
										
										
											2013-02-14 00:41:35 +08:00
										 |  |  | 		return nil, fmt.Errorf("Unknown aggregation type '%v'", aggrTypeStr) | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return ast.NewVectorAggregation(aggrType, vector.(ast.VectorNode), groupBy), nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewArithExpr(opTypeStr string, lhs ast.Node, rhs ast.Node) (ast.Node, error) { | 
					
						
							|  |  |  | 	var opTypes = map[string]ast.BinOpType{ | 
					
						
							|  |  |  | 		"+":   ast.ADD, | 
					
						
							|  |  |  | 		"-":   ast.SUB, | 
					
						
							|  |  |  | 		"*":   ast.MUL, | 
					
						
							|  |  |  | 		"/":   ast.DIV, | 
					
						
							|  |  |  | 		"%":   ast.MOD, | 
					
						
							|  |  |  | 		">":   ast.GT, | 
					
						
							|  |  |  | 		"<":   ast.LT, | 
					
						
							|  |  |  | 		"==":  ast.EQ, | 
					
						
							|  |  |  | 		"!=":  ast.NE, | 
					
						
							|  |  |  | 		">=":  ast.GE, | 
					
						
							|  |  |  | 		"<=":  ast.LE, | 
					
						
							|  |  |  | 		"AND": ast.AND, | 
					
						
							|  |  |  | 		"OR":  ast.OR, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	opType, ok := opTypes[opTypeStr] | 
					
						
							|  |  |  | 	if !ok { | 
					
						
							| 
									
										
										
										
											2013-02-14 00:41:35 +08:00
										 |  |  | 		return nil, fmt.Errorf("Invalid binary operator \"%v\"", opTypeStr) | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	expr, err := ast.NewArithExpr(opType, lhs, rhs) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2013-02-14 00:41:35 +08:00
										 |  |  | 		return nil, fmt.Errorf(err.Error()) | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return expr, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewMatrix(vector ast.Node, intervalStr string) (ast.MatrixNode, error) { | 
					
						
							|  |  |  | 	switch vector.(type) { | 
					
						
							|  |  |  | 	case *ast.VectorLiteral: | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			break | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2013-02-14 00:41:35 +08:00
										 |  |  | 		return nil, fmt.Errorf("Intervals are currently only supported for vector literals.") | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-02-14 00:41:35 +08:00
										 |  |  | 	interval, err := utility.StringToDuration(intervalStr) | 
					
						
							| 
									
										
										
										
											2013-01-08 06:24:26 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	vectorLiteral := vector.(*ast.VectorLiteral) | 
					
						
							|  |  |  | 	return ast.NewMatrixLiteral(vectorLiteral, interval), nil | 
					
						
							|  |  |  | } |