-
Notifications
You must be signed in to change notification settings - Fork 24
/
sma.go
112 lines (94 loc) · 3.06 KB
/
sma.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package components
import (
"fmt"
"go.uber.org/fx"
policylangv1 "github.com/fluxninja/aperture/v2/api/gen/proto/go/aperture/policy/language/v1"
"github.com/fluxninja/aperture/v2/pkg/config"
"github.com/fluxninja/aperture/v2/pkg/notifiers"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/iface"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/runtime"
)
// SMA is a Simple Moving Average filter.
type SMA struct {
policyReadAPI iface.Policy
sum float64
window int
buffer []float64
lastGoodOutput runtime.Reading
validDuringWarmup bool
invalidCounter int
}
// Make sure SMA complies with Component interface.
var _ runtime.Component = (*SMA)(nil)
// NewSMAAndOptions returns a new SMA filter and its Fx options.
func NewSMAAndOptions(smaProto *policylangv1.SMA, _ runtime.ComponentID, policyReadAPI iface.Policy) (*SMA, fx.Option, error) {
params := smaProto.GetParameters()
sma := &SMA{
policyReadAPI: policyReadAPI,
window: int(params.SmaWindow.AsDuration() / policyReadAPI.GetEvaluationInterval()),
buffer: make([]float64, 0),
lastGoodOutput: runtime.InvalidReading(),
validDuringWarmup: params.ValidDuringWarmup,
invalidCounter: 0,
}
return sma, fx.Options(), nil
}
// Name returns the name of the component.
func (sma *SMA) Name() string {
return "SMA"
}
// Type returns the type of the component.
func (sma *SMA) Type() runtime.ComponentType {
return runtime.ComponentTypeSignalProcessor
}
// ShortDescription returns a short description of the component.
func (sma *SMA) ShortDescription() string {
return fmt.Sprintf("window: %d", sma.window)
}
// IsActuator returns whether this component is a actuator or not.
func (*SMA) IsActuator() bool { return false }
// Execute implements runtime.Component.Execute.
func (sma *SMA) Execute(inPortReadings runtime.PortToReading, tickInfo runtime.TickInfo) (runtime.PortToReading, error) {
input := inPortReadings.ReadSingleReadingPort("input")
var output runtime.Reading
if input.Valid() {
sma.buffer = append(sma.buffer, input.Value())
if len(sma.buffer) > sma.window {
sma.sum -= sma.buffer[0]
sma.buffer = sma.buffer[1:]
}
sma.sum += input.Value()
if len(sma.buffer) == sma.window || sma.validDuringWarmup {
avg := sma.computeAverage()
output = runtime.NewReading(avg)
sma.invalidCounter = 0
} else {
output = runtime.InvalidReading()
}
} else {
sma.invalidCounter++
if sma.invalidCounter >= sma.window {
sma.buffer = []float64{}
sma.sum = 0
sma.invalidCounter = 0
output = runtime.InvalidReading()
} else {
output = sma.lastGoodOutput
}
}
// Set the last good output
if output.Valid() {
sma.lastGoodOutput = output
}
return runtime.PortToReading{
"output": []runtime.Reading{output},
}, nil
}
// DynamicConfigUpdate is a no-op for SMA.
func (sma *SMA) DynamicConfigUpdate(event notifiers.Event, unmarshaller config.Unmarshaller) {}
func (sma *SMA) computeAverage() float64 {
if len(sma.buffer) > 0 {
return sma.sum / float64(len(sma.buffer))
}
return 0
}