avg_sliding_window

package
v0.0.0-...-eb934a0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AdjustableClock

type AdjustableClock struct {
	// contains filtered or unexported fields
}

AdjustableClock provides a static clock to easily override the system time

func NewAdjustableClock

func NewAdjustableClock(now time.Time) *AdjustableClock

func (*AdjustableClock) Now

func (c *AdjustableClock) Now() time.Time

func (*AdjustableClock) Set

func (c *AdjustableClock) Set(now time.Time)

type AvgSlidingWindow

type AvgSlidingWindow struct {
	// contains filtered or unexported fields
}

AvgSlidingWindow calculates moving averages efficiently. Data points are rounded to nearest bucket of size `bucketSize`, and evicted when they are too old based on `windowLength`

func NewSlidingWindow

func NewSlidingWindow(opts ...SlidingWindowOpts) *AvgSlidingWindow

func (*AvgSlidingWindow) Add

func (sw *AvgSlidingWindow) Add(val float64)

Add inserts a new data point into the window, with value `val` and the current time

func (*AvgSlidingWindow) AddWithTime

func (sw *AvgSlidingWindow) AddWithTime(t time.Time, val float64)

AddWithTime inserts a new data point into the window, with value `val` and time `t`

func (*AvgSlidingWindow) Avg

func (sw *AvgSlidingWindow) Avg() float64

Avg retrieves the current average for the sliding window

func (*AvgSlidingWindow) Count

func (sw *AvgSlidingWindow) Count() uint

Count retrieves the data point count for the sliding window

func (*AvgSlidingWindow) Incr

func (sw *AvgSlidingWindow) Incr()

Incr is an alias to insert a data point with value float64(1) and the current time

func (*AvgSlidingWindow) Sum

func (sw *AvgSlidingWindow) Sum() float64

Sum retrieves the current sum for the sliding window

type Clock

type Clock interface {
	Now() time.Time
}

type DefaultClock

type DefaultClock struct{}

DefaultClock provides a clock that gets current time from the system time

func NewDefaultClock

func NewDefaultClock() *DefaultClock

func (DefaultClock) Now

func (c DefaultClock) Now() time.Time

type SlidingWindowOpts

type SlidingWindowOpts func(sw *AvgSlidingWindow)

func WithBucketSize

func WithBucketSize(bucketSize time.Duration) SlidingWindowOpts

func WithClock

func WithClock(clock Clock) SlidingWindowOpts

func WithWindowLength

func WithWindowLength(windowLength time.Duration) SlidingWindowOpts

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL