feat: linreg => slope 0 for const values (#9728)

* feat: linreg => slope 0 for const values

Signed-off-by: mtfoley <mtfoley.mae@gmail.com>
This commit is contained in:
Matthew 2021-11-15 18:03:22 -05:00 committed by GitHub
parent 5bec690282
commit 7ee52bfb86
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 4 deletions

View file

@ -716,8 +716,16 @@ func linearRegression(samples []Point, interceptTime int64) (slope, intercept fl
sumY, cY float64
sumXY, cXY float64
sumX2, cX2 float64
initY float64
constY bool
)
for _, sample := range samples {
initY = samples[0].V
constY = true
for i, sample := range samples {
// Set constY to false if any new y values are encountered.
if constY && i > 0 && sample.V != initY {
constY = false
}
n += 1.0
x := float64(sample.T-interceptTime) / 1e3
sumX, cX = kahanSumInc(x, sumX, cX)
@ -725,7 +733,12 @@ func linearRegression(samples []Point, interceptTime int64) (slope, intercept fl
sumXY, cXY = kahanSumInc(x*sample.V, sumXY, cXY)
sumX2, cX2 = kahanSumInc(x*x, sumX2, cX2)
}
if constY {
if math.IsInf(initY, 0) {
return math.NaN(), math.NaN()
}
return 0, initY
}
sumX = sumX + cX
sumY = sumY + cY
sumXY = sumXY + cXY

View file

@ -44,8 +44,14 @@ func TestDeriv(t *testing.T) {
a := storage.Appender(context.Background())
metric := labels.FromStrings("__name__", "foo")
a.Append(0, metric, 1493712816939, 1.0)
a.Append(0, metric, 1493712846939, 1.0)
start := 1493712816939
interval := 30 * 1000
// Introduce some timestamp jitter to test 0 slope case.
// https://github.com/prometheus/prometheus/issues/7180
for i := 0; i < 15; i++ {
jitter := 12 * i % 2
a.Append(0, metric, int64(start+interval*i+jitter), 1)
}
require.NoError(t, a.Commit())