add get_availability

This commit is contained in:
Jack 2016-09-30 13:28:01 -04:00
parent 0a612ba36c
commit 8d961d6ce6
2 changed files with 51 additions and 0 deletions

View file

@ -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}

View file

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