Skip to content

[FSSDK-11184] Update: Send CMAB uuid in impression events #458

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

Open
wants to merge 45 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
88b4f1e
update: integrate CMAB components into OptimizelyFactory
FarhanAnjum-opti Jun 4, 2025
2563c7b
update: add cmab_service parameter to Optimizely constructor for CMAB…
FarhanAnjum-opti Jun 4, 2025
fac8946
update: add docstring to DefaultCmabService class for improved docume…
FarhanAnjum-opti Jun 4, 2025
f74bc8c
update: implement CMAB support in bucketer and decision service, reve…
FarhanAnjum-opti Jun 13, 2025
6d1f73d
linting fix
FarhanAnjum-opti Jun 13, 2025
91d53b6
update: add cmab_uuid handling in DecisionService and related tests
FarhanAnjum-opti Jun 16, 2025
3eb755f
- updated function bucket_to_entity_id
FarhanAnjum-opti Jun 16, 2025
a5e4993
update: add None parameter to Decision constructor in user context tests
FarhanAnjum-opti Jun 16, 2025
c1cd97a
update: enhance CMAB decision handling and add related tests
FarhanAnjum-opti Jun 16, 2025
fd7c723
update: fix logger message formatting in CMAB experiment tests
FarhanAnjum-opti Jun 16, 2025
ec19c3b
mypy fix
FarhanAnjum-opti Jun 16, 2025
029262d
update: refine traffic allocation type hints and key naming in bucket…
FarhanAnjum-opti Jun 16, 2025
180fdee
update: remove unused import of cast in bucketer.py
FarhanAnjum-opti Jun 16, 2025
cd5ba39
update: fix return type for numeric_metric_value in get_numeric_value…
FarhanAnjum-opti Jun 16, 2025
92a3258
update: specify type hint for numeric_metric_value in get_numeric_val…
FarhanAnjum-opti Jun 16, 2025
fe100cb
update: fix logger reference in DefaultCmabClient initialization and …
FarhanAnjum-opti Jun 17, 2025
60a4ada
update: enhance error logging for CMAB fetch failures with detailed m…
FarhanAnjum-opti Jun 20, 2025
265d82b
update: enhance decision result handling by introducing VariationResu…
FarhanAnjum-opti Jun 20, 2025
6ca1102
update: refactor get_variation return structure and change tests acco…
FarhanAnjum-opti Jun 20, 2025
c2b3d96
-Error propagated to optimizely.py
FarhanAnjum-opti Jun 23, 2025
0e25622
update: add cmab_uuid parameter to impression events
FarhanAnjum-opti Jun 27, 2025
088f4af
update: add None parameter to impression events in decision tests
FarhanAnjum-opti Jun 27, 2025
b901c5f
update: modify get_variation to return VariationResult and adjust rel…
FarhanAnjum-opti Jun 27, 2025
fa77cca
Merge branch 'farhan-anjum/FSSDK-11175-add-decision-service-methods-f…
FarhanAnjum-opti Jun 27, 2025
d2fc631
update: unit test fixes
FarhanAnjum-opti Jun 27, 2025
752a030
Merge branch 'farhan-anjum/FSSDK-11175-add-decision-service-methods-f…
FarhanAnjum-opti Jun 27, 2025
cbf2c2c
update: include CMAB UUID in activation and add corresponding tests
FarhanAnjum-opti Jun 27, 2025
fdcdfbf
update: add tests for get_variation with and without CMAB UUID
FarhanAnjum-opti Jun 27, 2025
b9a8555
Revert "update: unit test fixes"
FarhanAnjum-opti Jun 30, 2025
a129854
Revert "update: modify get_variation to return VariationResult and ad…
FarhanAnjum-opti Jun 30, 2025
9d63477
Merge branch 'farhan-anjum/FSSDK-11175-add-decision-service-methods-f…
FarhanAnjum-opti Jun 30, 2025
1f7e2a9
update: make cmab_uuid parameter optional in _send_impression_event m…
FarhanAnjum-opti Jun 30, 2025
73a2802
chore: trigger CI by turning on python flag
FarhanAnjum-opti Jun 30, 2025
a6d9771
update: new class method to handle optimizely error decisions
FarhanAnjum-opti Jun 30, 2025
4743376
fix unit test
FarhanAnjum-opti Jun 30, 2025
6d79053
fix: update error logging format for CMAB fetch failures
FarhanAnjum-opti Jun 30, 2025
3c903c7
chore: trigger CI
FarhanAnjum-opti Jul 1, 2025
c637878
update: enhance decision service to handle error states and improve b…
FarhanAnjum-opti Jul 3, 2025
62bbf64
Merge branch 'farhan-anjum/FSSDK-11175-add-decision-service-methods-f…
FarhanAnjum-opti Jul 3, 2025
0bc4fbd
update: remove debug print statement from Optimizely class
FarhanAnjum-opti Jul 3, 2025
8668565
Merge branch 'farhan-anjum/FSSDK-11175-add-decision-service-methods-f…
FarhanAnjum-opti Jul 3, 2025
fcdad1f
update: enhance bucketing logic to support CMAB traffic allocations
FarhanAnjum-opti Jul 3, 2025
dd4790d
Merge branch 'farhan-anjum/FSSDK-11175-add-decision-service-methods-f…
FarhanAnjum-opti Jul 3, 2025
aca7df4
update: improve error logging for CMAB decision fetch failures
FarhanAnjum-opti Jul 3, 2025
c14b768
Merge branch 'farhan-anjum/FSSDK-11175-add-decision-service-methods-f…
FarhanAnjum-opti Jul 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions optimizely/bucketer.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,34 @@ def bucket(
and array of log messages representing decision making.
*/.
"""
variation_id, decide_reasons = self.bucket_to_entity_id(project_config, experiment, user_id, bucketing_id)
if variation_id:
variation = project_config.get_variation_from_id_by_experiment_id(experiment.id, variation_id)
return variation, decide_reasons

elif not decide_reasons:
message = 'Bucketed into an empty traffic range. Returning nil.'
project_config.logger.info(message)
decide_reasons.append(message)

return None, decide_reasons

def bucket_to_entity_id(
self, project_config: ProjectConfig,
experiment: Experiment, user_id: str, bucketing_id: str
) -> tuple[Optional[str], list[str]]:
"""
For a given experiment and bucketing ID determines variation ID to be shown to user.

Args:
project_config: Instance of ProjectConfig.
experiment: The experiment object (used for group/groupPolicy logic if needed).
user_id: The user ID string.
bucketing_id: The bucketing ID string for the user.

Returns:
Tuple of (entity_id or None, list of decide reasons).
"""
decide_reasons: list[str] = []
if not experiment:
return None, decide_reasons
Expand Down Expand Up @@ -151,16 +179,16 @@ def bucket(
project_config.logger.info(message)
decide_reasons.append(message)

traffic_allocations: list[TrafficAllocation] = experiment.trafficAllocation
if experiment.cmab:
traffic_allocations = [
{
"entityId": "$",
"endOfRange": experiment.cmab['trafficAllocation']
}
]
# Bucket user if not in white-list and in group (if any)
variation_id = self.find_bucket(project_config, bucketing_id,
experiment.id, experiment.trafficAllocation)
if variation_id:
variation = project_config.get_variation_from_id_by_experiment_id(experiment.id, variation_id)
return variation, decide_reasons

else:
message = 'Bucketed into an empty traffic range. Returning nil.'
project_config.logger.info(message)
decide_reasons.append(message)
experiment.id, traffic_allocations)

return None, decide_reasons
return variation_id, decide_reasons
12 changes: 12 additions & 0 deletions optimizely/cmab/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright 2025, Optimizely
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
12 changes: 12 additions & 0 deletions optimizely/cmab/cmab_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ class CmabCacheValue(TypedDict):


class DefaultCmabService:
"""
DefaultCmabService handles decisioning for Contextual Multi-Armed Bandit (CMAB) experiments,
including caching and filtering user attributes for efficient decision retrieval.
Attributes:
cmab_cache: LRUCache for user CMAB decisions.
cmab_client: Client to fetch decisions from the CMAB backend.
logger: Optional logger.
Methods:
get_decision: Retrieves a CMAB decision with caching and attribute filtering.
"""
def __init__(self, cmab_cache: LRUCache[str, CmabCacheValue],
cmab_client: DefaultCmabClient, logger: Optional[_logging.Logger] = None):
self.cmab_cache = cmab_cache
Expand Down
20 changes: 20 additions & 0 deletions optimizely/decision/optimizely_decision.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,23 @@ def as_json(self) -> dict[str, Any]:
'user_context': self.user_context.as_json() if self.user_context else None,
'reasons': self.reasons
}

@classmethod
def new_error_decision(cls, key: str, user: OptimizelyUserContext, reasons: list[str]) -> OptimizelyDecision:
"""Create a new OptimizelyDecision representing an error state.
Args:
key: The flag key
user: The user context
reasons: List of reasons explaining the error
Returns:
OptimizelyDecision with error state values
"""
return cls(
variation_key=None,
enabled=False,
variables={},
rule_key=None,
flag_key=key,
user_context=user,
reasons=[reasons[-1]] if reasons else []
)
Loading