From 59ce70df4be604947cb408c7622b63ce3bca1da4 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Thu, 23 Sep 2021 14:03:16 +0300 Subject: [PATCH] Alignment: fix interpolation --- pkg/timeseries/align.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/pkg/timeseries/align.go b/pkg/timeseries/align.go index 5a58ac34c..898071544 100644 --- a/pkg/timeseries/align.go +++ b/pkg/timeseries/align.go @@ -22,17 +22,19 @@ func (ts TimeSeries) Align(interval time.Duration) TimeSeries { pointFrameTs = point.GetTimeFrame(interval) if pointFrameTs.After(frameTs) { - pointsToAdd := make([]TimePoint, 0) + nullPointsToAdd := make([]TimePoint, 0) for frameTs.Before(pointFrameTs) { - pointsToAdd = append(pointsToAdd, TimePoint{Time: frameTs, Value: nil}) + nullPointsToAdd = append(nullPointsToAdd, TimePoint{Time: frameTs, Value: nil}) frameTs = frameTs.Add(interval) } - if len(pointsToAdd) > 1 { - alignedTs = append(alignedTs, pointsToAdd...) - } else if len(pointsToAdd) == 1 && i < ts.Len()-1 { + if len(nullPointsToAdd) > 1 { + alignedTs = append(alignedTs, nullPointsToAdd...) + } else if len(nullPointsToAdd) == 1 && i < ts.Len()-1 { // In case of 1 point gap, insert interpolated value to prevent unnecessary gaps - interpolatedPoint := pointsToAdd[0] - pointValue := linearInterpolation(interpolatedPoint.Time, ts[i], ts[i+1]) + interpolatedPoint := nullPointsToAdd[0] + left := alignedTs[len(alignedTs)-1] + right := TimePoint{Time: pointFrameTs, Value: point.Value} + pointValue := linearInterpolation(interpolatedPoint.Time, left, right) interpolatedPoint.Value = &pointValue alignedTs = append(alignedTs, interpolatedPoint) }