1+ import math
12import typing
23import uuid
34from dataclasses import dataclass , field
@@ -32,11 +33,17 @@ class MultivariateFeatureStateValueModel:
3233 mv_fs_value_uuid : str = field (default_factory = uuid .uuid4 )
3334
3435
36+ @dataclass
37+ class FeatureSegmentModel :
38+ priority : int = None
39+
40+
3541@dataclass
3642class FeatureStateModel :
3743 feature : FeatureModel
3844 enabled : bool
3945 django_id : int = None
46+ feature_segment : FeatureSegmentModel = None
4047 featurestate_uuid : str = field (default_factory = uuid .uuid4 )
4148 feature_state_value : typing .Any = field (default = None , init = False )
4249 multivariate_feature_state_values : typing .List [
@@ -58,6 +65,34 @@ def get_value(self, identity_id: typing.Union[int, str] = None) -> typing.Any:
5865 return self ._get_multivariate_value (identity_id )
5966 return self .feature_state_value
6067
68+ def is_higher_segment_priority (self , other : "FeatureStateModel" ) -> bool :
69+ """
70+ Returns `True` if `self` is higher segment priority than `other`
71+ (i.e. has lower value for feature_segment.priority)
72+
73+ NOTE:
74+ A segment will be considered higher priority only if:
75+ 1. `other` does not have a feature segment(i.e: it is an environment feature state or it's a
76+ feature state with feature segment but from an old document that does not have `feature_segment.priority`)
77+ but `self` does.
78+
79+ 2. `other` have a feature segment with high priority
80+
81+ """
82+
83+ try :
84+ return (
85+ getattr (
86+ self .feature_segment ,
87+ "priority" ,
88+ math .inf ,
89+ )
90+ < other .feature_segment .priority
91+ )
92+
93+ except (TypeError , AttributeError ):
94+ return False
95+
6196 def _get_multivariate_value (
6297 self , identity_id : typing .Union [int , str ]
6398 ) -> typing .Any :
0 commit comments