1
- # Copyright 2016-2017 , Optimizely
1
+ # Copyright 2016-2018 , Optimizely
2
2
# Licensed under the Apache License, Version 2.0 (the "License");
3
3
# you may not use this file except in compliance with the License.
4
4
# You may obtain a copy of the License at
@@ -35,78 +35,85 @@ class BaseEventBuilder(object):
35
35
36
36
def __init__ (self , config ):
37
37
self .config = config
38
- self .params = {}
39
38
40
39
@abstractproperty
41
40
class EventParams (object ):
42
41
pass
43
42
44
- def _add_project_id (self ):
45
- """ Add project ID to the event. """
43
+ def _get_project_id (self ):
44
+ """ Get project ID.
46
45
47
- self .params [self .EventParams .PROJECT_ID ] = self .config .get_project_id ()
48
-
49
- def _add_account_id (self ):
50
- """ Add account ID to the event. """
46
+ Returns:
47
+ Project ID of the datafile.
48
+ """
51
49
52
- self . params [ self . EventParams . ACCOUNT_ID ] = self .config .get_account_id ()
50
+ return self .config .get_project_id ()
53
51
54
- @abstractmethod
55
- def _add_user_id (self , user_id ):
56
- """ Add user ID to the event.
52
+ def _get_account_id (self ):
53
+ """ Get account ID.
57
54
58
- Args :
59
- user_id: ID of the user .
55
+ Returns :
56
+ Account ID in the datafile .
60
57
"""
61
- pass
58
+
59
+ return self .config .get_account_id ()
62
60
63
61
@abstractmethod
64
- def _add_attributes (self , attributes ):
65
- """ Add attribute(s) information to the event .
62
+ def _get_attributes (self , attributes ):
63
+ """ Get attribute(s) information.
66
64
67
65
Args:
68
66
attributes: Dict representing user attributes and values which need to be recorded.
69
67
"""
70
68
pass
71
69
72
- @abstractmethod
73
- def _add_source (self ):
74
- """ Add source information to the event. """
75
- pass
76
-
77
- def _add_revision (self ):
78
- """ Add datafile revision information to the event. """
79
- pass
70
+ def _get_anonymize_ip (self ):
71
+ """ Get IP anonymization bool
80
72
81
- def _add_anonymize_ip (self ):
82
- """ Add IP anonymization bool to the event """
73
+ Returns:
74
+ bool 'anonymizeIP' value in the datafile.
75
+ """
83
76
84
- self . params [ self . EventParams . ANONYMIZE_IP ] = self .config .get_anonymize_ip_value ()
77
+ return self .config .get_anonymize_ip_value ()
85
78
86
79
@abstractmethod
87
80
def _get_time (self ):
88
- """ Get time in milliseconds to be added to the event .
81
+ """ Get time in milliseconds to be added.
89
82
90
83
Returns:
91
- Current time in milliseconds.
84
+ int Current time in milliseconds.
92
85
"""
93
86
94
87
return int (round (time .time () * 1000 ))
95
88
96
- def _add_common_params (self , user_id , attributes ):
97
- """ Add params which are used same in both conversion and impression events.
89
+ def _get_common_params (self , user_id , attributes ):
90
+ """ Get params which are used same in both conversion and impression events.
98
91
99
92
Args:
100
93
user_id: ID for user.
101
94
attributes: Dict representing user attributes and values which need to be recorded.
95
+
96
+ Returns:
97
+ Dict consisting of parameters common to both impression and conversion events.
102
98
"""
99
+ commonParams = {}
100
+
101
+ commonParams [self .EventParams .PROJECT_ID ] = self ._get_project_id ()
102
+ commonParams [self .EventParams .ACCOUNT_ID ] = self ._get_account_id ()
103
+
104
+ visitor = {}
105
+ visitor [self .EventParams .END_USER_ID ] = user_id
106
+ visitor [self .EventParams .SNAPSHOTS ] = []
107
+
108
+ commonParams [self .EventParams .USERS ] = []
109
+ commonParams [self .EventParams .USERS ].append (visitor )
110
+ commonParams [self .EventParams .USERS ][0 ][self .EventParams .ATTRIBUTES ] = self ._get_attributes (attributes )
103
111
104
- self ._add_project_id ()
105
- self ._add_account_id ()
106
- self ._add_user_id (user_id )
107
- self ._add_attributes (attributes )
108
- self ._add_source ()
109
- self ._add_anonymize_ip ()
112
+ commonParams [self .EventParams .SOURCE_SDK_TYPE ] = 'python-sdk'
113
+ commonParams [self .EventParams .SOURCE_SDK_VERSION ] = version .__version__
114
+ commonParams [self .EventParams .ANONYMIZE_IP ] = self ._get_anonymize_ip ()
115
+
116
+ return commonParams
110
117
111
118
112
119
class EventBuilder (BaseEventBuilder ):
@@ -139,58 +146,45 @@ class EventParams(object):
139
146
CUSTOM = 'custom'
140
147
ANONYMIZE_IP = 'anonymize_ip'
141
148
142
- def _add_attributes (self , attributes ):
143
- """ Add attribute(s) information to the event .
149
+ def _get_attributes (self , attributes ):
150
+ """ Get attribute(s) information.
144
151
145
152
Args:
146
153
attributes: Dict representing user attributes and values which need to be recorded.
154
+
155
+ Returns:
156
+ List consisting of valid attributes for the user. Empty otherwise.
147
157
"""
148
158
149
- visitor = self .params [self .EventParams .USERS ][0 ]
150
- visitor [self .EventParams .ATTRIBUTES ] = []
159
+ params = []
151
160
152
161
if not attributes :
153
- return
162
+ return []
154
163
155
164
for attribute_key in attributes .keys ():
156
165
attribute_value = attributes .get (attribute_key )
157
166
# Omit falsy attribute values
158
167
if attribute_value :
159
168
attribute = self .config .get_attribute (attribute_key )
160
169
if attribute :
161
- visitor [ self . EventParams . ATTRIBUTES ] .append ({
170
+ params .append ({
162
171
self .EventParams .EVENT_ID : attribute .id ,
163
172
'key' : attribute_key ,
164
173
'type' : self .EventParams .CUSTOM ,
165
174
'value' : attribute_value ,
166
175
})
167
176
168
- def _add_source (self ):
169
- """ Add source information to the event. """
177
+ return params
170
178
171
- self .params [self .EventParams .SOURCE_SDK_TYPE ] = 'python-sdk'
172
- self .params [self .EventParams .SOURCE_SDK_VERSION ] = version .__version__
173
-
174
- def _add_user_id (self , user_id ):
175
- """ Add user ID to the event.
176
-
177
- Args:
178
- user_id: ID of the user.
179
- """
180
-
181
- self .params [self .EventParams .USERS ] = []
182
- # Add a single visitor
183
- visitor = {}
184
- visitor [self .EventParams .END_USER_ID ] = user_id
185
- visitor [self .EventParams .SNAPSHOTS ] = []
186
- self .params [self .EventParams .USERS ].append (visitor )
187
-
188
- def _add_required_params_for_impression (self , experiment , variation_id ):
189
- """ Add parameters that are required for the impression event to register.
179
+ def _get_required_params_for_impression (self , experiment , variation_id ):
180
+ """ Get parameters that are required for the impression event to register.
190
181
191
182
Args:
192
183
experiment: Experiment for which impression needs to be recorded.
193
184
variation_id: ID for variation which would be presented to user.
185
+
186
+ Returns:
187
+ Dict consisting of decisions and events info for impression event.
194
188
"""
195
189
snapshot = {}
196
190
@@ -207,19 +201,19 @@ def _add_required_params_for_impression(self, experiment, variation_id):
207
201
self .EventParams .UUID : str (uuid .uuid4 ())
208
202
}]
209
203
210
- visitor = self .params [self .EventParams .USERS ][0 ]
211
- visitor [self .EventParams .SNAPSHOTS ].append (snapshot )
204
+ return snapshot
212
205
213
- def _add_required_params_for_conversion (self , event_key , event_tags , decisions ):
214
- """ Add parameters that are required for the conversion event to register.
206
+ def _get_required_params_for_conversion (self , event_key , event_tags , decisions ):
207
+ """ Get parameters that are required for the conversion event to register.
215
208
216
209
Args:
217
210
event_key: Key representing the event which needs to be recorded.
218
211
event_tags: Dict representing metadata associated with the event.
219
212
decisions: List of tuples representing valid experiments IDs and variation IDs.
220
- """
221
213
222
- visitor = self .params [self .EventParams .USERS ][0 ]
214
+ Returns:
215
+ Dict consisting of the decisions and events info for conversion event.
216
+ """
223
217
224
218
for experiment_id , variation_id in decisions :
225
219
snapshot = {}
@@ -252,7 +246,8 @@ def _add_required_params_for_conversion(self, event_key, event_tags, decisions):
252
246
event_dict [self .EventParams .TAGS ] = event_tags
253
247
254
248
snapshot [self .EventParams .EVENTS ] = [event_dict ]
255
- visitor [self .EventParams .SNAPSHOTS ].append (snapshot )
249
+
250
+ return snapshot
256
251
257
252
def create_impression_event (self , experiment , variation_id , user_id , attributes ):
258
253
""" Create impression Event to be sent to the logging endpoint.
@@ -267,12 +262,13 @@ def create_impression_event(self, experiment, variation_id, user_id, attributes)
267
262
Event object encapsulating the impression event.
268
263
"""
269
264
270
- self .params = {}
271
- self ._add_common_params (user_id , attributes )
272
- self ._add_required_params_for_impression (experiment , variation_id )
265
+ params = self ._get_common_params (user_id , attributes )
266
+ impression_params = self ._get_required_params_for_impression (experiment , variation_id )
267
+
268
+ params [self .EventParams .USERS ][0 ][self .EventParams .SNAPSHOTS ].append (impression_params )
273
269
274
270
return Event (self .EVENTS_URL ,
275
- self . params ,
271
+ params ,
276
272
http_verb = self .HTTP_VERB ,
277
273
headers = self .HTTP_HEADERS )
278
274
@@ -290,10 +286,12 @@ def create_conversion_event(self, event_key, user_id, attributes, event_tags, de
290
286
Event object encapsulating the conversion event.
291
287
"""
292
288
293
- self .params = {}
294
- self ._add_common_params (user_id , attributes )
295
- self ._add_required_params_for_conversion (event_key , event_tags , decisions )
289
+ params = self ._get_common_params (user_id , attributes )
290
+ conversion_params = self ._get_required_params_for_conversion (event_key , event_tags , decisions )
291
+
292
+ params [self .EventParams .USERS ][0 ][self .EventParams .SNAPSHOTS ].append (conversion_params )
293
+
296
294
return Event (self .EVENTS_URL ,
297
- self . params ,
295
+ params ,
298
296
http_verb = self .HTTP_VERB ,
299
297
headers = self .HTTP_HEADERS )
0 commit comments