-
Notifications
You must be signed in to change notification settings - Fork 24
/
integrator.go
71 lines (57 loc) · 2.15 KB
/
integrator.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
package components
import (
"math"
"go.uber.org/fx"
policylangv1 "github.com/fluxninja/aperture/api/gen/proto/go/aperture/policy/language/v1"
"github.com/fluxninja/aperture/pkg/config"
"github.com/fluxninja/aperture/pkg/notifiers"
"github.com/fluxninja/aperture/pkg/policies/controlplane/iface"
"github.com/fluxninja/aperture/pkg/policies/controlplane/runtime"
)
// Integrator is a component that accumulates sum of signal every tick.
type Integrator struct {
sum float64
}
// Name implements runtime.Component.
func (*Integrator) Name() string { return "Integrator" }
// Type implements runtime.Component.
func (*Integrator) Type() runtime.ComponentType { return runtime.ComponentTypeSignalProcessor }
// ShortDescription implements runtime.Component.
func (in *Integrator) ShortDescription() string {
return ""
}
// NewIntegrator creates an integrator component.
func NewIntegrator() runtime.Component {
integrator := &Integrator{
sum: 0,
}
return integrator
}
// NewIntegratorAndOptions creates an integrator component and its fx options.
func NewIntegratorAndOptions(_ *policylangv1.Integrator, _ string, _ iface.Policy) (runtime.Component, fx.Option, error) {
return NewIntegrator(), fx.Options(), nil
}
// Execute implements runtime.Component.Execute.
func (in *Integrator) Execute(inPortReadings runtime.PortToReading, tickInfo runtime.TickInfo) (runtime.PortToReading, error) {
inputVal := inPortReadings.ReadSingleReadingPort("input")
resetVal := inPortReadings.ReadSingleReadingPort("reset")
if resetVal.Valid() && resetVal.Value() != 0 {
in.sum = 0
// reset existing min/max constraints
} else if inputVal.Valid() {
in.sum += inputVal.Value()
maxVal := inPortReadings.ReadSingleReadingPort("max")
if maxVal.Valid() {
in.sum = math.Min(in.sum, maxVal.Value())
}
minVal := inPortReadings.ReadSingleReadingPort("min")
if minVal.Valid() {
in.sum = math.Max(in.sum, minVal.Value())
}
}
return runtime.PortToReading{
"output": []runtime.Reading{runtime.NewReading(in.sum)},
}, nil
}
// DynamicConfigUpdate is a no-op for Integrator.
func (in *Integrator) DynamicConfigUpdate(event notifiers.Event, unmarshaller config.Unmarshaller) {}