Browse Source

Aggregate sender list for cached messages as far as possible.

Signed-off-by: Maximilian Wilhelm <max@rfc2324.org>
Maximilian Wilhelm 8 years ago
parent
commit
8489768786
1 changed files with 31 additions and 1 deletions
  1. 31 1
      modules/ffpb.py

+ 31 - 1
modules/ffpb.py

@@ -587,11 +587,41 @@ def ffpb_updatepeers(bot):
 def ffpb_print_cached_messages (bot):
     for msg, m_info in msg_cache.items ():
         if time.time () - m_info['time'] > msg_cache_time:
-            sender = ", ".join (sorted (m_info['nodes']))
+            sender = bot._ffpb_aggregate_node_ids (m_info['nodes'])
+
             bot.msg (bot.config.ffpb.msg_target, "[{0}] {1}".format (sender, msg))
+
             del msg_cache[msg]
 
 
+def _ffpb_aggregate_node_ids (bot, nodes):
+    """Aggregate given list of node IDs as far as possbile (read: combine gateways)
+    and return aggregated and ordered list."""
+
+    nodes_temp = {}
+    nodes_aggr = []
+
+    for node in nodes:
+        match = re.search ("^gw(\d+)(.*)?$", node)
+        if match:
+             expr = re.sub ("^gw(\d+)", "gw##", node)
+             if expr not in nodes_temp:
+                  nodes_temp[expr] = []
+
+             nodes_temp[expr].append (match.group (1))
+             continue
+
+        nodes_aggr.append (node)
+
+    for node, n_list in nodes_temp.items ():
+        if len (n_list) == 1:
+            nodes_aggr.append (re.sub ("gw##", "gw%s" % n_list[0], node))
+        else:
+            nodes_aggr.append (re.sub ("gw##", "gw{%s}" % ",".join (sorted (n_list)), node))
+
+    return ", ".join (sorted (nodes_aggr))
+
+
 def ffpb_fetch_stats(bot, url, memoryid):
     """Fetch a ffmap-style nodes.json from the given URL and
     store it in the bot's memory."""