Merge pull request #176 from lbryio/start-stream-event

Start stream event
This commit is contained in:
Job Evers‐Meltzer 2016-09-28 12:15:56 -07:00 committed by GitHub
commit e08a681c8f
2 changed files with 40 additions and 9 deletions

View file

@ -1,6 +1,21 @@
import logging
from lbrynet.analytics import utils from lbrynet.analytics import utils
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): class Events(object):
def __init__(self, context, lbry_id, session_id): def __init__(self, context, lbry_id, session_id):
self.context = context self.context = context
@ -19,6 +34,20 @@ class Events(object):
'timestamp': utils.now() 'timestamp': utils.now()
} }
def download_started(self, name, stream_info=None):
return {
'userId': 'lbry',
'event': 'Download Started',
'properties': {
'lbry_id': self.lbry_id,
'session_id': self.session_id,
'name': name,
'stream_info': get_sd_hash(stream_info)
},
'context': self.context,
'timestamp': utils.now()
}
def make_context(platform, wallet, is_dev=False): def make_context(platform, wallet, is_dev=False):
# TODO: distinguish between developer and release instances # TODO: distinguish between developer and release instances

View file

@ -561,10 +561,13 @@ class LBRYDaemon(jsonrpc.JSONRPC):
self.send_heartbeat.start(60) self.send_heartbeat.start(60)
def _send_heartbeat(self): def _send_heartbeat(self):
log.debug('Sending heartbeat')
heartbeat = self._events.heartbeat() heartbeat = self._events.heartbeat()
self.analytics_api.track(heartbeat) self.analytics_api.track(heartbeat)
def _send_download_started(self, name, stream_info=None):
event = self._events.download_started(name, stream_info)
self.analytics_api.track(event)
def _get_platform(self): def _get_platform(self):
r = { r = {
"processor": platform.processor(), "processor": platform.processor(),
@ -1129,6 +1132,7 @@ class LBRYDaemon(jsonrpc.JSONRPC):
Add a lbry file to the file manager, start the download, and return the new lbry file. Add a lbry file to the file manager, start the download, and return the new lbry file.
If it already exists in the file manager, return the existing lbry file If it already exists in the file manager, return the existing lbry file
""" """
self._send_download_started(name)
helper = _DownloadNameHelper( helper = _DownloadNameHelper(
self, name, timeout, download_directory, file_name, wait_for_write) self, name, timeout, download_directory, file_name, wait_for_write)
@ -2650,16 +2654,14 @@ class _DownloadNameHelper(object):
def _setup_stream(self, stream_info): def _setup_stream(self, stream_info):
stream_hash = get_sd_hash(stream_info) stream_hash = get_sd_hash(stream_info)
d = self.daemon._get_lbry_file_by_sd_hash(stream_hash) d = self.daemon._get_lbry_file_by_sd_hash(stream_hash)
d.addCallback(self._add_results_callback(stream_info)) d.addCallback(self._prepend_stream_info, stream_info)
return d return d
def _add_results_callback(self, stream_info): def _prepend_stream_info(self, lbry_file, stream_info):
def add_results(l): if lbry_file:
if l: if os.path.isfile(os.path.join(self.download_directory, lbry_file.file_name)):
if os.path.isfile(os.path.join(self.download_directory, l.file_name)): return defer.succeed((stream_info, lbry_file))
return defer.succeed((stream_info, l))
return defer.succeed((stream_info, None)) return defer.succeed((stream_info, None))
return add_results
def wait_or_get_stream(self, args): def wait_or_get_stream(self, args):
stream_info, lbry_file = args stream_info, lbry_file = args