Skip to content

Commit

Permalink
Merge pull request #43 from yuvipanda/override-timestamp
Browse files Browse the repository at this point in the history
Allow overriding timestamp of event
  • Loading branch information
Zsailer committed Feb 13, 2020
2 parents a23121a + 221e3f7 commit ede6d17
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
8 changes: 6 additions & 2 deletions jupyter_telemetry/eventlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def register_schema(self, schema):

self.schemas[(schema['$id'], schema['version'])] = schema

def record_event(self, schema_name, version, event):
def record_event(self, schema_name, version, event, timestamp_override=None):
"""
Record given event with schema has occurred.
"""
Expand All @@ -140,8 +140,12 @@ def record_event(self, schema_name, version, event):
schema = self.schemas[(schema_name, version)]
jsonschema.validate(event, schema)

if timestamp_override is None:
timestamp = datetime.utcnow()
else:
timestamp = timestamp_override
capsule = {
'__timestamp__': datetime.utcnow().isoformat() + 'Z',
'__timestamp__': timestamp.isoformat() + 'Z',
'__schema__': schema_name,
'__schema_version__': version,
'__metadata_version__': TELEMETRY_METADATA_VERSION
Expand Down
31 changes: 31 additions & 0 deletions tests/test_register_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tempfile

import jsonschema
from datetime import datetime, timedelta
import pytest
from ruamel.yaml import YAML

Expand Down Expand Up @@ -60,6 +61,36 @@ def test_reserved_properties():
})


def test_timestamp_override():
"""
Simple test for overriding timestamp
"""
schema = {
'$id': 'test/test',
'version': 1,
'properties': {
'something': {
'type': 'string'
},
},
}

output = io.StringIO()
handler = logging.StreamHandler(output)
el = EventLog(handlers=[handler])
el.register_schema(schema)
el.allowed_schemas = ['test/test']

timestamp_override = datetime.utcnow() - timedelta(days=1)
el.record_event('test/test', 1, {
'something': 'blah',
}, timestamp_override=timestamp_override)
handler.flush()

event_capsule = json.loads(output.getvalue())

assert event_capsule['__timestamp__'] == timestamp_override.isoformat() + 'Z'

def test_record_event():
"""
Simple test for emitting valid events
Expand Down

0 comments on commit ede6d17

Please sign in to comment.