-
Notifications
You must be signed in to change notification settings - Fork 25
/
extrapolator.go
82 lines (67 loc) · 2.95 KB
/
extrapolator.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
package components
import (
"fmt"
"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"
)
// Extrapolator takes an input signal and emits an output signal.
type Extrapolator struct {
// The last valid timestamp.
lastValidTimestamp time.Time
// The last output that was emitted as an output signal.
lastOutput runtime.Reading
// Maximum time interval for each extrapolation of signal is done; Reading becomes invalid after this interval.
maxExtrapolationInterval time.Duration
}
// Name implements runtime.Component.
func (*Extrapolator) Name() string { return "Extrapolator" }
// Type implements runtime.Component.
func (*Extrapolator) Type() runtime.ComponentType { return runtime.ComponentTypeSignalProcessor }
// ShortDescription implements runtime.Component.
func (exp *Extrapolator) ShortDescription() string {
return fmt.Sprintf("for: %s", exp.maxExtrapolationInterval)
}
// IsActuator implements runtime.Component.
func (*Extrapolator) IsActuator() bool { return false }
// Make sure Extrapolator complies with Component interface.
var _ runtime.Component = (*Extrapolator)(nil)
// NewExtrapolatorAndOptions creates a new Extrapolator Component.
func NewExtrapolatorAndOptions(extrapolatorProto *policylangv1.Extrapolator, _ runtime.ComponentID, _ iface.Policy) (runtime.Component, fx.Option, error) {
exp := Extrapolator{
maxExtrapolationInterval: extrapolatorProto.Parameters.MaxExtrapolationInterval.AsDuration(),
lastOutput: runtime.InvalidReading(),
lastValidTimestamp: time.Time{},
}
return &exp, fx.Options(), nil
}
// Execute implements runtime.Component.Execute.
func (exp *Extrapolator) Execute(inPortReadings runtime.PortToReading, tickInfo runtime.TickInfo) (runtime.PortToReading, error) {
input := inPortReadings.ReadSingleReadingPort("input")
output := runtime.InvalidReading()
if input.Valid() {
output = input
exp.lastOutput = output
exp.lastValidTimestamp = tickInfo.Timestamp()
} else {
// Check if elapsed since lastValidTimestamp has reached the maximum Extrapolation interval.
if tickInfo.Timestamp().Sub(exp.lastValidTimestamp) <= exp.maxExtrapolationInterval {
// If the signal is invalid, it repeats the last value for up to maxExtrapolationInterval.
output = exp.lastOutput
} else {
// When maxExtrapolationInterval is reached, reading becomes invalid.
exp.lastOutput = output
}
}
// If the signal returns, it resumes mirroring the input signal as output signal.
return runtime.PortToReading{
"output": []runtime.Reading{output},
}, nil
}
// DynamicConfigUpdate is a no-op for Extrapolator.
func (exp *Extrapolator) DynamicConfigUpdate(event notifiers.Event, unmarshaller config.Unmarshaller) {
}