From 8d961d6ce63637149c3e2e144508223b9e6ffd4b Mon Sep 17 00:00:00 2001 From: Jack Date: Fri, 30 Sep 2016 13:28:01 -0400 Subject: [PATCH] add get_availability --- lbrynet/core/BlobAvailability.py | 21 +++++++++++++++++++++ lbrynet/lbrynet_daemon/Daemon.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/lbrynet/core/BlobAvailability.py b/lbrynet/core/BlobAvailability.py index 6a17f3eed..890bd7e99 100644 --- a/lbrynet/core/BlobAvailability.py +++ b/lbrynet/core/BlobAvailability.py @@ -34,6 +34,27 @@ class BlobAvailabilityTracker(object): if self._check_mine.running: self._check_mine.stop() + def get_blob_availability(self, blob): + def _get_peer_count(peers): + have_blob = 0 + for peer in peers: + if peer.is_available(): + have_blob += 1 + return {blob: have_blob} + + d = self._peer_finder.find_peers_for_blob(blob) + d.addCallback(_get_peer_count) + return d + + def get_availability_for_blobs(self, blobs): + dl = [] + for blob in blobs: + if blob: + dl.append(self.get_blob_availability(blob)) + d = defer.DeferredList(dl) + d.addCallback(lambda results: [r[1] for r in results]) + return d + def _update_peers_for_blob(self, blob): def _save_peer_info(blob_hash, peers): v = {blob_hash: peers} diff --git a/lbrynet/lbrynet_daemon/Daemon.py b/lbrynet/lbrynet_daemon/Daemon.py index 7af9bc2e8..b2467531d 100644 --- a/lbrynet/lbrynet_daemon/Daemon.py +++ b/lbrynet/lbrynet_daemon/Daemon.py @@ -2581,6 +2581,36 @@ class Daemon(jsonrpc.JSONRPC): d = self._render_response(self.session.blob_tracker.last_mean_availability, OK_CODE) return d + def jsonrpc_get_availability(self, p): + """ + Get stream availability for a winning claim + + Arg: + name (str): lbry uri + + Returns: + peers per blob / total blobs + """ + + def _get_mean(blob_availabilities): + peer_counts = [] + for blob_availability in blob_availabilities: + for blob, peers in blob_availability.iteritems(): + peer_counts.append(peers) + return round(1.0 * sum(peer_counts) / len(peer_counts), 2) + + name = p['name'] + + d = self._resolve_name(name, force_refresh=True) + d.addCallback(get_sd_hash) + d.addCallback(self._download_sd_blob) + d.addCallback(lambda descriptor: [blob.get('blob_hash') for blob in descriptor['blobs']]) + d.addCallback(self.session.blob_tracker.get_availability_for_blobs) + d.addCallback(_get_mean) + d.addCallback(lambda result: self._render_response(result, OK_CODE)) + + return d + def get_lbrynet_version_from_github(): """Return the latest released version from github."""