Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more comment and code snippet for slot aggregator #57

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions metrics/percentage_time_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,18 @@ func GetOrRegisterTimeWindowPercentage(
type percentageDataAggregator struct{}

// Add implements the SlotAggregator[percentageData] interface.
func (percentageDataAggregator) Add(x, y percentageData) percentageData {
func (percentageDataAggregator) Add(acc, v percentageData) percentageData {
return percentageData{
total: x.total + y.total,
marks: x.marks + y.marks,
total: acc.total + v.total,
marks: acc.marks + v.marks,
}
}

// Sub implements the SlotAggregator[percentageData] interface.
func (percentageDataAggregator) Sub(x, y percentageData) percentageData {
func (percentageDataAggregator) Sub(acc, v percentageData) percentageData {
return percentageData{
total: x.total - y.total,
marks: x.marks - y.marks,
total: acc.total - v.total,
marks: acc.marks - v.marks,
}
}

Expand Down
44 changes: 34 additions & 10 deletions metrics/time_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,37 @@ import (
"time"
)

// SlotAggregator defines operators for slot data.
// SlotAggregator defines operators for slot data aggregation.
type SlotAggregator[T any] interface {
// Add returns the sum x+y. Note, x may be nil if T is pointer type.
Add(x, y T) T

// Sub returns the difference x-y.
Sub(x, y T) T
// Add returns the sum acc+v. The init value of acc is the default value of type T.
// See examples below:
//
// type FooData struct { x, y int }
//
// func (FooDataAggregator) Add(acc, v FooData) FooData {
// return FooData {
// x: acc.x + v.x,
// y: acc.y + v.y,
// }
// }
//
// Note, the acc may nil if T is pointer type, e.g. map[string]int, and requires to initialize:
//
// func (MapAggregator) Add(acc, v map[string]int) map[string]int {
// if (acc == nil) {
// acc = make(map[string]int)
// }
//
// for name, count := range v {
// acc[name] += count
// }
//
// return acc
// }
Add(acc, v T) T

// Sub returns the difference acc-v, please refer to examples of Add method.
Sub(acc, v T) T
}

type SimpleSlotData interface {
Expand All @@ -22,13 +46,13 @@ type SimpleSlotData interface {
type simpleSlotAggregator[T SimpleSlotData] struct{}

// Add implements the SlotAggregator[T] interface.
func (simpleSlotAggregator[T]) Add(x, y T) T {
return x + y
func (simpleSlotAggregator[T]) Add(acc, v T) T {
return acc + v
}

// Sub implements the SlotAggregator[T] interface.
func (simpleSlotAggregator[T]) Sub(x, y T) T {
return x - y
func (simpleSlotAggregator[T]) Sub(acc, v T) T {
return acc - v
}

// time window slot
Expand Down
Loading