Skip to content

Commit

Permalink
Merge pull request #14 from bigbluedisco/master
Browse files Browse the repository at this point in the history
Fix TimeWindow not resetting buckets when re-using them.
  • Loading branch information
ensary authored Nov 16, 2021
2 parents 150447a + 975d3d0 commit 1a3fec5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion time.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ func (w *TimePolicy) AppendWithTimestamp(value float64, timestamp time.Time) {

var adjustedTime, windowOffset = w.selectBucket(timestamp)
w.keepConsistent(adjustedTime, windowOffset)
w.window[windowOffset] = append(w.window[windowOffset], value)
if w.lastWindowOffset != windowOffset {
w.window[windowOffset] = []float64{value}
} else {
w.window[windowOffset] = append(w.window[windowOffset], value)
}
w.lastWindowTime = adjustedTime
w.lastWindowOffset = windowOffset
}
Expand Down
18 changes: 18 additions & 0 deletions time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ func TestTimeWindow(t *testing.T) {
if final != float64(numberBuckets) {
t.Fatal(final)
}

for x := 0; x < numberBuckets; x = x + 1 {
p.Append(2)
time.Sleep(bucketSize)
}

final = p.Reduce(func(w Window) float64 {
var result float64
for _, bucket := range w {
for _, point := range bucket {
result = result + point
}
}
return result
})
if final != 2*float64(numberBuckets) {
t.Fatal("got", final, "expected", 2*float64(numberBuckets))
}
}

func TestTimeWindowSelectBucket(t *testing.T) {
Expand Down

0 comments on commit 1a3fec5

Please sign in to comment.