Browse Source

BaseStorage: prefer .get_nodes() instead of .data

Helge Jung 9 years ago
parent
commit
120726a1f5
1 changed files with 16 additions and 23 deletions
  1. 16 23
      ffstatus/basestorage.py

+ 16 - 23
ffstatus/basestorage.py

@@ -89,12 +89,7 @@ class BaseStorage(object):
         sum_clients = 0
         clients = set()
 
-        for item_id in self.data:
-            if item_id.startswith('__'):
-                continue
-
-            node = self.data[item_id]
-
+        for node in self.get_nodes():
             nodes += 1
             if self.get_nodestatus(node=node) == 'active':
                 nodes_active += 1
@@ -122,11 +117,13 @@ class BaseStorage(object):
             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:
+        current = {}
+        for node in self.get_nodes():
+            item_id = node['node_id']
             if not item_id in newdata:
                 continue
 
+            current[item_id] = ffstatus.dict_merge(node, {})
             current[item_id]['aliases'] = []
             current[item_id]['clients'] = []
             current[item_id]['neighbours'] = []
@@ -201,20 +198,20 @@ class BaseStorage(object):
         If necessary, look through node aliases.
         """
 
-        # if we have a direct hit, return it immediately
-        if rawid in self.data:
-            return sanitize_node(self.data[rawid])
+        # look through all nodes
+        found = None
+        for node in self.get_nodes():
+            # if we have a direct hit, return it immediately
+            if node['node_id'] == rawid:
+                return sanitize_node(node)
 
-        # no direct hit -> search via aliases
-        nodeid = rawid
-        for nid in self.data:
-            node = self.data[nid]
+            # search through aliases
             if 'aliases' in node and rawid in node['aliases']:
-                nodeid = nid
+                found = node
 
         # return found node
-        if nodeid in self.data:
-            return sanitize_node(self.data[nodeid])
+        if not found is None:
+            return sanitize_node(found)
         else:
             return None
 
@@ -224,11 +221,7 @@ class BaseStorage(object):
         needle = mac.lower()
 
         # iterate over all nodes
-        for nodeid in self.data:
-            if nodeid.startswith('__'):
-                continue
-            node = self.data[nodeid]
-
+        for node in self.get_nodes():
             # check node's primary MAC
             if 'mac' in node and needle == node['mac'].lower():
                 return sanitize_node(node)