lbry-sdk/lbrynet/analytics/events.py

113 lines
3.3 KiB
Python
Raw Normal View History

2016-09-28 18:07:25 +02:00
import logging
2016-09-30 06:15:39 +02:00
from lbrynet.core import utils
2016-11-10 20:26:21 +01:00
from lbrynet.conf import LBRYUM_WALLET
2016-09-28 18:07:25 +02:00
log = logging.getLogger(__name__)
def get_sd_hash(stream_info):
if not stream_info:
return None
try:
return stream_info['sources']['lbry_sd_hash']
except (KeyError, TypeError, ValueError):
log.debug('Failed to get sd_hash from %s', stream_info, exc_info=True)
return None
class Events(object):
def __init__(self, context, lbryid, session_id):
2016-12-21 19:53:55 +01:00
"""Contains all of the analytics events that can be sent
Attributes:
context: usually the output of `make_context`
lbryid: id unique to this installation. Can be anything, but
generally should be base58 encoded.
session_id: id for tracking events during this session. Can be
anything, but generally should be base58 encoded.
"""
self.context = context
self.lbryid = lbryid
self.session_id = session_id
def update_context(self, context):
self.context = context
def server_startup(self):
return self._event('Server Startup')
def server_startup_success(self):
return self._event('Server Startup Success')
def server_startup_error(self, message):
return self._event('Server Startup Error', {
'message': message,
})
def heartbeat(self):
2016-09-30 06:15:39 +02:00
return self._event('Heartbeat')
2016-09-28 18:07:25 +02:00
def download_started(self, name, stream_info=None):
2016-09-30 06:15:39 +02:00
properties = {
'name': name,
'stream_info': get_sd_hash(stream_info)
}
return self._event('Download Started', properties)
def error(self, message, sd_hash=None):
properties = {
'message': message,
'stream_info': sd_hash
}
return self._event('Error', properties)
2016-09-30 06:26:27 +02:00
def metric_observed(self, metric_name, value):
properties = {
'value': value,
2016-09-30 06:26:27 +02:00
}
return self._event(metric_name, properties)
2016-09-30 06:26:27 +02:00
2016-09-30 06:15:39 +02:00
def _event(self, event, event_properties=None):
2016-09-28 18:07:25 +02:00
return {
'userId': 'lbry',
2016-09-30 06:15:39 +02:00
'event': event,
'properties': self._properties(event_properties),
2016-09-28 18:07:25 +02:00
'context': self.context,
2016-09-30 06:15:39 +02:00
'timestamp': utils.isonow()
}
def _properties(self, event_properties=None):
event_properties = event_properties or {}
properties = {
'lbry_id': self.lbryid,
2016-09-30 06:15:39 +02:00
'session_id': self.session_id,
2016-09-28 18:07:25 +02:00
}
2016-09-30 06:15:39 +02:00
properties.update(event_properties)
return properties
2016-09-28 18:07:25 +02:00
def make_context(platform, wallet):
return {
'app': {
'name': 'lbrynet',
'version': platform['lbrynet_version'],
'ui_version': platform['ui_version'],
'python_version': platform['python_version'],
'build': platform['build'],
'wallet': {
'name': wallet,
2016-11-10 20:26:21 +01:00
'version': platform['lbryum_version'] if wallet == LBRYUM_WALLET else None
},
},
# TODO: expand os info to give linux/osx specific info
'os': {
'name': platform['os_system'],
'version': platform['os_release']
},
'library': {
'name': 'lbrynet-analytics',
'version': '1.0.0'
},
}