@@ -124,27 +124,26 @@ def __init__(self, datafile, logger, error_handler):
124
124
# As we cannot create json variables in datafile directly, here we convert
125
125
# the variables of string type and json subType to json type
126
126
# This is needed to fully support json variables
127
- for feature in self .feature_key_map :
128
- for variable in self .feature_key_map [feature ].variables :
127
+ self .experiment_feature_map = {}
128
+ self .flag_rules_map = {}
129
+
130
+ for feature_key , feature_value in self .feature_key_map .items ():
131
+ for variable in self .feature_key_map [feature_key ].variables :
129
132
sub_type = variable .get ('subType' , '' )
130
133
if variable ['type' ] == entities .Variable .Type .STRING and sub_type == entities .Variable .Type .JSON :
131
134
variable ['type' ] = entities .Variable .Type .JSON
132
135
133
- # Dict containing map of experiment ID to feature ID.
134
- # for checking that experiment is a feature experiment or not.
135
- self .experiment_feature_map = {}
136
- for feature in self .feature_key_map .values ():
137
- feature .variables = self ._generate_key_map (feature .variables , 'key' , entities .Variable )
138
- for exp_id in feature .experimentIds :
136
+ # loop over features=flags already happening
137
+ # get feature variables for eacg flag/feature
138
+ feature_value .variables = self ._generate_key_map (feature_value .variables , 'key' , entities .Variable )
139
+ for exp_id in feature_value .experimentIds :
139
140
# Add this experiment in experiment-feature map.
140
- self .experiment_feature_map [exp_id ] = [feature .id ]
141
+ self .experiment_feature_map [exp_id ] = [feature_value .id ]
141
142
142
143
# all rules(experiment rules and delivery rules) for each flag
143
- self .flag_rules_map = {}
144
144
for flag in self .feature_flags :
145
-
146
145
experiments = []
147
- if not flag ['experimentIds' ] == '' :
146
+ if len ( flag ['experimentIds' ]) > 0 :
148
147
for exp_id in flag ['experimentIds' ]:
149
148
experiments .append (self .experiment_id_map [exp_id ])
150
149
if not flag ['rolloutId' ] == '' :
@@ -160,19 +159,7 @@ def __init__(self, datafile, logger, error_handler):
160
159
# All variations for each flag
161
160
# Datafile does not contain a separate entity for this.
162
161
# We collect variations used in each rule (experiment rules and delivery rules)
163
- self .flag_variations_map = {}
164
-
165
- for flag_key , rules in self .flag_rules_map .items ():
166
- variations = []
167
- for rule in rules :
168
- # get variations as objects (rule.variations gives list)
169
-
170
- variation_objects = self .variation_id_map_by_experiment_id [rule .id ].values ()
171
- for variation in variation_objects :
172
- if variation .id not in [var .id for var in variations ]:
173
- variations .append (variation )
174
-
175
- self .flag_variations_map [flag_key ] = variations
162
+ self .flag_variations_map = self .get_all_variations_for_each_rule (self .flag_rules_map )
176
163
177
164
@staticmethod
178
165
def _generate_key_map (entity_list , key , entity_class ):
@@ -674,3 +661,29 @@ def get_flag_variation_by_id(self, flag_key, variation_id):
674
661
return variation
675
662
676
663
return None
664
+
665
+ def get_all_variations_for_each_rule (self , flag_rules_map ):
666
+ """ Helper method to get all variation objects from each flag.
667
+ collects variations used in each rule (experiment rules and delivery rules).
668
+
669
+ Args:
670
+ flag_rules_map: A dictionary. A map of all rules per flag.
671
+
672
+ Returns:
673
+ Map of flag variations.
674
+ """
675
+ flag_variations_map = {}
676
+
677
+ for flag_key , rules in flag_rules_map .items ():
678
+ variations = []
679
+ for rule in rules :
680
+ # get variations as objects (rule.variations gives list)
681
+ variation_objects = self .variation_id_map_by_experiment_id [rule .id ].values ()
682
+ for variation in variation_objects :
683
+ # append variation if it's not already in the list
684
+ if variation not in variations :
685
+ variations .append (variation )
686
+
687
+ flag_variations_map [flag_key ] = variations
688
+
689
+ return flag_variations_map
0 commit comments