mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2025-01-07 08:32:18 +01:00
app/vmselect/promql: add lifetime(q[d])
function, which returns the lifetime of q
over d
in seconds.
This function is useful for determining time series lifetime. `d` must exceed the expected lifetime of the time series, otherwise the function would return values close to `d`.
This commit is contained in:
parent
0cb66a8f95
commit
dcce92c63c
@ -45,6 +45,7 @@ var rollupFuncs = map[string]newRollupFunc{
|
|||||||
"distinct_over_time": newRollupFuncOneArg(rollupDistinct),
|
"distinct_over_time": newRollupFuncOneArg(rollupDistinct),
|
||||||
"integrate": newRollupFuncOneArg(rollupIntegrate),
|
"integrate": newRollupFuncOneArg(rollupIntegrate),
|
||||||
"ideriv": newRollupFuncOneArg(rollupIderiv),
|
"ideriv": newRollupFuncOneArg(rollupIderiv),
|
||||||
|
"lifetime": newRollupFuncOneArg(rollupLifetime),
|
||||||
"rollup": newRollupFuncOneArg(rollupFake),
|
"rollup": newRollupFuncOneArg(rollupFake),
|
||||||
"rollup_rate": newRollupFuncOneArg(rollupFake), // + rollupFuncsRemoveCounterResets
|
"rollup_rate": newRollupFuncOneArg(rollupFake), // + rollupFuncsRemoveCounterResets
|
||||||
"rollup_deriv": newRollupFuncOneArg(rollupFake),
|
"rollup_deriv": newRollupFuncOneArg(rollupFake),
|
||||||
@ -725,6 +726,21 @@ func rollupIderiv(rfa *rollupFuncArg) float64 {
|
|||||||
return dv / (float64(dt) * 1e-3)
|
return dv / (float64(dt) * 1e-3)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func rollupLifetime(rfa *rollupFuncArg) float64 {
|
||||||
|
// Calculate the duration between the first and the last data points.
|
||||||
|
timestamps := rfa.timestamps
|
||||||
|
if math.IsNaN(rfa.prevValue) {
|
||||||
|
if len(timestamps) < 2 {
|
||||||
|
return nan
|
||||||
|
}
|
||||||
|
return float64(timestamps[len(timestamps)-1]-timestamps[0]) * 1e-3
|
||||||
|
}
|
||||||
|
if len(timestamps) == 0 {
|
||||||
|
return nan
|
||||||
|
}
|
||||||
|
return float64(timestamps[len(timestamps)-1]-rfa.prevTimestamp) * 1e-3
|
||||||
|
}
|
||||||
|
|
||||||
func rollupChanges(rfa *rollupFuncArg) float64 {
|
func rollupChanges(rfa *rollupFuncArg) float64 {
|
||||||
// There is no need in handling NaNs here, since they must be cleaned up
|
// There is no need in handling NaNs here, since they must be cleaned up
|
||||||
// before calling rollup funcs.
|
// before calling rollup funcs.
|
||||||
|
@ -584,6 +584,34 @@ func TestRollupFuncsNoWindow(t *testing.T) {
|
|||||||
timestampsExpected := []int64{10, 50, 90, 130}
|
timestampsExpected := []int64{10, 50, 90, 130}
|
||||||
testRowsEqual(t, values, rc.Timestamps, valuesExpected, timestampsExpected)
|
testRowsEqual(t, values, rc.Timestamps, valuesExpected, timestampsExpected)
|
||||||
})
|
})
|
||||||
|
t.Run("lifetime", func(t *testing.T) {
|
||||||
|
rc := rollupConfig{
|
||||||
|
Func: rollupLifetime,
|
||||||
|
Start: 0,
|
||||||
|
End: 160,
|
||||||
|
Step: 40,
|
||||||
|
Window: 0,
|
||||||
|
}
|
||||||
|
rc.Timestamps = getTimestamps(rc.Start, rc.End, rc.Step)
|
||||||
|
values := rc.Do(nil, testValues, testTimestamps)
|
||||||
|
valuesExpected := []float64{nan, 0.031, 0.044, 0.04, 0.01}
|
||||||
|
timestampsExpected := []int64{0, 40, 80, 120, 160}
|
||||||
|
testRowsEqual(t, values, rc.Timestamps, valuesExpected, timestampsExpected)
|
||||||
|
})
|
||||||
|
t.Run("lifetime", func(t *testing.T) {
|
||||||
|
rc := rollupConfig{
|
||||||
|
Func: rollupLifetime,
|
||||||
|
Start: 0,
|
||||||
|
End: 160,
|
||||||
|
Step: 40,
|
||||||
|
Window: 200,
|
||||||
|
}
|
||||||
|
rc.Timestamps = getTimestamps(rc.Start, rc.End, rc.Step)
|
||||||
|
values := rc.Do(nil, testValues, testTimestamps)
|
||||||
|
valuesExpected := []float64{nan, 0.031, 0.075, 0.115, 0.125}
|
||||||
|
timestampsExpected := []int64{0, 40, 80, 120, 160}
|
||||||
|
testRowsEqual(t, values, rc.Timestamps, valuesExpected, timestampsExpected)
|
||||||
|
})
|
||||||
t.Run("changes", func(t *testing.T) {
|
t.Run("changes", func(t *testing.T) {
|
||||||
rc := rollupConfig{
|
rc := rollupConfig{
|
||||||
Func: rollupChanges,
|
Func: rollupChanges,
|
||||||
|
Loading…
Reference in New Issue
Block a user