Skip to content

Commit a92f7d5

Browse files
committed
Use MetricAggregator interface for time windows
1 parent c2622f0 commit a92f7d5

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

api/interfaces.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ type MetricAggregator interface {
8080

8181
// IsEmpty returns true if no data exists in the window.
8282
IsEmpty(now time.Time) bool
83+
84+
// ResizeWindow resizes the window to the given duration.
85+
ResizeWindow(w time.Duration)
8386
}
8487

8588
// Reporter reports autoscaler metrics for monitoring.

manager/scaler.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,13 @@ import (
2525
"github.com/Fedosin/libkpa/metrics"
2626
)
2727

28-
// timeWindowInterface provides a common interface for TimeWindow operations
29-
type timeWindowInterface interface {
30-
Record(now time.Time, value float64)
31-
WindowAverage(now time.Time) float64
32-
IsEmpty(now time.Time) bool
33-
ResizeWindow(w time.Duration)
34-
}
35-
3628
// Scaler represents a single autoscaler instance that combines metric aggregation
3729
// with a sliding window autoscaling algorithm.
3830
type Scaler struct {
3931
name string
4032
algorithm *algorithm.SlidingWindowAutoscaler
41-
stableAggregator timeWindowInterface
42-
panicAggregator timeWindowInterface
33+
stableAggregator api.MetricAggregator
34+
panicAggregator api.MetricAggregator
4335
}
4436

4537
// NewScaler creates a new Scaler instance with the specified configuration.
@@ -68,7 +60,7 @@ func NewScaler(
6860
granularity := time.Second
6961

7062
// Create the appropriate metric aggregators based on algoType
71-
var stableAgg, panicAgg timeWindowInterface
63+
var stableAgg, panicAgg api.MetricAggregator
7264
switch algoType {
7365
case "linear":
7466
stableAgg, err = metrics.NewTimeWindow(cfg.StableWindow, granularity)

metrics/time_window.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"math"
2323
"sync"
2424
"time"
25+
26+
"github.com/Fedosin/libkpa/api"
2527
)
2628

2729
const (
@@ -64,6 +66,8 @@ type TimeWindow struct {
6466
windowTotal float64
6567
}
6668

69+
var _ api.MetricAggregator = (*TimeWindow)(nil)
70+
6771
// String implements the Stringer interface.
6872
func (t *TimeWindow) String() string {
6973
return fmt.Sprintf("%v", t.buckets)

metrics/weighted_time_window.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package metrics
1919
import (
2020
"math"
2121
"time"
22+
23+
"github.com/Fedosin/libkpa/api"
2224
)
2325

2426
// WeightedTimeWindow is the implementation of buckets, that
@@ -33,6 +35,8 @@ type WeightedTimeWindow struct {
3335
smoothingCoeff float64
3436
}
3537

38+
var _ api.MetricAggregator = (*WeightedTimeWindow)(nil)
39+
3640
// NewWeightedTimeWindow generates a new WeightedTimeWindow with the given
3741
// granularity.
3842
func NewWeightedTimeWindow(window, granularity time.Duration) (*WeightedTimeWindow, error) {

0 commit comments

Comments
 (0)