-
Notifications
You must be signed in to change notification settings - Fork 25
/
signal-generator.go
168 lines (144 loc) · 5.03 KB
/
signal-generator.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package components
import (
"fmt"
"math"
"time"
"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"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/runtime/tristate"
)
// SignalGenerator generates a signal based on the steps specified.
type SignalGenerator struct {
steps []*policylangv1.SignalGenerator_Parameters_Step
currentStep int
tickCount int32
atStart bool
atEnd bool
evaluationPeriod time.Duration
}
// Name implements runtime.Component.
func (*SignalGenerator) Name() string { return "SignalGenerator" }
// Type implements runtime.Component.
func (*SignalGenerator) Type() runtime.ComponentType { return runtime.ComponentTypeSource }
// ShortDescription implements runtime.Component.
func (sg *SignalGenerator) ShortDescription() string {
return fmt.Sprintf("SignalGenerator with %d steps", len(sg.steps))
}
// IsActuator implements runtime.Component.
func (*SignalGenerator) IsActuator() bool { return false }
// NewSignalGeneratorAndOptions creates a signal generator component and its fx options.
func NewSignalGeneratorAndOptions(generatorProto *policylangv1.SignalGenerator, _ runtime.ComponentID, policyReadAPI iface.Policy) (runtime.Component, fx.Option, error) {
evaluationPeriod := policyReadAPI.GetEvaluationInterval()
signalGenerator := &SignalGenerator{
steps: generatorProto.Parameters.Steps,
currentStep: 0,
tickCount: 0,
atStart: true,
atEnd: false,
evaluationPeriod: evaluationPeriod,
}
return signalGenerator, fx.Options(), nil
}
// Execute implements runtime.Component.Execute.
func (sg *SignalGenerator) Execute(inPortReadings runtime.PortToReading, tickInfo runtime.TickInfo) (runtime.PortToReading, error) {
resetVal := inPortReadings.ReadSingleReadingPort("reset")
forwardVal := inPortReadings.ReadSingleReadingPort("forward")
backwardVal := inPortReadings.ReadSingleReadingPort("backward")
direction := 0
forward := tristate.FromReading(forwardVal).IsTrue()
backward := tristate.FromReading(backwardVal).IsTrue()
if (forward && backward) ||
(!forward && !backward) {
direction = 0
} else if forward {
direction = 1
} else if backward {
direction = -1
}
if tristate.FromReading(resetVal).IsTrue() {
sg.currentStep = 0
sg.tickCount = 0
sg.atStart = true
sg.atEnd = false
} else if direction == 1 {
if sg.atStart {
sg.atStart = false
}
if !sg.atEnd {
sg.tickCount++
// Process tick updates currentStep and atEnd if necessary
sg.processForwardTick()
}
} else if direction == -1 {
if sg.atEnd {
sg.atEnd = false
}
if !sg.atStart {
sg.tickCount--
// Process tick updates currentStep and atStart if necessary
sg.processBackwardTick()
}
}
currentSignal := runtime.ConstantSignalFromProto(sg.steps[sg.currentStep].TargetOutput)
currentValue := currentSignal.Float()
if isInterpolable(currentSignal) {
if sg.currentStep > 0 {
tickFraction := float64(sg.tickCount) / float64(sg.getStepDuration())
previousSignal := runtime.ConstantSignalFromProto(sg.steps[sg.currentStep-1].TargetOutput)
if isInterpolable(previousSignal) {
previousValue := previousSignal.Float()
// If the previous and current values are not equal, interpolate between them. Avoid interpolation if the values are equal to prevent floating point errors.
if currentValue != previousValue {
// Interpolate between previous and current value
currentValue = previousValue + (currentValue-previousValue)*tickFraction
}
}
}
}
return runtime.PortToReading{
"output": []runtime.Reading{runtime.NewReading(currentValue)},
"at_start": []runtime.Reading{runtime.NewBoolReading(sg.atStart)},
"at_end": []runtime.Reading{runtime.NewBoolReading(sg.atEnd)},
}, nil
}
func isInterpolable(constantSignal *runtime.ConstantSignal) bool {
return !constantSignal.IsSpecial()
}
func (sg *SignalGenerator) processForwardTick() {
if sg.tickCount == sg.getStepDuration() {
sg.currentStep++
if sg.currentStep >= len(sg.steps) {
sg.currentStep = len(sg.steps) - 1
sg.atEnd = true
sg.tickCount = sg.getStepDuration() - 1
} else {
sg.tickCount = 0
}
}
}
func (sg *SignalGenerator) processBackwardTick() {
if sg.tickCount == -1 {
sg.currentStep--
if sg.currentStep < 0 {
sg.currentStep = 0
sg.atStart = true
sg.tickCount = 0
} else {
sg.tickCount = sg.getStepDuration() - 1
}
}
}
func (sg *SignalGenerator) getStepDuration() int32 {
stepDuration := sg.steps[sg.currentStep].Duration.AsDuration()
if stepDuration == 0 {
return 1
}
return int32(math.Ceil(float64(stepDuration) / float64(sg.evaluationPeriod)))
}
// DynamicConfigUpdate is a no-op for SignalGenerator.
func (*SignalGenerator) DynamicConfigUpdate(event notifiers.Event, unmarshaller config.Unmarshaller) {
}