@ -14,6 +14,7 @@
package ast
package ast
import (
import (
"container/heap"
"fmt"
"fmt"
"math"
"math"
"sort"
"sort"
@ -22,7 +23,6 @@ import (
clientmodel "github.com/prometheus/client_golang/model"
clientmodel "github.com/prometheus/client_golang/model"
"github.com/prometheus/prometheus/storage/metric"
"github.com/prometheus/prometheus/storage/metric"
"github.com/prometheus/prometheus/utility"
)
)
// Function represents a function of the expression language and is
// Function represents a function of the expression language and is
@ -138,7 +138,7 @@ func deltaImpl(timestamp clientmodel.Timestamp, view *viewAdapter, args []Node)
return resultVector
return resultVector
}
}
// === rate(node * MatrixNode) Vector ===
// === rate(node MatrixNode) Vector ===
func rateImpl ( timestamp clientmodel . Timestamp , view * viewAdapter , args [ ] Node ) interface { } {
func rateImpl ( timestamp clientmodel . Timestamp , view * viewAdapter , args [ ] Node ) interface { } {
args = append ( args , & ScalarLiteral { value : 1 } )
args = append ( args , & ScalarLiteral { value : 1 } )
vector := deltaImpl ( timestamp , view , args ) . ( Vector )
vector := deltaImpl ( timestamp , view , args ) . ( Vector )
@ -153,40 +153,97 @@ func rateImpl(timestamp clientmodel.Timestamp, view *viewAdapter, args []Node) i
return vector
return vector
}
}
type vectorByValueSorter struct {
type vectorByValueHeap Vector
vector Vector
func ( s vectorByValueHeap ) Len ( ) int {
return len ( s )
}
}
func ( s orter vectorByValueSorter ) Len ( ) int {
func ( s vectorByValueHeap ) Less ( i , j int ) bool {
return len ( sorter . vector )
return s [ i ] . Value < s [ j ] . Value
}
}
func ( s orter vectorByValueSorter ) Less ( i , j int ) bool {
func ( s vectorByValueHeap ) Swap ( i , j int ) {
return sorter . vector [ i ] . Value < sorter . vector [ j ] . Value
s [ i ] , s [ j ] = s [ j ] , s [ i ]
}
}
func ( s orter vectorByValueSorter ) Swap ( i , j int ) {
func ( s * vectorByValueHeap ) Push ( x interface { } ) {
sorter . vector [ i ] , sorter . vector [ j ] = sorter . vector [ j ] , sorter . vector [ i ]
* s = append ( * s , x . ( * clientmodel . Sample ) )
}
}
// === sort(node *VectorNode) Vector ===
func ( s * vectorByValueHeap ) Pop ( ) interface { } {
func sortImpl ( timestamp clientmodel . Timestamp , view * viewAdapter , args [ ] Node ) interface { } {
old := * s
byValueSorter := vectorByValueSorter {
n := len ( old )
vector : args [ 0 ] . ( VectorNode ) . Eval ( timestamp , view ) ,
el := old [ n - 1 ]
* s = old [ 0 : n - 1 ]
return el
}
type reverseHeap struct {
heap . Interface
}
}
func ( s reverseHeap ) Less ( i , j int ) bool {
return s . Interface . Less ( j , i )
}
// === sort(node VectorNode) Vector ===
func sortImpl ( timestamp clientmodel . Timestamp , view * viewAdapter , args [ ] Node ) interface { } {
byValueSorter := vectorByValueHeap ( args [ 0 ] . ( VectorNode ) . Eval ( timestamp , view ) )
sort . Sort ( byValueSorter )
sort . Sort ( byValueSorter )
return byValueSorter . vector
return Vector ( byValueSorter )
}
}
// === sortDesc(node *VectorNode) Vector ===
// === sortDesc(node VectorNode) Vector ===
func sortDescImpl ( timestamp clientmodel . Timestamp , view * viewAdapter , args [ ] Node ) interface { } {
func sortDescImpl ( timestamp clientmodel . Timestamp , view * viewAdapter , args [ ] Node ) interface { } {
descByValueSorter := utility . ReverseSorter {
byValueSorter := vectorByValueHeap ( args [ 0 ] . ( VectorNode ) . Eval ( timestamp , view ) )
Interface : vectorByValueSorter {
sort . Sort ( sort . Reverse ( byValueSorter ) )
vector : args [ 0 ] . ( VectorNode ) . Eval ( timestamp , view ) ,
return Vector ( byValueSorter )
} ,
}
// === topk(k ScalarNode, node VectorNode) Vector ===
func topkImpl ( timestamp clientmodel . Timestamp , view * viewAdapter , args [ ] Node ) interface { } {
k := int ( args [ 0 ] . ( ScalarNode ) . Eval ( timestamp , view ) )
if k < 1 {
return Vector { }
}
topk := make ( vectorByValueHeap , 0 , k )
vector := args [ 1 ] . ( VectorNode ) . Eval ( timestamp , view )
for _ , el := range vector {
if len ( topk ) < k || topk [ 0 ] . Value < el . Value {
if len ( topk ) == k {
heap . Pop ( & topk )
}
}
sort . Sort ( descByValueSorter )
heap . Push ( & topk , el )
return descByValueSorter . Interface . ( vectorByValueSorter ) . vector
}
}
sort . Sort ( sort . Reverse ( topk ) )
return Vector ( topk )
}
// === bottomk(k ScalarNode, node VectorNode) Vector ===
func bottomkImpl ( timestamp clientmodel . Timestamp , view * viewAdapter , args [ ] Node ) interface { } {
k := int ( args [ 0 ] . ( ScalarNode ) . Eval ( timestamp , view ) )
if k < 1 {
return Vector { }
}
bottomk := make ( vectorByValueHeap , 0 , k )
bkHeap := reverseHeap { Interface : & bottomk }
vector := args [ 1 ] . ( VectorNode ) . Eval ( timestamp , view )
for _ , el := range vector {
if len ( bottomk ) < k || bottomk [ 0 ] . Value > el . Value {
if len ( bottomk ) == k {
heap . Pop ( & bkHeap )
}
heap . Push ( & bkHeap , el )
}
}
sort . Sort ( bottomk )
return Vector ( bottomk )
}
}
// === sampleVectorImpl() Vector ===
// === sampleVectorImpl() Vector ===
@ -262,7 +319,7 @@ func sampleVectorImpl(timestamp clientmodel.Timestamp, view *viewAdapter, args [
}
}
}
}
// === scalar(node * VectorNode) Scalar ===
// === scalar(node VectorNode) Scalar ===
func scalarImpl ( timestamp clientmodel . Timestamp , view * viewAdapter , args [ ] Node ) interface { } {
func scalarImpl ( timestamp clientmodel . Timestamp , view * viewAdapter , args [ ] Node ) interface { } {
v := args [ 0 ] . ( VectorNode ) . Eval ( timestamp , view )
v := args [ 0 ] . ( VectorNode ) . Eval ( timestamp , view )
if len ( v ) != 1 {
if len ( v ) != 1 {
@ -369,6 +426,12 @@ var functions = map[string]*Function{
returnType : VECTOR ,
returnType : VECTOR ,
callFn : avgOverTimeImpl ,
callFn : avgOverTimeImpl ,
} ,
} ,
"bottomk" : {
name : "bottomk" ,
argTypes : [ ] ExprType { SCALAR , VECTOR } ,
returnType : VECTOR ,
callFn : bottomkImpl ,
} ,
"count_over_time" : {
"count_over_time" : {
name : "count_over_time" ,
name : "count_over_time" ,
argTypes : [ ] ExprType { MATRIX } ,
argTypes : [ ] ExprType { MATRIX } ,
@ -441,6 +504,12 @@ var functions = map[string]*Function{
returnType : SCALAR ,
returnType : SCALAR ,
callFn : timeImpl ,
callFn : timeImpl ,
} ,
} ,
"topk" : {
name : "topk" ,
argTypes : [ ] ExprType { SCALAR , VECTOR } ,
returnType : VECTOR ,
callFn : topkImpl ,
} ,
}
}
// GetFunction returns a predefined Function object for the given
// GetFunction returns a predefined Function object for the given