Browse Source

yanic: Use more recent node information if multiple are present.

 If there are multiple occurances for the same node in multiple input files
 make sure to use the more recent one. This may happen when a node is moved
 between sites and the old nodeinfo has not been purged yet.

Signed-off-by: Maximilian Wilhelm <max@sdn.clinic>
Maximilian Wilhelm 4 years ago
parent
commit
2a383fcaa7
1 changed files with 17 additions and 0 deletions
  1. 17 0
      yanic/ff_merge_nodes_json

+ 17 - 0
yanic/ff_merge_nodes_json

@@ -8,6 +8,7 @@ import argparse
 import json
 import json
 import os
 import os
 import sys
 import sys
+import time
 
 
 parser = argparse.ArgumentParser (description = 'Merge nodes.json files')
 parser = argparse.ArgumentParser (description = 'Merge nodes.json files')
 parser.add_argument ('files', help = 'Path for nodes.json file(s)', nargs = '+')
 parser.add_argument ('files', help = 'Path for nodes.json file(s)', nargs = '+')
@@ -27,6 +28,22 @@ for file_path in args.files:
 		sys.exit (1)
 		sys.exit (1)
 
 
 	for node in nodes['nodes']:
 	for node in nodes['nodes']:
+		node_id = node['nodeinfo']['node_id']
+
+		# If node_id has already been seen make sure to use the newer entry
+		if node_id in all_nodes:
+			try:
+				node_lastseen = time.strptime (node['lastseen'], "%Y-%m-%dT%H:%M:%S%z")
+				existing_node_lastseen = time.strptime (existing_node_lastseen['lastseen'], "%Y-%m-%dT%H:%M:%S%z")
+
+				# If the node information already stored in all_nodes is more
+				# recent than the node we just found, don't overwrite it.
+				if existing_node_lastseen > node_lastseen:
+					continue
+			except Exception:
+				# If parsing a timestamp fails just carry on
+				continue
+
 		all_nodes[node['nodeinfo']['node_id']] = node
 		all_nodes[node['nodeinfo']['node_id']] = node
 
 
 	for key in nodes.keys ():
 	for key in nodes.keys ():