2013-01-11 01:27:03 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2013-01-12 20:22:59 +00:00
|
|
|
"code.google.com/p/gorest"
|
2013-01-11 01:27:03 +00:00
|
|
|
"github.com/matttproud/prometheus/rules"
|
|
|
|
"github.com/matttproud/prometheus/rules/ast"
|
2013-01-15 10:30:55 +00:00
|
|
|
"sort"
|
2013-01-12 20:22:59 +00:00
|
|
|
"time"
|
2013-01-11 01:27:03 +00:00
|
|
|
)
|
2013-01-12 20:22:59 +00:00
|
|
|
|
2013-01-15 10:30:55 +00:00
|
|
|
func (serv MetricsService) Query(Expr string, Json string) (result string) {
|
2013-01-12 20:22:59 +00:00
|
|
|
exprNode, err := rules.LoadExprFromString(Expr)
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
2013-01-11 01:27:03 +00:00
|
|
|
|
2013-01-12 20:22:59 +00:00
|
|
|
timestamp := time.Now()
|
2013-01-11 01:27:03 +00:00
|
|
|
|
2013-01-12 20:22:59 +00:00
|
|
|
rb := serv.ResponseBuilder()
|
|
|
|
var format ast.OutputFormat
|
|
|
|
if Json != "" {
|
|
|
|
format = ast.JSON
|
|
|
|
rb.SetContentType(gorest.Application_Json)
|
|
|
|
} else {
|
|
|
|
format = ast.TEXT
|
|
|
|
rb.SetContentType(gorest.Text_Plain)
|
|
|
|
}
|
2013-01-11 02:17:58 +00:00
|
|
|
|
2013-01-12 20:22:59 +00:00
|
|
|
return ast.EvalToString(exprNode, ×tamp, format)
|
2013-01-11 01:27:03 +00:00
|
|
|
}
|
2013-01-15 10:30:55 +00:00
|
|
|
|
|
|
|
func (serv MetricsService) QueryRange(Expr string, End int64, Range int64, Step int64) string {
|
|
|
|
exprNode, err := rules.LoadExprFromString(Expr)
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
if exprNode.Type() != ast.VECTOR {
|
|
|
|
return "Expression does not evaluate to vector type" // TODO return errors correctly everywhere
|
|
|
|
}
|
|
|
|
rb := serv.ResponseBuilder()
|
|
|
|
rb.SetContentType(gorest.Application_Json)
|
|
|
|
|
|
|
|
if End == 0 {
|
|
|
|
End = time.Now().Unix()
|
|
|
|
}
|
|
|
|
|
|
|
|
if Step < 1 {
|
|
|
|
Step = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if End - Range < 0 {
|
|
|
|
Range = End
|
|
|
|
}
|
|
|
|
|
|
|
|
// Align the start to step "tick" boundary.
|
|
|
|
End -= End % Step
|
|
|
|
|
|
|
|
matrix := ast.EvalVectorRange(
|
|
|
|
exprNode.(ast.VectorNode),
|
|
|
|
time.Unix(End - Range, 0),
|
|
|
|
time.Unix(End, 0),
|
|
|
|
time.Duration(Step) * time.Second)
|
|
|
|
|
|
|
|
sort.Sort(matrix)
|
|
|
|
return ast.TypedValueToJSON(matrix, "matrix")
|
|
|
|
}
|