Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Feature/add timestamp to track #165

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions splitio/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ def _record_stats(self, impressions, start, operation):
self._logger.error('Error recording impressions and metrics')
self._logger.debug('Error: ', exc_info=True)

def track(self, key, traffic_type, event_type, value=None, properties=None):
def track(self, key, traffic_type, event_type, value=None, properties=None, timestamp=None): # pylint: disable=too-many-arguments
"""
Track an event.

Expand All @@ -380,6 +380,9 @@ def track(self, key, traffic_type, event_type, value=None, properties=None):
:type value: Number
:param properties: (Optional) properties associated to the event
:type properties: dict
:param timestamp: (Optional) Represents the time in milliseconds since epoch that the event
occurred
:type properties: int

:return: Whether the event was created or not.
:rtype: bool
Expand All @@ -399,17 +402,22 @@ def track(self, key, traffic_type, event_type, value=None, properties=None):

value = input_validator.validate_value(value)
valid, properties, size = input_validator.valid_properties(properties)
timestamp = input_validator.validate_timestamp(timestamp)

# pylint: disable=too-many-boolean-expressions
if key is None or event_type is None or traffic_type is None or value is False \
or valid is False:
or valid is False or timestamp is False:
return False

if timestamp is None:
timestamp = int(time.time()*1000)

event = Event(
key=key,
traffic_type_name=traffic_type,
event_type_id=event_type,
value=value,
timestamp=int(time.time()*1000),
timestamp=timestamp,
properties=properties,
)
return self._events_storage.put([EventWrapper(
Expand Down
16 changes: 16 additions & 0 deletions splitio/client/input_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,3 +545,19 @@ def valid_properties(properties):
_LOGGER.warning('Event has more than 300 properties. Some of them will be trimmed' +
' when processed')
return True, valid_properties if len(valid_properties) else None, size

def validate_timestamp(timestamp):
"""
Check if timestamp is valid for track.

:param timestamp: timestamp to be checked
:type timestamp: int
:return: timestamp
:rtype: int|None
"""
if timestamp is None:
return None
if not isinstance(timestamp, int):
_LOGGER.error('track: timestamp must be an integer.')
return False
return timestamp
15 changes: 15 additions & 0 deletions tests/client/test_input_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,21 @@ def test_track(self, mocker):
mocker.call("The maximum size allowed for the properties is 32768 bytes. Current one is 32952 bytes. Event not queued")
]

client._logger.reset_mock()
assert client.track("some_key", "traffic_type", "event_type", 1, None, None) is True
assert client._logger.error.mock_calls == []

client._logger.reset_mock()
assert client.track("some_key", "traffic_type", "event_type", 1, None, 1573936400000) is True
assert client._logger.error.mock_calls == []

# Test track with invalid timestamp
client._logger.reset_mock()
assert client.track("some_key", "traffic_type", "event_type", 1, None, "invalid_timestamp") is False
assert client._logger.error.mock_calls == [
mocker.call("track: timestamp must be an integer.")
]

def test_get_treatments(self, mocker):
"""Test getTreatments() method."""
split_mock = mocker.Mock(spec=Split)
Expand Down