deltatracker

package
v0.21.1 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package deltatracker provides a tracker for calculating differences in numeric values over time.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DeltaTracker

type DeltaTracker[K comparable, V Numeric] struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

DeltaTracker is a generic, thread-safe tracker for calculating differences in numeric values over time. K is the key type (e.g., int, string). V is the value type (e.g., int, int64, float32, float64).

Example
tracker := NewDeltaTracker[string, int]()
tracker.Set("key1", 10)
tracker.Set("key2", 20)
tracker.Cycle()
tracker.Set("key1", 15)
tracker.Set("key2", 30)
fmt.Println(tracker.Delta("key1"))
fmt.Println(tracker.Delta("key2"))
fmt.Println(tracker.Deltas())
Output:
5
10
map[key1:5 key2:10]

func NewDeltaTracker

func NewDeltaTracker[K comparable, V Numeric]() *DeltaTracker[K, V]

NewDeltaTracker creates a new generic tracker.

func (*DeltaTracker[K, V]) Cycle

func (t *DeltaTracker[K, V]) Cycle()

Cycle prepares the tracker for the next interval.

func (*DeltaTracker[K, V]) Delta

func (t *DeltaTracker[K, V]) Delta(id K) V

Delta returns the delta for a single key. Returns 0 if the key doesn't exist or has no previous value.

func (*DeltaTracker[K, V]) Deltas

func (t *DeltaTracker[K, V]) Deltas() map[K]V

Deltas returns a map of all calculated deltas for the current interval.

func (*DeltaTracker[K, V]) Previous added in v0.14.0

func (t *DeltaTracker[K, V]) Previous(id K) (V, bool)

Previous returns the previously recorded value for the given key, if it exists.

func (*DeltaTracker[K, V]) Set

func (t *DeltaTracker[K, V]) Set(id K, value V)

Set records the current value for a given ID.

type Numeric

type Numeric interface {
	constraints.Integer | constraints.Float
}

Numeric is a constraint that permits any integer or floating-point type.

Jump to

Keyboard shortcuts

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