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."""