|
@@ -69,6 +69,53 @@ class BaseStorage(object):
|
|
"""
|
|
"""
|
|
pass
|
|
pass
|
|
|
|
|
|
|
|
+ def merge_new_data(self, newdata):
|
|
|
|
+ """Updates data in the storage by merging the new data."""
|
|
|
|
+
|
|
|
|
+ if newdata is None or not isinstance(newdata, dict):
|
|
|
|
+ raise ValueError("Expected a dict as new data.")
|
|
|
|
+
|
|
|
|
+ # start merge on a copy of the current data
|
|
|
|
+ current = ffstatus.dict_merge(self.data, {})
|
|
|
|
+ for item_id in current:
|
|
|
|
+ if not item_id in newdata:
|
|
|
|
+ continue
|
|
|
|
+
|
|
|
|
+ current[item_id]['aliases'] = []
|
|
|
|
+ current[item_id]['clients'] = []
|
|
|
|
+ current[item_id]['neighbours'] = []
|
|
|
|
+
|
|
|
|
+ if not '__RAW__' in current[item_id]:
|
|
|
|
+ current[item_id]['__RAW__'] = {}
|
|
|
|
+
|
|
|
|
+ if '__RAW__' in newdata[item_id]:
|
|
|
|
+ for key in newdata[item_id]['__RAW__']:
|
|
|
|
+ if key in current[item_id]['__RAW__']:
|
|
|
|
+ del current[item_id]['__RAW__'][key]
|
|
|
|
+
|
|
|
|
+ # merge the dictionaries
|
|
|
|
+ updated = ffstatus.dict_merge(current, newdata)
|
|
|
|
+
|
|
|
|
+ # sanitize each item's data
|
|
|
|
+ for itemid in updated:
|
|
|
|
+ if itemid.startswith('__'):
|
|
|
|
+ continue
|
|
|
|
+ item = updated[itemid]
|
|
|
|
+
|
|
|
|
+ # remove node's MACs from clients list
|
|
|
|
+ clients = [x for x in item.get('clients', [])]
|
|
|
|
+ if 'mac' in item and item['mac'] in clients:
|
|
|
|
+ clients.remove(item['mac'])
|
|
|
|
+ for mac in item.get('macs', []):
|
|
|
|
+ if mac in clients:
|
|
|
|
+ clients.remove(mac)
|
|
|
|
+
|
|
|
|
+ # set clientcount
|
|
|
|
+ updated[itemid]['clientcount'] = len(clients)
|
|
|
|
+
|
|
|
|
+ # set the new data
|
|
|
|
+ self.__data = updated
|
|
|
|
+
|
|
def get_nodes(self, sortby=None, include_raw_data=False):
|
|
def get_nodes(self, sortby=None, include_raw_data=False):
|
|
"""Gets a list of all known nodes."""
|
|
"""Gets a list of all known nodes."""
|
|
|
|
|