Skip to content
Snippets Groups Projects
Commit 01f20fc7 authored by Robert Latta's avatar Robert Latta
Browse files

fixed carriers; added max-depth option

parent ac346fd5
No related branches found
No related tags found
No related merge requests found
......@@ -122,64 +122,70 @@ def circuit_tree(root_identifier: str):
strtobool(request.args.get('carriers', default='false', type=str))
interface_ = \
strtobool(request.args.get('interface', default='false', type=str))
max_depth = request.args.get('max-depth', default=0, type=int)
if carriers:
children_prop = 'carrier-circuits'
else:
children_prop = 'sub-circuits'
config = current_app.config["INVENTORY_PROVIDER_CONFIG"]
r = worker_common.get_current_redis(config)
def _get_childcircuit_tree_local(circuit_id):
circuit = r.get(f'ims:circuit_hierarchy:{circuit_id}')
if not circuit:
return None
circuit = json.loads(circuit.decode('utf-8'))[0]
_tree = [
f'{circuit["id"]} -- {circuit["name"]} -- '
f'prod: {circuit["product"]} -- '
f'spd: {circuit["speed"]} -- '
f'status: {circuit["status"]}'
]
if circuit.get(children_prop, None):
children = []
for child_id in circuit[children_prop]:
v = _get_childcircuit_tree_local(child_id)
if v:
children.append(v)
_tree.append(children)
else:
_tree.append([])
return _tree
config = current_app.config["INVENTORY_PROVIDER_CONFIG"]
r = worker_common.get_current_redis(config)
if interface_:
def _get_childcircuit_tree_local(circuit_id, depth):
circuit = r.get(f'ims:circuit_hierarchy:{circuit_id}')
if not circuit:
return None
circuit = json.loads(circuit.decode('utf-8'))[0]
_tree = [
f'{circuit["id"]} -- {circuit["name"]} -- '
f'prod: {circuit["product"]} -- '
f'spd: {circuit["speed"]} -- '
f'status: {circuit["status"]}'
]
if circuit.get(children_prop, None):
if max_depth != 0 and depth >= max_depth:
_tree.append([['...', []]])
return _tree
children = []
for child_id in circuit[children_prop]:
depth += 1
v = _get_childcircuit_tree_local(child_id, depth)
if v:
children.append(v)
_tree.append(children)
else:
_tree.append([])
return _tree
if_services = r.get(f'ims:interface_services:{root_identifier}')
if if_services:
logger.debug('1')
root_identifiers = [s['id'] for s in json.loads(if_services)]
children = []
for id_ in root_identifiers:
children.append(_get_childcircuit_tree_local(id_))
if interface_:
tree = [root_identifier, children]
else:
return "Nothing found"
if_services = r.get(f'ims:interface_services:{root_identifier}')
if if_services:
root_identifiers = [s['id'] for s in json.loads(if_services)]
children = []
for id_ in root_identifiers:
children.append(_get_childcircuit_tree_local(id_, 1))
tree = [root_identifier, children]
else:
try:
root_identifier = int(root_identifier)
except ValueError:
for k in r.scan_iter('ims:circuit_hierarchy:*', count=2000):
ch = r.get(k.decode('utf-8'))
details = json.loads(ch.decode('utf-8'))[0]
if root_identifier.lower() == details['name'].lower():
root_identifier = details['id']
break
else:
return f'No circuit found for: {root_identifier}'
tree = _get_childcircuit_tree_local(root_identifier)
return f'<pre>{format_tree(tree, format_node = itemgetter(0), get_children = itemgetter(1))}</pre>'
return "Nothing found"
else:
try:
root_identifier = int(root_identifier)
except ValueError:
for k in r.scan_iter('ims:circuit_hierarchy:*', count=2000):
ch = r.get(k.decode('utf-8'))
details = json.loads(ch.decode('utf-8'))[0]
if root_identifier.lower() == details['name'].lower():
root_identifier = details['id']
break
else:
return f'No circuit found for: {root_identifier}'
tree = _get_childcircuit_tree_local(root_identifier, 1)
f = format_tree(
tree,
format_node=itemgetter(0),
get_children=itemgetter(1))
return f'<pre>{f}</pre>'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment