benchdog 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +67 -0
- data/lib/benchdog/admin_api/audit_routes.rb +94 -0
- data/lib/benchdog/admin_api/flag_routes.rb +139 -0
- data/lib/benchdog/admin_api/meta_routes.rb +159 -0
- data/lib/benchdog/admin_api/request.rb +41 -0
- data/lib/benchdog/admin_api.rb +208 -0
- data/lib/benchdog/approvals.rb +60 -0
- data/lib/benchdog/audit.rb +76 -0
- data/lib/benchdog/bucket.rb +11 -0
- data/lib/benchdog/client.rb +36 -0
- data/lib/benchdog/evaluator.rb +194 -0
- data/lib/benchdog/expression.rb +122 -0
- data/lib/benchdog/flag.schema.json +419 -0
- data/lib/benchdog/payload.rb +18 -0
- data/lib/benchdog/schedule.rb +57 -0
- data/lib/benchdog/spec/store_examples.rb +121 -0
- data/lib/benchdog/store.rb +137 -0
- data/lib/benchdog/usage.rb +39 -0
- data/lib/benchdog/validation.rb +27 -0
- data/lib/benchdog/values.rb +49 -0
- data/lib/benchdog/version.rb +5 -0
- data/lib/benchdog.rb +23 -0
- data/sig/admin_api.rbs +202 -0
- data/sig/benchdog.rbs +127 -0
- data/sig/json_schemer.rbs +10 -0
- metadata +86 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "time"
|
|
5
|
+
|
|
6
|
+
module Benchdog
|
|
7
|
+
# Evaluates Flipper expressions (SPEC section 6).
|
|
8
|
+
module Expression
|
|
9
|
+
TimeValue = Values::TimeValue
|
|
10
|
+
|
|
11
|
+
# What an expression can read.
|
|
12
|
+
class Scope
|
|
13
|
+
attr_reader :flag_key, :actor, :now, :segments
|
|
14
|
+
|
|
15
|
+
def initialize(flag_key, actor, now, segments)
|
|
16
|
+
@flag_key = flag_key
|
|
17
|
+
@actor = actor
|
|
18
|
+
@now = now
|
|
19
|
+
@segments = segments
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
TRUTHY_BOOLEANS = [true, 1, "true", "1"].freeze
|
|
24
|
+
|
|
25
|
+
module_function
|
|
26
|
+
|
|
27
|
+
def truthy?(value)
|
|
28
|
+
!value.nil? && value != false
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def evaluate(expression, scope)
|
|
32
|
+
run(expression, scope, nil.to_a)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def run(expression, scope, visiting)
|
|
36
|
+
return expression unless expression.is_a?(Hash)
|
|
37
|
+
return nil unless expression.size == 1
|
|
38
|
+
|
|
39
|
+
name, raw = expression.first
|
|
40
|
+
args = (raw.is_a?(Array) ? raw : [raw]).map { |arg| run(arg, scope, visiting) }
|
|
41
|
+
call(name, args, scope, visiting)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def call(name, args, scope, visiting)
|
|
45
|
+
arity, function = FUNCTIONS[name]
|
|
46
|
+
return nil unless function
|
|
47
|
+
return nil unless arity.nil? || arity == args.size
|
|
48
|
+
|
|
49
|
+
function.call(args, scope, visiting)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def number?(value)
|
|
53
|
+
value.is_a?(Numeric)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def equal?(left, right)
|
|
57
|
+
return time_equal?(left, right) if left.is_a?(TimeValue) || right.is_a?(TimeValue)
|
|
58
|
+
return left == right if number?(left) && number?(right)
|
|
59
|
+
return false if left.is_a?(Hash) || left.is_a?(Array)
|
|
60
|
+
|
|
61
|
+
left.eql?(right)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def time_equal?(left, right)
|
|
65
|
+
left.is_a?(TimeValue) && right.is_a?(TimeValue) && left.seconds == right.seconds
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# The order of two numbers, two strings (by code point) or two times, else nil.
|
|
69
|
+
def compare(left, right)
|
|
70
|
+
if number?(left) && number?(right) then left <=> right
|
|
71
|
+
elsif left.is_a?(String) && right.is_a?(String) then left.codepoints <=> right.codepoints
|
|
72
|
+
elsif left.is_a?(TimeValue) && right.is_a?(TimeValue) then left.seconds <=> right.seconds
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def ordered(args)
|
|
77
|
+
diff = compare(args[0], args[1])
|
|
78
|
+
!diff.nil? && yield(diff)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def property(scope, name)
|
|
82
|
+
actor = scope.actor
|
|
83
|
+
return nil unless actor
|
|
84
|
+
return actor["id"] if name == "flipper_id"
|
|
85
|
+
|
|
86
|
+
actor["attributes"].to_h[name]
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def in_percentage?(scope, text, pct)
|
|
90
|
+
return false unless number?(pct)
|
|
91
|
+
|
|
92
|
+
Benchdog.bucket("#{scope.flag_key}#{Values.string_of(text)}") < pct * 1000
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def segment(scope, name, visiting)
|
|
96
|
+
expression = scope.segments[name]
|
|
97
|
+
return nil if expression.nil? || visiting.include?(name)
|
|
98
|
+
|
|
99
|
+
run(expression, scope, visiting + [name])
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# Each function: [arity, or nil for any; ->(args, scope, visiting)].
|
|
103
|
+
FUNCTIONS = {
|
|
104
|
+
"All" => [nil, ->(args, _, _) { args.all? { |a| truthy?(a) } }],
|
|
105
|
+
"Any" => [nil, ->(args, _, _) { args.any? { |a| truthy?(a) } }],
|
|
106
|
+
"Equal" => [2, ->(args, _, _) { equal?(args[0], args[1]) }],
|
|
107
|
+
"NotEqual" => [2, ->(args, _, _) { !equal?(args[0], args[1]) }],
|
|
108
|
+
"GreaterThan" => [2, ->(args, _, _) { ordered(args, &:positive?) }],
|
|
109
|
+
"GreaterThanOrEqualTo" => [2, ->(args, _, _) { ordered(args) { |d| d >= 0 } }],
|
|
110
|
+
"LessThan" => [2, ->(args, _, _) { ordered(args, &:negative?) }],
|
|
111
|
+
"LessThanOrEqualTo" => [2, ->(args, _, _) { ordered(args) { |d| d <= 0 } }],
|
|
112
|
+
"Boolean" => [1, ->(args, _, _) { TRUTHY_BOOLEANS.any? { |t| t.eql?(args[0]) } }],
|
|
113
|
+
"Number" => [1, ->(args, _, _) { Values.to_number(args[0]) }],
|
|
114
|
+
"String" => [1, ->(args, _, _) { Values.string_of(args[0]) }],
|
|
115
|
+
"Time" => [1, ->(args, _, _) { Values.to_time(args[0]) }],
|
|
116
|
+
"Now" => [0, ->(_, scope, _) { TimeValue.new(scope.now) }],
|
|
117
|
+
"Property" => [1, ->(args, scope, _) { property(scope, Values.string_of(args[0])) }],
|
|
118
|
+
"PercentageOfActors" => [2, ->(args, scope, _) { in_percentage?(scope, args[0], args[1]) }],
|
|
119
|
+
"Segment" => [1, ->(args, scope, visiting) { segment(scope, Values.string_of(args[0]), visiting) }]
|
|
120
|
+
}.freeze
|
|
121
|
+
end
|
|
122
|
+
end
|
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id": "https://joegaudet.com/benchdog/schema/flag.schema.json",
|
|
4
|
+
"title": "Benchdog flag",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"required": ["key"],
|
|
7
|
+
"additionalProperties": false,
|
|
8
|
+
"properties": {
|
|
9
|
+
"key": {
|
|
10
|
+
"$ref": "#/$defs/flagKey"
|
|
11
|
+
},
|
|
12
|
+
"type": {
|
|
13
|
+
"enum": ["boolean", "string", "number", "object"]
|
|
14
|
+
},
|
|
15
|
+
"variations": {
|
|
16
|
+
"type": "array",
|
|
17
|
+
"minItems": 1,
|
|
18
|
+
"items": {
|
|
19
|
+
"type": "object",
|
|
20
|
+
"required": ["key", "value"],
|
|
21
|
+
"additionalProperties": false,
|
|
22
|
+
"properties": {
|
|
23
|
+
"key": {
|
|
24
|
+
"type": "string",
|
|
25
|
+
"minLength": 1
|
|
26
|
+
},
|
|
27
|
+
"value": true
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"offVariation": {
|
|
32
|
+
"type": "string"
|
|
33
|
+
},
|
|
34
|
+
"onVariation": {
|
|
35
|
+
"type": "string"
|
|
36
|
+
},
|
|
37
|
+
"enabled": {
|
|
38
|
+
"type": "boolean"
|
|
39
|
+
},
|
|
40
|
+
"gates": {
|
|
41
|
+
"$ref": "#/$defs/gates"
|
|
42
|
+
},
|
|
43
|
+
"rules": {
|
|
44
|
+
"type": "array",
|
|
45
|
+
"items": {
|
|
46
|
+
"type": "object",
|
|
47
|
+
"required": ["id", "expression", "serve"],
|
|
48
|
+
"additionalProperties": false,
|
|
49
|
+
"properties": {
|
|
50
|
+
"id": {
|
|
51
|
+
"type": "string",
|
|
52
|
+
"minLength": 1
|
|
53
|
+
},
|
|
54
|
+
"expression": {
|
|
55
|
+
"$ref": "#/$defs/expression"
|
|
56
|
+
},
|
|
57
|
+
"serve": {
|
|
58
|
+
"$ref": "#/$defs/serve"
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"fallthrough": {
|
|
64
|
+
"$ref": "#/$defs/serve"
|
|
65
|
+
},
|
|
66
|
+
"prerequisites": {
|
|
67
|
+
"type": "array",
|
|
68
|
+
"items": {
|
|
69
|
+
"type": "object",
|
|
70
|
+
"required": ["flag", "variation"],
|
|
71
|
+
"additionalProperties": false,
|
|
72
|
+
"properties": {
|
|
73
|
+
"flag": {
|
|
74
|
+
"$ref": "#/$defs/flagKey"
|
|
75
|
+
},
|
|
76
|
+
"variation": {
|
|
77
|
+
"type": "string"
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
"schedule": {
|
|
83
|
+
"type": "array",
|
|
84
|
+
"items": {
|
|
85
|
+
"type": "object",
|
|
86
|
+
"required": ["at", "set"],
|
|
87
|
+
"additionalProperties": false,
|
|
88
|
+
"properties": {
|
|
89
|
+
"at": {
|
|
90
|
+
"type": "string",
|
|
91
|
+
"format": "date-time"
|
|
92
|
+
},
|
|
93
|
+
"set": {
|
|
94
|
+
"type": "object",
|
|
95
|
+
"minProperties": 1,
|
|
96
|
+
"additionalProperties": false,
|
|
97
|
+
"properties": {
|
|
98
|
+
"enabled": {
|
|
99
|
+
"type": "boolean"
|
|
100
|
+
},
|
|
101
|
+
"gates": {
|
|
102
|
+
"$ref": "#/$defs/gates"
|
|
103
|
+
},
|
|
104
|
+
"fallthrough": {
|
|
105
|
+
"$ref": "#/$defs/serve"
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
"owner": {
|
|
113
|
+
"type": "string",
|
|
114
|
+
"minLength": 1,
|
|
115
|
+
"maxLength": 200
|
|
116
|
+
},
|
|
117
|
+
"tags": {
|
|
118
|
+
"type": "array",
|
|
119
|
+
"items": {
|
|
120
|
+
"type": "string"
|
|
121
|
+
},
|
|
122
|
+
"uniqueItems": true
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
"allOf": [
|
|
126
|
+
{
|
|
127
|
+
"if": {
|
|
128
|
+
"properties": {
|
|
129
|
+
"type": {
|
|
130
|
+
"enum": ["string", "number", "object"]
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
"required": ["type"],
|
|
134
|
+
"type": "object"
|
|
135
|
+
},
|
|
136
|
+
"then": {
|
|
137
|
+
"required": ["variations", "offVariation", "onVariation"],
|
|
138
|
+
"properties": {
|
|
139
|
+
"variations": true,
|
|
140
|
+
"offVariation": true,
|
|
141
|
+
"onVariation": true
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
"if": {
|
|
147
|
+
"properties": {
|
|
148
|
+
"type": {
|
|
149
|
+
"const": "string"
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
"required": ["type"],
|
|
153
|
+
"type": "object"
|
|
154
|
+
},
|
|
155
|
+
"then": {
|
|
156
|
+
"properties": {
|
|
157
|
+
"variations": {
|
|
158
|
+
"items": {
|
|
159
|
+
"properties": {
|
|
160
|
+
"value": {
|
|
161
|
+
"type": "string"
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
"type": "object"
|
|
165
|
+
},
|
|
166
|
+
"type": "array"
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
"if": {
|
|
173
|
+
"properties": {
|
|
174
|
+
"type": {
|
|
175
|
+
"const": "number"
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
"required": ["type"],
|
|
179
|
+
"type": "object"
|
|
180
|
+
},
|
|
181
|
+
"then": {
|
|
182
|
+
"properties": {
|
|
183
|
+
"variations": {
|
|
184
|
+
"items": {
|
|
185
|
+
"properties": {
|
|
186
|
+
"value": {
|
|
187
|
+
"type": "number"
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
"type": "object"
|
|
191
|
+
},
|
|
192
|
+
"type": "array"
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
"if": {
|
|
199
|
+
"properties": {
|
|
200
|
+
"type": {
|
|
201
|
+
"const": "object"
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
"required": ["type"],
|
|
205
|
+
"type": "object"
|
|
206
|
+
},
|
|
207
|
+
"then": {
|
|
208
|
+
"properties": {
|
|
209
|
+
"variations": {
|
|
210
|
+
"items": {
|
|
211
|
+
"properties": {
|
|
212
|
+
"value": {
|
|
213
|
+
"type": "object"
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
"type": "object"
|
|
217
|
+
},
|
|
218
|
+
"type": "array"
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
"if": {
|
|
225
|
+
"anyOf": [
|
|
226
|
+
{
|
|
227
|
+
"not": {
|
|
228
|
+
"required": ["type"],
|
|
229
|
+
"properties": {
|
|
230
|
+
"type": true
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
},
|
|
234
|
+
{
|
|
235
|
+
"properties": {
|
|
236
|
+
"type": {
|
|
237
|
+
"const": "boolean"
|
|
238
|
+
}
|
|
239
|
+
},
|
|
240
|
+
"required": ["type"]
|
|
241
|
+
}
|
|
242
|
+
]
|
|
243
|
+
},
|
|
244
|
+
"then": {
|
|
245
|
+
"properties": {
|
|
246
|
+
"variations": {
|
|
247
|
+
"items": {
|
|
248
|
+
"properties": {
|
|
249
|
+
"value": {
|
|
250
|
+
"type": "boolean"
|
|
251
|
+
}
|
|
252
|
+
},
|
|
253
|
+
"type": "object"
|
|
254
|
+
},
|
|
255
|
+
"type": "array"
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
],
|
|
261
|
+
"$defs": {
|
|
262
|
+
"flagKey": {
|
|
263
|
+
"type": "string",
|
|
264
|
+
"pattern": "^[a-z0-9][a-z0-9_.:-]*$"
|
|
265
|
+
},
|
|
266
|
+
"percentage": {
|
|
267
|
+
"type": "number",
|
|
268
|
+
"minimum": 0,
|
|
269
|
+
"maximum": 100
|
|
270
|
+
},
|
|
271
|
+
"gates": {
|
|
272
|
+
"type": "object",
|
|
273
|
+
"additionalProperties": false,
|
|
274
|
+
"properties": {
|
|
275
|
+
"boolean": {
|
|
276
|
+
"type": "boolean"
|
|
277
|
+
},
|
|
278
|
+
"actors": {
|
|
279
|
+
"type": "array",
|
|
280
|
+
"items": {
|
|
281
|
+
"type": "string"
|
|
282
|
+
},
|
|
283
|
+
"uniqueItems": true
|
|
284
|
+
},
|
|
285
|
+
"groups": {
|
|
286
|
+
"type": "array",
|
|
287
|
+
"items": {
|
|
288
|
+
"type": "string"
|
|
289
|
+
},
|
|
290
|
+
"uniqueItems": true
|
|
291
|
+
},
|
|
292
|
+
"percentageOfActors": {
|
|
293
|
+
"$ref": "#/$defs/percentage"
|
|
294
|
+
},
|
|
295
|
+
"percentageOfTime": {
|
|
296
|
+
"$ref": "#/$defs/percentage"
|
|
297
|
+
},
|
|
298
|
+
"expression": {
|
|
299
|
+
"oneOf": [
|
|
300
|
+
{
|
|
301
|
+
"type": "null"
|
|
302
|
+
},
|
|
303
|
+
{
|
|
304
|
+
"$ref": "#/$defs/expression"
|
|
305
|
+
}
|
|
306
|
+
]
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
},
|
|
310
|
+
"serve": {
|
|
311
|
+
"oneOf": [
|
|
312
|
+
{
|
|
313
|
+
"type": "object",
|
|
314
|
+
"required": ["variation"],
|
|
315
|
+
"additionalProperties": false,
|
|
316
|
+
"properties": {
|
|
317
|
+
"variation": {
|
|
318
|
+
"type": "string"
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
},
|
|
322
|
+
{
|
|
323
|
+
"type": "object",
|
|
324
|
+
"required": ["rollout"],
|
|
325
|
+
"additionalProperties": false,
|
|
326
|
+
"properties": {
|
|
327
|
+
"rollout": {
|
|
328
|
+
"type": "array",
|
|
329
|
+
"minItems": 1,
|
|
330
|
+
"items": {
|
|
331
|
+
"type": "object",
|
|
332
|
+
"required": ["variation", "weight"],
|
|
333
|
+
"additionalProperties": false,
|
|
334
|
+
"properties": {
|
|
335
|
+
"variation": {
|
|
336
|
+
"type": "string"
|
|
337
|
+
},
|
|
338
|
+
"weight": {
|
|
339
|
+
"$ref": "#/$defs/percentage"
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
]
|
|
347
|
+
},
|
|
348
|
+
"args": {
|
|
349
|
+
"oneOf": [
|
|
350
|
+
{
|
|
351
|
+
"type": "array",
|
|
352
|
+
"items": {
|
|
353
|
+
"$ref": "#/$defs/expression"
|
|
354
|
+
}
|
|
355
|
+
},
|
|
356
|
+
{
|
|
357
|
+
"$ref": "#/$defs/expression"
|
|
358
|
+
}
|
|
359
|
+
]
|
|
360
|
+
},
|
|
361
|
+
"expression": {
|
|
362
|
+
"oneOf": [
|
|
363
|
+
{
|
|
364
|
+
"type": ["string", "number", "boolean", "null"]
|
|
365
|
+
},
|
|
366
|
+
{
|
|
367
|
+
"type": "object",
|
|
368
|
+
"minProperties": 1,
|
|
369
|
+
"maxProperties": 1,
|
|
370
|
+
"propertyNames": {
|
|
371
|
+
"enum": [
|
|
372
|
+
"All",
|
|
373
|
+
"Any",
|
|
374
|
+
"Equal",
|
|
375
|
+
"NotEqual",
|
|
376
|
+
"GreaterThan",
|
|
377
|
+
"GreaterThanOrEqualTo",
|
|
378
|
+
"LessThan",
|
|
379
|
+
"LessThanOrEqualTo",
|
|
380
|
+
"Property",
|
|
381
|
+
"Boolean",
|
|
382
|
+
"Number",
|
|
383
|
+
"String",
|
|
384
|
+
"Now",
|
|
385
|
+
"Time",
|
|
386
|
+
"PercentageOfActors",
|
|
387
|
+
"Segment"
|
|
388
|
+
]
|
|
389
|
+
},
|
|
390
|
+
"additionalProperties": {
|
|
391
|
+
"$ref": "#/$defs/args"
|
|
392
|
+
},
|
|
393
|
+
"not": {
|
|
394
|
+
"anyOf": [
|
|
395
|
+
{
|
|
396
|
+
"required": ["All"],
|
|
397
|
+
"properties": {
|
|
398
|
+
"All": {
|
|
399
|
+
"type": "array",
|
|
400
|
+
"maxItems": 0
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
},
|
|
404
|
+
{
|
|
405
|
+
"required": ["Any"],
|
|
406
|
+
"properties": {
|
|
407
|
+
"Any": {
|
|
408
|
+
"type": "array",
|
|
409
|
+
"maxItems": 0
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
]
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
]
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# The client payload.
|
|
4
|
+
module Benchdog
|
|
5
|
+
# The tag that makes a flag visible to browsers.
|
|
6
|
+
CLIENT_TAG = "client"
|
|
7
|
+
|
|
8
|
+
# The flag values for one context, for a browser (SPEC section 9). Only the flags with the
|
|
9
|
+
# "client" tag are in it.
|
|
10
|
+
def self.client_payload(flag_set, context, groups: {}, now: Time.now)
|
|
11
|
+
clock = -> { now.to_r }
|
|
12
|
+
flags = flag_set["flags"].select { |_, flag| flag["tags"].to_a.include?(CLIENT_TAG) }.to_h do |key, _|
|
|
13
|
+
result = evaluate(flag_set, key, context, nil, groups: groups, clock: clock)
|
|
14
|
+
[key, { "value" => result.value, "variation" => result.variation }]
|
|
15
|
+
end
|
|
16
|
+
{ "environment" => flag_set["environment"], "generatedAt" => Store.iso_time(now), "flags" => flags }.compact
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "time"
|
|
4
|
+
|
|
5
|
+
# The Benchdog core.
|
|
6
|
+
module Benchdog
|
|
7
|
+
# Applies the schedule steps at or before `now` (in seconds) (SPEC section 4).
|
|
8
|
+
def self.apply_schedule(flag, now)
|
|
9
|
+
due = due_steps(flag, now)
|
|
10
|
+
return flag if due.empty?
|
|
11
|
+
|
|
12
|
+
start = flag.merge("gates" => flag["gates"].to_h.dup)
|
|
13
|
+
due.reduce(start) { |result, step| apply_step(result, step["set"]) }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# The steps at or before `now`, by time, then in their order in the array.
|
|
17
|
+
def self.due_steps(flag, now)
|
|
18
|
+
flag["schedule"].to_a
|
|
19
|
+
.each_with_index
|
|
20
|
+
.map { |step, index| [step, index, Time.iso8601(step["at"]).to_r] }
|
|
21
|
+
.select { |_, _, at| at <= now }
|
|
22
|
+
.sort_by { |_, index, at| [at, index] }
|
|
23
|
+
.map(&:first)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.apply_step(flag, set)
|
|
27
|
+
result = flag.merge(set.slice("enabled", "fallthrough"))
|
|
28
|
+
result["gates"] = flag["gates"].merge(set["gates"].to_h)
|
|
29
|
+
result
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# The audit actor of the scheduler.
|
|
33
|
+
SCHEDULER_ACTOR = { "id" => "benchdog:scheduler", "name" => "Scheduler" }.freeze
|
|
34
|
+
|
|
35
|
+
# Writes each due schedule step into its flag, with a "schedule.apply" audit entry. The
|
|
36
|
+
# evaluator already applies due steps at read time, so this changes no result. Returns the
|
|
37
|
+
# changed flag keys.
|
|
38
|
+
def self.apply_due_schedules(store, environment, now = Time.now)
|
|
39
|
+
seconds = now.to_r
|
|
40
|
+
store.load(environment)["flags"].values.filter_map do |flag|
|
|
41
|
+
due = due_steps(flag, seconds)
|
|
42
|
+
next if due.empty?
|
|
43
|
+
|
|
44
|
+
store.write(environment, { "kind" => "putFlag", "flag" => scheduled(flag, seconds) },
|
|
45
|
+
{ "actor" => SCHEDULER_ACTOR, "source" => "worker", "action" => "schedule.apply",
|
|
46
|
+
"reason" => "Scheduled change at #{due.map { |s| s['at'] }.join(', ')}" })
|
|
47
|
+
flag["key"]
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# The flag with its due steps applied and removed from the schedule.
|
|
52
|
+
def self.scheduled(flag, now)
|
|
53
|
+
later = flag["schedule"].reject { |step| Time.iso8601(step["at"]).to_r <= now }
|
|
54
|
+
result = apply_schedule(flag, now).except("schedule")
|
|
55
|
+
later.empty? ? result : result.merge("schedule" => later)
|
|
56
|
+
end
|
|
57
|
+
end
|