2015-03-30 09:12:51 -07: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 10:13:36 -07:00
|
|
|
import (
|
|
|
|
"math"
|
2015-08-17 14:25:53 -07:00
|
|
|
"regexp"
|
2015-03-30 10:13:36 -07:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
2016-08-22 12:57:33 -07:00
|
|
|
"time"
|
2015-03-30 10:13:36 -07:00
|
|
|
|
2015-08-20 08:18:46 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2015-03-30 10:13:36 -07:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/storage/metric"
|
|
|
|
)
|
|
|
|
|
2015-03-30 09:12:51 -07:00
|
|
|
// Function represents a function of the expression language and is
|
|
|
|
// used by function nodes.
|
|
|
|
type Function struct {
|
|
|
|
Name string
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes []model.ValueType
|
2015-03-30 09:12:51 -07:00
|
|
|
OptionalArgs int
|
2015-08-24 09:04:41 -07:00
|
|
|
ReturnType model.ValueType
|
|
|
|
Call func(ev *evaluator, args Expressions) model.Value
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
|
|
|
|
2015-08-20 08:18:46 -07:00
|
|
|
// === time() model.SampleValue ===
|
2015-08-24 09:04:41 -07:00
|
|
|
func funcTime(ev *evaluator, args Expressions) model.Value {
|
|
|
|
return &model.Scalar{
|
2015-08-20 08:18:46 -07:00
|
|
|
Value: model.SampleValue(ev.Timestamp.Unix()),
|
2015-03-30 10:13:36 -07:00
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-28 13:13:41 -08:00
|
|
|
// extrapolatedRate is a utility function for rate/increase/delta.
|
|
|
|
// It calculates the rate (allowing for counter resets if isCounter is true),
|
|
|
|
// extrapolates if the first/last sample is close to the boundary, and returns
|
|
|
|
// the result as either per-second (if isRate is true) or overall.
|
|
|
|
func extrapolatedRate(ev *evaluator, arg Expr, isCounter bool, isRate bool) model.Value {
|
|
|
|
ms := arg.(*MatrixSelector)
|
|
|
|
|
|
|
|
rangeStart := ev.Timestamp.Add(-ms.Range - ms.Offset)
|
|
|
|
rangeEnd := ev.Timestamp.Add(-ms.Offset)
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
resultVector := vector{}
|
2015-03-30 10:13:36 -07:00
|
|
|
|
2015-11-28 13:13:41 -08:00
|
|
|
matrixValue := ev.evalMatrix(ms)
|
2015-03-30 10:13:36 -07:00
|
|
|
for _, samples := range matrixValue {
|
2015-11-28 13:13:41 -08:00
|
|
|
// No sense in trying to compute a rate without at least two points. Drop
|
2015-03-30 10:13:36 -07:00
|
|
|
// this vector element.
|
|
|
|
if len(samples.Values) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
2015-08-24 09:04:41 -07:00
|
|
|
var (
|
|
|
|
counterCorrection model.SampleValue
|
|
|
|
lastValue model.SampleValue
|
|
|
|
)
|
2015-03-30 10:13:36 -07:00
|
|
|
for _, sample := range samples.Values {
|
|
|
|
currentValue := sample.Value
|
|
|
|
if isCounter && currentValue < lastValue {
|
2016-08-12 11:21:04 -07:00
|
|
|
counterCorrection += lastValue
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
|
|
|
lastValue = currentValue
|
|
|
|
}
|
|
|
|
resultValue := lastValue - samples.Values[0].Value + counterCorrection
|
|
|
|
|
2015-11-28 13:13:41 -08:00
|
|
|
// Duration between first/last samples and boundary of range.
|
|
|
|
durationToStart := samples.Values[0].Timestamp.Sub(rangeStart).Seconds()
|
|
|
|
durationToEnd := rangeEnd.Sub(samples.Values[len(samples.Values)-1].Timestamp).Seconds()
|
|
|
|
|
|
|
|
sampledInterval := samples.Values[len(samples.Values)-1].Timestamp.Sub(samples.Values[0].Timestamp).Seconds()
|
|
|
|
averageDurationBetweenSamples := sampledInterval / float64(len(samples.Values)-1)
|
|
|
|
|
|
|
|
if isCounter && resultValue > 0 && samples.Values[0].Value >= 0 {
|
|
|
|
// Counters cannot be negative. If we have any slope at
|
|
|
|
// all (i.e. resultValue went up), we can extrapolate
|
|
|
|
// the zero point of the counter. If the duration to the
|
|
|
|
// zero point is shorter than the durationToStart, we
|
|
|
|
// take the zero point as the start of the series,
|
|
|
|
// thereby avoiding extrapolation to negative counter
|
|
|
|
// values.
|
|
|
|
durationToZero := sampledInterval * float64(samples.Values[0].Value/resultValue)
|
|
|
|
if durationToZero < durationToStart {
|
|
|
|
durationToStart = durationToZero
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the first/last samples are close to the boundaries of the range,
|
|
|
|
// extrapolate the result. This is as we expect that another sample
|
|
|
|
// will exist given the spacing between samples we've seen thus far,
|
|
|
|
// with an allowance for noise.
|
|
|
|
extrapolationThreshold := averageDurationBetweenSamples * 1.1
|
|
|
|
extrapolateToInterval := sampledInterval
|
|
|
|
|
|
|
|
if durationToStart < extrapolationThreshold {
|
|
|
|
extrapolateToInterval += durationToStart
|
|
|
|
} else {
|
|
|
|
extrapolateToInterval += averageDurationBetweenSamples / 2
|
|
|
|
}
|
|
|
|
if durationToEnd < extrapolationThreshold {
|
|
|
|
extrapolateToInterval += durationToEnd
|
|
|
|
} else {
|
|
|
|
extrapolateToInterval += averageDurationBetweenSamples / 2
|
|
|
|
}
|
|
|
|
resultValue = resultValue * model.SampleValue(extrapolateToInterval/sampledInterval)
|
|
|
|
if isRate {
|
|
|
|
resultValue = resultValue / model.SampleValue(ms.Range.Seconds())
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
resultSample := &sample{
|
2015-03-30 10:13:36 -07:00
|
|
|
Metric: samples.Metric,
|
|
|
|
Value: resultValue,
|
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
}
|
2015-08-20 08:18:46 -07:00
|
|
|
resultSample.Metric.Del(model.MetricNameLabel)
|
2015-03-30 10:13:36 -07:00
|
|
|
resultVector = append(resultVector, resultSample)
|
|
|
|
}
|
|
|
|
return resultVector
|
|
|
|
}
|
|
|
|
|
2015-11-28 13:13:41 -08:00
|
|
|
// === delta(matrix model.ValMatrix) Vector ===
|
|
|
|
func funcDelta(ev *evaluator, args Expressions) model.Value {
|
|
|
|
return extrapolatedRate(ev, args[0], false, false)
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === rate(node model.ValMatrix) Vector ===
|
|
|
|
func funcRate(ev *evaluator, args Expressions) model.Value {
|
2015-11-28 13:13:41 -08:00
|
|
|
return extrapolatedRate(ev, args[0], true, true)
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === increase(node model.ValMatrix) Vector ===
|
|
|
|
func funcIncrease(ev *evaluator, args Expressions) model.Value {
|
2015-11-28 13:13:41 -08:00
|
|
|
return extrapolatedRate(ev, args[0], true, false)
|
2015-05-24 11:48:56 -07:00
|
|
|
}
|
|
|
|
|
2015-10-09 09:58:43 -07:00
|
|
|
// === irate(node model.ValMatrix) Vector ===
|
|
|
|
func funcIrate(ev *evaluator, args Expressions) model.Value {
|
2016-08-08 01:52:00 -07:00
|
|
|
return instantValue(ev, args[0], true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// === idelta(node model.ValMatric) Vector ===
|
|
|
|
func funcIdelta(ev *evaluator, args Expressions) model.Value {
|
|
|
|
return instantValue(ev, args[0], false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func instantValue(ev *evaluator, arg Expr, isRate bool) model.Value {
|
2015-10-09 09:58:43 -07:00
|
|
|
resultVector := vector{}
|
2016-08-08 01:52:00 -07:00
|
|
|
for _, samples := range ev.evalMatrix(arg) {
|
2015-10-09 09:58:43 -07:00
|
|
|
// No sense in trying to compute a rate without at least two points. Drop
|
|
|
|
// this vector element.
|
|
|
|
if len(samples.Values) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
lastSample := samples.Values[len(samples.Values)-1]
|
|
|
|
previousSample := samples.Values[len(samples.Values)-2]
|
|
|
|
|
|
|
|
var resultValue model.SampleValue
|
2016-08-08 01:52:00 -07:00
|
|
|
if isRate && lastSample.Value < previousSample.Value {
|
2015-10-09 09:58:43 -07:00
|
|
|
// Counter reset.
|
|
|
|
resultValue = lastSample.Value
|
|
|
|
} else {
|
|
|
|
resultValue = lastSample.Value - previousSample.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
sampledInterval := lastSample.Timestamp.Sub(previousSample.Timestamp)
|
|
|
|
if sampledInterval == 0 {
|
|
|
|
// Avoid dividing by 0.
|
|
|
|
continue
|
|
|
|
}
|
2016-08-08 01:40:50 -07:00
|
|
|
|
2016-08-08 01:52:00 -07:00
|
|
|
if isRate {
|
|
|
|
// Convert to per-second.
|
|
|
|
resultValue /= model.SampleValue(sampledInterval.Seconds())
|
2016-08-08 01:40:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
resultSample := &sample{
|
|
|
|
Metric: samples.Metric,
|
|
|
|
Value: resultValue,
|
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
}
|
|
|
|
resultSample.Metric.Del(model.MetricNameLabel)
|
|
|
|
resultVector = append(resultVector, resultSample)
|
|
|
|
}
|
|
|
|
return resultVector
|
|
|
|
}
|
|
|
|
|
2016-03-09 19:29:02 -08:00
|
|
|
// Calculate the trend value at the given index i in raw data d.
|
|
|
|
// This is somewhat analogous to the slope of the trend at the given index.
|
|
|
|
// The argument "s" is the set of computed smoothed values.
|
|
|
|
// The argument "b" is the set of computed trend factors.
|
|
|
|
// The argument "d" is the set of raw input values.
|
|
|
|
func calcTrendValue(i int, sf, tf float64, s, b, d []float64) float64 {
|
|
|
|
if i == 0 {
|
|
|
|
return b[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
x := tf * (s[i] - s[i-1])
|
|
|
|
y := (1 - tf) * b[i-1]
|
|
|
|
|
|
|
|
// Cache the computed value.
|
|
|
|
b[i] = x + y
|
|
|
|
|
|
|
|
return b[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Holt-Winters is similar to a weighted moving average, where historical data has exponentially less influence on the current data.
|
2016-05-12 07:14:48 -07:00
|
|
|
// Holt-Winter also accounts for trends in data. The smoothing factor (0 < sf < 1) affects how historical data will affect the current
|
|
|
|
// data. A lower smoothing factor increases the influence of historical data. The trend factor (0 < tf < 1) affects
|
|
|
|
// how trends in historical data will affect the current data. A higher trend factor increases the influence.
|
2016-03-09 19:29:02 -08:00
|
|
|
// of trends. Algorithm taken from https://en.wikipedia.org/wiki/Exponential_smoothing titled: "Double exponential smoothing".
|
|
|
|
func funcHoltWinters(ev *evaluator, args Expressions) model.Value {
|
|
|
|
mat := ev.evalMatrix(args[0])
|
|
|
|
|
|
|
|
// The smoothing factor argument.
|
|
|
|
sf := ev.evalFloat(args[1])
|
|
|
|
|
|
|
|
// The trend factor argument.
|
|
|
|
tf := ev.evalFloat(args[2])
|
|
|
|
|
|
|
|
// Sanity check the input.
|
|
|
|
if sf <= 0 || sf >= 1 {
|
|
|
|
ev.errorf("invalid smoothing factor. Expected: 0 < sf < 1 got: %f", sf)
|
|
|
|
}
|
|
|
|
if tf <= 0 || tf >= 1 {
|
|
|
|
ev.errorf("invalid trend factor. Expected: 0 < tf < 1 got: %f", sf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make an output vector large enough to hold the entire result.
|
|
|
|
resultVector := make(vector, 0, len(mat))
|
|
|
|
|
|
|
|
// Create scratch values.
|
|
|
|
var s, b, d []float64
|
|
|
|
|
|
|
|
var l int
|
|
|
|
for _, samples := range mat {
|
|
|
|
l = len(samples.Values)
|
|
|
|
|
|
|
|
// Can't do the smoothing operation with less than two points.
|
|
|
|
if l < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resize scratch values.
|
|
|
|
if l != len(s) {
|
|
|
|
s = make([]float64, l)
|
|
|
|
b = make([]float64, l)
|
|
|
|
d = make([]float64, l)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in the d values with the raw values from the input.
|
|
|
|
for i, v := range samples.Values {
|
|
|
|
d[i] = float64(v.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set initial values.
|
|
|
|
s[0] = d[0]
|
|
|
|
b[0] = d[1] - d[0]
|
|
|
|
|
|
|
|
// Run the smoothing operation.
|
|
|
|
var x, y float64
|
|
|
|
for i := 1; i < len(d); i++ {
|
|
|
|
|
|
|
|
// Scale the raw value against the smoothing factor.
|
|
|
|
x = sf * d[i]
|
|
|
|
|
|
|
|
// Scale the last smoothed value with the trend at this point.
|
|
|
|
y = (1 - sf) * (s[i-1] + calcTrendValue(i-1, sf, tf, s, b, d))
|
|
|
|
|
|
|
|
s[i] = x + y
|
|
|
|
}
|
|
|
|
|
|
|
|
samples.Metric.Del(model.MetricNameLabel)
|
|
|
|
resultVector = append(resultVector, &sample{
|
|
|
|
Metric: samples.Metric,
|
|
|
|
Value: model.SampleValue(s[len(s)-1]), // The last value in the vector is the smoothed result.
|
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultVector
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === sort(node model.ValVector) Vector ===
|
|
|
|
func funcSort(ev *evaluator, args Expressions) model.Value {
|
2015-12-30 06:06:51 -08:00
|
|
|
// NaN should sort to the bottom, so take descending sort with NaN first and
|
|
|
|
// reverse it.
|
|
|
|
byValueSorter := vectorByReverseValueHeap(ev.evalVector(args[0]))
|
|
|
|
sort.Sort(sort.Reverse(byValueSorter))
|
2015-08-24 09:04:41 -07:00
|
|
|
return vector(byValueSorter)
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === sortDesc(node model.ValVector) Vector ===
|
|
|
|
func funcSortDesc(ev *evaluator, args Expressions) model.Value {
|
2015-12-30 06:06:51 -08:00
|
|
|
// NaN should sort to the bottom, so take ascending sort with NaN first and
|
|
|
|
// reverse it.
|
2015-03-30 10:13:36 -07:00
|
|
|
byValueSorter := vectorByValueHeap(ev.evalVector(args[0]))
|
|
|
|
sort.Sort(sort.Reverse(byValueSorter))
|
2015-08-24 09:04:41 -07:00
|
|
|
return vector(byValueSorter)
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
|
|
|
|
2015-11-19 16:42:35 -08:00
|
|
|
// === clamp_max(vector model.ValVector, max Scalar) Vector ===
|
|
|
|
func funcClampMax(ev *evaluator, args Expressions) model.Value {
|
|
|
|
vec := ev.evalVector(args[0])
|
|
|
|
max := ev.evalFloat(args[1])
|
|
|
|
for _, el := range vec {
|
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
|
|
|
el.Value = model.SampleValue(math.Min(max, float64(el.Value)))
|
|
|
|
}
|
|
|
|
return vec
|
|
|
|
}
|
|
|
|
|
|
|
|
// === clamp_min(vector model.ValVector, min Scalar) Vector ===
|
|
|
|
func funcClampMin(ev *evaluator, args Expressions) model.Value {
|
|
|
|
vec := ev.evalVector(args[0])
|
|
|
|
min := ev.evalFloat(args[1])
|
|
|
|
for _, el := range vec {
|
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
|
|
|
el.Value = model.SampleValue(math.Max(min, float64(el.Value)))
|
|
|
|
}
|
|
|
|
return vec
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === drop_common_labels(node model.ValVector) Vector ===
|
|
|
|
func funcDropCommonLabels(ev *evaluator, args Expressions) model.Value {
|
|
|
|
vec := ev.evalVector(args[0])
|
|
|
|
if len(vec) < 1 {
|
|
|
|
return vector{}
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
2015-08-20 08:18:46 -07:00
|
|
|
common := model.LabelSet{}
|
2015-08-24 09:04:41 -07:00
|
|
|
for k, v := range vec[0].Metric.Metric {
|
2015-03-30 10:13:36 -07:00
|
|
|
// TODO(julius): Should we also drop common metric names?
|
2015-08-20 08:18:46 -07:00
|
|
|
if k == model.MetricNameLabel {
|
2015-03-30 10:13:36 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
common[k] = v
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
for _, el := range vec[1:] {
|
2015-03-30 10:13:36 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
for _, el := range vec {
|
2015-03-30 10:13:36 -07:00
|
|
|
for k := range el.Metric.Metric {
|
|
|
|
if _, ok := common[k]; ok {
|
2015-08-20 08:18:46 -07:00
|
|
|
el.Metric.Del(k)
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-24 09:04:41 -07:00
|
|
|
return vec
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === round(vector model.ValVector, toNearest=1 Scalar) Vector ===
|
|
|
|
func funcRound(ev *evaluator, args Expressions) model.Value {
|
2015-03-30 10:13:36 -07:00
|
|
|
// 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
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
vec := ev.evalVector(args[0])
|
|
|
|
for _, el := range vec {
|
2015-08-20 08:18:46 -07:00
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
|
|
|
el.Value = model.SampleValue(math.Floor(float64(el.Value)*toNearestInverse+0.5) / toNearestInverse)
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
2015-08-24 09:04:41 -07:00
|
|
|
return vec
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === scalar(node model.ValVector) Scalar ===
|
|
|
|
func funcScalar(ev *evaluator, args Expressions) model.Value {
|
2015-03-30 10:13:36 -07:00
|
|
|
v := ev.evalVector(args[0])
|
|
|
|
if len(v) != 1 {
|
2015-09-18 07:49:32 -07:00
|
|
|
return &model.Scalar{
|
|
|
|
Value: model.SampleValue(math.NaN()),
|
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &model.Scalar{
|
|
|
|
Value: model.SampleValue(v[0].Value),
|
|
|
|
Timestamp: ev.Timestamp,
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === count_scalar(vector model.ValVector) model.SampleValue ===
|
|
|
|
func funcCountScalar(ev *evaluator, args Expressions) model.Value {
|
|
|
|
return &model.Scalar{
|
2015-08-20 08:18:46 -07:00
|
|
|
Value: model.SampleValue(len(ev.evalVector(args[0]))),
|
2015-03-30 10:13:36 -07:00
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
func aggrOverTime(ev *evaluator, args Expressions, aggrFn func([]model.SamplePair) model.SampleValue) model.Value {
|
|
|
|
mat := ev.evalMatrix(args[0])
|
|
|
|
resultVector := vector{}
|
2015-03-30 10:13:36 -07:00
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
for _, el := range mat {
|
2015-03-30 10:13:36 -07:00
|
|
|
if len(el.Values) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-08-20 08:18:46 -07:00
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
2015-08-24 09:04:41 -07:00
|
|
|
resultVector = append(resultVector, &sample{
|
2015-03-30 10:13:36 -07:00
|
|
|
Metric: el.Metric,
|
|
|
|
Value: aggrFn(el.Values),
|
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return resultVector
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === avg_over_time(matrix model.ValMatrix) Vector ===
|
|
|
|
func funcAvgOverTime(ev *evaluator, args Expressions) model.Value {
|
2015-08-22 05:52:35 -07:00
|
|
|
return aggrOverTime(ev, args, func(values []model.SamplePair) model.SampleValue {
|
2015-08-20 08:18:46 -07:00
|
|
|
var sum model.SampleValue
|
2015-03-30 10:13:36 -07:00
|
|
|
for _, v := range values {
|
|
|
|
sum += v.Value
|
|
|
|
}
|
2015-08-20 08:18:46 -07:00
|
|
|
return sum / model.SampleValue(len(values))
|
2015-03-30 10:13:36 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === count_over_time(matrix model.ValMatrix) Vector ===
|
|
|
|
func funcCountOverTime(ev *evaluator, args Expressions) model.Value {
|
2015-08-22 05:52:35 -07:00
|
|
|
return aggrOverTime(ev, args, func(values []model.SamplePair) model.SampleValue {
|
2015-08-20 08:18:46 -07:00
|
|
|
return model.SampleValue(len(values))
|
2015-03-30 10:13:36 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === floor(vector model.ValVector) Vector ===
|
|
|
|
func funcFloor(ev *evaluator, args Expressions) model.Value {
|
2015-03-30 10:13:36 -07:00
|
|
|
vector := ev.evalVector(args[0])
|
|
|
|
for _, el := range vector {
|
2015-08-20 08:18:46 -07:00
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
|
|
|
el.Value = model.SampleValue(math.Floor(float64(el.Value)))
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === max_over_time(matrix model.ValMatrix) Vector ===
|
|
|
|
func funcMaxOverTime(ev *evaluator, args Expressions) model.Value {
|
2015-08-22 05:52:35 -07:00
|
|
|
return aggrOverTime(ev, args, func(values []model.SamplePair) model.SampleValue {
|
2015-03-30 10:13:36 -07:00
|
|
|
max := math.Inf(-1)
|
|
|
|
for _, v := range values {
|
|
|
|
max = math.Max(max, float64(v.Value))
|
|
|
|
}
|
2015-08-20 08:18:46 -07:00
|
|
|
return model.SampleValue(max)
|
2015-03-30 10:13:36 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === min_over_time(matrix model.ValMatrix) Vector ===
|
|
|
|
func funcMinOverTime(ev *evaluator, args Expressions) model.Value {
|
2015-08-22 05:52:35 -07:00
|
|
|
return aggrOverTime(ev, args, func(values []model.SamplePair) model.SampleValue {
|
2015-03-30 10:13:36 -07:00
|
|
|
min := math.Inf(1)
|
|
|
|
for _, v := range values {
|
|
|
|
min = math.Min(min, float64(v.Value))
|
|
|
|
}
|
2015-08-20 08:18:46 -07:00
|
|
|
return model.SampleValue(min)
|
2015-03-30 10:13:36 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === sum_over_time(matrix model.ValMatrix) Vector ===
|
|
|
|
func funcSumOverTime(ev *evaluator, args Expressions) model.Value {
|
2015-08-22 05:52:35 -07:00
|
|
|
return aggrOverTime(ev, args, func(values []model.SamplePair) model.SampleValue {
|
2015-08-20 08:18:46 -07:00
|
|
|
var sum model.SampleValue
|
2015-03-30 10:13:36 -07:00
|
|
|
for _, v := range values {
|
|
|
|
sum += v.Value
|
|
|
|
}
|
|
|
|
return sum
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-07-08 05:22:22 -07:00
|
|
|
// === quantile_over_time(matrix model.ValMatrix) Vector ===
|
|
|
|
func funcQuantileOverTime(ev *evaluator, args Expressions) model.Value {
|
|
|
|
q := ev.evalFloat(args[0])
|
|
|
|
mat := ev.evalMatrix(args[1])
|
|
|
|
resultVector := vector{}
|
|
|
|
|
|
|
|
for _, el := range mat {
|
|
|
|
if len(el.Values) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
2016-07-08 05:48:48 -07:00
|
|
|
values := make(vectorByValueHeap, 0, len(el.Values))
|
2016-07-08 05:33:20 -07:00
|
|
|
for _, v := range el.Values {
|
2016-07-08 05:48:48 -07:00
|
|
|
values = append(values, &sample{Value: v.Value})
|
2016-07-08 05:33:20 -07:00
|
|
|
}
|
2016-07-08 05:22:22 -07:00
|
|
|
resultVector = append(resultVector, &sample{
|
|
|
|
Metric: el.Metric,
|
2016-07-08 05:33:20 -07:00
|
|
|
Value: model.SampleValue(quantile(q, values)),
|
2016-07-08 05:22:22 -07:00
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return resultVector
|
|
|
|
}
|
|
|
|
|
2016-07-15 16:34:44 -07:00
|
|
|
// === stddev_over_time(matrix model.ValMatrix) Vector ===
|
|
|
|
func funcStddevOverTime(ev *evaluator, args Expressions) model.Value {
|
|
|
|
return aggrOverTime(ev, args, func(values []model.SamplePair) model.SampleValue {
|
|
|
|
var sum, squaredSum, count model.SampleValue
|
|
|
|
for _, v := range values {
|
|
|
|
sum += v.Value
|
|
|
|
squaredSum += v.Value * v.Value
|
2016-08-24 09:37:09 -07:00
|
|
|
count++
|
2016-07-15 16:34:44 -07:00
|
|
|
}
|
|
|
|
avg := sum / count
|
|
|
|
return model.SampleValue(math.Sqrt(float64(squaredSum/count - avg*avg)))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// === stdvar_over_time(matrix model.ValMatrix) Vector ===
|
|
|
|
func funcStdvarOverTime(ev *evaluator, args Expressions) model.Value {
|
|
|
|
return aggrOverTime(ev, args, func(values []model.SamplePair) model.SampleValue {
|
|
|
|
var sum, squaredSum, count model.SampleValue
|
|
|
|
for _, v := range values {
|
|
|
|
sum += v.Value
|
|
|
|
squaredSum += v.Value * v.Value
|
2016-08-24 09:37:09 -07:00
|
|
|
count++
|
2016-07-15 16:34:44 -07:00
|
|
|
}
|
|
|
|
avg := sum / count
|
|
|
|
return squaredSum/count - avg*avg
|
|
|
|
})
|
2016-07-08 05:48:48 -07:00
|
|
|
}
|
2016-07-15 16:34:44 -07:00
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === abs(vector model.ValVector) Vector ===
|
|
|
|
func funcAbs(ev *evaluator, args Expressions) model.Value {
|
2015-03-30 10:13:36 -07:00
|
|
|
vector := ev.evalVector(args[0])
|
|
|
|
for _, el := range vector {
|
2015-08-20 08:18:46 -07:00
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
|
|
|
el.Value = model.SampleValue(math.Abs(float64(el.Value)))
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === absent(vector model.ValVector) Vector ===
|
|
|
|
func funcAbsent(ev *evaluator, args Expressions) model.Value {
|
2015-03-30 10:13:36 -07:00
|
|
|
if len(ev.evalVector(args[0])) > 0 {
|
2015-08-24 09:04:41 -07:00
|
|
|
return vector{}
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
2015-08-20 08:18:46 -07:00
|
|
|
m := model.Metric{}
|
2015-03-30 10:13:36 -07:00
|
|
|
if vs, ok := args[0].(*VectorSelector); ok {
|
|
|
|
for _, matcher := range vs.LabelMatchers {
|
2015-08-20 08:18:46 -07:00
|
|
|
if matcher.Type == metric.Equal && matcher.Name != model.MetricNameLabel {
|
2015-03-30 10:13:36 -07:00
|
|
|
m[matcher.Name] = matcher.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-24 09:04:41 -07:00
|
|
|
return vector{
|
|
|
|
&sample{
|
|
|
|
Metric: metric.Metric{
|
2015-03-30 10:13:36 -07:00
|
|
|
Metric: m,
|
|
|
|
Copied: true,
|
|
|
|
},
|
|
|
|
Value: 1,
|
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === ceil(vector model.ValVector) Vector ===
|
|
|
|
func funcCeil(ev *evaluator, args Expressions) model.Value {
|
2015-03-30 10:13:36 -07:00
|
|
|
vector := ev.evalVector(args[0])
|
|
|
|
for _, el := range vector {
|
2015-08-20 08:18:46 -07:00
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
|
|
|
el.Value = model.SampleValue(math.Ceil(float64(el.Value)))
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === exp(vector model.ValVector) Vector ===
|
|
|
|
func funcExp(ev *evaluator, args Expressions) model.Value {
|
2015-03-30 10:13:36 -07:00
|
|
|
vector := ev.evalVector(args[0])
|
|
|
|
for _, el := range vector {
|
2015-08-20 08:18:46 -07:00
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
|
|
|
el.Value = model.SampleValue(math.Exp(float64(el.Value)))
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
|
|
|
// === sqrt(vector VectorNode) Vector ===
|
2015-08-24 09:04:41 -07:00
|
|
|
func funcSqrt(ev *evaluator, args Expressions) model.Value {
|
2015-03-30 10:13:36 -07:00
|
|
|
vector := ev.evalVector(args[0])
|
|
|
|
for _, el := range vector {
|
2015-08-20 08:18:46 -07:00
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
|
|
|
el.Value = model.SampleValue(math.Sqrt(float64(el.Value)))
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === ln(vector model.ValVector) Vector ===
|
|
|
|
func funcLn(ev *evaluator, args Expressions) model.Value {
|
2015-03-30 10:13:36 -07:00
|
|
|
vector := ev.evalVector(args[0])
|
|
|
|
for _, el := range vector {
|
2015-08-20 08:18:46 -07:00
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
|
|
|
el.Value = model.SampleValue(math.Log(float64(el.Value)))
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === log2(vector model.ValVector) Vector ===
|
|
|
|
func funcLog2(ev *evaluator, args Expressions) model.Value {
|
2015-03-30 10:13:36 -07:00
|
|
|
vector := ev.evalVector(args[0])
|
|
|
|
for _, el := range vector {
|
2015-08-20 08:18:46 -07:00
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
|
|
|
el.Value = model.SampleValue(math.Log2(float64(el.Value)))
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === log10(vector model.ValVector) Vector ===
|
|
|
|
func funcLog10(ev *evaluator, args Expressions) model.Value {
|
2015-03-30 10:13:36 -07:00
|
|
|
vector := ev.evalVector(args[0])
|
|
|
|
for _, el := range vector {
|
2015-08-20 08:18:46 -07:00
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
|
|
|
el.Value = model.SampleValue(math.Log10(float64(el.Value)))
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
|
|
|
return vector
|
|
|
|
}
|
|
|
|
|
2016-02-24 08:16:24 -08:00
|
|
|
// linearRegression performs a least-square linear regression analysis on the
|
|
|
|
// provided SamplePairs. It returns the slope, and the intercept value at the
|
|
|
|
// provided time.
|
|
|
|
func linearRegression(samples []model.SamplePair, interceptTime model.Time) (slope, intercept model.SampleValue) {
|
|
|
|
var (
|
|
|
|
n model.SampleValue
|
|
|
|
sumX, sumY model.SampleValue
|
|
|
|
sumXY, sumX2 model.SampleValue
|
|
|
|
)
|
|
|
|
for _, sample := range samples {
|
|
|
|
x := model.SampleValue(
|
|
|
|
model.Time(sample.Timestamp-interceptTime).UnixNano(),
|
|
|
|
) / 1e9
|
|
|
|
n += 1.0
|
|
|
|
sumY += sample.Value
|
|
|
|
sumX += x
|
|
|
|
sumXY += x * sample.Value
|
|
|
|
sumX2 += x * x
|
|
|
|
}
|
|
|
|
covXY := sumXY - sumX*sumY/n
|
|
|
|
varX := sumX2 - sumX*sumX/n
|
|
|
|
|
|
|
|
slope = covXY / varX
|
|
|
|
intercept = sumY/n - slope*sumX/n
|
2016-03-09 06:06:00 -08:00
|
|
|
return slope, intercept
|
2016-02-24 08:16:24 -08:00
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === deriv(node model.ValMatrix) Vector ===
|
|
|
|
func funcDeriv(ev *evaluator, args Expressions) model.Value {
|
|
|
|
mat := ev.evalMatrix(args[0])
|
2016-02-24 08:16:24 -08:00
|
|
|
resultVector := make(vector, 0, len(mat))
|
2015-03-30 10:13:36 -07:00
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
for _, samples := range mat {
|
2015-03-30 10:13:36 -07:00
|
|
|
// No sense in trying to compute a derivative without at least two points.
|
|
|
|
// Drop this vector element.
|
|
|
|
if len(samples.Values) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
2016-02-24 08:16:24 -08:00
|
|
|
slope, _ := linearRegression(samples.Values, 0)
|
2015-08-24 09:04:41 -07:00
|
|
|
resultSample := &sample{
|
2015-03-30 10:13:36 -07:00
|
|
|
Metric: samples.Metric,
|
2016-02-24 08:16:24 -08:00
|
|
|
Value: slope,
|
2015-03-30 10:13:36 -07:00
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
}
|
2015-08-20 08:18:46 -07:00
|
|
|
resultSample.Metric.Del(model.MetricNameLabel)
|
2015-03-30 10:13:36 -07:00
|
|
|
resultVector = append(resultVector, resultSample)
|
|
|
|
}
|
|
|
|
return resultVector
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === predict_linear(node model.ValMatrix, k model.ValScalar) Vector ===
|
|
|
|
func funcPredictLinear(ev *evaluator, args Expressions) model.Value {
|
2016-02-24 08:16:24 -08:00
|
|
|
mat := ev.evalMatrix(args[0])
|
|
|
|
resultVector := make(vector, 0, len(mat))
|
|
|
|
duration := model.SampleValue(ev.evalFloat(args[1]))
|
2015-07-28 04:30:57 -07:00
|
|
|
|
2016-02-24 08:16:24 -08:00
|
|
|
for _, samples := range mat {
|
|
|
|
// No sense in trying to predict anything without at least two points.
|
|
|
|
// Drop this vector element.
|
2015-07-28 04:30:57 -07:00
|
|
|
if len(samples.Values) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
2016-02-24 08:16:24 -08:00
|
|
|
slope, intercept := linearRegression(samples.Values, ev.Timestamp)
|
|
|
|
resultSample := &sample{
|
|
|
|
Metric: samples.Metric,
|
|
|
|
Value: slope*duration + intercept,
|
|
|
|
Timestamp: ev.Timestamp,
|
2015-07-28 04:30:57 -07:00
|
|
|
}
|
2016-02-24 08:16:24 -08:00
|
|
|
resultSample.Metric.Del(model.MetricNameLabel)
|
|
|
|
resultVector = append(resultVector, resultSample)
|
2015-07-28 04:30:57 -07:00
|
|
|
}
|
2016-02-24 08:16:24 -08:00
|
|
|
return resultVector
|
2015-07-28 04:30:57 -07:00
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === histogram_quantile(k model.ValScalar, vector model.ValVector) Vector ===
|
|
|
|
func funcHistogramQuantile(ev *evaluator, args Expressions) model.Value {
|
2015-08-20 08:18:46 -07:00
|
|
|
q := model.SampleValue(ev.evalFloat(args[0]))
|
2015-03-30 10:13:36 -07:00
|
|
|
inVec := ev.evalVector(args[1])
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
outVec := vector{}
|
2015-03-30 10:13:36 -07:00
|
|
|
signatureToMetricWithBuckets := map[uint64]*metricWithBuckets{}
|
|
|
|
for _, el := range inVec {
|
|
|
|
upperBound, err := strconv.ParseFloat(
|
2015-08-20 08:18:46 -07:00
|
|
|
string(el.Metric.Metric[model.BucketLabel]), 64,
|
2015-03-30 10:13:36 -07:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
// Oops, no bucket label or malformed label value. Skip.
|
|
|
|
// TODO(beorn7): Issue a warning somehow.
|
|
|
|
continue
|
|
|
|
}
|
2015-08-20 08:18:46 -07:00
|
|
|
signature := model.SignatureWithoutLabels(el.Metric.Metric, excludedLabels)
|
2015-03-30 10:13:36 -07:00
|
|
|
mb, ok := signatureToMetricWithBuckets[signature]
|
|
|
|
if !ok {
|
2015-08-20 08:18:46 -07:00
|
|
|
el.Metric.Del(model.BucketLabel)
|
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
2015-03-30 10:13:36 -07:00
|
|
|
mb = &metricWithBuckets{el.Metric, nil}
|
|
|
|
signatureToMetricWithBuckets[signature] = mb
|
|
|
|
}
|
|
|
|
mb.buckets = append(mb.buckets, bucket{upperBound, el.Value})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mb := range signatureToMetricWithBuckets {
|
2015-08-24 09:04:41 -07:00
|
|
|
outVec = append(outVec, &sample{
|
2015-03-30 10:13:36 -07:00
|
|
|
Metric: mb.metric,
|
2016-07-08 05:33:20 -07:00
|
|
|
Value: model.SampleValue(bucketQuantile(q, mb.buckets)),
|
2015-03-30 10:13:36 -07:00
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return outVec
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === resets(matrix model.ValMatrix) Vector ===
|
|
|
|
func funcResets(ev *evaluator, args Expressions) model.Value {
|
2015-05-26 08:47:52 -07:00
|
|
|
in := ev.evalMatrix(args[0])
|
2015-08-24 09:04:41 -07:00
|
|
|
out := make(vector, 0, len(in))
|
2015-05-26 08:47:52 -07:00
|
|
|
|
|
|
|
for _, samples := range in {
|
|
|
|
resets := 0
|
2015-08-20 08:18:46 -07:00
|
|
|
prev := model.SampleValue(samples.Values[0].Value)
|
2015-05-26 08:47:52 -07:00
|
|
|
for _, sample := range samples.Values[1:] {
|
|
|
|
current := sample.Value
|
|
|
|
if current < prev {
|
|
|
|
resets++
|
|
|
|
}
|
|
|
|
prev = current
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
rs := &sample{
|
2015-05-26 08:47:52 -07:00
|
|
|
Metric: samples.Metric,
|
2015-08-20 08:18:46 -07:00
|
|
|
Value: model.SampleValue(resets),
|
2015-05-26 08:47:52 -07:00
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
}
|
2015-08-20 08:18:46 -07:00
|
|
|
rs.Metric.Del(model.MetricNameLabel)
|
2015-05-26 08:47:52 -07:00
|
|
|
out = append(out, rs)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === changes(matrix model.ValMatrix) Vector ===
|
|
|
|
func funcChanges(ev *evaluator, args Expressions) model.Value {
|
2015-05-26 10:01:34 -07:00
|
|
|
in := ev.evalMatrix(args[0])
|
2015-08-24 09:04:41 -07:00
|
|
|
out := make(vector, 0, len(in))
|
2015-05-26 10:01:34 -07:00
|
|
|
|
|
|
|
for _, samples := range in {
|
|
|
|
changes := 0
|
2015-08-20 08:18:46 -07:00
|
|
|
prev := model.SampleValue(samples.Values[0].Value)
|
2015-05-26 10:01:34 -07:00
|
|
|
for _, sample := range samples.Values[1:] {
|
|
|
|
current := sample.Value
|
|
|
|
if current != prev {
|
|
|
|
changes++
|
|
|
|
}
|
|
|
|
prev = current
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
rs := &sample{
|
2015-05-26 10:01:34 -07:00
|
|
|
Metric: samples.Metric,
|
2015-08-20 08:18:46 -07:00
|
|
|
Value: model.SampleValue(changes),
|
2015-05-26 10:01:34 -07:00
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
}
|
2015-08-20 08:18:46 -07:00
|
|
|
rs.Metric.Del(model.MetricNameLabel)
|
2015-05-26 10:01:34 -07:00
|
|
|
out = append(out, rs)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
// === label_replace(vector model.ValVector, dst_label, replacement, src_labelname, regex model.ValString) Vector ===
|
|
|
|
func funcLabelReplace(ev *evaluator, args Expressions) model.Value {
|
2015-08-17 14:25:53 -07:00
|
|
|
var (
|
|
|
|
vector = ev.evalVector(args[0])
|
2015-08-20 08:18:46 -07:00
|
|
|
dst = model.LabelName(ev.evalString(args[1]).Value)
|
2015-08-17 14:25:53 -07:00
|
|
|
repl = ev.evalString(args[2]).Value
|
2015-08-20 08:18:46 -07:00
|
|
|
src = model.LabelName(ev.evalString(args[3]).Value)
|
2015-08-17 14:25:53 -07:00
|
|
|
regexStr = ev.evalString(args[4]).Value
|
|
|
|
)
|
|
|
|
|
2015-09-01 06:35:20 -07:00
|
|
|
regex, err := regexp.Compile("^(?:" + regexStr + ")$")
|
2015-08-17 14:25:53 -07:00
|
|
|
if err != nil {
|
|
|
|
ev.errorf("invalid regular expression in label_replace(): %s", regexStr)
|
|
|
|
}
|
2015-08-20 08:18:46 -07:00
|
|
|
if !model.LabelNameRE.MatchString(string(dst)) {
|
2015-08-17 14:25:53 -07:00
|
|
|
ev.errorf("invalid destination label name in label_replace(): %s", dst)
|
|
|
|
}
|
|
|
|
|
2015-08-20 08:18:46 -07:00
|
|
|
outSet := make(map[model.Fingerprint]struct{}, len(vector))
|
2015-08-17 14:25:53 -07: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 08:18:46 -07:00
|
|
|
el.Metric.Del(dst)
|
2015-08-17 14:25:53 -07:00
|
|
|
} else {
|
2015-08-20 08:18:46 -07:00
|
|
|
el.Metric.Set(dst, model.LabelValue(res))
|
2015-08-17 14:25:53 -07: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-09-13 01:13:22 -07:00
|
|
|
// === vector(s scalar) Vector ===
|
2015-09-11 04:09:34 -07:00
|
|
|
func funcVector(ev *evaluator, args Expressions) model.Value {
|
|
|
|
return vector{
|
|
|
|
&sample{
|
2015-09-16 05:33:12 -07:00
|
|
|
Metric: metric.Metric{},
|
2015-09-11 04:09:34 -07:00
|
|
|
Value: model.SampleValue(ev.evalFloat(args[0])),
|
|
|
|
Timestamp: ev.Timestamp,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-29 10:22:12 -07:00
|
|
|
// Common code for date related functions.
|
|
|
|
func dateWrapper(ev *evaluator, args Expressions, f func(time.Time) model.SampleValue) model.Value {
|
|
|
|
var v vector
|
|
|
|
if len(args) == 0 {
|
|
|
|
v = vector{
|
|
|
|
&sample{
|
|
|
|
Metric: metric.Metric{},
|
|
|
|
Value: model.SampleValue(ev.Timestamp.Unix()),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
v = ev.evalVector(args[0])
|
|
|
|
}
|
|
|
|
for _, el := range v {
|
2016-08-22 13:08:13 -07:00
|
|
|
el.Metric.Del(model.MetricNameLabel)
|
2016-08-29 10:22:12 -07:00
|
|
|
t := time.Unix(int64(el.Value), 0).UTC()
|
|
|
|
el.Value = f(t)
|
2016-08-22 13:08:13 -07:00
|
|
|
}
|
2016-08-29 10:22:12 -07:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// === days_in_month(v vector) scalar ===
|
|
|
|
func funcDaysInMonth(ev *evaluator, args Expressions) model.Value {
|
|
|
|
return dateWrapper(ev, args, func(t time.Time) model.SampleValue {
|
|
|
|
return model.SampleValue(32 - time.Date(t.Year(), t.Month(), 32, 0, 0, 0, 0, time.UTC).Day())
|
|
|
|
})
|
2016-08-22 13:08:13 -07:00
|
|
|
}
|
|
|
|
|
2016-08-22 12:57:33 -07:00
|
|
|
// === day_of_month(v vector) scalar ===
|
|
|
|
func funcDayOfMonth(ev *evaluator, args Expressions) model.Value {
|
2016-08-29 10:22:12 -07:00
|
|
|
return dateWrapper(ev, args, func(t time.Time) model.SampleValue {
|
|
|
|
return model.SampleValue(t.Day())
|
|
|
|
})
|
2016-08-22 12:57:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// === day_of_week(v vector) scalar ===
|
|
|
|
func funcDayOfWeek(ev *evaluator, args Expressions) model.Value {
|
2016-08-29 10:22:12 -07:00
|
|
|
return dateWrapper(ev, args, func(t time.Time) model.SampleValue {
|
|
|
|
return model.SampleValue(t.Weekday())
|
|
|
|
})
|
2016-08-22 12:57:33 -07:00
|
|
|
}
|
|
|
|
|
2016-08-23 14:34:22 -07:00
|
|
|
// === hour(v vector) scalar ===
|
|
|
|
func funcHour(ev *evaluator, args Expressions) model.Value {
|
2016-08-29 10:22:12 -07:00
|
|
|
return dateWrapper(ev, args, func(t time.Time) model.SampleValue {
|
|
|
|
return model.SampleValue(t.Hour())
|
|
|
|
})
|
2016-08-22 12:57:33 -07:00
|
|
|
}
|
|
|
|
|
2016-09-12 12:29:44 -07:00
|
|
|
// === minute(v vector) scalar ===
|
|
|
|
func funcMinute(ev *evaluator, args Expressions) model.Value {
|
|
|
|
return dateWrapper(ev, args, func(t time.Time) model.SampleValue {
|
|
|
|
return model.SampleValue(t.Minute())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-23 14:34:22 -07:00
|
|
|
// === month(v vector) scalar ===
|
|
|
|
func funcMonth(ev *evaluator, args Expressions) model.Value {
|
2016-08-29 10:22:12 -07:00
|
|
|
return dateWrapper(ev, args, func(t time.Time) model.SampleValue {
|
|
|
|
return model.SampleValue(t.Month())
|
|
|
|
})
|
2016-08-22 12:57:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// === year(v vector) scalar ===
|
|
|
|
func funcYear(ev *evaluator, args Expressions) model.Value {
|
2016-08-29 10:22:12 -07:00
|
|
|
return dateWrapper(ev, args, func(t time.Time) model.SampleValue {
|
|
|
|
return model.SampleValue(t.Year())
|
|
|
|
})
|
2016-08-22 12:57:33 -07:00
|
|
|
}
|
|
|
|
|
2015-03-30 09:12:51 -07:00
|
|
|
var functions = map[string]*Function{
|
|
|
|
"abs": {
|
|
|
|
Name: "abs",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValVector},
|
|
|
|
ReturnType: model.ValVector,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcAbs,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
|
|
|
"absent": {
|
|
|
|
Name: "absent",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValVector},
|
|
|
|
ReturnType: model.ValVector,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcAbsent,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
|
|
|
"avg_over_time": {
|
|
|
|
Name: "avg_over_time",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValMatrix},
|
|
|
|
ReturnType: model.ValVector,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcAvgOverTime,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
|
|
|
"ceil": {
|
|
|
|
Name: "ceil",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValVector},
|
|
|
|
ReturnType: model.ValVector,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcCeil,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
2015-05-26 10:01:34 -07:00
|
|
|
"changes": {
|
|
|
|
Name: "changes",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValMatrix},
|
|
|
|
ReturnType: model.ValVector,
|
2015-05-26 10:01:34 -07:00
|
|
|
Call: funcChanges,
|
|
|
|
},
|
2015-11-19 16:42:35 -08:00
|
|
|
"clamp_max": {
|
|
|
|
Name: "clamp_max",
|
|
|
|
ArgTypes: []model.ValueType{model.ValVector, model.ValScalar},
|
|
|
|
ReturnType: model.ValVector,
|
|
|
|
Call: funcClampMax,
|
|
|
|
},
|
|
|
|
"clamp_min": {
|
|
|
|
Name: "clamp_min",
|
|
|
|
ArgTypes: []model.ValueType{model.ValVector, model.ValScalar},
|
|
|
|
ReturnType: model.ValVector,
|
|
|
|
Call: funcClampMin,
|
|
|
|
},
|
2015-03-30 09:12:51 -07:00
|
|
|
"count_over_time": {
|
|
|
|
Name: "count_over_time",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValMatrix},
|
|
|
|
ReturnType: model.ValVector,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcCountOverTime,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
|
|
|
"count_scalar": {
|
|
|
|
Name: "count_scalar",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValVector},
|
|
|
|
ReturnType: model.ValScalar,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcCountScalar,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
2016-08-22 13:08:13 -07:00
|
|
|
"days_in_month": {
|
2016-08-29 10:22:12 -07:00
|
|
|
Name: "days_in_month",
|
|
|
|
ArgTypes: []model.ValueType{model.ValVector},
|
|
|
|
OptionalArgs: 1,
|
|
|
|
ReturnType: model.ValVector,
|
|
|
|
Call: funcDaysInMonth,
|
2016-08-22 13:08:13 -07:00
|
|
|
},
|
2016-08-22 12:57:33 -07:00
|
|
|
"day_of_month": {
|
2016-08-29 10:22:12 -07:00
|
|
|
Name: "day_of_month",
|
|
|
|
ArgTypes: []model.ValueType{model.ValVector},
|
|
|
|
OptionalArgs: 1,
|
|
|
|
ReturnType: model.ValVector,
|
|
|
|
Call: funcDayOfMonth,
|
2016-08-22 12:57:33 -07:00
|
|
|
},
|
|
|
|
"day_of_week": {
|
2016-08-29 10:22:12 -07:00
|
|
|
Name: "day_of_week",
|
|
|
|
ArgTypes: []model.ValueType{model.ValVector},
|
|
|
|
OptionalArgs: 1,
|
|
|
|
ReturnType: model.ValVector,
|
|
|
|
Call: funcDayOfWeek,
|
2016-08-22 12:57:33 -07:00
|
|
|
},
|
2015-03-30 09:12:51 -07:00
|
|
|
"delta": {
|
2015-10-10 07:41:23 -07:00
|
|
|
Name: "delta",
|
|
|
|
ArgTypes: []model.ValueType{model.ValMatrix},
|
|
|
|
ReturnType: model.ValVector,
|
|
|
|
Call: funcDelta,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
|
|
|
"deriv": {
|
|
|
|
Name: "deriv",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValMatrix},
|
|
|
|
ReturnType: model.ValVector,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcDeriv,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
|
|
|
"drop_common_labels": {
|
|
|
|
Name: "drop_common_labels",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValVector},
|
|
|
|
ReturnType: model.ValVector,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcDropCommonLabels,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
|
|
|
"exp": {
|
|
|
|
Name: "exp",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValVector},
|
|
|
|
ReturnType: model.ValVector,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcExp,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
|
|
|
"floor": {
|
|
|
|
Name: "floor",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValVector},
|
|
|
|
ReturnType: model.ValVector,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcFloor,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
|
|
|
"histogram_quantile": {
|
|
|
|
Name: "histogram_quantile",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValScalar, model.ValVector},
|
|
|
|
ReturnType: model.ValVector,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcHistogramQuantile,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
2016-03-09 19:29:02 -08:00
|
|
|
"holt_winters": {
|
|
|
|
Name: "holt_winters",
|
|
|
|
ArgTypes: []model.ValueType{model.ValMatrix, model.ValScalar, model.ValScalar},
|
|
|
|
ReturnType: model.ValVector,
|
|
|
|
Call: funcHoltWinters,
|
|
|
|
},
|
2016-08-23 14:34:22 -07:00
|
|
|
"hour": {
|
2016-08-29 10:22:12 -07:00
|
|
|
Name: "hour",
|
|
|
|
ArgTypes: []model.ValueType{model.ValVector},
|
|
|
|
OptionalArgs: 1,
|
|
|
|
ReturnType: model.ValVector,
|
|
|
|
Call: funcHour,
|
2015-10-09 09:58:43 -07:00
|
|
|
},
|
2016-08-08 01:40:50 -07:00
|
|
|
"idelta": {
|
|
|
|
Name: "idelta",
|
|
|
|
ArgTypes: []model.ValueType{model.ValMatrix},
|
|
|
|
ReturnType: model.ValVector,
|
|
|
|
Call: funcIdelta,
|
|
|
|
},
|
2016-08-22 13:00:53 -07:00
|
|
|
"increase": {
|
|
|
|
Name: "increase",
|
|
|
|
ArgTypes: []model.ValueType{model.ValMatrix},
|
|
|
|
ReturnType: model.ValVector,
|
|
|
|
Call: funcIncrease,
|
|
|
|
},
|
|
|
|
"irate": {
|
|
|
|
Name: "irate",
|
|
|
|
ArgTypes: []model.ValueType{model.ValMatrix},
|
|
|
|
ReturnType: model.ValVector,
|
|
|
|
Call: funcIrate,
|
|
|
|
},
|
2015-08-17 14:25:53 -07:00
|
|
|
"label_replace": {
|
|
|
|
Name: "label_replace",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValVector, model.ValString, model.ValString, model.ValString, model.ValString},
|
|
|
|
ReturnType: model.ValVector,
|
2015-08-17 14:25:53 -07:00
|
|
|
Call: funcLabelReplace,
|
|
|
|
},
|
2015-03-30 09:12:51 -07:00
|
|
|
"ln": {
|
|
|
|
Name: "ln",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValVector},
|
|
|
|
ReturnType: model.ValVector,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcLn,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
|
|
|
"log10": {
|
|
|
|
Name: "log10",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValVector},
|
|
|
|
ReturnType: model.ValVector,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcLog10,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
|
|
|
"log2": {
|
|
|
|
Name: "log2",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValVector},
|
|
|
|
ReturnType: model.ValVector,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcLog2,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
|
|
|
"max_over_time": {
|
|
|
|
Name: "max_over_time",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValMatrix},
|
|
|
|
ReturnType: model.ValVector,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcMaxOverTime,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
|
|
|
"min_over_time": {
|
|
|
|
Name: "min_over_time",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValMatrix},
|
|
|
|
ReturnType: model.ValVector,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcMinOverTime,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
2016-09-12 12:29:44 -07:00
|
|
|
"minute": {
|
|
|
|
Name: "minute",
|
|
|
|
ArgTypes: []model.ValueType{model.ValVector},
|
|
|
|
OptionalArgs: 1,
|
|
|
|
ReturnType: model.ValVector,
|
|
|
|
Call: funcMinute,
|
|
|
|
},
|
2016-08-23 14:34:22 -07:00
|
|
|
"month": {
|
2016-08-29 10:22:12 -07:00
|
|
|
Name: "month",
|
|
|
|
ArgTypes: []model.ValueType{model.ValVector},
|
|
|
|
OptionalArgs: 1,
|
|
|
|
ReturnType: model.ValVector,
|
|
|
|
Call: funcMonth,
|
2016-08-22 12:57:33 -07:00
|
|
|
},
|
2015-07-28 04:30:57 -07:00
|
|
|
"predict_linear": {
|
|
|
|
Name: "predict_linear",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValMatrix, model.ValScalar},
|
|
|
|
ReturnType: model.ValVector,
|
2015-07-28 04:30:57 -07:00
|
|
|
Call: funcPredictLinear,
|
|
|
|
},
|
2016-07-08 05:22:22 -07:00
|
|
|
"quantile_over_time": {
|
|
|
|
Name: "quantile_over_time",
|
|
|
|
ArgTypes: []model.ValueType{model.ValScalar, model.ValMatrix},
|
|
|
|
ReturnType: model.ValVector,
|
|
|
|
Call: funcQuantileOverTime,
|
|
|
|
},
|
2015-03-30 09:12:51 -07:00
|
|
|
"rate": {
|
|
|
|
Name: "rate",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValMatrix},
|
|
|
|
ReturnType: model.ValVector,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcRate,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
2015-05-26 08:47:52 -07:00
|
|
|
"resets": {
|
|
|
|
Name: "resets",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValMatrix},
|
|
|
|
ReturnType: model.ValVector,
|
2015-05-26 08:47:52 -07:00
|
|
|
Call: funcResets,
|
|
|
|
},
|
2015-03-30 09:12:51 -07:00
|
|
|
"round": {
|
|
|
|
Name: "round",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValVector, model.ValScalar},
|
2015-03-30 09:12:51 -07:00
|
|
|
OptionalArgs: 1,
|
2015-08-24 09:04:41 -07:00
|
|
|
ReturnType: model.ValVector,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcRound,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
|
|
|
"scalar": {
|
|
|
|
Name: "scalar",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValVector},
|
|
|
|
ReturnType: model.ValScalar,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcScalar,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
|
|
|
"sort": {
|
|
|
|
Name: "sort",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValVector},
|
|
|
|
ReturnType: model.ValVector,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcSort,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
|
|
|
"sort_desc": {
|
|
|
|
Name: "sort_desc",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValVector},
|
|
|
|
ReturnType: model.ValVector,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcSortDesc,
|
|
|
|
},
|
|
|
|
"sqrt": {
|
|
|
|
Name: "sqrt",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValVector},
|
|
|
|
ReturnType: model.ValVector,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcSqrt,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
2016-07-15 16:34:44 -07:00
|
|
|
"stddev_over_time": {
|
|
|
|
Name: "stddev_over_time",
|
|
|
|
ArgTypes: []model.ValueType{model.ValMatrix},
|
|
|
|
ReturnType: model.ValVector,
|
|
|
|
Call: funcStddevOverTime,
|
|
|
|
},
|
|
|
|
"stdvar_over_time": {
|
|
|
|
Name: "stdvar_over_time",
|
|
|
|
ArgTypes: []model.ValueType{model.ValMatrix},
|
|
|
|
ReturnType: model.ValVector,
|
|
|
|
Call: funcStdvarOverTime,
|
|
|
|
},
|
2015-03-30 09:12:51 -07:00
|
|
|
"sum_over_time": {
|
|
|
|
Name: "sum_over_time",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{model.ValMatrix},
|
|
|
|
ReturnType: model.ValVector,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcSumOverTime,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
|
|
|
"time": {
|
|
|
|
Name: "time",
|
2015-08-24 09:04:41 -07:00
|
|
|
ArgTypes: []model.ValueType{},
|
|
|
|
ReturnType: model.ValScalar,
|
2015-03-30 10:13:36 -07:00
|
|
|
Call: funcTime,
|
2015-03-30 09:12:51 -07:00
|
|
|
},
|
2015-09-11 04:09:34 -07:00
|
|
|
"vector": {
|
2015-09-16 05:33:12 -07:00
|
|
|
Name: "vector",
|
|
|
|
ArgTypes: []model.ValueType{model.ValScalar},
|
|
|
|
ReturnType: model.ValVector,
|
|
|
|
Call: funcVector,
|
2015-09-11 04:09:34 -07:00
|
|
|
},
|
2016-08-22 12:57:33 -07:00
|
|
|
"year": {
|
2016-08-29 10:22:12 -07:00
|
|
|
Name: "year",
|
|
|
|
ArgTypes: []model.ValueType{model.ValVector},
|
|
|
|
OptionalArgs: 1,
|
|
|
|
ReturnType: model.ValVector,
|
|
|
|
Call: funcYear,
|
2016-08-22 12:57:33 -07:00
|
|
|
},
|
2015-03-30 09:12:51 -07:00
|
|
|
}
|
|
|
|
|
2015-03-30 10:13:36 -07:00
|
|
|
// getFunction returns a predefined Function object for the given name.
|
|
|
|
func getFunction(name string) (*Function, bool) {
|
2015-03-30 09:12:51 -07:00
|
|
|
function, ok := functions[name]
|
|
|
|
return function, ok
|
|
|
|
}
|
2015-03-30 10:13:36 -07:00
|
|
|
|
2015-08-24 09:04:41 -07:00
|
|
|
type vectorByValueHeap vector
|
2015-03-30 10:13:36 -07:00
|
|
|
|
|
|
|
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{}) {
|
2015-08-24 09:04:41 -07:00
|
|
|
*s = append(*s, x.(*sample))
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *vectorByValueHeap) Pop() interface{} {
|
|
|
|
old := *s
|
|
|
|
n := len(old)
|
|
|
|
el := old[n-1]
|
|
|
|
*s = old[0 : n-1]
|
|
|
|
return el
|
|
|
|
}
|
|
|
|
|
2015-12-22 05:53:43 -08:00
|
|
|
type vectorByReverseValueHeap vector
|
|
|
|
|
|
|
|
func (s vectorByReverseValueHeap) Len() int {
|
|
|
|
return len(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s vectorByReverseValueHeap) Less(i, j int) bool {
|
|
|
|
if math.IsNaN(float64(s[i].Value)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return s[i].Value > s[j].Value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s vectorByReverseValueHeap) Swap(i, j int) {
|
|
|
|
s[i], s[j] = s[j], s[i]
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|
|
|
|
|
2015-12-22 05:53:43 -08:00
|
|
|
func (s *vectorByReverseValueHeap) Push(x interface{}) {
|
|
|
|
*s = append(*s, x.(*sample))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *vectorByReverseValueHeap) Pop() interface{} {
|
|
|
|
old := *s
|
|
|
|
n := len(old)
|
|
|
|
el := old[n-1]
|
|
|
|
*s = old[0 : n-1]
|
|
|
|
return el
|
2015-03-30 10:13:36 -07:00
|
|
|
}
|