From be8024e18c010495efebc62c0252f2577d4d4111 Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Tue, 17 Sep 2013 14:59:31 +0200 Subject: [PATCH] Add scalar() function. Change-Id: I1d1183e926a18fc98c9e94bbb9a808a3fb313102 --- rules/ast/functions.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/rules/ast/functions.go b/rules/ast/functions.go index da3a9c765..2f541d7df 100644 --- a/rules/ast/functions.go +++ b/rules/ast/functions.go @@ -16,6 +16,7 @@ package ast import ( "errors" "fmt" + "math" "sort" "time" @@ -255,6 +256,15 @@ func sampleVectorImpl(timestamp time.Time, view *viewAdapter, args []Node) inter } } +// === scalar(node *VectorNode) Scalar === +func scalarImpl(timestamp time.Time, view *viewAdapter, args []Node) interface{} { + v := args[0].(VectorNode).Eval(timestamp, view) + if len(v) != 1 { + return clientmodel.SampleValue(math.NaN()) + } + return clientmodel.SampleValue(v[0].Value) +} + var functions = map[string]*Function{ "delta": { name: "delta", @@ -274,6 +284,12 @@ var functions = map[string]*Function{ returnType: VECTOR, callFn: sampleVectorImpl, }, + "scalar": { + name: "scalar", + argTypes: []ExprType{VECTOR}, + returnType: SCALAR, + callFn: scalarImpl, + }, "sort": { name: "sort", argTypes: []ExprType{VECTOR},