-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path_allocation.py
More file actions
209 lines (170 loc) · 6.1 KB
/
_allocation.py
File metadata and controls
209 lines (170 loc) · 6.1 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
# ------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# -------------------------------------------------------------------------
"""Allocation model for feature variant assignment."""
from typing import cast, List, Optional, Mapping, Dict, Any, Union
from dataclasses import dataclass
from ._constants import DEFAULT_WHEN_ENABLED, DEFAULT_WHEN_DISABLED, USER, GROUP, PERCENTILE, SEED
@dataclass
class UserAllocation:
"""
Represents a user allocation.
"""
variant: str
users: List[str]
@dataclass
class GroupAllocation:
"""
Represents a group allocation.
"""
variant: str
groups: List[str]
class PercentileAllocation:
"""
Represents a percentile allocation.
"""
def __init__(self) -> None:
self._variant: Optional[str] = None
self._percentile_from: int = 0
self._percentile_to: int = 0
@classmethod
def convert_from_json(cls, json: Mapping[str, Union[str, int]]) -> "PercentileAllocation":
"""
Convert a JSON object to PercentileAllocation.
:param dict json: JSON object.
:return: PercentileAllocation
:rtype: PercentileAllocation
"""
if not json:
raise ValueError("Percentile allocation is not valid.")
user_allocation = cls()
variant = json.get("variant")
if not variant or not isinstance(variant, str):
raise ValueError("Percentile allocation does not have a valid assigned variant.")
user_allocation._variant = variant
percentile_from = json.get("from", 0)
if not isinstance(percentile_from, int):
raise ValueError("Percentile allocation does not have a valid starting percentile.")
user_allocation._percentile_from = percentile_from
percentile_to = json.get("to")
if not percentile_to or not isinstance(percentile_to, int):
raise ValueError("Percentile allocation does not have a valid ending percentile.")
user_allocation._percentile_to = percentile_to
return user_allocation
@property
def variant(self) -> Optional[str]:
"""
Get the variant for the allocation.
:return: Variant for the allocation.
:rtype: str
"""
return self._variant
@property
def percentile_from(self) -> int:
"""
Get the starting percentile for the allocation.
:return: Starting percentile for the allocation.
:rtype: int
"""
return self._percentile_from
@property
def percentile_to(self) -> int:
"""
Get the ending percentile for the allocation.
:return: Ending percentile for the allocation.
:rtype: int
"""
return self._percentile_to
class Allocation:
"""
Represents an allocation configuration for a feature flag.
"""
def __init__(self) -> None:
self._default_when_enabled = None
self._default_when_disabled = None
self._user: List[UserAllocation] = []
self._group: List[GroupAllocation] = []
self._percentile: List[PercentileAllocation] = []
self._seed = None
@classmethod
def convert_from_json(cls, json: Dict[str, Any]) -> Optional["Allocation"]:
"""
Convert a JSON object to Allocation.
:param json: JSON object
:type json: dict
:return: Allocation
:rtype: Allocation
"""
if not json:
return None
allocation = cls()
allocation._default_when_enabled = json.get(DEFAULT_WHEN_ENABLED)
allocation._default_when_disabled = json.get(DEFAULT_WHEN_DISABLED)
allocation._user = []
allocation._group = []
allocation._percentile = []
allocations: List[Any] = []
if USER in json:
allocations = cast(List[Any], json.get(USER, []))
for user_allocation in allocations:
allocation._user.append(UserAllocation(**user_allocation))
if GROUP in json:
allocations = cast(List[Any], json.get(GROUP, []))
for group_allocation in allocations:
allocation._group.append(GroupAllocation(**group_allocation))
if PERCENTILE in json:
allocations = cast(List[Any], json.get(PERCENTILE, []))
for percentile_allocation in allocations:
allocation._percentile.append(PercentileAllocation.convert_from_json(percentile_allocation))
allocation._seed = json.get(SEED, allocation._seed)
return allocation
@property
def default_when_enabled(self) -> Optional[str]:
"""
Get the default variant when the feature flag is enabled.
:return: Default variant when the feature flag is enabled.
:rtype: str
"""
return self._default_when_enabled
@property
def default_when_disabled(self) -> Optional[str]:
"""
Get the default variant when the feature flag is disabled.
:return: Default variant when the feature flag is disabled.
:rtype: str
"""
return self._default_when_disabled
@property
def user(self) -> List[UserAllocation]:
"""
Get the user allocations.
:return: User allocations.
:rtype: list[UserAllocation]
"""
return self._user
@property
def group(self) -> List[GroupAllocation]:
"""
Get the group allocations.
:return: Group allocations.
:rtype: list[GroupAllocation]
"""
return self._group
@property
def percentile(self) -> List[PercentileAllocation]:
"""
Get the percentile allocations.
:return: Percentile allocations.
:rtype: list[PercentileAllocation]
"""
return self._percentile
@property
def seed(self) -> Optional[str]:
"""
Get the seed for the allocation.
:return: Seed for the allocation.
:rtype: str
"""
return self._seed