Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36
[AI-FSSDK] [FSSDK-12369] Add local holdouts support to Python SDK#512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
fdcb27d65e390c0d6e3ad7cb671ba2bdaa3File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -223,6 +223,7 @@ def __init__( | ||
| trafficAllocation: list[TrafficAllocation], | ||
| audienceIds: list[str], | ||
| audienceConditions: Optional[Sequence[str | list[str]]] = None, | ||
| includedRules: Optional[list[str]] = None, | ||
| **kwargs: Any | ||
| ): | ||
| self.id = id | ||
| @@ -232,6 +233,8 @@ def __init__( | ||
| self.trafficAllocation = trafficAllocation | ||
| self.audienceIds = audienceIds | ||
| self.audienceConditions = audienceConditions | ||
| # None = global holdout (applies to all rules); list of rule IDs = local holdout | ||
| self.included_rules: Optional[list[str]] = includedRules | ||
| def get_audience_conditions_or_ids(self) -> Sequence[str | list[str]]: | ||
| """Returns audienceConditions if present, otherwise audienceIds. | ||
| @@ -241,6 +244,18 @@ def get_audience_conditions_or_ids(self) -> Sequence[str | list[str]]: | ||
| """ | ||
| return self.audienceConditions if self.audienceConditions is not None else self.audienceIds | ||
| @property | ||
| def is_global(self) -> bool: | ||
| """Check if this is a global holdout (applies to all rules across all flags). | ||
| A holdout is global when includedRules is None (absent from datafile). | ||
| An empty list [] is a local holdout that targets no rules (different from global). | ||
| Returns: | ||
| True if included_rules is None (global), False otherwise (local). | ||
| """ | ||
| return self.included_rules is None | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good | ||
| @property | ||
| def is_activated(self) -> bool: | ||
| """Check if the holdout is activated (running). | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -93,7 +93,10 @@ def __init__(self, datafile: str | bytes, logger: Logger, error_handler: Any): | ||
| holdouts_data: list[types.HoldoutDict] = config.get('holdouts', []) | ||
| self.holdouts: list[entities.Holdout] = [] | ||
| self.holdout_id_map: dict[str, entities.Holdout] = {} | ||
| self.flag_holdouts_map: dict[str, list[entities.Holdout]] = {} | ||
| # Global holdouts (includedRules is None) — evaluated at flag level before any rule | ||
| self.global_holdouts: list[entities.Holdout] = [] | ||
| # Rule-level holdouts — map from rule ID to holdouts targeting that rule | ||
| self.rule_holdouts_map: dict[str, list[entities.Holdout]] = {} | ||
| # Convert holdout dicts to Holdout entities | ||
| for holdout_data in holdouts_data: | ||
| @@ -108,6 +111,16 @@ def __init__(self, datafile: str | bytes, logger: Logger, error_handler: Any): | ||
| # Map by ID for quick lookup | ||
| self.holdout_id_map[holdout.id] = holdout | ||
| # Classify holdout as global or local based on includedRules | ||
| if holdout.is_global: | ||
| self.global_holdouts.append(holdout) | ||
| else: | ||
| # Local holdout — register for each targeted rule ID | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good | ||
| for rule_id in (holdout.included_rules or []): | ||
| if rule_id not in self.rule_holdouts_map: | ||
| self.rule_holdouts_map[rule_id] = [] | ||
| self.rule_holdouts_map[rule_id].append(holdout) | ||
| # Utility maps for quick lookup | ||
| self.group_id_map: dict[str, entities.Group] = self._generate_key_map(self.groups, 'id', entities.Group) | ||
| self.experiment_id_map: dict[str, entities.Experiment] = self._generate_key_map( | ||
| @@ -240,11 +253,6 @@ def __init__(self, datafile: str | bytes, logger: Logger, error_handler: Any): | ||
| everyone_else_variation.variables, 'id', entities.Variation.VariableUsage | ||
| ) | ||
| # Map all running holdouts to this flag | ||
| applicable_holdouts = list(self.holdout_id_map.values()) | ||
| if applicable_holdouts: | ||
| self.flag_holdouts_map[feature.key] = applicable_holdouts | ||
| rollout = None if len(feature.rolloutId) == 0 else self.rollout_id_map[feature.rolloutId] | ||
| if rollout: | ||
| for exp in rollout.experiments: | ||
| @@ -878,19 +886,30 @@ def get_flag_variation( | ||
| return None | ||
| def get_holdouts_for_flag(self, flag_key: str) -> list[entities.Holdout]: | ||
| """ Helper method to get holdouts from an applied feature flag. | ||
| def get_global_holdouts(self) -> list[entities.Holdout]: | ||
| """Return all global holdouts (includedRules is None). | ||
| Args: | ||
| flag_key: Key of the feature flag. | ||
| Global holdouts are evaluated at flag level before any rule is checked. | ||
| Returns: | ||
| The holdouts that apply for a specific flag as Holdout entity objects. | ||
| List of global Holdout entities that are currently running. | ||
| """ | ||
| if not self.holdouts: | ||
| return [] | ||
| return self.global_holdouts | ||
| def get_holdouts_for_rule(self, rule_id: str) -> list[entities.Holdout]: | ||
| """Return local holdouts that target a specific rule. | ||
| return self.flag_holdouts_map.get(flag_key, []) | ||
| Local holdouts are evaluated per-rule, before the rule's audience and | ||
| traffic allocation checks. A rule ID not present in any holdout's | ||
| includedRules simply returns an empty list — silently skipped. | ||
| Args: | ||
| rule_id: The experiment or delivery rule ID to look up. | ||
| Returns: | ||
| List of local Holdout entities targeting the given rule ID. | ||
| """ | ||
| return self.rule_holdouts_map.get(rule_id, []) | ||
| def get_holdout(self, holdout_id: str) -> Optional[entities.Holdout]: | ||
| """ Helper method to get holdout from holdout ID. | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good