-
Notifications
You must be signed in to change notification settings - Fork 36
feat(eventprocessor): Datamodel for event processor #192
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
232f0a0
feat: Add event_batch datamodels.
2320939
fix: unit test fixes.
fbb5a68
Merge branch 'master' into epmodels
mnoman09 8a5f531
Merge branch 'master' into sohail/pr-189
mnoman09 c829fca
Merge branch 'master' into sohail/pr-189
mnoman09 c3c9d46
Addressed feedback. Restructured classes.
msohailhussain 23ab6ce
fix: addressed more feedback
6474e98
update: removed print statements.
mnoman09 d4f2c66
Merge branch 'master' into sohail/pr-189
rashidsp 80b0963
fix: addressed minor feedback.
071460a
fix: addressed minor feedback.
900d96d
Merge branch 'sohail/pr-189' of github.com:optimizely/python-sdk into…
8fabcdb
Merge branch 'sohail/pr-189' of github.com:optimizely/python-sdk into…
a6ff8fe
Merge branch 'sohail/pr-189' of github.com:optimizely/python-sdk into…
mariamjamal94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Copyright 2019, 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# Copyright 2019 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. | ||
|
||
import json | ||
|
||
|
||
class EventBatch(object): | ||
""" Class respresenting Event Batch. """ | ||
|
||
def __init__(self, account_id, project_id, revision, client_name, client_version, | ||
anonymize_ip, enrich_decisions=True, visitors=None): | ||
self.account_id = account_id | ||
self.project_id = project_id | ||
self.revision = revision | ||
self.client_name = client_name | ||
self.client_version = client_version | ||
self.anonymize_ip = anonymize_ip | ||
self.enrich_decisions = enrich_decisions | ||
self.visitors = visitors or [] | ||
|
||
def __eq__(self, other): | ||
batch_obj = json.loads(json.dumps(self.__dict__, default=lambda o: o.__dict__), | ||
object_pairs_hook=self._dict_clean) | ||
return batch_obj == other | ||
|
||
def _dict_clean(self, obj): | ||
""" Helper method to remove keys from dictionary with None values. """ | ||
|
||
result = {} | ||
for k, v in obj: | ||
if v is None and k in ['revenue', 'value', 'tags', 'decisions']: | ||
continue | ||
else: | ||
result[k] = v | ||
return result | ||
|
||
|
||
class Decision(object): | ||
""" Class respresenting Decision. """ | ||
|
||
def __init__(self, campaign_id, experiment_id, variation_id): | ||
self.campaign_id = campaign_id | ||
self.experiment_id = experiment_id | ||
self.variation_id = variation_id | ||
|
||
|
||
class Snapshot(object): | ||
""" Class representing Snapshot. """ | ||
|
||
def __init__(self, events, decisions=None): | ||
self.events = events | ||
self.decisions = decisions | ||
|
||
|
||
class SnapshotEvent(object): | ||
""" Class representing Snapshot Event. """ | ||
|
||
def __init__(self, entity_id, uuid, key, timestamp, revenue=None, value=None, tags=None): | ||
self.entity_id = entity_id | ||
self.uuid = uuid | ||
self.key = key | ||
self.timestamp = timestamp | ||
self.revenue = revenue | ||
self.value = value | ||
self.tags = tags | ||
|
||
|
||
class Visitor(object): | ||
""" Class representing Visitor. """ | ||
|
||
def __init__(self, snapshots, attributes, visitor_id): | ||
self.snapshots = snapshots | ||
self.attributes = attributes | ||
self.visitor_id = visitor_id | ||
|
||
|
||
class VisitorAttribute(object): | ||
""" Class representing Visitor Attribute. """ | ||
|
||
def __init__(self, entity_id, key, attribute_type, value): | ||
self.entity_id = entity_id | ||
self.key = key | ||
self.type = attribute_type | ||
self.value = value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Copyright 2019 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. | ||
|
||
import time | ||
import uuid | ||
|
||
from optimizely import version | ||
|
||
CLIENT_NAME = 'python-sdk' | ||
|
||
|
||
class UserEvent(object): | ||
""" Class respresenting User Event. """ | ||
|
||
def __init__(self, event_context, user_id, visitor_attributes, bot_filtering=None): | ||
self.event_context = event_context | ||
self.user_id = user_id | ||
self.visitor_attributes = visitor_attributes | ||
self.bot_filtering = bot_filtering | ||
self.uuid = self._get_uuid() | ||
self.timestamp = self._get_time() | ||
|
||
def _get_time(self): | ||
return int(round(time.time() * 1000)) | ||
|
||
def _get_uuid(self): | ||
return str(uuid.uuid4()) | ||
|
||
|
||
class ImpressionEvent(UserEvent): | ||
""" Class representing Impression Event. """ | ||
|
||
def __init__(self, event_context, user_id, experiment, visitor_attributes, variation, bot_filtering=None): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can probably move user_id, bot_filtering and attributes to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feedback not addressed. Is there a reason for not moving this? |
||
super(ImpressionEvent, self).__init__(event_context, user_id, visitor_attributes, bot_filtering) | ||
self.experiment = experiment | ||
self.variation = variation | ||
|
||
|
||
class ConversionEvent(UserEvent): | ||
""" Class representing Conversion Event. """ | ||
|
||
def __init__(self, event_context, event, user_id, visitor_attributes, event_tags, bot_filtering=None): | ||
super(ConversionEvent, self).__init__(event_context, user_id, visitor_attributes, bot_filtering) | ||
self.event = event | ||
self.event_tags = event_tags | ||
|
||
|
||
class EventContext(object): | ||
""" Class respresenting User Event Context. """ | ||
|
||
def __init__(self, account_id, project_id, revision, anonymize_ip): | ||
self.account_id = account_id | ||
self.project_id = project_id | ||
self.revision = revision | ||
self.client_name = CLIENT_NAME | ||
self.client_version = version.__version__ | ||
self.anonymize_ip = anonymize_ip |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# Copyright 2019, 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. | ||
|
||
from optimizely import version | ||
from optimizely.event import payload | ||
from . import base | ||
|
||
|
||
class EventPayloadTest(base.BaseTest): | ||
aliabbasrizvi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def test_impression_event_equals_serialized_payload(self): | ||
expected_params = { | ||
'account_id': '12001', | ||
'project_id': '111001', | ||
'visitors': [{ | ||
'visitor_id': 'test_user', | ||
'attributes': [{ | ||
'type': 'custom', | ||
'value': 'test_value', | ||
'entity_id': '111094', | ||
'key': 'test_attribute' | ||
}], | ||
'snapshots': [{ | ||
'decisions': [{ | ||
'variation_id': '111129', | ||
'experiment_id': '111127', | ||
'campaign_id': '111182' | ||
}], | ||
'events': [{ | ||
'timestamp': 42123, | ||
'entity_id': '111182', | ||
'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c', | ||
'key': 'campaign_activated' | ||
}] | ||
}] | ||
}], | ||
'client_name': 'python-sdk', | ||
'client_version': version.__version__, | ||
'enrich_decisions': True, | ||
'anonymize_ip': False, | ||
'revision': '42' | ||
} | ||
|
||
batch = payload.EventBatch('12001', '111001', '42', 'python-sdk', version.__version__, | ||
False, True) | ||
visitor_attr = payload.VisitorAttribute('111094', 'test_attribute', 'custom', 'test_value') | ||
event = payload.SnapshotEvent('111182', 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c', 'campaign_activated', | ||
42123) | ||
event_decision = payload.Decision('111182', '111127', '111129') | ||
|
||
snapshots = payload.Snapshot([event], [event_decision]) | ||
user = payload.Visitor([snapshots], [visitor_attr], 'test_user') | ||
|
||
batch.visitors = [user] | ||
|
||
self.assertEqual(batch, expected_params) | ||
|
||
def test_conversion_event_equals_serialized_payload(self): | ||
expected_params = { | ||
'account_id': '12001', | ||
'project_id': '111001', | ||
'visitors': [{ | ||
'visitor_id': 'test_user', | ||
'attributes': [{ | ||
'type': 'custom', | ||
'value': 'test_value', | ||
'entity_id': '111094', | ||
'key': 'test_attribute' | ||
}, { | ||
'type': 'custom', | ||
'value': 'test_value2', | ||
'entity_id': '111095', | ||
'key': 'test_attribute2' | ||
}], | ||
'snapshots': [{ | ||
'events': [{ | ||
'timestamp': 42123, | ||
'entity_id': '111182', | ||
'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c', | ||
'key': 'campaign_activated', | ||
'revenue': 4200, | ||
'tags': { | ||
'non-revenue': 'abc', | ||
'revenue': 4200, | ||
'value': 1.234 | ||
}, | ||
'value': 1.234 | ||
}] | ||
}] | ||
}], | ||
'client_name': 'python-sdk', | ||
'client_version': version.__version__, | ||
'enrich_decisions': True, | ||
'anonymize_ip': False, | ||
'revision': '42' | ||
} | ||
|
||
batch = payload.EventBatch('12001', '111001', '42', 'python-sdk', version.__version__, | ||
False, True) | ||
visitor_attr_1 = payload.VisitorAttribute('111094', 'test_attribute', 'custom', 'test_value') | ||
visitor_attr_2 = payload.VisitorAttribute('111095', 'test_attribute2', 'custom', 'test_value2') | ||
event = payload.SnapshotEvent('111182', 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c', 'campaign_activated', | ||
42123, 4200, 1.234, {'revenue': 4200, 'value': 1.234, 'non-revenue': 'abc'}) | ||
|
||
snapshots = payload.Snapshot([event]) | ||
user = payload.Visitor([snapshots], [visitor_attr_1, visitor_attr_2], 'test_user') | ||
|
||
batch.visitors = [user] | ||
|
||
self.assertEqual(batch, expected_params) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.