new: [server/api] improved children handling - if too many children are

returned it's stripped - and a sample is extracted
This commit is contained in:
Alexandre Dulaunoy 2021-12-04 13:59:19 +01:00
parent 0357542bbd
commit add29e6ca3
Signed by: adulau
GPG key ID: 09E2CD4944E6CBCD

View file

@ -1,4 +1,4 @@
version = "1.1" version = "1.2"
from flask import Flask, url_for, send_from_directory, render_template, make_response, request from flask import Flask, url_for, send_from_directory, render_template, make_response, request
from flask_restx import Resource, Api, reqparse from flask_restx import Resource, Api, reqparse
import redis import redis
@ -152,8 +152,15 @@ class lookup(Resource):
h['parents'] = parents h['parents'] = parents
if rdb.exists("c:{}".format(sha1)): if rdb.exists("c:{}".format(sha1)):
children = [] children = []
for child in rdb.smembers("c:{}".format(sha1)): card = rdb.scard("c:{}".format(sha1))
children.append(child) if card <= 15:
c = rdb.smembers("c:{}".format(sha1))
else:
c = rdb.srandmember("c:{}".format(sha1), number=10)
h['hashlookup:children-total'] = card
for child in c:
child_details = rdb.hgetall("h:{}".format(child))
children.append(child_details)
h['children'] = children h['children'] = children
h = calculate_trust(hobject=h) h = calculate_trust(hobject=h)
return h return h
@ -205,12 +212,20 @@ class lookup(Resource):
for parent in p: for parent in p:
parent_details = rdb.hgetall("h:{}".format(parent)) parent_details = rdb.hgetall("h:{}".format(parent))
parents.append(parent_details) parents.append(parent_details)
h['parents'] = parents h['parents'] = parents
if rdb.exists("c:{}".format(k)): if rdb.exists("c:{}".format(k)):
children = [] children = []
for child in rdb.smembers("c:{}".format(k)): card = rdb.scard("c:{}".format(k))
children.append(child) if card <= 15:
c = rdb.smembers("c:{}".format(k))
else:
c = rdb.srandmember("c:{}".format(k), number=10)
h['hashlookup:children-total'] = card
for child in c:
child_details = rdb.hgetall("h:{}".format(child))
children.append(child_details)
h['children'] = children h['children'] = children
h = calculate_trust(hobject=h) h = calculate_trust(hobject=h)
return h return h
@ -268,9 +283,17 @@ class lookup(Resource):
h['parents'] = parents h['parents'] = parents
if rdb.exists("c:{}".format(sha1)): if rdb.exists("c:{}".format(sha1)):
children = [] children = []
for child in rdb.smembers("c:{}".format(sha1)): card = rdb.scard("c:{}".format(sha1))
children.append(child) if card <= 15:
c = rdb.smembers("c:{}".format(sha1))
else:
c = rdb.srandmember("c:{}".format(sha1), number=10)
h['hashlookup:children-total'] = card
for child in c:
child_details = rdb.hgetall("h:{}".format(child))
children.append(child_details)
h['children'] = children h['children'] = children
h = calculate_trust(hobject=h) h = calculate_trust(hobject=h)
return h return h