Skip to content

Commit c73f410

Browse files
[AI-FSSDK] [FSSDK-12337] Return Variation entity from _get_everyone_else_variation
- Build Variation entity once in helper, derive dict from it in caller - Addresses PR review comment from jaeopt
1 parent 3f05ad8 commit c73f410

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

optimizely/project_config.py

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -238,26 +238,29 @@ def __init__(self, datafile: str | bytes, logger: Logger, error_handler: Any):
238238
if everyone_else_variation is not None:
239239
for experiment in rules:
240240
if experiment.type == 'feature_rollout':
241-
experiment.variations.append(everyone_else_variation)
241+
experiment.variations.append({
242+
'id': everyone_else_variation.id,
243+
'key': everyone_else_variation.key,
244+
'featureEnabled': everyone_else_variation.featureEnabled,
245+
'variables': cast(
246+
list[types.VariableDict],
247+
everyone_else_variation.variables,
248+
),
249+
})
242250
experiment.trafficAllocation.append({
243-
'entityId': everyone_else_variation['id'],
251+
'entityId': everyone_else_variation.id,
244252
'endOfRange': 10000,
245253
})
246-
var_entity = entities.Variation(
247-
id=everyone_else_variation['id'],
248-
key=everyone_else_variation['key'],
249-
featureEnabled=bool(everyone_else_variation.get('featureEnabled', False)),
250-
variables=cast(
251-
Optional[list[entities.Variable]],
252-
everyone_else_variation.get('variables'),
253-
),
254+
self.variation_key_map[experiment.key][everyone_else_variation.key] = everyone_else_variation
255+
self.variation_id_map[experiment.key][everyone_else_variation.id] = everyone_else_variation
256+
self.variation_id_map_by_experiment_id[experiment.id][everyone_else_variation.id] = (
257+
everyone_else_variation
254258
)
255-
self.variation_key_map[experiment.key][var_entity.key] = var_entity
256-
self.variation_id_map[experiment.key][var_entity.id] = var_entity
257-
self.variation_id_map_by_experiment_id[experiment.id][var_entity.id] = var_entity
258-
self.variation_key_map_by_experiment_id[experiment.id][var_entity.key] = var_entity
259-
self.variation_variable_usage_map[var_entity.id] = self._generate_key_map(
260-
var_entity.variables, 'id', entities.Variation.VariableUsage
259+
self.variation_key_map_by_experiment_id[experiment.id][everyone_else_variation.key] = (
260+
everyone_else_variation
261+
)
262+
self.variation_variable_usage_map[everyone_else_variation.id] = self._generate_key_map(
263+
everyone_else_variation.variables, 'id', entities.Variation.VariableUsage
261264
)
262265

263266
flag_id = feature.id
@@ -695,7 +698,7 @@ def get_rollout_from_id(self, rollout_id: str) -> Optional[entities.Layer]:
695698
self.logger.error(f'Rollout with ID "{rollout_id}" is not in datafile.')
696699
return None
697700

698-
def _get_everyone_else_variation(self, flag: entities.FeatureFlag) -> Optional[types.VariationDict]:
701+
def _get_everyone_else_variation(self, flag: entities.FeatureFlag) -> Optional[entities.Variation]:
699702
""" Get the "everyone else" variation for a feature flag.
700703
701704
The "everyone else" rule is the last experiment in the flag's rollout,
@@ -705,7 +708,7 @@ def _get_everyone_else_variation(self, flag: entities.FeatureFlag) -> Optional[t
705708
flag: The feature flag to get the everyone else variation for.
706709
707710
Returns:
708-
The "everyone else" variation dict, or None if not available.
711+
The "everyone else" Variation entity, or None if not available.
709712
"""
710713
if not flag.rolloutId:
711714
return None
@@ -719,7 +722,16 @@ def _get_everyone_else_variation(self, flag: entities.FeatureFlag) -> Optional[t
719722
if not variations:
720723
return None
721724

722-
return variations[0]
725+
variation_dict = variations[0]
726+
return entities.Variation(
727+
id=variation_dict['id'],
728+
key=variation_dict['key'],
729+
featureEnabled=bool(variation_dict.get('featureEnabled', False)),
730+
variables=cast(
731+
Optional[list[entities.Variable]],
732+
variation_dict.get('variables'),
733+
),
734+
)
723735

724736
def get_variable_value_for_variation(
725737
self, variable: Optional[entities.Variable], variation: Optional[Union[entities.Variation, VariationDict]]

0 commit comments

Comments
 (0)