-
Notifications
You must be signed in to change notification settings - Fork 24
/
min.go
64 lines (51 loc) · 1.89 KB
/
min.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
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"
)
// Min takes array of signals and emits minimum value.
type Min struct{}
// Name implements runtime.Component.
func (*Min) Name() string { return "Min" }
// Type implements runtime.Component.
func (*Min) Type() runtime.ComponentType { return runtime.ComponentTypeSignalProcessor }
// ShortDescription implements runtime.Component.
func (*Min) ShortDescription() string { return "" }
// Make sure Min complies with Component interface.
var _ runtime.Component = (*Min)(nil)
// NewMinAndOptions creates a new Min Component.
func NewMinAndOptions(_ *policylangv1.Min, _ string, _ iface.Policy) (runtime.Component, fx.Option, error) {
min := Min{}
return &min, fx.Options(), nil
}
// Execute implements runtime.Component.Execute.
func (min *Min) Execute(inPortReadings runtime.PortToReading, tickInfo runtime.TickInfo) (runtime.PortToReading, error) {
minValue := 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() < minValue {
minValue = singleInput.Value()
}
}
output = runtime.NewReading(minValue)
} else {
output = runtime.InvalidReading()
}
return runtime.PortToReading{
"output": []runtime.Reading{output},
}, nil
}
// DynamicConfigUpdate is a no-op for Min.
func (min *Min) DynamicConfigUpdate(event notifiers.Event, unmarshaller config.Unmarshaller) {}