-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsplits.py
More file actions
344 lines (293 loc) · 11 KB
/
splits.py
File metadata and controls
344 lines (293 loc) · 11 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
"""Splits module."""
from enum import Enum
from collections import namedtuple
import logging
from splitio.models import MatcherNotFoundException
from splitio.models.grammar import condition
_LOGGER = logging.getLogger(__name__)
SplitView = namedtuple(
'SplitView',
['name', 'traffic_type', 'killed', 'treatments', 'change_number', 'configs', 'default_treatment', 'sets', 'impressions_disabled', 'prerequisites']
)
_DEFAULT_CONDITIONS_TEMPLATE = {
"conditionType": "ROLLOUT",
"matcherGroup": {
"combiner": "AND",
"matchers": [
{
"keySelector": None,
"matcherType": "ALL_KEYS",
"negate": False,
"userDefinedSegmentMatcherData": None,
"whitelistMatcherData": None,
"unaryNumericMatcherData": None,
"betweenMatcherData": None,
"dependencyMatcherData": None,
"booleanMatcherData": None,
"stringMatcherData": None
}]
},
"partitions": [
{
"treatment": "control",
"size": 100
}
],
"label": "targeting rule type unsupported by sdk"
}
class Prerequisites(object):
"""Prerequisites."""
def __init__(self, feature_flag_name, treatments):
self._feature_flag_name = feature_flag_name
self._treatments = treatments
@property
def feature_flag_name(self):
"""Return featur eflag name."""
return self._feature_flag_name
@property
def treatments(self):
"""Return treatments."""
return self._treatments
def to_json(self):
to_return = []
for feature_flag_name in self._feature_flag_name:
to_return.append({"n": feature_flag_name, "ts": [treatment for treatment in self._treatments]})
return to_return
class Status(Enum):
"""Split status."""
ACTIVE = "ACTIVE"
ARCHIVED = "ARCHIVED"
class HashAlgorithm(Enum):
"""Hash algorithm names."""
LEGACY = 1
MURMUR = 2
class Split(object): # pylint: disable=too-many-instance-attributes
"""Split model object."""
def __init__( # pylint: disable=too-many-arguments
self,
name,
seed,
killed,
default_treatment,
traffic_type_name,
status,
change_number,
conditions=None,
algo=None,
traffic_allocation=None,
traffic_allocation_seed=None,
configurations=None,
sets=None,
impressions_disabled=None,
prerequisites = None
):
"""
Class constructor.
:param name: Name of the feature
:type name: unicode
:param seed: Seed
:type seed: int
:param killed: Whether the split is killed or not
:type killed: bool
:param default_treatment: Default treatment for the split
:type default_treatment: str
:param conditions: Set of conditions to test
:type conditions: list
:param algo: Hash algorithm to use when splitting.
:type algo: HashAlgorithm
:param traffic_allocation: Percentage of traffic to consider.
:type traffic_allocation: int
:pram traffic_allocation_seed: Seed used to hash traffic allocation.
:type traffic_allocation_seed: int
:pram sets: list of flag sets
:type sets: list
:pram impressions_disabled: track impressions flag
:type impressions_disabled: boolean
:pram prerequisites: prerequisites
:type prerequisites: List of Preqreuisites
"""
self._name = name
self._seed = seed
self._killed = killed
self._default_treatment = default_treatment
self._traffic_type_name = traffic_type_name
try:
self._status = Status(status)
except ValueError:
self._status = Status.ARCHIVED
self._change_number = change_number
self._conditions = conditions if conditions is not None else []
if traffic_allocation is None:
self._traffic_allocation = 100
elif traffic_allocation >= 0 and traffic_allocation <= 100:
self._traffic_allocation = traffic_allocation
else:
self._traffic_allocation = 100
self._traffic_allocation_seed = traffic_allocation_seed
try:
self._algo = HashAlgorithm(algo)
except ValueError:
self._algo = HashAlgorithm.LEGACY
self._configurations = configurations
self._sets = set(sets) if sets is not None else set()
self._impressions_disabled = impressions_disabled if impressions_disabled is not None else False
self._prerequisites = prerequisites if prerequisites is not None else []
@property
def name(self):
"""Return name."""
return self._name
@property
def seed(self):
"""Return seed."""
return self._seed
@property
def algo(self):
"""Return hash algorithm."""
return self._algo
@property
def killed(self):
"""Return whether the split has been killed."""
return self._killed
@property
def default_treatment(self):
"""Return the default treatment."""
return self._default_treatment
@property
def traffic_type_name(self):
"""Return the traffic type of the split."""
return self._traffic_type_name
@property
def status(self):
"""Return the status of the split."""
return self._status
@property
def change_number(self):
"""Return the change number of the split."""
return self._change_number
@property
def conditions(self):
"""Return the condition list of the split."""
return self._conditions
@property
def traffic_allocation(self):
"""Return the traffic allocation percentage of the split."""
return self._traffic_allocation
@property
def traffic_allocation_seed(self):
"""Return the traffic allocation seed of the split."""
return self._traffic_allocation_seed
@property
def sets(self):
"""Return the flag sets of the split."""
return self._sets
@property
def impressions_disabled(self):
"""Return impressions_disabled of the split."""
return self._impressions_disabled
@property
def prerequisites(self):
"""Return prerequisites of the split."""
return self._prerequisites
def get_configurations_for(self, treatment):
"""Return the mapping of treatments to configurations."""
return self._configurations.get(treatment) if self._configurations else None
def get_segment_names(self):
"""
Return a list of segment names referenced in all matchers from this split.
:return: List of segment names.
:rtype: list(string)
"""
return [name for cond in self.conditions for name in cond.get_segment_names()]
def to_json(self):
"""Return a JSON representation of this split."""
return {
'changeNumber': self.change_number,
'trafficTypeName': self.traffic_type_name,
'name': self.name,
'trafficAllocation': self.traffic_allocation,
'trafficAllocationSeed': self.traffic_allocation_seed,
'seed': self.seed,
'status': self.status.value,
'killed': self.killed,
'defaultTreatment': self.default_treatment,
'algo': self.algo.value,
'conditions': [c.to_json() for c in self.conditions],
'configurations': self._configurations,
'sets': list(self._sets),
'impressionsDisabled': self._impressions_disabled,
'prerequisites': [prerequisite.to_json() for prerequisite in self._prerequisites]
}
def to_split_view(self):
"""
Return a SplitView for the manager.
:return: A portion of the split useful for inspecting by the user.
:rtype: SplitView
"""
return SplitView(
self.name,
self.traffic_type_name,
self.killed,
list(set(part.treatment for cond in self.conditions for part in cond.partitions)),
self.change_number,
self._configurations if self._configurations is not None else {},
self._default_treatment,
list(self._sets) if self._sets is not None else [],
self._impressions_disabled,
self._prerequisites
)
def local_kill(self, default_treatment, change_number):
"""
Perform split kill.
:param default_treatment: name of the default treatment to return
:type default_treatment: str
:param change_number: change_number
:type change_number: int
"""
self._default_treatment = default_treatment
self._change_number = change_number
self._killed = True
def __str__(self):
"""Return string representation."""
return 'name: {name}, seed: {seed}, killed: {killed}, ' \
'default treatment: {default_treatment}, ' \
'conditions: {conditions}'.format(
name=self._name, seed=self._seed, killed=self._killed,
default_treatment=self._default_treatment,
conditions=','.join(map(str, self._conditions))
)
def from_raw(raw_split):
"""
Parse a split from a JSON portion of splitChanges.
:param raw_split: JSON object extracted from a splitChange's split array (splitChanges response)
:type raw_split: dict
:return: A parsed Split object capable of performing evaluations.
:rtype: Split
"""
try:
conditions = [condition.from_raw(c) for c in raw_split['conditions']]
except MatcherNotFoundException as e:
_LOGGER.error(str(e))
_LOGGER.debug("Using default conditions template for feature flag: %s", raw_split['name'])
conditions = [condition.from_raw(_DEFAULT_CONDITIONS_TEMPLATE)]
return Split(
raw_split['name'],
raw_split['seed'],
raw_split['killed'],
raw_split['defaultTreatment'],
raw_split['trafficTypeName'],
raw_split['status'],
raw_split['changeNumber'],
conditions,
raw_split.get('algo'),
traffic_allocation=raw_split.get('trafficAllocation'),
traffic_allocation_seed=raw_split.get('trafficAllocationSeed'),
configurations=raw_split.get('configurations'),
sets=set(raw_split.get('sets')) if raw_split.get('sets') is not None else [],
impressions_disabled=raw_split.get('impressionsDisabled') if raw_split.get('impressionsDisabled') is not None else False,
prerequisites=from_raw_prerequisites(raw_split.get('prerequisites')) if raw_split.get('prerequisites') is not None else []
)
def from_raw_prerequisites(raw_prerequisites):
to_return = []
for prerequisite in raw_prerequisites:
to_return.append(Prerequisites(prerequisite['n'], prerequisite['ts']))
return to_return