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-07-20 19:00:34 +02:00
|
|
|
|
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:
|
2017-04-05 01:42:35 +02:00
|
|
|
return stream_info['stream']['source']['source']
|
2016-09-28 18:07:25 +02:00
|
|
|
except (KeyError, TypeError, ValueError):
|
|
|
|
log.debug('Failed to get sd_hash from %s', stream_info, exc_info=True)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2016-07-20 19:00:34 +02:00
|
|
|
class Events(object):
|
2017-02-02 16:23:17 +01:00
|
|
|
def __init__(self, context, installation_id, 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`
|
2017-02-02 16:23:17 +01:00
|
|
|
installation_id: id unique to this installation. Can be anything, but
|
2016-12-21 19:53:55 +01:00
|
|
|
generally should be base58 encoded.
|
|
|
|
session_id: id for tracking events during this session. Can be
|
|
|
|
anything, but generally should be base58 encoded.
|
|
|
|
"""
|
2016-07-20 19:00:34 +02:00
|
|
|
self.context = context
|
2017-02-02 16:23:17 +01:00
|
|
|
self.installation_id = installation_id
|
2016-07-20 19:00:34 +02:00
|
|
|
self.session_id = session_id
|
|
|
|
|
2016-11-10 21:49:51 +01:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
|
2016-07-20 19:00:34 +02:00
|
|
|
def heartbeat(self):
|
2016-09-30 06:15:39 +02:00
|
|
|
return self._event('Heartbeat')
|
2016-07-20 19:00:34 +02:00
|
|
|
|
2016-12-30 19:35:17 +01:00
|
|
|
def download_started(self, *args, **kwargs):
|
|
|
|
properties = download_properties(*args, **kwargs)
|
2016-09-30 06:15:39 +02:00
|
|
|
return self._event('Download Started', properties)
|
|
|
|
|
2016-12-30 19:35:17 +01:00
|
|
|
def download_errored(self, *args, **kwargs):
|
|
|
|
properties = download_properties(*args, **kwargs)
|
2016-12-30 18:37:11 +01:00
|
|
|
return self._event('Download Errored', properties)
|
|
|
|
|
2016-12-30 19:35:17 +01:00
|
|
|
def download_finished(self, *args, **kwargs):
|
|
|
|
properties = download_properties(*args, **kwargs)
|
2016-12-30 18:37:11 +01:00
|
|
|
return self._event('Download Finished', properties)
|
|
|
|
|
2017-01-29 22:47:00 +01:00
|
|
|
def error(self, log_record):
|
|
|
|
"""Record when a log message of ERROR or higher was emitted"""
|
2016-11-10 21:49:51 +01:00
|
|
|
properties = {
|
2017-01-29 22:47:00 +01:00
|
|
|
'message': log_record.message,
|
|
|
|
'module': log_record.module,
|
|
|
|
'lineno': log_record.lineno,
|
|
|
|
'name': log_record.name,
|
2017-02-11 20:47:45 +01:00
|
|
|
'traceback': log_record.exc_text,
|
2016-11-10 21:49:51 +01:00
|
|
|
}
|
|
|
|
return self._event('Error', properties)
|
|
|
|
|
2016-09-30 06:26:27 +02:00
|
|
|
def metric_observed(self, metric_name, value):
|
|
|
|
properties = {
|
2016-10-11 22:11:59 +02:00
|
|
|
'value': value,
|
2016-09-30 06:26:27 +02:00
|
|
|
}
|
2016-10-11 22:11:59 +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 = {
|
2017-02-02 16:23:17 +01:00
|
|
|
'lbry_id': self.installation_id,
|
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
|
|
|
|
2016-07-20 19:00:34 +02:00
|
|
|
|
2016-11-10 21:49:51 +01:00
|
|
|
def make_context(platform, wallet):
|
2016-07-20 19:00:34 +02:00
|
|
|
return {
|
|
|
|
'app': {
|
|
|
|
'name': 'lbrynet',
|
|
|
|
'version': platform['lbrynet_version'],
|
|
|
|
'python_version': platform['python_version'],
|
2016-11-10 21:49:51 +01:00
|
|
|
'build': platform['build'],
|
2016-07-20 19:00:34 +02:00
|
|
|
'wallet': {
|
|
|
|
'name': wallet,
|
2016-11-10 20:26:21 +01:00
|
|
|
'version': platform['lbryum_version'] if wallet == LBRYUM_WALLET else None
|
2016-07-20 19:00:34 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
# 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'
|
|
|
|
},
|
|
|
|
}
|
2016-12-30 19:35:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
def download_properties(id_, name, stream_info=None):
|
|
|
|
return {
|
|
|
|
'download_id': id_,
|
|
|
|
'name': name,
|
|
|
|
'stream_info': get_sd_hash(stream_info)
|
|
|
|
}
|