-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmodels.py
More file actions
186 lines (156 loc) · 5.9 KB
/
models.py
File metadata and controls
186 lines (156 loc) · 5.9 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
from __future__ import annotations
import typing
from dataclasses import dataclass, field
from flag_engine.features.models import FeatureStateModel
from flag_engine.result.types import EvaluationResult, FlagResult
from flagsmith.analytics import AnalyticsProcessor
from flagsmith.exceptions import FlagsmithFeatureDoesNotExistError
@dataclass
class BaseFlag:
enabled: bool
value: typing.Union[str, int, float, bool, None]
@dataclass
class DefaultFlag(BaseFlag):
is_default: bool = field(default=True)
@dataclass
class Flag(BaseFlag):
feature_id: int
feature_name: str
is_default: bool = field(default=False)
@classmethod
def from_feature_state_model(
cls,
feature_state_model: FeatureStateModel,
identity_id: typing.Optional[typing.Union[str, int]] = None,
) -> Flag:
return Flag(
enabled=feature_state_model.enabled,
value=feature_state_model.get_value(identity_id=identity_id),
feature_name=feature_state_model.feature.name,
feature_id=feature_state_model.feature.id,
)
@classmethod
def from_evaluation_result(
cls,
flag: FlagResult,
identity_id: typing.Optional[typing.Union[str, int]] = None,
) -> Flag:
return Flag(
enabled=flag["enabled"],
value=flag["value"],
feature_name=flag["name"],
feature_id=int(flag["feature_key"]),
)
@classmethod
def from_api_flag(cls, flag_data: typing.Mapping[str, typing.Any]) -> Flag:
return Flag(
enabled=flag_data["enabled"],
value=flag_data["feature_state_value"],
feature_name=flag_data["feature"]["name"],
feature_id=flag_data["feature"]["id"],
)
@dataclass
class Flags:
flags: typing.Dict[str, Flag] = field(default_factory=dict)
default_flag_handler: typing.Optional[typing.Callable[[str], DefaultFlag]] = None
_analytics_processor: typing.Optional[AnalyticsProcessor] = None
@classmethod
def from_feature_state_models(
cls,
feature_states: typing.Sequence[FeatureStateModel],
analytics_processor: typing.Optional[AnalyticsProcessor],
default_flag_handler: typing.Optional[typing.Callable[[str], DefaultFlag]],
identity_id: typing.Optional[typing.Union[str, int]] = None,
) -> Flags:
flags = {
feature_state.feature.name: Flag.from_feature_state_model(
feature_state, identity_id=identity_id
)
for feature_state in feature_states
}
return cls(
flags=flags,
default_flag_handler=default_flag_handler,
_analytics_processor=analytics_processor,
)
@classmethod
def from_evaluation_result(
cls,
evaluation_result: EvaluationResult,
analytics_processor: typing.Optional[AnalyticsProcessor],
default_flag_handler: typing.Optional[typing.Callable[[str], DefaultFlag]],
identity_id: typing.Optional[typing.Union[str, int]] = None,
) -> Flags:
return cls(
flags={
flag["name"]: Flag(
enabled=flag["enabled"],
value=flag["value"],
feature_name=flag["name"],
feature_id=int(flag["feature_key"]),
)
for flag in evaluation_result["flags"]
},
default_flag_handler=default_flag_handler,
_analytics_processor=analytics_processor,
)
@classmethod
def from_api_flags(
cls,
api_flags: typing.Sequence[typing.Mapping[str, typing.Any]],
analytics_processor: typing.Optional[AnalyticsProcessor],
default_flag_handler: typing.Optional[typing.Callable[[str], DefaultFlag]],
) -> Flags:
flags = {
flag_data["feature"]["name"]: Flag.from_api_flag(flag_data)
for flag_data in api_flags
}
return cls(
flags=flags,
default_flag_handler=default_flag_handler,
_analytics_processor=analytics_processor,
)
def all_flags(self) -> typing.List[Flag]:
"""
Get a list of all Flag objects.
:return: list of Flag objects.
"""
return list(self.flags.values())
def is_feature_enabled(self, feature_name: str) -> bool:
"""
Check whether a given feature is enabled.
:param feature_name: the name of the feature to check if enabled.
:return: Boolean representing the enabled state of a given feature.
:raises FlagsmithClientError: if feature doesn't exist
"""
return self.get_flag(feature_name).enabled
def get_feature_value(self, feature_name: str) -> typing.Any:
"""
Get the value of a particular feature.
:param feature_name: the name of the feature to retrieve the value of.
:return: the value of the given feature.
:raises FlagsmithClientError: if feature doesn't exist
"""
return self.get_flag(feature_name).value
def get_flag(self, feature_name: str) -> typing.Union[DefaultFlag, Flag]:
"""
Get a specific flag given the feature name.
:param feature_name: the name of the feature to retrieve the flag for.
:return: DefaultFlag | Flag object.
:raises FlagsmithClientError: if feature doesn't exist
"""
try:
flag = self.flags[feature_name]
except KeyError:
if self.default_flag_handler:
return self.default_flag_handler(feature_name)
raise FlagsmithFeatureDoesNotExistError(
"Feature does not exist: %s" % feature_name
)
if self._analytics_processor and hasattr(flag, "feature_name"):
self._analytics_processor.track_feature(flag.feature_name)
return flag
@dataclass
class Segment:
id: int
name: str