Compress json depth to 8 (HTB max)

This commit is contained in:
Robert Chacón 2023-01-15 22:03:38 -07:00 committed by GitHub
parent 9afc07cec2
commit 5cb5e8e7aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -528,6 +528,38 @@ def refreshShapers():
minDownload, minUpload = findBandwidthMins(network, 0)
logging.info("Found the bandwidth minimums for each node")
# Compress network.json. HTB only supports 8 levels of HTB depth. Compress to 8 layers if beyond 8.
def flattenB(data):
newDict = {}
for node in data:
if isinstance(node, str):
if (isinstance(data[node], dict)) and (node != 'children'):
newDict[node] = dict(data[node])
if 'children' in data[node]:
result = flattenB(data[node]['children'])
del newDict[node]['children']
newDict.update(result)
return newDict
def flattenA(data, depth):
newDict = {}
for node in data:
if isinstance(node, str):
if (isinstance(data[node], dict)) and (node != 'children'):
newDict[node] = dict(data[node])
if 'children' in data[node]:
result = flattenA(data[node]['children'], depth+2)
del newDict[node]['children']
if depth <= 8:
newDict[node]['children'] = result
else:
flattened = flattenB(data[node]['children'])
if 'children' in newDict[node]:
newDict[node]['children'].update(flattened)
else:
newDict[node]['children'] = flattened
return newDict
network = flattenA(network, 1)
# Parse network structure and add devices from ShapedDevices.csv
parentNodes = []
minorByCPUpreloaded = {}