2015-03-30 16:12:51 +00:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package promql
|
|
|
|
|
2015-03-30 17:13:36 +00:00
|
|
|
import (
|
|
|
|
"container/heap"
|
|
|
|
"math"
|
2015-08-17 21:25:53 +00:00
|
|
|
"regexp"
|
2015-03-30 17:13:36 +00:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2015-03-30 17:13:36 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/storage/metric"
|
|
|
|
)
|
|
|
|
|
2015-03-30 16:12:51 +00:00
|
|
|
// Function represents a function of the expression language and is
|
|
|
|
// used by function nodes.
|
|
|
|
type Function struct {
|
|
|
|
Name string
|
|
|
|
ArgTypes []ExprType
|
|
|
|
OptionalArgs int
|
|
|
|
ReturnType ExprType
|
2015-03-30 17:13:36 +00:00
|
|
|
Call func(ev *evaluator, args Expressions) Value
|
|
|
|
}
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
// === time() model.SampleValue ===
|
2015-03-30 17:13:36 +00:00
|
|
|
func funcTime(ev *evaluator, args Expressions) Value {
|
|
|
|
return &Scalar{
|
2015-08-20 15:18:46 +00:00
|
|
|
Value: model.SampleValue(ev.Timestamp.Unix()),
|
2015-03-30 17:13:36 +00:00
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// === delta(matrix ExprMatrix, isCounter=0 ExprScalar) Vector ===
|
|
|
|
func funcDelta(ev *evaluator, args Expressions) Value {
|
|
|
|
isCounter := len(args) >= 2 && ev.evalInt(args[1]) > 0
|
|
|
|
resultVector := Vector{}
|
|
|
|
|
|
|
|
// If we treat these metrics as counters, we need to fetch all values
|
|
|
|
// in the interval to find breaks in the timeseries' monotonicity.
|
|
|
|
// I.e. if a counter resets, we want to ignore that reset.
|
|
|
|
var matrixValue Matrix
|
|
|
|
if isCounter {
|
|
|
|
matrixValue = ev.evalMatrix(args[0])
|
|
|
|
} else {
|
|
|
|
matrixValue = ev.evalMatrixBounds(args[0])
|
|
|
|
}
|
|
|
|
for _, samples := range matrixValue {
|
|
|
|
// No sense in trying to compute a delta without at least two points. Drop
|
|
|
|
// this vector element.
|
|
|
|
if len(samples.Values) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
counterCorrection := model.SampleValue(0)
|
|
|
|
lastValue := model.SampleValue(0)
|
2015-03-30 17:13:36 +00:00
|
|
|
for _, sample := range samples.Values {
|
|
|
|
currentValue := sample.Value
|
|
|
|
if isCounter && currentValue < lastValue {
|
|
|
|
counterCorrection += lastValue - currentValue
|
|
|
|
}
|
|
|
|
lastValue = currentValue
|
|
|
|
}
|
|
|
|
resultValue := lastValue - samples.Values[0].Value + counterCorrection
|
|
|
|
|
|
|
|
targetInterval := args[0].(*MatrixSelector).Range
|
|
|
|
sampledInterval := samples.Values[len(samples.Values)-1].Timestamp.Sub(samples.Values[0].Timestamp)
|
|
|
|
if sampledInterval == 0 {
|
|
|
|
// Only found one sample. Cannot compute a rate from this.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Correct for differences in target vs. actual delta interval.
|
|
|
|
//
|
|
|
|
// Above, we didn't actually calculate the delta for the specified target
|
|
|
|
// interval, but for an interval between the first and last found samples
|
|
|
|
// under the target interval, which will usually have less time between
|
|
|
|
// them. Depending on how many samples are found under a target interval,
|
|
|
|
// the delta results are distorted and temporal aliasing occurs (ugly
|
|
|
|
// bumps). This effect is corrected for below.
|
2015-08-20 15:18:46 +00:00
|
|
|
intervalCorrection := model.SampleValue(targetInterval) / model.SampleValue(sampledInterval)
|
2015-03-30 17:13:36 +00:00
|
|
|
resultValue *= intervalCorrection
|
|
|
|
|
|
|
|
resultSample := &Sample{
|
|
|
|
Metric: samples.Metric,
|
|
|
|
Value: resultValue,
|
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
}
|
2015-08-20 15:18:46 +00:00
|
|
|
resultSample.Metric.Del(model.MetricNameLabel)
|
2015-03-30 17:13:36 +00:00
|
|
|
resultVector = append(resultVector, resultSample)
|
|
|
|
}
|
|
|
|
return resultVector
|
|
|
|
}
|
|
|
|
|
|
|
|
// === rate(node ExprMatrix) Vector ===
|
|
|
|
func funcRate(ev *evaluator, args Expressions) Value {
|
|
|
|
args = append(args, &NumberLiteral{1})
|
|
|
|
vector := funcDelta(ev, args).(Vector)
|
|
|
|
|
|
|
|
// TODO: could be other type of ExprMatrix in the future (right now, only
|
|
|
|
// MatrixSelector exists). Find a better way of getting the duration of a
|
|
|
|
// matrix, such as looking at the samples themselves.
|
|
|
|
interval := args[0].(*MatrixSelector).Range
|
|
|
|
for i := range vector {
|
2015-08-20 15:18:46 +00:00
|
|
|
vector[i].Value /= model.SampleValue(interval / time.Second)
|
2015-03-30 17:13:36 +00:00
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
2015-05-24 18:48:56 +00:00
|
|
|
// === increase(node ExprMatrix) Vector ===
|
|
|
|
func funcIncrease(ev *evaluator, args Expressions) Value {
|
|
|
|
args = append(args, &NumberLiteral{1})
|
|
|
|
vector := funcDelta(ev, args).(Vector)
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
2015-03-30 17:13:36 +00:00
|
|
|
// === sort(node ExprVector) Vector ===
|
|
|
|
func funcSort(ev *evaluator, args Expressions) Value {
|
|
|
|
byValueSorter := vectorByValueHeap(ev.evalVector(args[0]))
|
|
|
|
sort.Sort(byValueSorter)
|
|
|
|
return Vector(byValueSorter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// === sortDesc(node ExprVector) Vector ===
|
|
|
|
func funcSortDesc(ev *evaluator, args Expressions) Value {
|
|
|
|
byValueSorter := vectorByValueHeap(ev.evalVector(args[0]))
|
|
|
|
sort.Sort(sort.Reverse(byValueSorter))
|
|
|
|
return Vector(byValueSorter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// === topk(k ExprScalar, node ExprVector) Vector ===
|
|
|
|
func funcTopk(ev *evaluator, args Expressions) Value {
|
|
|
|
k := ev.evalInt(args[0])
|
|
|
|
if k < 1 {
|
|
|
|
return Vector{}
|
|
|
|
}
|
|
|
|
vector := ev.evalVector(args[1])
|
|
|
|
|
|
|
|
topk := make(vectorByValueHeap, 0, k)
|
|
|
|
|
|
|
|
for _, el := range vector {
|
|
|
|
if len(topk) < k || topk[0].Value < el.Value {
|
|
|
|
if len(topk) == k {
|
|
|
|
heap.Pop(&topk)
|
|
|
|
}
|
|
|
|
heap.Push(&topk, el)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Sort(sort.Reverse(topk))
|
|
|
|
return Vector(topk)
|
|
|
|
}
|
|
|
|
|
|
|
|
// === bottomk(k ExprScalar, node ExprVector) Vector ===
|
|
|
|
func funcBottomk(ev *evaluator, args Expressions) Value {
|
|
|
|
k := ev.evalInt(args[0])
|
|
|
|
if k < 1 {
|
|
|
|
return Vector{}
|
|
|
|
}
|
|
|
|
vector := ev.evalVector(args[1])
|
|
|
|
|
|
|
|
bottomk := make(vectorByValueHeap, 0, k)
|
|
|
|
bkHeap := reverseHeap{Interface: &bottomk}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// === drop_common_labels(node ExprVector) Vector ===
|
|
|
|
func funcDropCommonLabels(ev *evaluator, args Expressions) Value {
|
|
|
|
vector := ev.evalVector(args[0])
|
|
|
|
if len(vector) < 1 {
|
|
|
|
return Vector{}
|
|
|
|
}
|
2015-08-20 15:18:46 +00:00
|
|
|
common := model.LabelSet{}
|
2015-03-30 17:13:36 +00:00
|
|
|
for k, v := range vector[0].Metric.Metric {
|
|
|
|
// TODO(julius): Should we also drop common metric names?
|
2015-08-20 15:18:46 +00:00
|
|
|
if k == model.MetricNameLabel {
|
2015-03-30 17:13:36 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
common[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, el := range vector[1:] {
|
|
|
|
for k, v := range common {
|
|
|
|
if el.Metric.Metric[k] != v {
|
|
|
|
// Deletion of map entries while iterating over them is safe.
|
|
|
|
// From http://golang.org/ref/spec#For_statements:
|
|
|
|
// "If map entries that have not yet been reached are deleted during
|
|
|
|
// iteration, the corresponding iteration values will not be produced."
|
|
|
|
delete(common, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, el := range vector {
|
|
|
|
for k := range el.Metric.Metric {
|
|
|
|
if _, ok := common[k]; ok {
|
2015-08-20 15:18:46 +00:00
|
|
|
el.Metric.Del(k)
|
2015-03-30 17:13:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
|
|
|
// === round(vector ExprVector, toNearest=1 Scalar) Vector ===
|
|
|
|
func funcRound(ev *evaluator, args Expressions) Value {
|
|
|
|
// round returns a number rounded to toNearest.
|
|
|
|
// Ties are solved by rounding up.
|
|
|
|
toNearest := float64(1)
|
|
|
|
if len(args) >= 2 {
|
|
|
|
toNearest = ev.evalFloat(args[1])
|
|
|
|
}
|
|
|
|
// Invert as it seems to cause fewer floating point accuracy issues.
|
|
|
|
toNearestInverse := 1.0 / toNearest
|
|
|
|
|
|
|
|
vector := ev.evalVector(args[0])
|
|
|
|
for _, el := range vector {
|
2015-08-20 15:18:46 +00:00
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
|
|
|
el.Value = model.SampleValue(math.Floor(float64(el.Value)*toNearestInverse+0.5) / toNearestInverse)
|
2015-03-30 17:13:36 +00:00
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
|
|
|
// === scalar(node ExprVector) Scalar ===
|
|
|
|
func funcScalar(ev *evaluator, args Expressions) Value {
|
|
|
|
v := ev.evalVector(args[0])
|
|
|
|
if len(v) != 1 {
|
2015-08-20 15:18:46 +00:00
|
|
|
return &Scalar{model.SampleValue(math.NaN()), ev.Timestamp}
|
2015-03-30 17:13:36 +00:00
|
|
|
}
|
2015-08-20 15:18:46 +00:00
|
|
|
return &Scalar{model.SampleValue(v[0].Value), ev.Timestamp}
|
2015-03-30 17:13:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// === count_scalar(vector ExprVector) model.SampleValue ===
|
|
|
|
func funcCountScalar(ev *evaluator, args Expressions) Value {
|
|
|
|
return &Scalar{
|
2015-08-20 15:18:46 +00:00
|
|
|
Value: model.SampleValue(len(ev.evalVector(args[0]))),
|
2015-03-30 17:13:36 +00:00
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-22 12:52:35 +00:00
|
|
|
func aggrOverTime(ev *evaluator, args Expressions, aggrFn func([]model.SamplePair) model.SampleValue) Value {
|
2015-03-30 17:13:36 +00:00
|
|
|
matrix := ev.evalMatrix(args[0])
|
|
|
|
resultVector := Vector{}
|
|
|
|
|
|
|
|
for _, el := range matrix {
|
|
|
|
if len(el.Values) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
2015-03-30 17:13:36 +00:00
|
|
|
resultVector = append(resultVector, &Sample{
|
|
|
|
Metric: el.Metric,
|
|
|
|
Value: aggrFn(el.Values),
|
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return resultVector
|
|
|
|
}
|
|
|
|
|
|
|
|
// === avg_over_time(matrix ExprMatrix) Vector ===
|
|
|
|
func funcAvgOverTime(ev *evaluator, args Expressions) Value {
|
2015-08-22 12:52:35 +00:00
|
|
|
return aggrOverTime(ev, args, func(values []model.SamplePair) model.SampleValue {
|
2015-08-20 15:18:46 +00:00
|
|
|
var sum model.SampleValue
|
2015-03-30 17:13:36 +00:00
|
|
|
for _, v := range values {
|
|
|
|
sum += v.Value
|
|
|
|
}
|
2015-08-20 15:18:46 +00:00
|
|
|
return sum / model.SampleValue(len(values))
|
2015-03-30 17:13:36 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// === count_over_time(matrix ExprMatrix) Vector ===
|
|
|
|
func funcCountOverTime(ev *evaluator, args Expressions) Value {
|
2015-08-22 12:52:35 +00:00
|
|
|
return aggrOverTime(ev, args, func(values []model.SamplePair) model.SampleValue {
|
2015-08-20 15:18:46 +00:00
|
|
|
return model.SampleValue(len(values))
|
2015-03-30 17:13:36 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// === floor(vector ExprVector) Vector ===
|
|
|
|
func funcFloor(ev *evaluator, args Expressions) Value {
|
|
|
|
vector := ev.evalVector(args[0])
|
|
|
|
for _, el := range vector {
|
2015-08-20 15:18:46 +00:00
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
|
|
|
el.Value = model.SampleValue(math.Floor(float64(el.Value)))
|
2015-03-30 17:13:36 +00:00
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
|
|
|
// === max_over_time(matrix ExprMatrix) Vector ===
|
|
|
|
func funcMaxOverTime(ev *evaluator, args Expressions) Value {
|
2015-08-22 12:52:35 +00:00
|
|
|
return aggrOverTime(ev, args, func(values []model.SamplePair) model.SampleValue {
|
2015-03-30 17:13:36 +00:00
|
|
|
max := math.Inf(-1)
|
|
|
|
for _, v := range values {
|
|
|
|
max = math.Max(max, float64(v.Value))
|
|
|
|
}
|
2015-08-20 15:18:46 +00:00
|
|
|
return model.SampleValue(max)
|
2015-03-30 17:13:36 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// === min_over_time(matrix ExprMatrix) Vector ===
|
|
|
|
func funcMinOverTime(ev *evaluator, args Expressions) Value {
|
2015-08-22 12:52:35 +00:00
|
|
|
return aggrOverTime(ev, args, func(values []model.SamplePair) model.SampleValue {
|
2015-03-30 17:13:36 +00:00
|
|
|
min := math.Inf(1)
|
|
|
|
for _, v := range values {
|
|
|
|
min = math.Min(min, float64(v.Value))
|
|
|
|
}
|
2015-08-20 15:18:46 +00:00
|
|
|
return model.SampleValue(min)
|
2015-03-30 17:13:36 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// === sum_over_time(matrix ExprMatrix) Vector ===
|
|
|
|
func funcSumOverTime(ev *evaluator, args Expressions) Value {
|
2015-08-22 12:52:35 +00:00
|
|
|
return aggrOverTime(ev, args, func(values []model.SamplePair) model.SampleValue {
|
2015-08-20 15:18:46 +00:00
|
|
|
var sum model.SampleValue
|
2015-03-30 17:13:36 +00:00
|
|
|
for _, v := range values {
|
|
|
|
sum += v.Value
|
|
|
|
}
|
|
|
|
return sum
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// === abs(vector ExprVector) Vector ===
|
|
|
|
func funcAbs(ev *evaluator, args Expressions) Value {
|
|
|
|
vector := ev.evalVector(args[0])
|
|
|
|
for _, el := range vector {
|
2015-08-20 15:18:46 +00:00
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
|
|
|
el.Value = model.SampleValue(math.Abs(float64(el.Value)))
|
2015-03-30 17:13:36 +00:00
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
|
|
|
// === absent(vector ExprVector) Vector ===
|
|
|
|
func funcAbsent(ev *evaluator, args Expressions) Value {
|
|
|
|
if len(ev.evalVector(args[0])) > 0 {
|
|
|
|
return Vector{}
|
|
|
|
}
|
2015-08-20 15:18:46 +00:00
|
|
|
m := model.Metric{}
|
2015-03-30 17:13:36 +00:00
|
|
|
if vs, ok := args[0].(*VectorSelector); ok {
|
|
|
|
for _, matcher := range vs.LabelMatchers {
|
2015-08-20 15:18:46 +00:00
|
|
|
if matcher.Type == metric.Equal && matcher.Name != model.MetricNameLabel {
|
2015-03-30 17:13:36 +00:00
|
|
|
m[matcher.Name] = matcher.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Vector{
|
|
|
|
&Sample{
|
2015-08-20 15:18:46 +00:00
|
|
|
Metric: model.COWMetric{
|
2015-03-30 17:13:36 +00:00
|
|
|
Metric: m,
|
|
|
|
Copied: true,
|
|
|
|
},
|
|
|
|
Value: 1,
|
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// === ceil(vector ExprVector) Vector ===
|
|
|
|
func funcCeil(ev *evaluator, args Expressions) Value {
|
|
|
|
vector := ev.evalVector(args[0])
|
|
|
|
for _, el := range vector {
|
2015-08-20 15:18:46 +00:00
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
|
|
|
el.Value = model.SampleValue(math.Ceil(float64(el.Value)))
|
2015-03-30 17:13:36 +00:00
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
|
|
|
// === exp(vector ExprVector) Vector ===
|
|
|
|
func funcExp(ev *evaluator, args Expressions) Value {
|
|
|
|
vector := ev.evalVector(args[0])
|
|
|
|
for _, el := range vector {
|
2015-08-20 15:18:46 +00:00
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
|
|
|
el.Value = model.SampleValue(math.Exp(float64(el.Value)))
|
2015-03-30 17:13:36 +00:00
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
|
|
|
// === sqrt(vector VectorNode) Vector ===
|
|
|
|
func funcSqrt(ev *evaluator, args Expressions) Value {
|
|
|
|
vector := ev.evalVector(args[0])
|
|
|
|
for _, el := range vector {
|
2015-08-20 15:18:46 +00:00
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
|
|
|
el.Value = model.SampleValue(math.Sqrt(float64(el.Value)))
|
2015-03-30 17:13:36 +00:00
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
|
|
|
// === ln(vector ExprVector) Vector ===
|
|
|
|
func funcLn(ev *evaluator, args Expressions) Value {
|
|
|
|
vector := ev.evalVector(args[0])
|
|
|
|
for _, el := range vector {
|
2015-08-20 15:18:46 +00:00
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
|
|
|
el.Value = model.SampleValue(math.Log(float64(el.Value)))
|
2015-03-30 17:13:36 +00:00
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
|
|
|
// === log2(vector ExprVector) Vector ===
|
|
|
|
func funcLog2(ev *evaluator, args Expressions) Value {
|
|
|
|
vector := ev.evalVector(args[0])
|
|
|
|
for _, el := range vector {
|
2015-08-20 15:18:46 +00:00
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
|
|
|
el.Value = model.SampleValue(math.Log2(float64(el.Value)))
|
2015-03-30 17:13:36 +00:00
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
|
|
|
// === log10(vector ExprVector) Vector ===
|
|
|
|
func funcLog10(ev *evaluator, args Expressions) Value {
|
|
|
|
vector := ev.evalVector(args[0])
|
|
|
|
for _, el := range vector {
|
2015-08-20 15:18:46 +00:00
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
|
|
|
el.Value = model.SampleValue(math.Log10(float64(el.Value)))
|
2015-03-30 17:13:36 +00:00
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
|
|
|
// === deriv(node ExprMatrix) Vector ===
|
|
|
|
func funcDeriv(ev *evaluator, args Expressions) Value {
|
|
|
|
resultVector := Vector{}
|
|
|
|
matrix := ev.evalMatrix(args[0])
|
|
|
|
|
|
|
|
for _, samples := range matrix {
|
|
|
|
// No sense in trying to compute a derivative without at least two points.
|
|
|
|
// Drop this vector element.
|
|
|
|
if len(samples.Values) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Least squares.
|
2015-08-20 15:18:46 +00:00
|
|
|
var (
|
|
|
|
n model.SampleValue
|
|
|
|
sumX, sumY model.SampleValue
|
|
|
|
sumXY, sumX2 model.SampleValue
|
|
|
|
)
|
2015-03-30 17:13:36 +00:00
|
|
|
for _, sample := range samples.Values {
|
2015-08-20 15:18:46 +00:00
|
|
|
x := model.SampleValue(sample.Timestamp.UnixNano() / 1e9)
|
2015-03-30 17:13:36 +00:00
|
|
|
n += 1.0
|
|
|
|
sumY += sample.Value
|
|
|
|
sumX += x
|
|
|
|
sumXY += x * sample.Value
|
|
|
|
sumX2 += x * x
|
|
|
|
}
|
|
|
|
numerator := sumXY - sumX*sumY/n
|
|
|
|
denominator := sumX2 - (sumX*sumX)/n
|
|
|
|
|
|
|
|
resultValue := numerator / denominator
|
|
|
|
|
|
|
|
resultSample := &Sample{
|
|
|
|
Metric: samples.Metric,
|
|
|
|
Value: resultValue,
|
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
}
|
2015-08-20 15:18:46 +00:00
|
|
|
resultSample.Metric.Del(model.MetricNameLabel)
|
2015-03-30 17:13:36 +00:00
|
|
|
resultVector = append(resultVector, resultSample)
|
|
|
|
}
|
|
|
|
return resultVector
|
|
|
|
}
|
|
|
|
|
2015-07-28 11:30:57 +00:00
|
|
|
// === predict_linear(node ExprMatrix, k ExprScalar) Vector ===
|
|
|
|
func funcPredictLinear(ev *evaluator, args Expressions) Value {
|
|
|
|
vector := funcDeriv(ev, args[0:1]).(Vector)
|
2015-08-20 15:18:46 +00:00
|
|
|
duration := model.SampleValue(model.SampleValue(ev.evalFloat(args[1])))
|
2015-07-28 11:30:57 +00:00
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
excludedLabels := map[model.LabelName]struct{}{
|
|
|
|
model.MetricNameLabel: {},
|
2015-07-28 11:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate predicted delta over the duration.
|
2015-08-20 15:18:46 +00:00
|
|
|
signatureToDelta := map[uint64]model.SampleValue{}
|
2015-07-28 11:30:57 +00:00
|
|
|
for _, el := range vector {
|
2015-08-20 15:18:46 +00:00
|
|
|
signature := model.SignatureWithoutLabels(el.Metric.Metric, excludedLabels)
|
2015-07-28 11:30:57 +00:00
|
|
|
signatureToDelta[signature] = el.Value * duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// add predicted delta to last value.
|
|
|
|
matrixBounds := ev.evalMatrixBounds(args[0])
|
|
|
|
outVec := make(Vector, 0, len(signatureToDelta))
|
|
|
|
for _, samples := range matrixBounds {
|
|
|
|
if len(samples.Values) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
2015-08-20 15:18:46 +00:00
|
|
|
signature := model.SignatureWithoutLabels(samples.Metric.Metric, excludedLabels)
|
2015-07-28 11:30:57 +00:00
|
|
|
delta, ok := signatureToDelta[signature]
|
|
|
|
if ok {
|
2015-08-20 15:18:46 +00:00
|
|
|
samples.Metric.Del(model.MetricNameLabel)
|
2015-07-28 11:30:57 +00:00
|
|
|
outVec = append(outVec, &Sample{
|
|
|
|
Metric: samples.Metric,
|
|
|
|
Value: delta + samples.Values[1].Value,
|
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return outVec
|
|
|
|
}
|
|
|
|
|
2015-03-30 17:13:36 +00:00
|
|
|
// === histogram_quantile(k ExprScalar, vector ExprVector) Vector ===
|
|
|
|
func funcHistogramQuantile(ev *evaluator, args Expressions) Value {
|
2015-08-20 15:18:46 +00:00
|
|
|
q := model.SampleValue(ev.evalFloat(args[0]))
|
2015-03-30 17:13:36 +00:00
|
|
|
inVec := ev.evalVector(args[1])
|
|
|
|
|
|
|
|
outVec := Vector{}
|
|
|
|
signatureToMetricWithBuckets := map[uint64]*metricWithBuckets{}
|
|
|
|
for _, el := range inVec {
|
|
|
|
upperBound, err := strconv.ParseFloat(
|
2015-08-20 15:18:46 +00:00
|
|
|
string(el.Metric.Metric[model.BucketLabel]), 64,
|
2015-03-30 17:13:36 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
// Oops, no bucket label or malformed label value. Skip.
|
|
|
|
// TODO(beorn7): Issue a warning somehow.
|
|
|
|
continue
|
|
|
|
}
|
2015-08-20 15:18:46 +00:00
|
|
|
signature := model.SignatureWithoutLabels(el.Metric.Metric, excludedLabels)
|
2015-03-30 17:13:36 +00:00
|
|
|
mb, ok := signatureToMetricWithBuckets[signature]
|
|
|
|
if !ok {
|
2015-08-20 15:18:46 +00:00
|
|
|
el.Metric.Del(model.BucketLabel)
|
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
2015-03-30 17:13:36 +00:00
|
|
|
mb = &metricWithBuckets{el.Metric, nil}
|
|
|
|
signatureToMetricWithBuckets[signature] = mb
|
|
|
|
}
|
|
|
|
mb.buckets = append(mb.buckets, bucket{upperBound, el.Value})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mb := range signatureToMetricWithBuckets {
|
|
|
|
outVec = append(outVec, &Sample{
|
|
|
|
Metric: mb.metric,
|
2015-08-20 15:18:46 +00:00
|
|
|
Value: model.SampleValue(quantile(q, mb.buckets)),
|
2015-03-30 17:13:36 +00:00
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return outVec
|
2015-03-30 16:12:51 +00:00
|
|
|
}
|
|
|
|
|
2015-05-26 15:47:52 +00:00
|
|
|
// === resets(matrix ExprMatrix) Vector ===
|
|
|
|
func funcResets(ev *evaluator, args Expressions) Value {
|
|
|
|
in := ev.evalMatrix(args[0])
|
|
|
|
out := make(Vector, 0, len(in))
|
|
|
|
|
|
|
|
for _, samples := range in {
|
|
|
|
resets := 0
|
2015-08-20 15:18:46 +00:00
|
|
|
prev := model.SampleValue(samples.Values[0].Value)
|
2015-05-26 15:47:52 +00:00
|
|
|
for _, sample := range samples.Values[1:] {
|
|
|
|
current := sample.Value
|
|
|
|
if current < prev {
|
|
|
|
resets++
|
|
|
|
}
|
|
|
|
prev = current
|
|
|
|
}
|
|
|
|
|
|
|
|
rs := &Sample{
|
|
|
|
Metric: samples.Metric,
|
2015-08-20 15:18:46 +00:00
|
|
|
Value: model.SampleValue(resets),
|
2015-05-26 15:47:52 +00:00
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
}
|
2015-08-20 15:18:46 +00:00
|
|
|
rs.Metric.Del(model.MetricNameLabel)
|
2015-05-26 15:47:52 +00:00
|
|
|
out = append(out, rs)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2015-05-26 17:01:34 +00:00
|
|
|
// === changes(matrix ExprMatrix) Vector ===
|
|
|
|
func funcChanges(ev *evaluator, args Expressions) Value {
|
|
|
|
in := ev.evalMatrix(args[0])
|
|
|
|
out := make(Vector, 0, len(in))
|
|
|
|
|
|
|
|
for _, samples := range in {
|
|
|
|
changes := 0
|
2015-08-20 15:18:46 +00:00
|
|
|
prev := model.SampleValue(samples.Values[0].Value)
|
2015-05-26 17:01:34 +00:00
|
|
|
for _, sample := range samples.Values[1:] {
|
|
|
|
current := sample.Value
|
|
|
|
if current != prev {
|
|
|
|
changes++
|
|
|
|
}
|
|
|
|
prev = current
|
|
|
|
}
|
|
|
|
|
|
|
|
rs := &Sample{
|
|
|
|
Metric: samples.Metric,
|
2015-08-20 15:18:46 +00:00
|
|
|
Value: model.SampleValue(changes),
|
2015-05-26 17:01:34 +00:00
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
}
|
2015-08-20 15:18:46 +00:00
|
|
|
rs.Metric.Del(model.MetricNameLabel)
|
2015-05-26 17:01:34 +00:00
|
|
|
out = append(out, rs)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2015-08-17 21:25:53 +00:00
|
|
|
// === label_replace(vector ExprVector, dst_label, replacement, src_labelname, regex ExprString) Vector ===
|
|
|
|
func funcLabelReplace(ev *evaluator, args Expressions) Value {
|
|
|
|
var (
|
|
|
|
vector = ev.evalVector(args[0])
|
2015-08-20 15:18:46 +00:00
|
|
|
dst = model.LabelName(ev.evalString(args[1]).Value)
|
2015-08-17 21:25:53 +00:00
|
|
|
repl = ev.evalString(args[2]).Value
|
2015-08-20 15:18:46 +00:00
|
|
|
src = model.LabelName(ev.evalString(args[3]).Value)
|
2015-08-17 21:25:53 +00:00
|
|
|
regexStr = ev.evalString(args[4]).Value
|
|
|
|
)
|
|
|
|
|
|
|
|
regex, err := regexp.Compile(regexStr)
|
|
|
|
if err != nil {
|
|
|
|
ev.errorf("invalid regular expression in label_replace(): %s", regexStr)
|
|
|
|
}
|
2015-08-20 15:18:46 +00:00
|
|
|
if !model.LabelNameRE.MatchString(string(dst)) {
|
2015-08-17 21:25:53 +00:00
|
|
|
ev.errorf("invalid destination label name in label_replace(): %s", dst)
|
|
|
|
}
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
outSet := make(map[model.Fingerprint]struct{}, len(vector))
|
2015-08-17 21:25:53 +00:00
|
|
|
for _, el := range vector {
|
|
|
|
srcVal := string(el.Metric.Metric[src])
|
|
|
|
indexes := regex.FindStringSubmatchIndex(srcVal)
|
|
|
|
// If there is no match, no replacement should take place.
|
|
|
|
if indexes == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
res := regex.ExpandString([]byte{}, repl, srcVal, indexes)
|
|
|
|
if len(res) == 0 {
|
2015-08-20 15:18:46 +00:00
|
|
|
el.Metric.Del(dst)
|
2015-08-17 21:25:53 +00:00
|
|
|
} else {
|
2015-08-20 15:18:46 +00:00
|
|
|
el.Metric.Set(dst, model.LabelValue(res))
|
2015-08-17 21:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fp := el.Metric.Metric.Fingerprint()
|
|
|
|
if _, exists := outSet[fp]; exists {
|
|
|
|
ev.errorf("duplicated label set in output of label_replace(): %s", el.Metric.Metric)
|
|
|
|
} else {
|
|
|
|
outSet[fp] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
2015-03-30 16:12:51 +00:00
|
|
|
var functions = map[string]*Function{
|
|
|
|
"abs": {
|
|
|
|
Name: "abs",
|
|
|
|
ArgTypes: []ExprType{ExprVector},
|
|
|
|
ReturnType: ExprVector,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcAbs,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
"absent": {
|
|
|
|
Name: "absent",
|
|
|
|
ArgTypes: []ExprType{ExprVector},
|
|
|
|
ReturnType: ExprVector,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcAbsent,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
2015-05-24 18:48:56 +00:00
|
|
|
"increase": {
|
|
|
|
Name: "increase",
|
|
|
|
ArgTypes: []ExprType{ExprMatrix},
|
|
|
|
ReturnType: ExprVector,
|
|
|
|
Call: funcIncrease,
|
|
|
|
},
|
2015-03-30 16:12:51 +00:00
|
|
|
"avg_over_time": {
|
|
|
|
Name: "avg_over_time",
|
|
|
|
ArgTypes: []ExprType{ExprMatrix},
|
|
|
|
ReturnType: ExprVector,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcAvgOverTime,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
"bottomk": {
|
|
|
|
Name: "bottomk",
|
|
|
|
ArgTypes: []ExprType{ExprScalar, ExprVector},
|
|
|
|
ReturnType: ExprVector,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcBottomk,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
"ceil": {
|
|
|
|
Name: "ceil",
|
|
|
|
ArgTypes: []ExprType{ExprVector},
|
|
|
|
ReturnType: ExprVector,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcCeil,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
2015-05-26 17:01:34 +00:00
|
|
|
"changes": {
|
|
|
|
Name: "changes",
|
|
|
|
ArgTypes: []ExprType{ExprMatrix},
|
|
|
|
ReturnType: ExprVector,
|
|
|
|
Call: funcChanges,
|
|
|
|
},
|
2015-03-30 16:12:51 +00:00
|
|
|
"count_over_time": {
|
|
|
|
Name: "count_over_time",
|
|
|
|
ArgTypes: []ExprType{ExprMatrix},
|
|
|
|
ReturnType: ExprVector,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcCountOverTime,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
"count_scalar": {
|
|
|
|
Name: "count_scalar",
|
|
|
|
ArgTypes: []ExprType{ExprVector},
|
|
|
|
ReturnType: ExprScalar,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcCountScalar,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
"delta": {
|
|
|
|
Name: "delta",
|
|
|
|
ArgTypes: []ExprType{ExprMatrix, ExprScalar},
|
|
|
|
OptionalArgs: 1, // The 2nd argument is deprecated.
|
|
|
|
ReturnType: ExprVector,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcDelta,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
"deriv": {
|
|
|
|
Name: "deriv",
|
|
|
|
ArgTypes: []ExprType{ExprMatrix},
|
|
|
|
ReturnType: ExprVector,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcDeriv,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
"drop_common_labels": {
|
|
|
|
Name: "drop_common_labels",
|
|
|
|
ArgTypes: []ExprType{ExprVector},
|
|
|
|
ReturnType: ExprVector,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcDropCommonLabels,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
"exp": {
|
|
|
|
Name: "exp",
|
|
|
|
ArgTypes: []ExprType{ExprVector},
|
|
|
|
ReturnType: ExprVector,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcExp,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
"floor": {
|
|
|
|
Name: "floor",
|
|
|
|
ArgTypes: []ExprType{ExprVector},
|
|
|
|
ReturnType: ExprVector,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcFloor,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
"histogram_quantile": {
|
|
|
|
Name: "histogram_quantile",
|
|
|
|
ArgTypes: []ExprType{ExprScalar, ExprVector},
|
|
|
|
ReturnType: ExprVector,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcHistogramQuantile,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
2015-08-17 21:25:53 +00:00
|
|
|
"label_replace": {
|
|
|
|
Name: "label_replace",
|
|
|
|
ArgTypes: []ExprType{ExprVector, ExprString, ExprString, ExprString, ExprString},
|
|
|
|
ReturnType: ExprVector,
|
|
|
|
Call: funcLabelReplace,
|
|
|
|
},
|
2015-03-30 16:12:51 +00:00
|
|
|
"ln": {
|
|
|
|
Name: "ln",
|
|
|
|
ArgTypes: []ExprType{ExprVector},
|
|
|
|
ReturnType: ExprVector,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcLn,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
"log10": {
|
|
|
|
Name: "log10",
|
|
|
|
ArgTypes: []ExprType{ExprVector},
|
|
|
|
ReturnType: ExprVector,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcLog10,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
"log2": {
|
|
|
|
Name: "log2",
|
|
|
|
ArgTypes: []ExprType{ExprVector},
|
|
|
|
ReturnType: ExprVector,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcLog2,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
"max_over_time": {
|
|
|
|
Name: "max_over_time",
|
|
|
|
ArgTypes: []ExprType{ExprMatrix},
|
|
|
|
ReturnType: ExprVector,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcMaxOverTime,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
"min_over_time": {
|
|
|
|
Name: "min_over_time",
|
|
|
|
ArgTypes: []ExprType{ExprMatrix},
|
|
|
|
ReturnType: ExprVector,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcMinOverTime,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
2015-07-28 11:30:57 +00:00
|
|
|
"predict_linear": {
|
|
|
|
Name: "predict_linear",
|
|
|
|
ArgTypes: []ExprType{ExprMatrix, ExprScalar},
|
|
|
|
ReturnType: ExprVector,
|
|
|
|
Call: funcPredictLinear,
|
|
|
|
},
|
2015-03-30 16:12:51 +00:00
|
|
|
"rate": {
|
|
|
|
Name: "rate",
|
|
|
|
ArgTypes: []ExprType{ExprMatrix},
|
|
|
|
ReturnType: ExprVector,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcRate,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
2015-05-26 15:47:52 +00:00
|
|
|
"resets": {
|
|
|
|
Name: "resets",
|
|
|
|
ArgTypes: []ExprType{ExprMatrix},
|
|
|
|
ReturnType: ExprVector,
|
|
|
|
Call: funcResets,
|
|
|
|
},
|
2015-03-30 16:12:51 +00:00
|
|
|
"round": {
|
|
|
|
Name: "round",
|
|
|
|
ArgTypes: []ExprType{ExprVector, ExprScalar},
|
|
|
|
OptionalArgs: 1,
|
|
|
|
ReturnType: ExprVector,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcRound,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
"scalar": {
|
|
|
|
Name: "scalar",
|
|
|
|
ArgTypes: []ExprType{ExprVector},
|
|
|
|
ReturnType: ExprScalar,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcScalar,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
"sort": {
|
|
|
|
Name: "sort",
|
|
|
|
ArgTypes: []ExprType{ExprVector},
|
|
|
|
ReturnType: ExprVector,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcSort,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
"sort_desc": {
|
|
|
|
Name: "sort_desc",
|
|
|
|
ArgTypes: []ExprType{ExprVector},
|
|
|
|
ReturnType: ExprVector,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcSortDesc,
|
|
|
|
},
|
|
|
|
"sqrt": {
|
|
|
|
Name: "sqrt",
|
|
|
|
ArgTypes: []ExprType{ExprVector},
|
|
|
|
ReturnType: ExprVector,
|
|
|
|
Call: funcSqrt,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
"sum_over_time": {
|
|
|
|
Name: "sum_over_time",
|
|
|
|
ArgTypes: []ExprType{ExprMatrix},
|
|
|
|
ReturnType: ExprVector,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcSumOverTime,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
"time": {
|
|
|
|
Name: "time",
|
|
|
|
ArgTypes: []ExprType{},
|
|
|
|
ReturnType: ExprScalar,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcTime,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
"topk": {
|
|
|
|
Name: "topk",
|
|
|
|
ArgTypes: []ExprType{ExprScalar, ExprVector},
|
|
|
|
ReturnType: ExprVector,
|
2015-03-30 17:13:36 +00:00
|
|
|
Call: funcTopk,
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-03-30 17:13:36 +00:00
|
|
|
// getFunction returns a predefined Function object for the given name.
|
|
|
|
func getFunction(name string) (*Function, bool) {
|
2015-03-30 16:12:51 +00:00
|
|
|
function, ok := functions[name]
|
|
|
|
return function, ok
|
|
|
|
}
|
2015-03-30 17:13:36 +00:00
|
|
|
|
|
|
|
type vectorByValueHeap Vector
|
|
|
|
|
|
|
|
func (s vectorByValueHeap) Len() int {
|
|
|
|
return len(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s vectorByValueHeap) Less(i, j int) bool {
|
|
|
|
if math.IsNaN(float64(s[i].Value)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return s[i].Value < s[j].Value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s vectorByValueHeap) Swap(i, j int) {
|
|
|
|
s[i], s[j] = s[j], s[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *vectorByValueHeap) Push(x interface{}) {
|
|
|
|
*s = append(*s, x.(*Sample))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *vectorByValueHeap) Pop() interface{} {
|
|
|
|
old := *s
|
|
|
|
n := len(old)
|
|
|
|
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)
|
|
|
|
}
|