-
Notifications
You must be signed in to change notification settings - Fork 25
/
max.go
67 lines (53 loc) · 2 KB
/
max.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
package components
import (
"math"
"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"
)
// Max takes array of signals and emits maximum value.
type Max struct{}
// Name implements runtime.Component.
func (*Max) Name() string { return "Max" }
// Type implements runtime.Component.
func (*Max) Type() runtime.ComponentType { return runtime.ComponentTypeSignalProcessor }
// ShortDescription implements runtime.Component.
func (*Max) ShortDescription() string { return "" }
// IsActuator implements runtime.Component.
func (*Max) IsActuator() bool { return false }
// Make sure Max complies with Component interface.
var _ runtime.Component = (*Max)(nil)
// NewMaxAndOptions creates a new Max Component.
func NewMaxAndOptions(_ *policylangv1.Max, _ runtime.ComponentID, _ iface.Policy) (runtime.Component, fx.Option, error) {
max := Max{}
return &max, fx.Options(), nil
}
// Execute implements runtime.Component.Execute.
func (max *Max) Execute(inPortReadings runtime.PortToReading, tickInfo runtime.TickInfo) (runtime.PortToReading, error) {
maxValue := -math.MaxFloat64
inputs := inPortReadings.ReadRepeatedReadingPort("inputs")
output := runtime.InvalidReading()
if len(inputs) > 0 {
for _, singleInput := range inputs {
if !singleInput.Valid() {
return runtime.PortToReading{
"output": []runtime.Reading{output},
}, nil
}
if singleInput.Value() > maxValue {
maxValue = singleInput.Value()
}
}
output = runtime.NewReading(maxValue)
} else {
output = runtime.InvalidReading()
}
return runtime.PortToReading{
"output": []runtime.Reading{output},
}, nil
}
// DynamicConfigUpdate is a no-op for Max.
func (max *Max) DynamicConfigUpdate(event notifiers.Event, unmarshaller config.Unmarshaller) {}