Skip to content

Commit

Permalink
Merge pull request #12 from arazmj/master
Browse files Browse the repository at this point in the history
Added AppendWithTimestamp
  • Loading branch information
ensary authored Nov 16, 2021
2 parents fe8c9d1 + bf24726 commit 150447a
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,23 @@ func (w *TimePolicy) selectBucket(currentTime time.Time) (int64, int) {
return adjustedTime, windowOffset
}

// Append a value to the window using a time bucketing strategy.
func (w *TimePolicy) Append(value float64) {
// AppendWithTimestamp same as Append but with timestamp as parameter
func (w *TimePolicy) AppendWithTimestamp(value float64, timestamp time.Time) {
w.lock.Lock()
defer w.lock.Unlock()

var adjustedTime, windowOffset = w.selectBucket(time.Now())
var adjustedTime, windowOffset = w.selectBucket(timestamp)
w.keepConsistent(adjustedTime, windowOffset)
w.window[windowOffset] = append(w.window[windowOffset], value)
w.lastWindowTime = adjustedTime
w.lastWindowOffset = windowOffset
}

// Append a value to the window using a time bucketing strategy.
func (w *TimePolicy) Append(value float64) {
w.AppendWithTimestamp(value, time.Now())
}

// Reduce the window to a single value using a reduction function.
func (w *TimePolicy) Reduce(f func(Window) float64) float64 {
w.lock.Lock()
Expand Down

0 comments on commit 150447a

Please sign in to comment.