Skip to content

Commit 368afe3

Browse files
Add revision information to log events (#36)
1 parent f76eb4b commit 368afe3

File tree

5 files changed

+61
-32
lines changed

5 files changed

+61
-32
lines changed

optimizely/event_builder.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ def _add_time(self):
7878
""" Add time information to the event. """
7979
pass
8080

81+
def _add_revision(self):
82+
""" Add datafile revision information to the event. """
83+
pass
84+
8185
def _add_common_params(self, user_id, attributes):
8286
""" Add params which are used same in both conversion and impression events.
8387
@@ -91,6 +95,7 @@ def _add_common_params(self, user_id, attributes):
9195
self._add_user_id(user_id)
9296
self._add_attributes(attributes)
9397
self._add_source()
98+
self._add_revision()
9499
self._add_time()
95100

96101

@@ -273,6 +278,7 @@ class EventParams(object):
273278
USER_FEATURES = 'userFeatures'
274279
DECISION = 'decision'
275280
LAYER_STATES = 'layerStates'
281+
REVISION = 'revision'
276282
TIME = 'timestamp'
277283
SOURCE_SDK_TYPE = 'clientEngine'
278284
SOURCE_SDK_VERSION = 'clientVersion'
@@ -311,6 +317,10 @@ def _add_source(self):
311317
self.params[self.EventParams.SOURCE_SDK_TYPE] = 'python-sdk'
312318
self.params[self.EventParams.SOURCE_SDK_VERSION] = version.__version__
313319

320+
def _add_revision(self):
321+
""" Add datafile revision information to the event. """
322+
self.params[self.EventParams.REVISION] = self.config.get_revision()
323+
314324
def _add_time(self):
315325
""" Add time information to the event. """
316326

@@ -373,6 +383,7 @@ def _add_required_params_for_conversion(self, event_key, user_id, event_tags, va
373383
if variation:
374384
self.params[self.EventParams.LAYER_STATES].append({
375385
self.EventParams.LAYER_ID: experiment.layerId,
386+
self.EventParams.REVISION: self.config.get_revision(),
376387
self.EventParams.ACTION_TRIGGERED: True,
377388
self.EventParams.DECISION: {
378389
self.EventParams.EXPERIMENT_ID: experiment.id,

optimizely/project_config.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2016, Optimizely
1+
# Copyright 2016-2017, Optimizely
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at
@@ -125,6 +125,15 @@ def get_version(self):
125125

126126
return self.version
127127

128+
def get_revision(self):
129+
""" Get revision of the datafile.
130+
131+
Returns:
132+
Revision of the datafile.
133+
"""
134+
135+
return self.revision
136+
128137
def get_account_id(self):
129138
""" Get account ID from the config.
130139

tests/test_config.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2016, Optimizely
1+
# Copyright 2016-2017, Optimizely
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at
@@ -161,6 +161,11 @@ def test_get_version(self):
161161

162162
self.assertEqual('1', self.project_config.get_version())
163163

164+
def test_get_revision(self):
165+
""" Test that revision is retrieved correctly when using get_revision. """
166+
167+
self.assertEqual('42', self.project_config.get_revision())
168+
164169
def test_get_account_id(self):
165170
""" Test that account ID is retrieved correctly when using get_account_id. """
166171

tests/test_event_builder.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ def test_create_impression_event(self):
7575
}
7676
with mock.patch('time.time', return_value=42):
7777
event_obj = self.event_builder.create_impression_event(
78-
self.project_config.get_experiment_from_key('test_experiment'),
79-
'111129', 'test_user', None
78+
self.project_config.get_experiment_from_key('test_experiment'), '111129', 'test_user', None
8079
)
8180
self._validate_event_object(event_obj,
8281
event_builder.EventBuilderV1.OFFLINE_API_PATH.format(project_id='111001'),
@@ -262,6 +261,7 @@ def test_create_impression_event(self):
262261
'variationId': '111129',
263262
'isLayerHoldback': False
264263
},
264+
'revision': '42',
265265
'timestamp': 42123,
266266
'isGlobalHoldback': False,
267267
'userFeatures': [],
@@ -270,8 +270,7 @@ def test_create_impression_event(self):
270270
}
271271
with mock.patch('time.time', return_value=42.123):
272272
event_obj = self.event_builder.create_impression_event(
273-
self.project_config.get_experiment_from_key('test_experiment'),
274-
'111129', 'test_user', None
273+
self.project_config.get_experiment_from_key('test_experiment'), '111129', 'test_user', None
275274
)
276275
self._validate_event_object(event_obj,
277276
event_builder.EventBuilderV2.IMPRESSION_ENDPOINT,
@@ -288,6 +287,7 @@ def test_create_impression_event__with_attributes(self):
288287
'projectId': '111001',
289288
'layerId': '111182',
290289
'visitorId': 'test_user',
290+
'revision': '42',
291291
'decision': {
292292
'experimentId': '111127',
293293
'variationId': '111129',
@@ -328,8 +328,10 @@ def test_create_conversion_event__with_attributes(self):
328328
'eventEntityId': '111095',
329329
'eventMetrics': [],
330330
'eventFeatures': [],
331+
'revision': '42',
331332
'layerStates': [{
332333
'layerId': '111182',
334+
'revision': '42',
333335
'decision': {
334336
'experimentId': '111127',
335337
'variationId': '111129',
@@ -368,6 +370,7 @@ def test_create_conversion_event__with_attributes_no_match(self):
368370
'accountId': '12001',
369371
'projectId': '111001',
370372
'visitorId': 'test_user',
373+
'revision': '42',
371374
'eventName': 'test_event',
372375
'eventEntityId': '111095',
373376
'eventMetrics': [],
@@ -401,22 +404,20 @@ def test_create_conversion_event__with_event_value(self):
401404
'name': 'revenue',
402405
'value': 4200
403406
}],
404-
'eventFeatures': [
405-
{
407+
'eventFeatures': [{
406408
'id': 'non-revenue',
407409
'type': 'custom',
408410
'value': 'abc',
409411
'shouldIndex': False,
410-
},
411-
{
412+
}, {
412413
'id': 'revenue',
413414
'type': 'custom',
414415
'value': 4200,
415416
'shouldIndex': False,
416-
}
417-
],
417+
}],
418418
'layerStates': [{
419419
'layerId': '111182',
420+
'revision': '42',
420421
'decision': {
421422
'experimentId': '111127',
422423
'variationId': '111129',
@@ -426,6 +427,7 @@ def test_create_conversion_event__with_event_value(self):
426427
}
427428
],
428429
'timestamp': 42123,
430+
'revision': '42',
429431
'isGlobalHoldback': False,
430432
'userFeatures': [{
431433
'id': '111094',
@@ -458,23 +460,22 @@ def test_create_conversion_event__with_invalid_event_value(self):
458460
'visitorId': 'test_user',
459461
'eventName': 'test_event',
460462
'eventEntityId': '111095',
463+
'revision': '42',
461464
'eventMetrics': [],
462-
'eventFeatures': [
463-
{
465+
'eventFeatures': [{
464466
'id': 'non-revenue',
465467
'type': 'custom',
466468
'value': 'abc',
467469
'shouldIndex': False,
468-
},
469-
{
470+
}, {
470471
'id': 'revenue',
471472
'type': 'custom',
472473
'value': '4200',
473474
'shouldIndex': False,
474-
}
475-
],
475+
}],
476476
'layerStates': [{
477477
'layerId': '111182',
478+
'revision': '42',
478479
'decision': {
479480
'experimentId': '111127',
480481
'variationId': '111129',

tests/test_optimizely.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ def test_activate(self):
540540
'accountId': '12001',
541541
'projectId': '111001',
542542
'layerId': '111182',
543+
'revision': '42',
543544
'decision': {
544545
'variationId': '111129',
545546
'isLayerHoldback': False,
@@ -575,6 +576,7 @@ def test_activate__with_attributes__audience_match(self):
575576
'accountId': '12001',
576577
'projectId': '111001',
577578
'layerId': '111182',
579+
'revision': '42',
578580
'decision': {
579581
'variationId': '111129',
580582
'isLayerHoldback': False,
@@ -702,7 +704,9 @@ def test_track__with_attributes(self):
702704
'eventFeatures': [],
703705
'eventMetrics': [],
704706
'timestamp': 42000,
707+
'revision': '42',
705708
'layerStates': [{
709+
'revision': '42',
706710
'decision': {
707711
'variationId': '111128',
708712
'isLayerHoldback': False,
@@ -758,6 +762,7 @@ def test_track__with_event_value(self):
758762
'visitorId': 'test_user',
759763
'clientVersion': version.__version__,
760764
'clientEngine': 'python-sdk',
765+
'revision': '42',
761766
'userFeatures': [{
762767
'shouldIndex': True,
763768
'type': 'custom',
@@ -769,26 +774,24 @@ def test_track__with_event_value(self):
769774
'isGlobalHoldback': False,
770775
'eventEntityId': '111095',
771776
'eventName': 'test_event',
772-
'eventFeatures': [
773-
{
777+
'eventFeatures': [{
774778
'id': 'non-revenue',
775779
'type': 'custom',
776780
'value': 'abc',
777781
'shouldIndex': False,
778-
},
779-
{
782+
}, {
780783
'id': 'revenue',
781784
'type': 'custom',
782785
'value': 4200,
783786
'shouldIndex': False,
784-
},
785-
],
787+
}],
786788
'eventMetrics': [{
787789
'name': 'revenue',
788790
'value': 4200
789791
}],
790792
'timestamp': 42000,
791793
'layerStates': [{
794+
'revision': '42',
792795
'decision': {
793796
'variationId': '111128',
794797
'isLayerHoldback': False,
@@ -830,20 +833,20 @@ def test_track__with_deprecated_event_value(self):
830833
'isGlobalHoldback': False,
831834
'eventEntityId': '111095',
832835
'eventName': 'test_event',
833-
'eventFeatures': [
834-
{
836+
'eventFeatures': [{
835837
'id': 'revenue',
836838
'type': 'custom',
837839
'value': 4200,
838840
'shouldIndex': False,
839-
}
840-
],
841+
}],
841842
'eventMetrics': [{
842843
'name': 'revenue',
843844
'value': 4200
844845
}],
845846
'timestamp': 42000,
847+
'revision': '42',
846848
'layerStates': [{
849+
'revision': '42',
847850
'decision': {
848851
'variationId': '111128',
849852
'isLayerHoldback': False,
@@ -875,6 +878,7 @@ def test_track__with_invalid_event_value(self):
875878
'visitorId': 'test_user',
876879
'clientVersion': version.__version__,
877880
'clientEngine': 'python-sdk',
881+
'revision': '42',
878882
'userFeatures': [{
879883
'shouldIndex': True,
880884
'type': 'custom',
@@ -886,17 +890,16 @@ def test_track__with_invalid_event_value(self):
886890
'isGlobalHoldback': False,
887891
'eventEntityId': '111095',
888892
'eventName': 'test_event',
889-
'eventFeatures': [
890-
{
893+
'eventFeatures': [{
891894
'id': 'revenue',
892895
'type': 'custom',
893896
'value': '4200',
894897
'shouldIndex': False,
895-
}
896-
],
898+
}],
897899
'eventMetrics': [],
898900
'timestamp': 42000,
899901
'layerStates': [{
902+
'revision': '42',
900903
'decision': {
901904
'variationId': '111128',
902905
'isLayerHoldback': False,

0 commit comments

Comments
 (0)