-
Notifications
You must be signed in to change notification settings - Fork 880
/
evaluate.go
269 lines (236 loc) · 5.71 KB
/
evaluate.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package evaluate
import (
"errors"
"fmt"
"math"
"reflect"
"strconv"
"time"
"github.com/antonmedv/expr"
"github.com/antonmedv/expr/file"
"github.com/sirupsen/logrus"
"github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
)
func EvaluateResult(result any, metric v1alpha1.Metric, logCtx logrus.Entry) (v1alpha1.AnalysisPhase, error) {
successCondition := false
failCondition := false
var err error
if metric.SuccessCondition != "" {
successCondition, err = EvalCondition(result, metric.SuccessCondition)
if err != nil {
return v1alpha1.AnalysisPhaseError, err
}
}
if metric.FailureCondition != "" {
failCondition, err = EvalCondition(result, metric.FailureCondition)
if err != nil {
return v1alpha1.AnalysisPhaseError, err
}
}
switch {
case metric.SuccessCondition == "" && metric.FailureCondition == "":
//Always return success unless there is an error
return v1alpha1.AnalysisPhaseSuccessful, nil
case metric.SuccessCondition != "" && metric.FailureCondition == "":
// Without a failure condition, a measurement is considered a failure if the measurement's success condition is not true
failCondition = !successCondition
case metric.SuccessCondition == "" && metric.FailureCondition != "":
// Without a success condition, a measurement is considered a successful if the measurement's failure condition is not true
successCondition = !failCondition
}
if failCondition {
return v1alpha1.AnalysisPhaseFailed, nil
}
if !failCondition && !successCondition {
return v1alpha1.AnalysisPhaseInconclusive, nil
}
// If we reach this code path, failCondition is false and successCondition is true
return v1alpha1.AnalysisPhaseSuccessful, nil
}
func EvalTime(expression string) (time.Time, error) {
var err error
env := map[string]any{
"isNaN": math.IsNaN,
"isInf": isInf,
}
unwrapFileErr := func(e error) error {
if fileErr, ok := err.(*file.Error); ok {
e = errors.New(fileErr.Message)
}
return e
}
program, err := expr.Compile(expression, expr.Env(env))
if err != nil {
return time.Time{}, unwrapFileErr(err)
}
output, err := expr.Run(program, env)
if err != nil {
return time.Time{}, unwrapFileErr(err)
}
switch val := output.(type) {
case time.Time:
return val, nil
default:
return time.Time{}, fmt.Errorf("expected time.Time, but got %T", val)
}
}
// EvalCondition evaluates the condition with the resultValue as an input
func EvalCondition(resultValue any, condition string) (bool, error) {
var err error
env := map[string]any{
"result": valueFromPointer(resultValue),
"asInt": asInt,
"asFloat": asFloat,
"isNaN": math.IsNaN,
"isInf": isInf,
"isNil": isNilFunc(resultValue),
"default": defaultFunc(resultValue),
}
unwrapFileErr := func(e error) error {
if fileErr, ok := err.(*file.Error); ok {
e = errors.New(fileErr.Message)
}
return e
}
program, err := expr.Compile(condition, expr.Env(env))
if err != nil {
return false, unwrapFileErr(err)
}
output, err := expr.Run(program, env)
if err != nil {
return false, unwrapFileErr(err)
}
switch val := output.(type) {
case bool:
return val, nil
default:
return false, fmt.Errorf("expected bool, but got %T", val)
}
}
func isInf(f float64) bool {
return math.IsInf(f, 0)
}
func asInt(in any) int64 {
switch i := in.(type) {
case float64:
return int64(i)
case float32:
return int64(i)
case int64:
return i
case int32:
return int64(i)
case int16:
return int64(i)
case int8:
return int64(i)
case int:
return int64(i)
case uint64:
return int64(i)
case uint32:
return int64(i)
case uint16:
return int64(i)
case uint8:
return int64(i)
case uint:
return int64(i)
case string:
inAsInt, err := strconv.ParseInt(i, 10, 64)
if err == nil {
return inAsInt
}
panic(err)
}
panic(fmt.Sprintf("asInt() not supported on %v %v", reflect.TypeOf(in), in))
}
func asFloat(in any) float64 {
switch i := in.(type) {
case float64:
return i
case float32:
return float64(i)
case int64:
return float64(i)
case int32:
return float64(i)
case int16:
return float64(i)
case int8:
return float64(i)
case int:
return float64(i)
case uint64:
return float64(i)
case uint32:
return float64(i)
case uint16:
return float64(i)
case uint8:
return float64(i)
case uint:
return float64(i)
case string:
inAsFloat, err := strconv.ParseFloat(i, 64)
if err == nil {
return inAsFloat
}
panic(err)
}
panic(fmt.Sprintf("asFloat() not supported on %v %v", reflect.TypeOf(in), in))
}
// Check whether two slices of type string are equal or not.
func Equal(a, b []string) bool {
if len(a) != len(b) {
return false
}
left := make(map[string]bool)
for _, x := range a {
left[x] = true
}
for _, x := range b {
if !left[x] {
return false
}
}
return true
}
func defaultFunc(resultValue any) func(any, any) any {
return func(_ any, defaultValue any) any {
if isNil(resultValue) {
return defaultValue
}
return valueFromPointer(resultValue)
}
}
func isNilFunc(resultValue any) func(any) bool {
return func(_ any) bool {
return isNil(resultValue)
}
}
// isNil is courtesy of: https://gist.github.com/mangatmodi/06946f937cbff24788fa1d9f94b6b138
func isNil(in any) (out bool) {
if in == nil {
out = true
return
}
switch reflect.TypeOf(in).Kind() {
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
out = reflect.ValueOf(in).IsNil()
}
return
}
// valueFromPointer allows pointers to be passed in from the provider, but then extracts the value from
// the pointer if the pointer is not nil, else returns nil
func valueFromPointer(in any) (out any) {
if isNil(in) {
return
}
if reflect.TypeOf(in).Kind() != reflect.Ptr {
out = in
return
}
out = reflect.ValueOf(in).Elem().Interface()
return
}