wingify-fme-python-sdk 1.70.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tests/data/__init__.py +13 -0
- tests/data/dummy_test_data_reader.py +63 -0
- tests/data/user_storage.py +48 -0
- tests/e2e/__init__.py +13 -0
- tests/e2e/custom_bucketing_test.py +514 -0
- tests/e2e/force_users_test.py +368 -0
- tests/e2e/get_flag_test.py +195 -0
- tests/unit/__init__.py +13 -0
- tests/unit/packages/__init__.py +13 -0
- tests/unit/packages/decision_maker/__init__.py +13 -0
- tests/unit/packages/decision_maker/decision_maker_test.py +83 -0
- tests/unit/packages/segmentation_evaluator/__init__.py +13 -0
- tests/unit/packages/segmentation_evaluator/segmentation_evaluator_test.py +284 -0
- tests/unit/packages/segmentation_evaluator/segmentation_manager_test.py +215 -0
- tests/unit/packages/segmentation_evaluator/web_testing_segment_test.py +216 -0
- tests/unit/services/__init__.py +0 -0
- tests/unit/services/hooks_manager_test.py +80 -0
- tests/unit/services/internal_events_sampling_service_test.py +160 -0
- tests/unit/settings/__init__.py +13 -0
- tests/unit/settings/settings_schema_validation_test.py +67 -0
- tests/unit/utils/__init__.py +0 -0
- tests/unit/utils/campaign_util_test.py +248 -0
- tests/unit/utils/decision_util_force_users_test.py +790 -0
- tests/unit/utils/log_message_test.py +65 -0
- tests/unit/vwo_wingify_test.py +175 -0
- tests/unit/wingify_client_validation_test.py +72 -0
- vwo/__init__.py +35 -0
- vwo/models/__init__.py +13 -0
- vwo/models/batch_event_data.py +19 -0
- vwo/models/user/__init__.py +13 -0
- vwo/models/user/context_vwo_model.py +21 -0
- vwo/models/user/get_flag.py +19 -0
- vwo/models/vwo_options_model.py +21 -0
- vwo/packages/__init__.py +13 -0
- vwo/packages/logger/__init__.py +13 -0
- vwo/packages/logger/enums/__init__.py +13 -0
- vwo/packages/logger/enums/log_level_enum.py +19 -0
- vwo/packages/storage/__init__.py +13 -0
- vwo/packages/storage/connector.py +19 -0
- vwo/vwo.py +58 -0
- vwo/vwo_builder.py +21 -0
- vwo/vwo_client.py +22 -0
- wingify/__init__.py +36 -0
- wingify/api/__init__.py +13 -0
- wingify/api/get_flag_api.py +727 -0
- wingify/api/set_attribute_api.py +60 -0
- wingify/api/track_api.py +99 -0
- wingify/constants/Constants.py +94 -0
- wingify/constants/__init__.py +13 -0
- wingify/constants/sampled_debug_error_template_keys.py +34 -0
- wingify/decorators/__init__.py +13 -0
- wingify/decorators/storage_decorator.py +92 -0
- wingify/enums/__init__.py +13 -0
- wingify/enums/api_enum.py +26 -0
- wingify/enums/campaign_type_enum.py +22 -0
- wingify/enums/debug_category_enum.py +24 -0
- wingify/enums/event_enum.py +26 -0
- wingify/enums/headers_enum.py +21 -0
- wingify/enums/hooks_enum.py +24 -0
- wingify/enums/status_enum.py +21 -0
- wingify/enums/storage_enum.py +25 -0
- wingify/enums/url_enum.py +29 -0
- wingify/models/Alias.py +60 -0
- wingify/models/AliasSetResponse.py +43 -0
- wingify/models/__init__.py +13 -0
- wingify/models/batch_event_data.py +68 -0
- wingify/models/campaign/__init__.py +13 -0
- wingify/models/campaign/campaign_model.py +140 -0
- wingify/models/campaign/feature_model.py +95 -0
- wingify/models/campaign/holdout_model.py +94 -0
- wingify/models/campaign/impact_campaign_model.py +60 -0
- wingify/models/campaign/metric_model.py +42 -0
- wingify/models/campaign/rule_model.py +42 -0
- wingify/models/campaign/variable_model.py +44 -0
- wingify/models/campaign/variation_model.py +109 -0
- wingify/models/gateway_service_model.py +48 -0
- wingify/models/schemas/__init__.py +13 -0
- wingify/models/schemas/campaign_group_schema.py +19 -0
- wingify/models/schemas/campaign_metric_schema.py +26 -0
- wingify/models/schemas/campaign_schema.py +37 -0
- wingify/models/schemas/campaign_variation_schema.py +34 -0
- wingify/models/schemas/empty_object_schema.py +15 -0
- wingify/models/schemas/feature_schema.py +38 -0
- wingify/models/schemas/group_schema.py +37 -0
- wingify/models/schemas/holdout_schema.py +29 -0
- wingify/models/schemas/rule_schema.py +24 -0
- wingify/models/schemas/settings_schema.py +47 -0
- wingify/models/schemas/variable_schema.py +24 -0
- wingify/models/settings/__init__.py +13 -0
- wingify/models/settings/internal_event_sampling_settings_model.py +53 -0
- wingify/models/settings/settings_model.py +127 -0
- wingify/models/storage/__init__.py +13 -0
- wingify/models/storage/storage_model.py +84 -0
- wingify/models/user/__init__.py +13 -0
- wingify/models/user/context_model.py +113 -0
- wingify/models/user/context_vwo_model.py +34 -0
- wingify/models/user/get_flag.py +47 -0
- wingify/models/vwo_options_model.py +21 -0
- wingify/models/wingify_options_model.py +129 -0
- wingify/packages/__init__.py +13 -0
- wingify/packages/decision_maker/__init__.py +13 -0
- wingify/packages/decision_maker/decision_maker.py +89 -0
- wingify/packages/logger/__init__.py +13 -0
- wingify/packages/logger/core/__init__.py +13 -0
- wingify/packages/logger/core/log_manager.py +123 -0
- wingify/packages/logger/core/log_transport_manager.py +57 -0
- wingify/packages/logger/enums/__init__.py +13 -0
- wingify/packages/logger/enums/log_level_enum.py +24 -0
- wingify/packages/logger/enums/log_level_number_enum.py +21 -0
- wingify/packages/logger/log_message_builder.py +70 -0
- wingify/packages/logger/logger.py +38 -0
- wingify/packages/logger/transports/__init__.py +13 -0
- wingify/packages/logger/transports/console_transport.py +43 -0
- wingify/packages/network_layer/__init__.py +13 -0
- wingify/packages/network_layer/client/__init__.py +13 -0
- wingify/packages/network_layer/client/network_client.py +167 -0
- wingify/packages/network_layer/handlers/__init__.py +13 -0
- wingify/packages/network_layer/handlers/request_handler.py +40 -0
- wingify/packages/network_layer/manager/__init__.py +13 -0
- wingify/packages/network_layer/manager/network_manager.py +79 -0
- wingify/packages/network_layer/models/__init__.py +13 -0
- wingify/packages/network_layer/models/global_request_model.py +70 -0
- wingify/packages/network_layer/models/request_model.py +133 -0
- wingify/packages/network_layer/models/response_model.py +55 -0
- wingify/packages/segmentation_evaluator/__init__.py +13 -0
- wingify/packages/segmentation_evaluator/core/__init__.py +13 -0
- wingify/packages/segmentation_evaluator/core/segmentation_manager.py +140 -0
- wingify/packages/segmentation_evaluator/enums/__init__.py +13 -0
- wingify/packages/segmentation_evaluator/enums/segment_operand_regex_enum.py +31 -0
- wingify/packages/segmentation_evaluator/enums/segment_operand_value_enum.py +29 -0
- wingify/packages/segmentation_evaluator/enums/segment_operator_value_enum.py +37 -0
- wingify/packages/segmentation_evaluator/evaluators/__init__.py +13 -0
- wingify/packages/segmentation_evaluator/evaluators/segment_evaluator.py +237 -0
- wingify/packages/segmentation_evaluator/evaluators/segment_operand_evaluator.py +518 -0
- wingify/packages/segmentation_evaluator/utils/__init__.py +13 -0
- wingify/packages/segmentation_evaluator/utils/segment_util.py +33 -0
- wingify/packages/segmentation_evaluator/utils/web_testing_segment_util.py +295 -0
- wingify/packages/storage/__init__.py +13 -0
- wingify/packages/storage/connector.py +25 -0
- wingify/packages/storage/storage.py +39 -0
- wingify/resources/__init__.py +13 -0
- wingify/resources/debug-messages.json +21 -0
- wingify/resources/error-messages.json +62 -0
- wingify/resources/info-message.json +47 -0
- wingify/resources/trace-messages.json +1 -0
- wingify/resources/warn-messages.json +1 -0
- wingify/services/__init__.py +13 -0
- wingify/services/batch_event_queue.py +140 -0
- wingify/services/campaign_decision_service.py +268 -0
- wingify/services/hooks_manager.py +65 -0
- wingify/services/internal_events_sampling_service.py +62 -0
- wingify/services/settings_manager.py +293 -0
- wingify/services/storage_service.py +58 -0
- wingify/services/url_service.py +51 -0
- wingify/utils/__init__.py +13 -0
- wingify/utils/aliasing_util.py +108 -0
- wingify/utils/brand_context.py +26 -0
- wingify/utils/brand_util.py +37 -0
- wingify/utils/campaign_util.py +592 -0
- wingify/utils/data_type_util.py +125 -0
- wingify/utils/debugger_service_util.py +58 -0
- wingify/utils/decision_util.py +589 -0
- wingify/utils/event_util.py +85 -0
- wingify/utils/function_util.py +188 -0
- wingify/utils/gateway_service_util.py +179 -0
- wingify/utils/holdout_util.py +264 -0
- wingify/utils/impression_util.py +151 -0
- wingify/utils/internal_events_sampling_util.py +106 -0
- wingify/utils/log_message_util.py +95 -0
- wingify/utils/meg_util.py +561 -0
- wingify/utils/model_utils.py +136 -0
- wingify/utils/network_util.py +706 -0
- wingify/utils/rule_evaluation_util.py +102 -0
- wingify/utils/settings_util.py +39 -0
- wingify/utils/usage_stats_util.py +116 -0
- wingify/utils/uuid_util.py +81 -0
- wingify/vwo_client.py +21 -0
- wingify/wingify.py +159 -0
- wingify/wingify_builder.py +282 -0
- wingify/wingify_client.py +520 -0
- wingify_fme_python_sdk-1.70.0.dist-info/METADATA +1415 -0
- wingify_fme_python_sdk-1.70.0.dist-info/RECORD +186 -0
- wingify_fme_python_sdk-1.70.0.dist-info/WHEEL +5 -0
- wingify_fme_python_sdk-1.70.0.dist-info/licenses/LICENSE +202 -0
- wingify_fme_python_sdk-1.70.0.dist-info/licenses/NOTICE +18 -0
- wingify_fme_python_sdk-1.70.0.dist-info/top_level.txt +3 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# Copyright 2024 Wingify Software Pvt. Ltd.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
import threading
|
|
17
|
+
import queue
|
|
18
|
+
from typing import Dict, Any
|
|
19
|
+
from wingify.packages.network_layer.manager.network_manager import NetworkManager
|
|
20
|
+
from ..utils.network_util import send_post_batch_request
|
|
21
|
+
from ..utils.log_message_util import error_messages, info_messages
|
|
22
|
+
from wingify.packages.logger.core.log_manager import LogManager
|
|
23
|
+
from ..enums.api_enum import ApiEnum
|
|
24
|
+
|
|
25
|
+
class BatchEventQueue:
|
|
26
|
+
MAX_EVENTS_PER_REQUEST = 5000
|
|
27
|
+
|
|
28
|
+
def __init__(self, events_per_request: int, request_time_interval: int, account_id: int, sdk_key: str, flush_callback=None):
|
|
29
|
+
"""
|
|
30
|
+
Initializes the batch event queue with the provided parameters.
|
|
31
|
+
|
|
32
|
+
:param events_per_request: The number of events to send per batch.
|
|
33
|
+
:param request_time_interval: The time interval (in seconds) between batch requests.
|
|
34
|
+
:param account_id: The account ID for the batch events.
|
|
35
|
+
:param sdk_key: The SDK key for the batch events.
|
|
36
|
+
:param flush_callback: Optional callback function that gets called when the queue is flushed.
|
|
37
|
+
"""
|
|
38
|
+
self.batch_queue = []
|
|
39
|
+
self.events_per_request = events_per_request
|
|
40
|
+
self.request_time_interval = request_time_interval
|
|
41
|
+
self.account_id = account_id
|
|
42
|
+
self.sdk_key = sdk_key
|
|
43
|
+
self.is_batch_processing = False
|
|
44
|
+
self.lock = threading.Lock()
|
|
45
|
+
self.flush_callback = flush_callback
|
|
46
|
+
|
|
47
|
+
self.create_new_batch_timer()
|
|
48
|
+
|
|
49
|
+
def enqueue(self, event_data: Dict[str, Any]):
|
|
50
|
+
with self.lock:
|
|
51
|
+
self.batch_queue.append(event_data)
|
|
52
|
+
# Print the current batch queue size
|
|
53
|
+
LogManager.get_instance().info(
|
|
54
|
+
info_messages.get('BATCH_QUEUE_SIZE').format(
|
|
55
|
+
size=len(self.batch_queue)
|
|
56
|
+
)
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
# If batch size reaches the limit, trigger flush
|
|
60
|
+
if len(self.batch_queue) >= self.events_per_request:
|
|
61
|
+
self.flush()
|
|
62
|
+
|
|
63
|
+
def create_new_batch_timer(self):
|
|
64
|
+
"""Create a timer to flush the batch queue at the specified interval."""
|
|
65
|
+
self.timer = threading.Timer(self.request_time_interval, self.flush)
|
|
66
|
+
self.timer.start()
|
|
67
|
+
|
|
68
|
+
def clear_request_timer(self):
|
|
69
|
+
"""
|
|
70
|
+
Cancels the timer if it exists.
|
|
71
|
+
"""
|
|
72
|
+
if self.timer:
|
|
73
|
+
self.timer.cancel() # Cancel the ongoing timer
|
|
74
|
+
|
|
75
|
+
def flush_and_clear_timer(self):
|
|
76
|
+
"""
|
|
77
|
+
Flushes the queue and clears the timer.
|
|
78
|
+
"""
|
|
79
|
+
flush_result = self.flush(manual=True)
|
|
80
|
+
if isinstance(self.timer, threading.Timer):
|
|
81
|
+
self.clear_request_timer() # Cancel the request timer
|
|
82
|
+
|
|
83
|
+
return flush_result
|
|
84
|
+
|
|
85
|
+
def flush(self, manual=False):
|
|
86
|
+
self.is_batch_processing = True
|
|
87
|
+
if manual:
|
|
88
|
+
LogManager.get_instance().info(
|
|
89
|
+
info_messages.get('BATCH_FLUSH_MANUAL')
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
events_to_send = self.batch_queue[:]
|
|
94
|
+
self.batch_queue.clear()
|
|
95
|
+
|
|
96
|
+
# Log before sending batch events
|
|
97
|
+
LogManager.get_instance().info(
|
|
98
|
+
info_messages.get('BATCH_FLUSH_STARTED').format(
|
|
99
|
+
eventCount=len(events_to_send)
|
|
100
|
+
)
|
|
101
|
+
)
|
|
102
|
+
network_instance = NetworkManager.get_instance()
|
|
103
|
+
is_sent_successfully = False
|
|
104
|
+
|
|
105
|
+
# Use background thread to handle the batch event sending
|
|
106
|
+
def send_request():
|
|
107
|
+
try:
|
|
108
|
+
is_sent_successfully = self.send_batch_events(events_to_send)
|
|
109
|
+
if is_sent_successfully:
|
|
110
|
+
LogManager.get_instance().info(
|
|
111
|
+
info_messages.get('BATCH_FLUSH_SUCCESS').format(
|
|
112
|
+
eventCount=len(events_to_send)
|
|
113
|
+
)
|
|
114
|
+
)
|
|
115
|
+
else:
|
|
116
|
+
LogManager.get_instance().error(
|
|
117
|
+
error_messages.get("BATCH_FLUSH_FAILED")
|
|
118
|
+
)
|
|
119
|
+
self.batch_queue.extend(events_to_send)
|
|
120
|
+
except Exception as e:
|
|
121
|
+
LogManager.get_instance().error_log("ERROR_FLUSHING_BATCH_EVENTS", data={"error": str(e)}, debug_data={"an": ApiEnum.FLUSH_EVENTS.value})
|
|
122
|
+
self.batch_queue.extend(events_to_send)
|
|
123
|
+
finally:
|
|
124
|
+
# Reset the flag after flush
|
|
125
|
+
self.is_batch_processing = False
|
|
126
|
+
|
|
127
|
+
# Execute the batch sending in the background thread
|
|
128
|
+
network_instance.execute_in_background(send_request)
|
|
129
|
+
self.clear_request_timer()
|
|
130
|
+
self.create_new_batch_timer()
|
|
131
|
+
return is_sent_successfully
|
|
132
|
+
|
|
133
|
+
def send_batch_events(self, events):
|
|
134
|
+
""" Send the batch events asynchronously. """
|
|
135
|
+
try:
|
|
136
|
+
is_sent_successfully = send_post_batch_request(events, self.account_id, self.sdk_key, self.flush_callback)
|
|
137
|
+
return is_sent_successfully
|
|
138
|
+
except Exception as ex:
|
|
139
|
+
LogManager.get_instance().error_log("ERROR_FLUSHING_BATCH_EVENTS", data={"error": str(ex)}, debug_data={"an": ApiEnum.FLUSH_EVENTS.value})
|
|
140
|
+
return False
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
# Copyright 2024-2026 Wingify Software Pvt. Ltd.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
from ..models.campaign.campaign_model import CampaignModel
|
|
17
|
+
from ..enums.campaign_type_enum import CampaignTypeEnum
|
|
18
|
+
from ..models.campaign.variation_model import VariationModel
|
|
19
|
+
from ..packages.logger.core.log_manager import LogManager
|
|
20
|
+
from ..utils.log_message_util import error_messages, debug_messages, info_messages
|
|
21
|
+
from ..constants.Constants import Constants
|
|
22
|
+
from ..packages.decision_maker.decision_maker import DecisionMaker
|
|
23
|
+
from ..models.user.context_model import ContextModel
|
|
24
|
+
from ..packages.segmentation_evaluator.core.segmentation_manager import (
|
|
25
|
+
SegmentationManager,
|
|
26
|
+
)
|
|
27
|
+
from ..utils.data_type_util import is_object
|
|
28
|
+
from ..models.vwo_options_model import VWOOptionsModel
|
|
29
|
+
from typing import List
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class CampaignDecisionService:
|
|
33
|
+
def __init__(self):
|
|
34
|
+
self._decision_maker = DecisionMaker()
|
|
35
|
+
|
|
36
|
+
def is_user_part_of_campaign(self, context: ContextModel, campaign: CampaignModel) -> bool:
|
|
37
|
+
"""
|
|
38
|
+
Calculate if this user should become part of the campaign or not.
|
|
39
|
+
|
|
40
|
+
:param context: the context model containing user ID and custom bucketing seed
|
|
41
|
+
:param campaign: campaign model for getting the value of traffic allotted to the campaign
|
|
42
|
+
:return: True if User is a part of Campaign, False otherwise
|
|
43
|
+
"""
|
|
44
|
+
from ..utils.campaign_util import get_bucketing_id_for_user
|
|
45
|
+
|
|
46
|
+
user_id = context.get_id()
|
|
47
|
+
if not campaign or not user_id:
|
|
48
|
+
return False
|
|
49
|
+
|
|
50
|
+
bucketing_id = get_bucketing_id_for_user(context)
|
|
51
|
+
|
|
52
|
+
is_rollout_or_personalize = campaign.get_type() in [
|
|
53
|
+
CampaignTypeEnum.ROLLOUT.value,
|
|
54
|
+
CampaignTypeEnum.PERSONALIZE.value,
|
|
55
|
+
]
|
|
56
|
+
|
|
57
|
+
# Get salt and traffic allocation based on campaign type
|
|
58
|
+
if is_rollout_or_personalize:
|
|
59
|
+
first_variation = campaign.get_variations()[0]
|
|
60
|
+
salt = first_variation.get_salt()
|
|
61
|
+
traffic_allocation = first_variation.get_weight()
|
|
62
|
+
else:
|
|
63
|
+
salt = campaign.get_salt()
|
|
64
|
+
traffic_allocation = campaign.get_percent_traffic()
|
|
65
|
+
|
|
66
|
+
# Generate bucket key using bucketing_id (custom seed or user_id)
|
|
67
|
+
bucket_key = f"{salt}_{bucketing_id}" if salt else f"{campaign.get_id()}_{bucketing_id}"
|
|
68
|
+
|
|
69
|
+
value_assigned_to_user = self._decision_maker.get_bucket_value_for_user(
|
|
70
|
+
bucket_key
|
|
71
|
+
)
|
|
72
|
+
is_user_part = (
|
|
73
|
+
value_assigned_to_user != 0 and value_assigned_to_user <= traffic_allocation
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
LogManager.get_instance().info(
|
|
77
|
+
info_messages.get("USER_PART_OF_CAMPAIGN").format(
|
|
78
|
+
userId=f"{user_id} (Seed: {bucketing_id})" if (bucketing_id != user_id) else user_id,
|
|
79
|
+
notPart="" if is_user_part else "not",
|
|
80
|
+
campaignKey=(
|
|
81
|
+
campaign.get_rule_key()
|
|
82
|
+
if campaign.get_type() == CampaignTypeEnum.AB.value
|
|
83
|
+
else campaign.get_name() + "_" + campaign.get_rule_key()
|
|
84
|
+
),
|
|
85
|
+
)
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
return is_user_part
|
|
89
|
+
|
|
90
|
+
def get_variation(
|
|
91
|
+
self, variations: List[VariationModel], bucket_value: int
|
|
92
|
+
) -> VariationModel:
|
|
93
|
+
"""
|
|
94
|
+
Returns the Variation by checking the Start and End Bucket Allocations of each Variation.
|
|
95
|
+
|
|
96
|
+
:param variations: List of variations in the campaign
|
|
97
|
+
:param bucket_value: the bucket Value of the user
|
|
98
|
+
:return: VariationModel allotted to the user or None if not found
|
|
99
|
+
"""
|
|
100
|
+
for variation in variations:
|
|
101
|
+
if (
|
|
102
|
+
variation.get_start_range_variation()
|
|
103
|
+
<= bucket_value
|
|
104
|
+
<= variation.get_end_range_variation()
|
|
105
|
+
):
|
|
106
|
+
return variation
|
|
107
|
+
|
|
108
|
+
return None
|
|
109
|
+
|
|
110
|
+
def check_in_range(
|
|
111
|
+
self, variation: VariationModel, bucket_value: int
|
|
112
|
+
) -> VariationModel:
|
|
113
|
+
"""
|
|
114
|
+
Checks if the bucket value is in the range of the given variation.
|
|
115
|
+
|
|
116
|
+
:param variation: Variation to check against
|
|
117
|
+
:param bucket_value: The bucket value of the user
|
|
118
|
+
:return: The variation if in range, None otherwise
|
|
119
|
+
"""
|
|
120
|
+
if (
|
|
121
|
+
variation.get_start_range_variation()
|
|
122
|
+
<= bucket_value
|
|
123
|
+
<= variation.get_end_range_variation()
|
|
124
|
+
):
|
|
125
|
+
return variation
|
|
126
|
+
return None
|
|
127
|
+
|
|
128
|
+
def bucket_user_to_variation(
|
|
129
|
+
self, context: ContextModel, account_id: str, campaign: CampaignModel
|
|
130
|
+
) -> VariationModel:
|
|
131
|
+
"""
|
|
132
|
+
Validates the User ID and generates Variation into which the User is bucketed in.
|
|
133
|
+
|
|
134
|
+
:param context: the context model containing user ID and custom bucketing seed
|
|
135
|
+
:param account_id: the unique ID assigned to Account
|
|
136
|
+
:param campaign: the Campaign of which User is a part of
|
|
137
|
+
:return: VariationModel into which user is bucketed in or None if not
|
|
138
|
+
"""
|
|
139
|
+
from ..utils.campaign_util import get_bucketing_id_for_user
|
|
140
|
+
|
|
141
|
+
user_id = context.get_id()
|
|
142
|
+
|
|
143
|
+
bucketing_id = get_bucketing_id_for_user(context)
|
|
144
|
+
|
|
145
|
+
if not campaign or not bucketing_id:
|
|
146
|
+
return None
|
|
147
|
+
|
|
148
|
+
multiplier = 1 if campaign.get_percent_traffic() else None
|
|
149
|
+
percent_traffic = campaign.get_percent_traffic()
|
|
150
|
+
salt = campaign.get_salt()
|
|
151
|
+
if salt:
|
|
152
|
+
bucket_key = f"{salt}_{account_id}_{bucketing_id}"
|
|
153
|
+
else:
|
|
154
|
+
bucket_key = f"{campaign.get_id()}_{account_id}_{bucketing_id}"
|
|
155
|
+
hash_value = self._decision_maker.generate_hash_value(bucket_key)
|
|
156
|
+
bucket_value = self._decision_maker.generate_bucket_value(
|
|
157
|
+
hash_value, Constants.MAX_TRAFFIC_VALUE, multiplier
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
LogManager.get_instance().debug(
|
|
161
|
+
debug_messages.get("USER_BUCKET_TO_VARIATION").format(
|
|
162
|
+
userId=f"{user_id} (Seed: {bucketing_id})" if (bucketing_id != user_id) else user_id,
|
|
163
|
+
campaignKey=(
|
|
164
|
+
campaign.get_rule_key()
|
|
165
|
+
if campaign.get_type() == CampaignTypeEnum.AB.value
|
|
166
|
+
else campaign.get_name() + "_" + campaign.get_rule_key()
|
|
167
|
+
),
|
|
168
|
+
percentTraffic=percent_traffic,
|
|
169
|
+
bucketValue=bucket_value,
|
|
170
|
+
hashValue=hash_value,
|
|
171
|
+
)
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
return self.get_variation(campaign.get_variations(), bucket_value)
|
|
175
|
+
|
|
176
|
+
def get_pre_segmentation_decision(
|
|
177
|
+
self, campaign: CampaignModel, context: ContextModel
|
|
178
|
+
) -> bool:
|
|
179
|
+
"""
|
|
180
|
+
Validates the pre-segmentation decision for the campaign and context.
|
|
181
|
+
|
|
182
|
+
:param campaign: CampaignModel containing campaign data
|
|
183
|
+
:param context: ContextModel containing user context data
|
|
184
|
+
:return: True if segmentation is valid, False otherwise
|
|
185
|
+
"""
|
|
186
|
+
campaign_type = campaign.get_type()
|
|
187
|
+
segments = {}
|
|
188
|
+
|
|
189
|
+
if campaign_type in [
|
|
190
|
+
CampaignTypeEnum.ROLLOUT.value,
|
|
191
|
+
CampaignTypeEnum.PERSONALIZE.value,
|
|
192
|
+
]:
|
|
193
|
+
segments = campaign.get_variations()[0].get_segments()
|
|
194
|
+
elif campaign_type == CampaignTypeEnum.AB.value:
|
|
195
|
+
segments = campaign.get_segments()
|
|
196
|
+
|
|
197
|
+
if is_object(segments) and not segments:
|
|
198
|
+
LogManager.get_instance().info(
|
|
199
|
+
info_messages.get("SEGMENTATION_SKIP").format(
|
|
200
|
+
userId=context.get_id(),
|
|
201
|
+
campaignKey=(
|
|
202
|
+
campaign.get_rule_key()
|
|
203
|
+
if campaign.get_type() == CampaignTypeEnum.AB.value
|
|
204
|
+
else campaign.get_name() + "_" + campaign.get_rule_key()
|
|
205
|
+
),
|
|
206
|
+
)
|
|
207
|
+
)
|
|
208
|
+
return True
|
|
209
|
+
else:
|
|
210
|
+
pre_segmentation_result = (
|
|
211
|
+
SegmentationManager.get_instance().validate_segmentation(
|
|
212
|
+
segments, context.get_custom_variables()
|
|
213
|
+
)
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
if not pre_segmentation_result:
|
|
217
|
+
LogManager.get_instance().info(
|
|
218
|
+
info_messages.get("SEGMENTATION_STATUS").format(
|
|
219
|
+
userId=context.get_id(),
|
|
220
|
+
campaignKey=(
|
|
221
|
+
campaign.get_rule_key()
|
|
222
|
+
if campaign.get_type() == CampaignTypeEnum.AB.value
|
|
223
|
+
else campaign.get_name() + "_" + campaign.get_rule_key()
|
|
224
|
+
),
|
|
225
|
+
status="failed",
|
|
226
|
+
)
|
|
227
|
+
)
|
|
228
|
+
return False
|
|
229
|
+
|
|
230
|
+
LogManager.get_instance().info(
|
|
231
|
+
info_messages.get("SEGMENTATION_STATUS").format(
|
|
232
|
+
userId=context.get_id(),
|
|
233
|
+
campaignKey=(
|
|
234
|
+
campaign.get_rule_key()
|
|
235
|
+
if campaign.get_type() == CampaignTypeEnum.AB.value
|
|
236
|
+
else campaign.get_name() + "_" + campaign.get_rule_key()
|
|
237
|
+
),
|
|
238
|
+
status="passed",
|
|
239
|
+
)
|
|
240
|
+
)
|
|
241
|
+
return True
|
|
242
|
+
|
|
243
|
+
def get_variation_alloted(
|
|
244
|
+
self, context: ContextModel, account_id: str, campaign: CampaignModel
|
|
245
|
+
) -> VariationModel:
|
|
246
|
+
"""
|
|
247
|
+
Determines the variation allocated to the user for a given campaign.
|
|
248
|
+
|
|
249
|
+
:param context: the context model containing user ID and custom bucketing seed
|
|
250
|
+
:param account_id: the unique ID assigned to Account
|
|
251
|
+
:param campaign: the Campaign of which User is a part of
|
|
252
|
+
:return: The allocated VariationModel or None if not allocated
|
|
253
|
+
"""
|
|
254
|
+
is_user_part = self.is_user_part_of_campaign(context, campaign)
|
|
255
|
+
|
|
256
|
+
if campaign.get_type() in [
|
|
257
|
+
CampaignTypeEnum.ROLLOUT.value,
|
|
258
|
+
CampaignTypeEnum.PERSONALIZE.value,
|
|
259
|
+
]:
|
|
260
|
+
if is_user_part:
|
|
261
|
+
return campaign.get_variations()[0]
|
|
262
|
+
else:
|
|
263
|
+
return None
|
|
264
|
+
else:
|
|
265
|
+
if is_user_part:
|
|
266
|
+
return self.bucket_user_to_variation(context, account_id, campaign)
|
|
267
|
+
else:
|
|
268
|
+
return None
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# Copyright 2024-2026 Wingify Software Pvt. Ltd.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
from typing import Callable, Dict, Any, Optional
|
|
17
|
+
from ..packages.logger.core.log_manager import LogManager
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class HooksManager:
|
|
21
|
+
def __init__(self, options: Dict[str, Any]):
|
|
22
|
+
"""
|
|
23
|
+
Initialize the HooksManager with the given options.
|
|
24
|
+
|
|
25
|
+
:param options: A dictionary containing configuration options.
|
|
26
|
+
"""
|
|
27
|
+
self.callback: Optional[Callable[[Dict[str, Any]], None]] = options.get(
|
|
28
|
+
"integrations", {}
|
|
29
|
+
).get("callback")
|
|
30
|
+
self.is_callback_function = callable(self.callback)
|
|
31
|
+
self.decision: Dict[str, Any] = {}
|
|
32
|
+
|
|
33
|
+
def execute(self, properties: Dict[str, Any]) -> None:
|
|
34
|
+
"""
|
|
35
|
+
Executes the callback if it's a valid function.
|
|
36
|
+
|
|
37
|
+
:param properties: A dictionary of properties to pass to the callback.
|
|
38
|
+
"""
|
|
39
|
+
if self.is_callback_function and self.callback:
|
|
40
|
+
try:
|
|
41
|
+
self.callback(properties)
|
|
42
|
+
except Exception as err:
|
|
43
|
+
LogManager.get_instance().error_log(
|
|
44
|
+
"INTEGRATIONS_CALLBACK_FAILED",
|
|
45
|
+
data={"err": str(err)},
|
|
46
|
+
debug_data={"an": properties.get("api")},
|
|
47
|
+
should_send_log_to_vwo=False,
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
def set(self, properties: Dict[str, Any]) -> None:
|
|
51
|
+
"""
|
|
52
|
+
Sets properties to the decision object if the callback is a function.
|
|
53
|
+
|
|
54
|
+
:param properties: A dictionary of properties to set.
|
|
55
|
+
"""
|
|
56
|
+
if self.is_callback_function:
|
|
57
|
+
self.decision = properties
|
|
58
|
+
|
|
59
|
+
def get(self) -> Dict[str, Any]:
|
|
60
|
+
"""
|
|
61
|
+
Retrieves the decision object.
|
|
62
|
+
|
|
63
|
+
:return: The decision object as a dictionary.
|
|
64
|
+
"""
|
|
65
|
+
return self.decision
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Copyright 2024-2026 Wingify Software Pvt. Ltd.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
import random
|
|
16
|
+
from typing import Callable, Optional
|
|
17
|
+
from ..models.settings.settings_model import SettingsModel
|
|
18
|
+
from ..utils.internal_events_sampling_util import (
|
|
19
|
+
should_apply_sampling,
|
|
20
|
+
get_usage_stats_sampling_percent,
|
|
21
|
+
get_debug_event_sampling_percent,
|
|
22
|
+
passes_sampling_percent,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
class InternalEventsSamplingService:
|
|
26
|
+
"""
|
|
27
|
+
Applies sampling rules for internal SDK events:
|
|
28
|
+
`vwo_sdkUsageStats` and sampled `vwo_sdkDebug`.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
def __init__(self, random_value_provider: Optional[Callable[[], float]] = None):
|
|
32
|
+
"""
|
|
33
|
+
Creates a sampling service.
|
|
34
|
+
:param random_value_provider: Optional supplier returning a value in [0, 1) for sampling checks; overridable in tests.
|
|
35
|
+
"""
|
|
36
|
+
self._random_value_provider = random_value_provider if random_value_provider is not None else random.random
|
|
37
|
+
|
|
38
|
+
def should_send_usage_stats_event(self, settings: Optional[SettingsModel]) -> bool:
|
|
39
|
+
"""
|
|
40
|
+
Determines whether the usage-stats event should be sent.
|
|
41
|
+
On server, always sends unless `alwaysApplySampling.server` is enabled.
|
|
42
|
+
:param settings: Parsed settings from the server
|
|
43
|
+
:return: `True` when the usage-stats event should be sent
|
|
44
|
+
"""
|
|
45
|
+
if not should_apply_sampling(settings):
|
|
46
|
+
return True
|
|
47
|
+
|
|
48
|
+
usage_stats_sampling_percent = get_usage_stats_sampling_percent(settings)
|
|
49
|
+
return passes_sampling_percent(usage_stats_sampling_percent, self._random_value_provider())
|
|
50
|
+
|
|
51
|
+
def should_send_sampled_debug_event(self, settings: Optional[SettingsModel]) -> bool:
|
|
52
|
+
"""
|
|
53
|
+
Determines whether a sampled debug event should be sent.
|
|
54
|
+
Only called for `msg_t` keys in the sampled set; always-send keys bypass this.
|
|
55
|
+
:param settings: Parsed settings from the server
|
|
56
|
+
:return: `True` when the sampled debug event should be sent
|
|
57
|
+
"""
|
|
58
|
+
if not should_apply_sampling(settings):
|
|
59
|
+
return True
|
|
60
|
+
|
|
61
|
+
debug_sampling_percent = get_debug_event_sampling_percent(settings)
|
|
62
|
+
return passes_sampling_percent(debug_sampling_percent, self._random_value_provider())
|