Add count_scalar() function.

Change-Id: I63f09dd0479d0a6b016f5f857dd39dcbda56c7f9
This commit is contained in:
Julius Volz 2014-01-30 13:07:26 +01:00
parent 18d9d00100
commit 7e9ecaac3a
3 changed files with 24 additions and 1 deletions

View file

@ -265,7 +265,18 @@ func scalarImpl(timestamp clientmodel.Timestamp, view *viewAdapter, args []Node)
return clientmodel.SampleValue(v[0].Value)
}
// === count_scalar(vector VectorNode) model.SampleValue ===
func countScalarImpl(timestamp clientmodel.Timestamp, view *viewAdapter, args []Node) interface{} {
return clientmodel.SampleValue(len(args[0].(VectorNode).Eval(timestamp, view)))
}
var functions = map[string]*Function{
"count_scalar": {
name: "count_scalar",
argTypes: []ExprType{VECTOR},
returnType: SCALAR,
callFn: countScalarImpl,
},
"delta": {
name: "delta",
argTypes: []ExprType{MATRIX, SCALAR},

View file

@ -159,7 +159,7 @@ func EvalToString(node Node, timestamp clientmodel.Timestamp, format OutputForma
evalTimer.Stop()
switch format {
case TEXT:
return fmt.Sprintf("scalar: %v", scalar)
return fmt.Sprintf("scalar: %v @[%v]", scalar, timestamp)
case JSON:
return TypedValueToJSON(scalar, "scalar")
}

View file

@ -382,6 +382,18 @@ func TestExpressions(t *testing.T) {
output: []string{`testcounter_reset_end => -90 @[%v]`},
fullRanges: 1,
intervalRanges: 0,
}, {
// count_scalar for a non-empty vector should return scalar element count.
expr: `count_scalar(http_requests)`,
output: []string{`scalar: 8 @[%v]`},
fullRanges: 0,
intervalRanges: 8,
}, {
// count_scalar for an empty vector should return scalar 0.
expr: `count_scalar(nonexistent)`,
output: []string{`scalar: 0 @[%v]`},
fullRanges: 0,
intervalRanges: 0,
}, {
// Empty expressions shouldn"t parse.
expr: ``,