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.
Files changed (186) hide show
  1. tests/data/__init__.py +13 -0
  2. tests/data/dummy_test_data_reader.py +63 -0
  3. tests/data/user_storage.py +48 -0
  4. tests/e2e/__init__.py +13 -0
  5. tests/e2e/custom_bucketing_test.py +514 -0
  6. tests/e2e/force_users_test.py +368 -0
  7. tests/e2e/get_flag_test.py +195 -0
  8. tests/unit/__init__.py +13 -0
  9. tests/unit/packages/__init__.py +13 -0
  10. tests/unit/packages/decision_maker/__init__.py +13 -0
  11. tests/unit/packages/decision_maker/decision_maker_test.py +83 -0
  12. tests/unit/packages/segmentation_evaluator/__init__.py +13 -0
  13. tests/unit/packages/segmentation_evaluator/segmentation_evaluator_test.py +284 -0
  14. tests/unit/packages/segmentation_evaluator/segmentation_manager_test.py +215 -0
  15. tests/unit/packages/segmentation_evaluator/web_testing_segment_test.py +216 -0
  16. tests/unit/services/__init__.py +0 -0
  17. tests/unit/services/hooks_manager_test.py +80 -0
  18. tests/unit/services/internal_events_sampling_service_test.py +160 -0
  19. tests/unit/settings/__init__.py +13 -0
  20. tests/unit/settings/settings_schema_validation_test.py +67 -0
  21. tests/unit/utils/__init__.py +0 -0
  22. tests/unit/utils/campaign_util_test.py +248 -0
  23. tests/unit/utils/decision_util_force_users_test.py +790 -0
  24. tests/unit/utils/log_message_test.py +65 -0
  25. tests/unit/vwo_wingify_test.py +175 -0
  26. tests/unit/wingify_client_validation_test.py +72 -0
  27. vwo/__init__.py +35 -0
  28. vwo/models/__init__.py +13 -0
  29. vwo/models/batch_event_data.py +19 -0
  30. vwo/models/user/__init__.py +13 -0
  31. vwo/models/user/context_vwo_model.py +21 -0
  32. vwo/models/user/get_flag.py +19 -0
  33. vwo/models/vwo_options_model.py +21 -0
  34. vwo/packages/__init__.py +13 -0
  35. vwo/packages/logger/__init__.py +13 -0
  36. vwo/packages/logger/enums/__init__.py +13 -0
  37. vwo/packages/logger/enums/log_level_enum.py +19 -0
  38. vwo/packages/storage/__init__.py +13 -0
  39. vwo/packages/storage/connector.py +19 -0
  40. vwo/vwo.py +58 -0
  41. vwo/vwo_builder.py +21 -0
  42. vwo/vwo_client.py +22 -0
  43. wingify/__init__.py +36 -0
  44. wingify/api/__init__.py +13 -0
  45. wingify/api/get_flag_api.py +727 -0
  46. wingify/api/set_attribute_api.py +60 -0
  47. wingify/api/track_api.py +99 -0
  48. wingify/constants/Constants.py +94 -0
  49. wingify/constants/__init__.py +13 -0
  50. wingify/constants/sampled_debug_error_template_keys.py +34 -0
  51. wingify/decorators/__init__.py +13 -0
  52. wingify/decorators/storage_decorator.py +92 -0
  53. wingify/enums/__init__.py +13 -0
  54. wingify/enums/api_enum.py +26 -0
  55. wingify/enums/campaign_type_enum.py +22 -0
  56. wingify/enums/debug_category_enum.py +24 -0
  57. wingify/enums/event_enum.py +26 -0
  58. wingify/enums/headers_enum.py +21 -0
  59. wingify/enums/hooks_enum.py +24 -0
  60. wingify/enums/status_enum.py +21 -0
  61. wingify/enums/storage_enum.py +25 -0
  62. wingify/enums/url_enum.py +29 -0
  63. wingify/models/Alias.py +60 -0
  64. wingify/models/AliasSetResponse.py +43 -0
  65. wingify/models/__init__.py +13 -0
  66. wingify/models/batch_event_data.py +68 -0
  67. wingify/models/campaign/__init__.py +13 -0
  68. wingify/models/campaign/campaign_model.py +140 -0
  69. wingify/models/campaign/feature_model.py +95 -0
  70. wingify/models/campaign/holdout_model.py +94 -0
  71. wingify/models/campaign/impact_campaign_model.py +60 -0
  72. wingify/models/campaign/metric_model.py +42 -0
  73. wingify/models/campaign/rule_model.py +42 -0
  74. wingify/models/campaign/variable_model.py +44 -0
  75. wingify/models/campaign/variation_model.py +109 -0
  76. wingify/models/gateway_service_model.py +48 -0
  77. wingify/models/schemas/__init__.py +13 -0
  78. wingify/models/schemas/campaign_group_schema.py +19 -0
  79. wingify/models/schemas/campaign_metric_schema.py +26 -0
  80. wingify/models/schemas/campaign_schema.py +37 -0
  81. wingify/models/schemas/campaign_variation_schema.py +34 -0
  82. wingify/models/schemas/empty_object_schema.py +15 -0
  83. wingify/models/schemas/feature_schema.py +38 -0
  84. wingify/models/schemas/group_schema.py +37 -0
  85. wingify/models/schemas/holdout_schema.py +29 -0
  86. wingify/models/schemas/rule_schema.py +24 -0
  87. wingify/models/schemas/settings_schema.py +47 -0
  88. wingify/models/schemas/variable_schema.py +24 -0
  89. wingify/models/settings/__init__.py +13 -0
  90. wingify/models/settings/internal_event_sampling_settings_model.py +53 -0
  91. wingify/models/settings/settings_model.py +127 -0
  92. wingify/models/storage/__init__.py +13 -0
  93. wingify/models/storage/storage_model.py +84 -0
  94. wingify/models/user/__init__.py +13 -0
  95. wingify/models/user/context_model.py +113 -0
  96. wingify/models/user/context_vwo_model.py +34 -0
  97. wingify/models/user/get_flag.py +47 -0
  98. wingify/models/vwo_options_model.py +21 -0
  99. wingify/models/wingify_options_model.py +129 -0
  100. wingify/packages/__init__.py +13 -0
  101. wingify/packages/decision_maker/__init__.py +13 -0
  102. wingify/packages/decision_maker/decision_maker.py +89 -0
  103. wingify/packages/logger/__init__.py +13 -0
  104. wingify/packages/logger/core/__init__.py +13 -0
  105. wingify/packages/logger/core/log_manager.py +123 -0
  106. wingify/packages/logger/core/log_transport_manager.py +57 -0
  107. wingify/packages/logger/enums/__init__.py +13 -0
  108. wingify/packages/logger/enums/log_level_enum.py +24 -0
  109. wingify/packages/logger/enums/log_level_number_enum.py +21 -0
  110. wingify/packages/logger/log_message_builder.py +70 -0
  111. wingify/packages/logger/logger.py +38 -0
  112. wingify/packages/logger/transports/__init__.py +13 -0
  113. wingify/packages/logger/transports/console_transport.py +43 -0
  114. wingify/packages/network_layer/__init__.py +13 -0
  115. wingify/packages/network_layer/client/__init__.py +13 -0
  116. wingify/packages/network_layer/client/network_client.py +167 -0
  117. wingify/packages/network_layer/handlers/__init__.py +13 -0
  118. wingify/packages/network_layer/handlers/request_handler.py +40 -0
  119. wingify/packages/network_layer/manager/__init__.py +13 -0
  120. wingify/packages/network_layer/manager/network_manager.py +79 -0
  121. wingify/packages/network_layer/models/__init__.py +13 -0
  122. wingify/packages/network_layer/models/global_request_model.py +70 -0
  123. wingify/packages/network_layer/models/request_model.py +133 -0
  124. wingify/packages/network_layer/models/response_model.py +55 -0
  125. wingify/packages/segmentation_evaluator/__init__.py +13 -0
  126. wingify/packages/segmentation_evaluator/core/__init__.py +13 -0
  127. wingify/packages/segmentation_evaluator/core/segmentation_manager.py +140 -0
  128. wingify/packages/segmentation_evaluator/enums/__init__.py +13 -0
  129. wingify/packages/segmentation_evaluator/enums/segment_operand_regex_enum.py +31 -0
  130. wingify/packages/segmentation_evaluator/enums/segment_operand_value_enum.py +29 -0
  131. wingify/packages/segmentation_evaluator/enums/segment_operator_value_enum.py +37 -0
  132. wingify/packages/segmentation_evaluator/evaluators/__init__.py +13 -0
  133. wingify/packages/segmentation_evaluator/evaluators/segment_evaluator.py +237 -0
  134. wingify/packages/segmentation_evaluator/evaluators/segment_operand_evaluator.py +518 -0
  135. wingify/packages/segmentation_evaluator/utils/__init__.py +13 -0
  136. wingify/packages/segmentation_evaluator/utils/segment_util.py +33 -0
  137. wingify/packages/segmentation_evaluator/utils/web_testing_segment_util.py +295 -0
  138. wingify/packages/storage/__init__.py +13 -0
  139. wingify/packages/storage/connector.py +25 -0
  140. wingify/packages/storage/storage.py +39 -0
  141. wingify/resources/__init__.py +13 -0
  142. wingify/resources/debug-messages.json +21 -0
  143. wingify/resources/error-messages.json +62 -0
  144. wingify/resources/info-message.json +47 -0
  145. wingify/resources/trace-messages.json +1 -0
  146. wingify/resources/warn-messages.json +1 -0
  147. wingify/services/__init__.py +13 -0
  148. wingify/services/batch_event_queue.py +140 -0
  149. wingify/services/campaign_decision_service.py +268 -0
  150. wingify/services/hooks_manager.py +65 -0
  151. wingify/services/internal_events_sampling_service.py +62 -0
  152. wingify/services/settings_manager.py +293 -0
  153. wingify/services/storage_service.py +58 -0
  154. wingify/services/url_service.py +51 -0
  155. wingify/utils/__init__.py +13 -0
  156. wingify/utils/aliasing_util.py +108 -0
  157. wingify/utils/brand_context.py +26 -0
  158. wingify/utils/brand_util.py +37 -0
  159. wingify/utils/campaign_util.py +592 -0
  160. wingify/utils/data_type_util.py +125 -0
  161. wingify/utils/debugger_service_util.py +58 -0
  162. wingify/utils/decision_util.py +589 -0
  163. wingify/utils/event_util.py +85 -0
  164. wingify/utils/function_util.py +188 -0
  165. wingify/utils/gateway_service_util.py +179 -0
  166. wingify/utils/holdout_util.py +264 -0
  167. wingify/utils/impression_util.py +151 -0
  168. wingify/utils/internal_events_sampling_util.py +106 -0
  169. wingify/utils/log_message_util.py +95 -0
  170. wingify/utils/meg_util.py +561 -0
  171. wingify/utils/model_utils.py +136 -0
  172. wingify/utils/network_util.py +706 -0
  173. wingify/utils/rule_evaluation_util.py +102 -0
  174. wingify/utils/settings_util.py +39 -0
  175. wingify/utils/usage_stats_util.py +116 -0
  176. wingify/utils/uuid_util.py +81 -0
  177. wingify/vwo_client.py +21 -0
  178. wingify/wingify.py +159 -0
  179. wingify/wingify_builder.py +282 -0
  180. wingify/wingify_client.py +520 -0
  181. wingify_fme_python_sdk-1.70.0.dist-info/METADATA +1415 -0
  182. wingify_fme_python_sdk-1.70.0.dist-info/RECORD +186 -0
  183. wingify_fme_python_sdk-1.70.0.dist-info/WHEEL +5 -0
  184. wingify_fme_python_sdk-1.70.0.dist-info/licenses/LICENSE +202 -0
  185. wingify_fme_python_sdk-1.70.0.dist-info/licenses/NOTICE +18 -0
  186. wingify_fme_python_sdk-1.70.0.dist-info/top_level.txt +3 -0
@@ -0,0 +1,727 @@
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 Dict, Any, List
17
+ from ..models.settings.settings_model import SettingsModel
18
+ from ..models.user.context_model import ContextModel
19
+ from ..services.hooks_manager import HooksManager
20
+ from ..enums.api_enum import ApiEnum
21
+ from ..enums.campaign_type_enum import CampaignTypeEnum
22
+ from ..utils.log_message_util import debug_messages, info_messages, error_messages
23
+ from ..packages.logger.core.log_manager import LogManager
24
+ from ..utils.function_util import (
25
+ get_feature_from_key,
26
+ get_specific_rules_based_on_type,
27
+ get_all_experiment_rules,
28
+ )
29
+ from ..models.campaign.campaign_model import CampaignModel
30
+ from ..models.campaign.variation_model import VariationModel
31
+ from ..packages.segmentation_evaluator.core.segmentation_manager import (
32
+ SegmentationManager,
33
+ )
34
+ from ..utils.rule_evaluation_util import evaluate_rule
35
+ from ..utils.decision_util import evaluate_traffic_and_get_variation
36
+ from ..utils.impression_util import (
37
+ send_impression_for_variation_shown_batch,
38
+ send_impression_for_variation_shown,
39
+ create_and_send_impression_for_usage_tracking,
40
+ )
41
+ from ..models.user.get_flag import GetFlag
42
+ from ..services.storage_service import StorageService
43
+ from ..decorators.storage_decorator import StorageDecorator
44
+ from ..utils.campaign_util import (
45
+ get_experiment_key,
46
+ get_rollout_key,
47
+ get_variation_from_campaign_key,
48
+ )
49
+ from ..enums.debug_category_enum import DebugCategoryEnum
50
+ from ..packages.logger.enums.log_level_enum import LogLevelEnum
51
+ from ..constants.Constants import Constants
52
+ from ..utils.debugger_service_util import send_debug_event_to_vwo
53
+ from ..utils.network_util import get_track_user_payload_data
54
+ from ..enums.event_enum import EventEnum
55
+ from ..services.settings_manager import SettingsManager
56
+ from ..utils.function_util import (
57
+ get_current_unix_timestamp,
58
+ )
59
+ from ..utils.holdout_util import get_matched_holdouts, get_applicable_holdouts, send_network_calls_for_not_in_holdouts
60
+
61
+
62
+ class GetFlagApi:
63
+ def __init__(self):
64
+ self._should_check_for_experiment_rules = False
65
+ self._passed_rules_information: Dict[str, Any] = {}
66
+ self._evaluated_feature_map: Dict[str, Any] = {}
67
+
68
+ def get(
69
+ self,
70
+ feature_key: str,
71
+ settings: SettingsModel,
72
+ context: ContextModel,
73
+ hook_manager: HooksManager,
74
+ ) -> GetFlag:
75
+ """
76
+ Gets the flag values for the given feature key.
77
+
78
+ :param feature_key: The key of the feature to get the flag for.
79
+ :param settings: The settings file containing the account settings.
80
+ :param context: The context of the user.
81
+ :param hook_manager: The hook manager to execute hooks.
82
+ """
83
+
84
+ # Flag for usage tracking - false if no variation shown call is sent
85
+ is_variation_shown_fired = False
86
+
87
+ feature = get_feature_from_key(settings, feature_key)
88
+ is_enabled = False
89
+ variables = []
90
+ notInHoldoutIds: List[Any] = [];
91
+ # Holdout-related keys as constants
92
+ IS_IN_HOLDOUT_ID_KEY = "isInHoldoutId"
93
+ HOLDOUT_GROUP_ID_KEY = "holdoutGroupId"
94
+ NOT_IN_HOLDOUT_ID_KEY = "notInHoldoutId"
95
+ MATCHED_HOLDOUTS_KEY = "matchedHoldouts"
96
+ NOT_MATCHED_HOLDOUTS_KEY = "notMatchedHoldouts"
97
+ HOLDOUT_PAYLOADS_KEY = "holdoutPayloads"
98
+
99
+ decision = {
100
+ "featureName": feature.get_name() if feature else None,
101
+ "featureId": feature.get_id() if feature else None,
102
+ "featureKey": feature.get_key() if feature else None,
103
+ "userId": context.get_id() if context else None,
104
+ "api": ApiEnum.GET_FLAG.value,
105
+ "holdoutIDs": [],
106
+ "isPartOfHoldout": False,
107
+ "isHoldoutPresent": False,
108
+ "isUserPartOfCampaign": False,
109
+ }
110
+
111
+ debug_event_props = {
112
+ "an": ApiEnum.GET_FLAG.value,
113
+ "fk": feature_key,
114
+ "uuid": context.get_vwo_uuid(),
115
+ "sId": context.get_session_id(),
116
+ }
117
+
118
+ storage_service = StorageService()
119
+ stored_data = StorageDecorator().get_feature_from_storage(
120
+ feature_key, context, storage_service
121
+ )
122
+ batchPayload = []
123
+
124
+
125
+ # Check for holdout group persistence in storage and settings
126
+ stored_is_in_holdout_id: List[Any] = (
127
+ stored_data.get(IS_IN_HOLDOUT_ID_KEY)
128
+ or stored_data.get(HOLDOUT_GROUP_ID_KEY)
129
+ or []
130
+ ) if stored_data else []
131
+ stored_not_in_holdout_id: List[Any] = (
132
+ stored_data.get(NOT_IN_HOLDOUT_ID_KEY) or []
133
+ ) if stored_data else []
134
+ if len(stored_is_in_holdout_id) > 0 or len(stored_not_in_holdout_id) > 0:
135
+ # get all applicable holdouts for the feature from the latest settings
136
+ applicable_holdouts = get_applicable_holdouts(settings, feature.get_id())
137
+
138
+ if len(applicable_holdouts) > 0:
139
+ for holdout in applicable_holdouts:
140
+ holdout_id = holdout.get_id()
141
+ # Check if stored_is_in_holdout_id contains the holdout_id
142
+ if holdout_id in stored_is_in_holdout_id:
143
+ LogManager.get_instance().info(
144
+ info_messages.get("STORED_HOLDOUT_DECISION_FOUND").format(
145
+ featureKey=feature_key,
146
+ userId=context.get_id(),
147
+ holdoutId=holdout_id,
148
+ )
149
+ )
150
+ # Evaluate the new holdouts in the settings file and prepare impressions
151
+ holdout_result = get_matched_holdouts(settings, feature, context, stored_data)
152
+
153
+ matched_holdouts = holdout_result.get(MATCHED_HOLDOUTS_KEY, [])
154
+ not_matched_holdouts = holdout_result.get(NOT_MATCHED_HOLDOUTS_KEY, [])
155
+ holdout_payloads = holdout_result.get(HOLDOUT_PAYLOADS_KEY, [])
156
+ # updatedHoldoutIds = storedIsInHoldoutId + [id for matched_holdouts]
157
+ updated_holdout_ids = (
158
+ stored_is_in_holdout_id
159
+ ) + [h.get_id() for h in matched_holdouts]
160
+ updated_not_in_holdout_ids = (
161
+ stored_not_in_holdout_id
162
+ ) + [h.get_id() for h in not_matched_holdouts]
163
+
164
+ # Store the updated holdout ids in storage
165
+ StorageDecorator().set_data_in_storage(
166
+ {
167
+ "featureKey": feature_key,
168
+ "context": context,
169
+ IS_IN_HOLDOUT_ID_KEY: updated_holdout_ids,
170
+ NOT_IN_HOLDOUT_ID_KEY: updated_not_in_holdout_ids,
171
+ },
172
+ storage_service
173
+ )
174
+
175
+ # Send impression for the new holdouts
176
+ if SettingsManager.get_instance().is_gateway_service_provided:
177
+ for payload in holdout_payloads:
178
+ send_impression_for_variation_shown(
179
+ payload,
180
+ context
181
+ )
182
+ else:
183
+ send_impression_for_variation_shown_batch(holdout_payloads, account_id=settings.get_account_id(), sdk_key=settings.get_sdk_key())
184
+
185
+ # if impression is fired for any of the holdouts, set is_variation_shown_fired to True
186
+ if holdout_payloads and len(holdout_payloads) > 0:
187
+ is_variation_shown_fired = True
188
+
189
+ # Case: User is already part of a holdout (stored decision).
190
+ if settings.get_is_tracking_usage_enabled() and not is_variation_shown_fired:
191
+ create_and_send_impression_for_usage_tracking(settings, feature_key, context)
192
+
193
+ # Return a disabled flag as user is in persistent holdout
194
+ return GetFlag(is_enabled=False, variables=[], session_id=context.get_session_id(), uuid=context.get_vwo_uuid())
195
+ if stored_data and stored_data.get("experimentVariationId"):
196
+ if "experimentKey" in stored_data:
197
+ variation = get_variation_from_campaign_key(
198
+ settings,
199
+ stored_data["experimentKey"],
200
+ stored_data["experimentVariationId"],
201
+ )
202
+
203
+ if variation:
204
+ LogManager.get_instance().info(
205
+ info_messages.get("STORED_VARIATION_FOUND").format(
206
+ variationKey=variation.get_name(),
207
+ userId=context.get_id(),
208
+ experimentType="experiment",
209
+ experimentKey=stored_data["experimentKey"],
210
+ )
211
+ )
212
+ decision["isUserPartOfCampaign"] = True
213
+ # send network calls for holdouts that are newly added in settings and are not present in storage
214
+ # Calculate the initial size of not_in_holdout_id
215
+ initial_size = len(stored_data.get("notInHoldoutId") or []) if stored_data else 0
216
+ # send network calls for not_in_holdout_id
217
+ updated_not_in_holdouts = send_network_calls_for_not_in_holdouts(settings, feature, context, decision, stored_data, storage_service)
218
+ # if variationShown event is fired, set is_variation_shown_fired to True - Optimization for newly added holdouts
219
+ if updated_not_in_holdouts and len(updated_not_in_holdouts) > initial_size:
220
+ is_variation_shown_fired = True
221
+
222
+ # Case: Feature found in storage
223
+ # Send usage tracking for cached experiment decision if usage tracking is enabled
224
+ if settings.get_is_tracking_usage_enabled() and not is_variation_shown_fired:
225
+ create_and_send_impression_for_usage_tracking(settings, feature_key, context)
226
+
227
+ return GetFlag(is_enabled=True, variables=variation.get_variables(), session_id=context.get_session_id(), uuid=context.get_vwo_uuid())
228
+ elif (
229
+ stored_data
230
+ and stored_data.get("rolloutKey")
231
+ and stored_data.get("rolloutId")
232
+ ):
233
+ variation = get_variation_from_campaign_key(
234
+ settings, stored_data["rolloutKey"], stored_data["rolloutVariationId"]
235
+ )
236
+ if variation:
237
+ LogManager.get_instance().info(
238
+ info_messages.get("STORED_VARIATION_FOUND").format(
239
+ variationKey=variation.get_name(),
240
+ userId=context.get_id(),
241
+ experimentType="rollout",
242
+ experimentKey=stored_data["rolloutKey"],
243
+ )
244
+ )
245
+
246
+ decision["isUserPartOfCampaign"] = True
247
+ # network calls for holdouts that are newly added in settings and are not present in storage
248
+ # and return the updated not in holdout ids
249
+ initial_size = len(stored_data.get("notInHoldoutId") or []) if stored_data else 0
250
+ updated_not_in_holdout_ids = send_network_calls_for_not_in_holdouts(settings, feature, context, decision, stored_data, storage_service)
251
+ if updated_not_in_holdout_ids and len(updated_not_in_holdout_ids) > initial_size:
252
+ is_variation_shown_fired = True
253
+ # push the updated not in holdout ids to the notInHoldoutIds array
254
+ notInHoldoutIds.extend(updated_not_in_holdout_ids)
255
+
256
+ is_enabled = True
257
+ variables = variation.get_variables()
258
+
259
+ self._should_check_for_experiment_rules = True
260
+ feature_info = {
261
+ "rolloutId": stored_data["rolloutId"],
262
+ "rolloutKey": stored_data["rolloutKey"],
263
+ "rolloutVariationId": stored_data["rolloutVariationId"],
264
+ }
265
+ self._evaluated_feature_map[feature_key] = feature_info
266
+ self._passed_rules_information.update(feature_info)
267
+
268
+ if feature is None:
269
+ LogManager.get_instance().error_log("FEATURE_NOT_FOUND", data={"featureKey": feature_key}, debug_data = debug_event_props)
270
+ is_enabled = False
271
+
272
+ # Case: Feature not found
273
+ # If usage tracking is enabled, send usage tracking impression
274
+ if settings.get_is_tracking_usage_enabled() and not is_variation_shown_fired:
275
+ create_and_send_impression_for_usage_tracking(settings, feature_key, context)
276
+
277
+ return GetFlag(is_enabled=is_enabled, variables=variables, session_id=context.get_session_id(), uuid=context.get_vwo_uuid())
278
+
279
+ if context.get_session_id() is None:
280
+ context.set_session_id(get_current_unix_timestamp())
281
+
282
+ SegmentationManager.get_instance().set_contextual_data(
283
+ settings, feature, context
284
+ )
285
+
286
+ if not is_enabled:
287
+ result = get_matched_holdouts(settings, feature, context, stored_data) or {}
288
+ matched_holdouts = result.get(MATCHED_HOLDOUTS_KEY, [])
289
+ not_matched_holdouts = result.get(NOT_MATCHED_HOLDOUTS_KEY, [])
290
+ holdout_payloads = result.get(HOLDOUT_PAYLOADS_KEY, [])
291
+
292
+ decision["isPartOfHoldout"] = matched_holdouts is not None and len(matched_holdouts) > 0
293
+
294
+ if (
295
+ (matched_holdouts is not None and len(matched_holdouts) > 0)
296
+ or (not_matched_holdouts is not None and len(not_matched_holdouts) > 0)
297
+ ):
298
+ decision["isHoldoutPresent"] = True
299
+
300
+ if matched_holdouts is not None and len(matched_holdouts) > 0:
301
+ qualified_holdout_names = ",".join([holdout.get_name() for holdout in matched_holdouts])
302
+ decision["holdoutIDs"] = [holdout.get_id() for holdout in matched_holdouts]
303
+
304
+ LogManager.get_instance().info(
305
+ info_messages.get("USER_IN_HOLDOUT_GROUP").format(
306
+ userId=context.get_id(),
307
+ holdoutGroupName=qualified_holdout_names,
308
+ featureKey=feature_key,
309
+ )
310
+ )
311
+
312
+ StorageDecorator().set_data_in_storage(
313
+ {
314
+ "featureKey": feature_key,
315
+ "context": context,
316
+ IS_IN_HOLDOUT_ID_KEY: [holdout.get_id() for holdout in matched_holdouts],
317
+ NOT_IN_HOLDOUT_ID_KEY: [holdout.get_id() for holdout in not_matched_holdouts],
318
+ },
319
+ storage_service,
320
+ )
321
+
322
+ decision["isEnabled"] = False
323
+
324
+ hook_manager.set(decision)
325
+ hook_manager.execute(hook_manager.get())
326
+
327
+ if SettingsManager.get_instance().is_gateway_service_provided:
328
+ for payload in holdout_payloads:
329
+ send_impression_for_variation_shown(payload, context)
330
+ else:
331
+ send_impression_for_variation_shown_batch(
332
+ holdout_payloads, settings.get_account_id(), settings.get_sdk_key()
333
+ )
334
+
335
+ return GetFlag(
336
+ is_enabled=False,
337
+ variables=[],
338
+ session_id=context.get_session_id(),
339
+ uuid=context.get_vwo_uuid(),
340
+ )
341
+ else:
342
+ LogManager.get_instance().info(
343
+ info_messages.get("USER_NOT_EXCLUDED_DUE_TO_HOLDOUT").format(
344
+ featureKey=feature_key,
345
+ userId=context.get_id(),
346
+ )
347
+ )
348
+ notInHoldoutIds.extend([holdout.get_id() for holdout in not_matched_holdouts])
349
+
350
+ if SettingsManager.get_instance().is_gateway_service_provided:
351
+ for payload in holdout_payloads:
352
+ send_impression_for_variation_shown(payload, context)
353
+ else:
354
+ batchPayload.extend(holdout_payloads)
355
+
356
+ if holdout_payloads and len(holdout_payloads) > 0:
357
+ is_variation_shown_fired = True
358
+
359
+ roll_out_rules = get_specific_rules_based_on_type(
360
+ feature, CampaignTypeEnum.ROLLOUT.value
361
+ )
362
+
363
+ rollout_variation_to_return = None
364
+
365
+ if roll_out_rules and not is_enabled:
366
+ rollout_rules_to_evaluate: List[CampaignModel] = []
367
+
368
+ for rule in roll_out_rules:
369
+ (
370
+ pre_segmentation_result,
371
+ whitelisted_object,
372
+ updated_decision,
373
+ payload,
374
+ ) = evaluate_rule(
375
+ settings,
376
+ feature,
377
+ rule,
378
+ context,
379
+ self._evaluated_feature_map,
380
+ {},
381
+ storage_service,
382
+ decision,
383
+ )
384
+ decision.update(updated_decision)
385
+
386
+ is_force_user_applicable = (
387
+ whitelisted_object is not None
388
+ and isinstance(whitelisted_object, dict)
389
+ and len(whitelisted_object) > 0
390
+ )
391
+
392
+ if is_force_user_applicable:
393
+ is_enabled = True
394
+ self._should_check_for_experiment_rules = True
395
+ decision["isUserPartOfCampaign"] = True
396
+ rollout_variation_to_return = whitelisted_object["variation"]
397
+ variables = whitelisted_object["variation"].get_variables()
398
+
399
+ self._evaluated_feature_map[feature_key] = {
400
+ "rolloutId": rule.get_id(),
401
+ "rolloutKey": get_rollout_key(rule, feature_key),
402
+ "rolloutVariationId": whitelisted_object["variation"].get_id(),
403
+ }
404
+
405
+ self._update_integrations_decision_object(
406
+ rule, whitelisted_object["variation"], decision
407
+ )
408
+
409
+ if (
410
+ SettingsManager.get_instance().is_gateway_service_provided
411
+ and payload is not None
412
+ and len(payload) > 0
413
+ ):
414
+ is_variation_shown_fired = True
415
+ send_impression_for_variation_shown(payload, context)
416
+ else:
417
+ if payload is not None and len(payload) > 0:
418
+ is_variation_shown_fired = True
419
+ batchPayload.append(payload)
420
+
421
+ break
422
+ elif pre_segmentation_result:
423
+ rollout_rules_to_evaluate.append(rule)
424
+
425
+ if (
426
+ SettingsManager.get_instance().is_gateway_service_provided
427
+ and payload is not None
428
+ and len(payload) > 0
429
+ ):
430
+ send_impression_for_variation_shown(payload, context)
431
+ else:
432
+ if payload is not None and len(payload) > 0:
433
+ batchPayload.append(payload)
434
+
435
+ self._evaluated_feature_map[feature_key] = {
436
+ "rolloutId": rule.get_id(),
437
+ "rolloutKey": get_rollout_key(rule, feature_key),
438
+ "rolloutVariationId": (
439
+ rule.get_variations()[0].get_id()
440
+ if rule.get_variations()
441
+ else None
442
+ ),
443
+ }
444
+ break
445
+
446
+ if rollout_rules_to_evaluate.__len__() > 0 and rollout_variation_to_return is None:
447
+ variation = evaluate_traffic_and_get_variation(
448
+ settings, rollout_rules_to_evaluate[0], context
449
+ )
450
+
451
+ if isinstance(variation, VariationModel) and not None:
452
+ is_enabled = True
453
+ decision["isUserPartOfCampaign"] = True
454
+ variables = variation.get_variables()
455
+ self._should_check_for_experiment_rules = True
456
+ self._update_integrations_decision_object(
457
+ rollout_rules_to_evaluate[0], variation, decision
458
+ )
459
+
460
+ payload = get_track_user_payload_data(
461
+ settings,
462
+ EventEnum.VARIATION_SHOWN.value,
463
+ rollout_rules_to_evaluate[0].get_id(),
464
+ variation.get_id(),
465
+ context,
466
+ )
467
+ if (
468
+ SettingsManager.get_instance().is_gateway_service_provided
469
+ and payload is not None
470
+ and len(payload) > 0
471
+ ):
472
+ # set is_variation_shown_fired to true as the rollout impression is sent
473
+ is_variation_shown_fired = True
474
+ send_impression_for_variation_shown(payload, context)
475
+ else:
476
+ if payload is not None and len(payload) > 0:
477
+ # set is_variation_shown_fired to true as the rollout impression is sent
478
+ is_variation_shown_fired = True
479
+ batchPayload.append(payload)
480
+
481
+ if not roll_out_rules:
482
+ LogManager.get_instance().debug(
483
+ debug_messages.get("EXPERIMENTS_EVALUATION_WHEN_NO_ROLLOUT_PRESENT")
484
+ )
485
+ self._should_check_for_experiment_rules = True
486
+
487
+ if self._should_check_for_experiment_rules:
488
+ experiment_rules_to_evaluate: List[CampaignModel] = []
489
+
490
+ experiment_rules = get_all_experiment_rules(feature)
491
+ meg_group_winner_campaigns = {}
492
+
493
+ for rule in experiment_rules:
494
+ (
495
+ pre_segmentation_result,
496
+ whitelisted_object,
497
+ updated_decision,
498
+ payload,
499
+ ) = evaluate_rule(
500
+ settings,
501
+ feature,
502
+ rule,
503
+ context,
504
+ self._evaluated_feature_map,
505
+ meg_group_winner_campaigns,
506
+ storage_service,
507
+ decision,
508
+ )
509
+
510
+ decision.update(updated_decision)
511
+
512
+ if pre_segmentation_result:
513
+ if whitelisted_object is None:
514
+ experiment_rules_to_evaluate.append(rule)
515
+ else:
516
+ if (
517
+ SettingsManager.get_instance().is_gateway_service_provided
518
+ and payload is not None
519
+ and len(payload) > 0
520
+ ):
521
+ # set is_variation_shown_fired to true as the experiment impression is sent (for whitelisted user)
522
+ # this prevents sending usage tracking impression for the experiment campaign when checked at the bottom
523
+ is_variation_shown_fired = True
524
+ send_impression_for_variation_shown(payload, context)
525
+ else:
526
+ if payload is not None and len(payload) > 0:
527
+ # set is_variation_shown_fired to true as the experiment impression is sent (for whitelisted user)
528
+ # this prevents sending usage tracking impression for the experiment campaign when checked at the bottom
529
+ is_variation_shown_fired = True
530
+ batchPayload.append(payload)
531
+
532
+ is_enabled = True
533
+ decision["isUserPartOfCampaign"] = True
534
+ variables = whitelisted_object["variation"].get_variables()
535
+ self._passed_rules_information.update(
536
+ {
537
+ "experimentId": rule.get_id(),
538
+ "experimentKey": get_experiment_key(
539
+ rule, feature.get_key()
540
+ ),
541
+ "experimentVariationId": whitelisted_object[
542
+ "variationId"
543
+ ],
544
+ }
545
+ )
546
+
547
+ break
548
+
549
+ if experiment_rules_to_evaluate.__len__() > 0:
550
+ variation = evaluate_traffic_and_get_variation(
551
+ settings, experiment_rules_to_evaluate[0], context
552
+ )
553
+
554
+ if isinstance(variation, VariationModel) and not None:
555
+ is_enabled = True
556
+ decision["isUserPartOfCampaign"] = True
557
+ variables = variation.get_variables()
558
+
559
+ self._update_integrations_decision_object(
560
+ experiment_rules_to_evaluate[0], variation, decision
561
+ )
562
+
563
+ payload = get_track_user_payload_data(
564
+ settings,
565
+ EventEnum.VARIATION_SHOWN.value,
566
+ experiment_rules_to_evaluate[0].get_id(),
567
+ variation.get_id(),
568
+ context,
569
+ )
570
+ if (
571
+ SettingsManager.get_instance().is_gateway_service_provided
572
+ and payload is not None
573
+ and len(payload) > 0
574
+ ):
575
+ # set is_variation_shown_fired to true as the experiment impression is sent.
576
+ is_variation_shown_fired = True
577
+ send_impression_for_variation_shown(payload, context)
578
+ else:
579
+ if payload is not None and len(payload) > 0:
580
+ # set is_variation_shown_fired to true as the experiment impression is sent.
581
+ is_variation_shown_fired = True
582
+ batchPayload.append(payload)
583
+
584
+ if is_enabled:
585
+ StorageDecorator().set_data_in_storage(
586
+ {
587
+ "featureKey": feature_key,
588
+ "context": context,
589
+ **self._passed_rules_information,
590
+ NOT_IN_HOLDOUT_ID_KEY: notInHoldoutIds,
591
+ },
592
+ storage_service,
593
+ )
594
+ else:
595
+ StorageDecorator().set_data_in_storage(
596
+ {
597
+ "featureKey": feature_key,
598
+ "context": context,
599
+ NOT_IN_HOLDOUT_ID_KEY: notInHoldoutIds,
600
+ },
601
+ storage_service,
602
+ )
603
+
604
+ hook_manager.set(decision)
605
+ hook_manager.execute(hook_manager.get())
606
+
607
+ # send debug event, if debugger is enabled
608
+ if feature.get_is_debugger_enabled():
609
+ debug_event_props["cg"] = DebugCategoryEnum.DECISION.value
610
+ debug_event_props["lt"] = LogLevelEnum.INFO.value
611
+ debug_event_props["msg_t"] = Constants.FLAG_DECISION_GIVEN
612
+
613
+ # update debug event props with decision keys
614
+ self.update_debug_event_props_with_message(debug_event_props, decision)
615
+ send_debug_event_to_vwo(debug_event_props);
616
+
617
+ if (
618
+ feature.get_impact_campaign()
619
+ and feature.get_impact_campaign().get_campaign_id()
620
+ ):
621
+ LogManager.get_instance().info(
622
+ info_messages.get("IMPACT_ANALYSIS").format(
623
+ userId=context.get_id(),
624
+ featureKey=feature_key,
625
+ status=(
626
+ "enabled" if is_enabled else "disabled"
627
+ ),
628
+ )
629
+ )
630
+
631
+ payload = get_track_user_payload_data(
632
+ settings,
633
+ EventEnum.VARIATION_SHOWN.value,
634
+ feature.get_impact_campaign().get_campaign_id(),
635
+ (2 if is_enabled else 1),
636
+ context
637
+ )
638
+ if (
639
+ SettingsManager.get_instance().is_gateway_service_provided
640
+ and payload is not None
641
+ and len(payload) > 0
642
+ ):
643
+ # set is_variation_shown_fired to true as the impact analysis impression is sent.
644
+ is_variation_shown_fired = True
645
+ send_impression_for_variation_shown(payload, context)
646
+ else:
647
+ if payload is not None and len(payload) > 0:
648
+ # set is_variation_shown_fired to true as the impact analysis impression is sent.
649
+ is_variation_shown_fired = True
650
+ batchPayload.append(payload)
651
+
652
+ if not SettingsManager.get_instance().is_gateway_service_provided:
653
+ send_impression_for_variation_shown_batch(
654
+ batchPayload, settings.get_account_id(), settings.get_sdk_key()
655
+ )
656
+
657
+ # Send usage tracking call when no primary variationShown event was dispatched.
658
+ # If a primary event was fired, the server already has the usage tracking signal.
659
+ if settings.get_is_tracking_usage_enabled() and not is_variation_shown_fired:
660
+ create_and_send_impression_for_usage_tracking(settings, feature_key, context)
661
+
662
+ return GetFlag(is_enabled=is_enabled, variables=variables, session_id=context.get_session_id(), uuid=context.get_vwo_uuid())
663
+
664
+ def _update_integrations_decision_object(
665
+ self,
666
+ campaign: CampaignModel,
667
+ variation: VariationModel,
668
+ decision: Dict[str, Any],
669
+ ) -> None:
670
+ if campaign.get_type() == CampaignTypeEnum.ROLLOUT.value:
671
+ self._passed_rules_information.update(
672
+ {
673
+ "rolloutId": campaign.get_id(),
674
+ "rolloutKey": get_rollout_key(
675
+ campaign, decision.get("featureKey")
676
+ ),
677
+ "rolloutVariationId": variation.get_id(),
678
+ }
679
+ )
680
+ else:
681
+ self._passed_rules_information.update(
682
+ {
683
+ "experimentId": campaign.get_id(),
684
+ "experimentKey": get_experiment_key(
685
+ campaign, decision.get("featureKey")
686
+ ),
687
+ "experimentVariationId": variation.get_id(),
688
+ }
689
+ )
690
+ decision.update(self._passed_rules_information)
691
+
692
+ def update_debug_event_props_with_message(
693
+ self,
694
+ debug_event_props: Dict[str, Any],
695
+ decision: Dict[str, Any],
696
+ ) -> None:
697
+ """
698
+ Updates the debug event props with the decision keys.
699
+
700
+ :param debug_event_props: The debug event props to update.
701
+ :param decision: The decision object to extract the keys from.
702
+ """
703
+ feature_key = decision.get("featureKey") or ""
704
+ message = f"Flag decision given for feature:{feature_key}."
705
+
706
+ rollout_key = decision.get("rolloutKey")
707
+ rollout_variation_id = decision.get("rolloutVariationId")
708
+ if rollout_key and rollout_variation_id:
709
+ prefix = f"{feature_key}_"
710
+ rollout_suffix = (
711
+ rollout_key[len(prefix):]
712
+ if isinstance(rollout_key, str) and isinstance(feature_key, str) and rollout_key.startswith(prefix)
713
+ else rollout_key
714
+ )
715
+ message += f" Got rollout:{rollout_suffix} with variation:{rollout_variation_id}"
716
+
717
+ experiment_key = decision.get("experimentKey")
718
+ experiment_variation_id = decision.get("experimentVariationId")
719
+ if experiment_key and experiment_variation_id:
720
+ prefix = f"{feature_key}_"
721
+ experiment_suffix = (
722
+ experiment_key[len(prefix):]
723
+ if isinstance(experiment_key, str) and isinstance(feature_key, str) and experiment_key.startswith(prefix)
724
+ else experiment_key
725
+ )
726
+ message += f" and experiment:{experiment_suffix} with variation:{experiment_variation_id}"
727
+ debug_event_props["msg"] = message