From b201fc331c2628a089bce36bfcf770a62463d5f6 Mon Sep 17 00:00:00 2001 From: Job Evers-Meltzer Date: Wed, 28 Sep 2016 09:06:51 -0700 Subject: [PATCH 1/2] simplify callback when downloading --- lbrynet/lbrynet_daemon/LBRYDaemon.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lbrynet/lbrynet_daemon/LBRYDaemon.py b/lbrynet/lbrynet_daemon/LBRYDaemon.py index eb57d50c0..17157e38b 100644 --- a/lbrynet/lbrynet_daemon/LBRYDaemon.py +++ b/lbrynet/lbrynet_daemon/LBRYDaemon.py @@ -2650,16 +2650,14 @@ class _DownloadNameHelper(object): def _setup_stream(self, stream_info): stream_hash = get_sd_hash(stream_info) 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 - def _add_results_callback(self, stream_info): - def add_results(l): - if l: - if os.path.isfile(os.path.join(self.download_directory, l.file_name)): - return defer.succeed((stream_info, l)) - return defer.succeed((stream_info, None)) - return add_results + def _prepend_stream_info(self, lbry_file, stream_info): + if lbry_file: + if os.path.isfile(os.path.join(self.download_directory, lbry_file.file_name)): + return defer.succeed((stream_info, lbry_file)) + return defer.succeed((stream_info, None)) def wait_or_get_stream(self, args): stream_info, lbry_file = args From 6a72b3683e617658ad5d74959f5563b27d8967fe Mon Sep 17 00:00:00 2001 From: Job Evers-Meltzer Date: Wed, 28 Sep 2016 09:07:25 -0700 Subject: [PATCH 2/2] send Download Started event --- lbrynet/analytics/events.py | 29 ++++++++++++++++++++++++++++ lbrynet/lbrynet_daemon/LBRYDaemon.py | 6 +++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lbrynet/analytics/events.py b/lbrynet/analytics/events.py index 3f6e58135..8b1c63287 100644 --- a/lbrynet/analytics/events.py +++ b/lbrynet/analytics/events.py @@ -1,6 +1,21 @@ +import logging + 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): def __init__(self, context, lbry_id, session_id): self.context = context @@ -19,6 +34,20 @@ class Events(object): '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): # TODO: distinguish between developer and release instances diff --git a/lbrynet/lbrynet_daemon/LBRYDaemon.py b/lbrynet/lbrynet_daemon/LBRYDaemon.py index 17157e38b..ce7ea97af 100644 --- a/lbrynet/lbrynet_daemon/LBRYDaemon.py +++ b/lbrynet/lbrynet_daemon/LBRYDaemon.py @@ -561,10 +561,13 @@ class LBRYDaemon(jsonrpc.JSONRPC): self.send_heartbeat.start(60) def _send_heartbeat(self): - log.debug('Sending heartbeat') heartbeat = self._events.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): r = { "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. If it already exists in the file manager, return the existing lbry file """ + self._send_download_started(name) helper = _DownloadNameHelper( self, name, timeout, download_directory, file_name, wait_for_write)