claim_list and claim_list_mine in Daemon return sorted results

This commit is contained in:
Sergey Rozhnov 2018-05-16 18:29:44 +04:00 committed by jleute
parent bc6fd892a2
commit 4aa503b863
4 changed files with 92 additions and 1 deletions

View file

@ -36,6 +36,7 @@ from lbrynet.daemon.Downloader import GetStream
from lbrynet.daemon.Publisher import Publisher from lbrynet.daemon.Publisher import Publisher
from lbrynet.daemon.ExchangeRateManager import ExchangeRateManager from lbrynet.daemon.ExchangeRateManager import ExchangeRateManager
from lbrynet.daemon.auth.server import AuthJSONRPCServer from lbrynet.daemon.auth.server import AuthJSONRPCServer
from lbrynet.daemon.claims_comparator import arrange_results
from lbrynet.core.PaymentRateManager import OnlyFreePaymentsManager from lbrynet.core.PaymentRateManager import OnlyFreePaymentsManager
from lbrynet.core import utils, system_info from lbrynet.core import utils, system_info
from lbrynet.core.StreamDescriptor import StreamDescriptorIdentifier, download_sd_blob from lbrynet.core.StreamDescriptor import StreamDescriptorIdentifier, download_sd_blob
@ -2293,6 +2294,7 @@ class Daemon(AuthJSONRPCServer):
""" """
d = self.session.wallet.get_name_claims() d = self.session.wallet.get_name_claims()
d.addCallback(arrange_results)
d.addCallback(lambda claims: self._render_response(claims)) d.addCallback(lambda claims: self._render_response(claims))
return d return d
@ -2331,7 +2333,8 @@ class Daemon(AuthJSONRPCServer):
""" """
claims = yield self.session.wallet.get_claims_for_name(name) claims = yield self.session.wallet.get_claims_for_name(name)
defer.returnValue(claims) result = arrange_results(claims)
defer.returnValue(result)
@defer.inlineCallbacks @defer.inlineCallbacks
def jsonrpc_claim_list_by_channel(self, page=0, page_size=10, uri=None, uris=[]): def jsonrpc_claim_list_by_channel(self, page=0, page_size=10, uri=None, uris=[]):

View file

@ -0,0 +1,36 @@
_comparison_order = ['height', 'name', 'claim_id'] # TODO outpoint
def arrange_results(claims):
for claim in claims:
results = claim['result']
sorted_results = sorted(results, cmp=_compare_results)
claim['result'] = sorted_results
return claims
def _compare_results(left, right):
"""
:type left: dict
:type right: dict
"""
result = 0
for attribute in _comparison_order:
left_value = left[attribute]
right_value = right[attribute]
sub_result = _cmp(left_value, right_value)
if sub_result is not 0:
result = sub_result
break
return result
def _cmp(left, right):
if left == right:
return 0
elif left < right:
return -1
else:
return 1

View file

View file

@ -0,0 +1,52 @@
import unittest
from lbrynet.daemon.claims_comparator import arrange_results
class ClaimsComparatorTest(unittest.TestCase):
def test_arrange_results(self):
results = [
{
'height': 1,
'name': 'res',
'claim_id': 'ccc'
},
{
'height': 1,
'name': 'res',
'claim_id': 'aaa'
},
{
'height': 1,
'name': 'res',
'claim_id': 'bbb'
}
]
data = {'result': results}
expected = [
{
'height': 1,
'name': 'res',
'claim_id': 'aaa'
},
{
'height': 1,
'name': 'res',
'claim_id': 'bbb'
},
{
'height': 1,
'name': 'res',
'claim_id': 'ccc'
}
]
claims = arrange_results([data])
claim = claims[0]
actual = claim['result']
self.assertEqual(expected, actual)
if __name__ == '__main__':
unittest.main()