-
Notifications
You must be signed in to change notification settings - Fork 25
/
reading.go
76 lines (65 loc) · 2.02 KB
/
reading.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
package sim
import (
"math"
rt "github.com/fluxninja/aperture/v2/pkg/policies/controlplane/runtime"
)
// Reading is implementation of rt.Reading designed for tests.
//
// It avoids the problem of invalid readings being non-equal due to being
// represented as NaNs, so that comparing invalid readings works as expected.
type Reading struct {
value float64 // never NaN
invalid bool // value=0 if invalid=true
}
// Valid implements runtime.Reading.
func (r Reading) Valid() bool { return !r.invalid }
// Value implements runtime.Reading.
func (r Reading) Value() float64 { return r.value }
// NewReading creates a Reading from a float. NaN is treated as invalid reading.
func NewReading(value float64) Reading {
if math.IsNaN(value) {
return Reading{invalid: true}
} else {
return Reading{value: value}
}
}
// InvalidReading creates a new invalid Reading.
func InvalidReading() Reading {
return Reading{invalid: true}
}
// NewReadings creates a slice of readings from a slice of floats.
func NewReadings(values []float64) []Reading {
readings := make([]Reading, 0, len(values))
for _, value := range values {
if math.IsNaN(value) {
readings = append(readings, Reading{invalid: true})
} else {
readings = append(readings, Reading{value: value})
}
}
return readings
}
// ReadingFromRt converts runtime.Reading to Reading.
func ReadingFromRt(rtReading rt.Reading) Reading {
if rtReading.Valid() {
return Reading{value: rtReading.Value()}
} else {
return Reading{invalid: true}
}
}
// ReadingsFromRt converts runtime.Readings to Readings.
func ReadingsFromRt(rtReadings []rt.Reading) []Reading {
readings := make([]Reading, 0, len(rtReadings))
for _, rtReading := range rtReadings {
readings = append(readings, ReadingFromRt(rtReading))
}
return readings
}
// ToRtReadings converts Readings to rt.Readings.
func ToRtReadings(readings []Reading) []rt.Reading {
rtReadings := make([]rt.Reading, 0, len(readings))
for _, reading := range readings {
rtReadings = append(rtReadings, reading)
}
return rtReadings
}