From cc66682192dcff4ef97c04c24d01182f16a91b0b Mon Sep 17 00:00:00 2001
From: Robert Latta <robert.latta@geant.org>
Date: Thu, 25 Mar 2021 17:41:06 +0000
Subject: [PATCH] added calculated-speed to circuit info

---
 inventory_provider/tasks/worker.py | 39 +++++++++++++++++++++++++++---
 1 file changed, 36 insertions(+), 3 deletions(-)

diff --git a/inventory_provider/tasks/worker.py b/inventory_provider/tasks/worker.py
index 550690ee..02f93249 100644
--- a/inventory_provider/tasks/worker.py
+++ b/inventory_provider/tasks/worker.py
@@ -3,6 +3,7 @@ import functools
 import json
 import logging
 import os
+import re
 import subprocess
 import tempfile
 import threading
@@ -481,6 +482,36 @@ def update_circuit_hierarchy_and_port_id_services(self, use_current=False):
     port_id_services = defaultdict(list)
     interface_services = defaultdict(list)
 
+    def _convert_to_bits(value, unit):
+        unit = unit.lower()
+        conversions = {
+            'm': 1000000,
+            'mb': 1000000,
+            'g': 1000000000,
+            'gbe': 1000000000,
+        }
+        return int(value) * conversions[unit]
+
+    def _get_speed(circuit_id):
+        c = hierarchy[circuit_id]
+        pattern = re.compile(r'^(\d+)([a-zA-z]+)$')
+        m = pattern.match(c['speed'])
+        if m:
+            try:
+                return _convert_to_bits(m[1], m[2])
+            except KeyError as e:
+                logger.debug(f'Could not find key: {e} '
+                             f'for circuit: {circuit_id}')
+                return 0
+        else:
+            if c['circuit-type'] == 'service' \
+                    or c['product'].lower() == 'ethernet':
+                return sum(
+                    (_get_speed(x) for x in c['carrier-circuits'])
+                )
+            else:
+                return 0
+
     def _populate_hierarchy():
         nonlocal hierarchy
         hierarchy = {d['id']: d for d in ims_data.get_circuit_hierarchy(ds1)}
@@ -591,7 +622,6 @@ def update_circuit_hierarchy_and_port_id_services(self, use_current=False):
                 f"{details['interface_name']}"
             circuits = port_id_services.get(details['port_id'], [])
 
-            # add fibre-routes to circuits
             for circ in circuits:
                 circ['fibre-routes'] = \
                     [{
@@ -601,6 +631,7 @@ def update_circuit_hierarchy_and_port_id_services(self, use_current=False):
                     } for x in set(_get_fibre_routes(circ['id']))]
                 circ['top-level-services'] = \
                     get_top_level_services(circ['id'])
+                circ['calculated-speed'] = _get_speed(circ['id'])
                 _format_service(circ)
 
             interface_services[k].extend(circuits)
@@ -610,14 +641,16 @@ def update_circuit_hierarchy_and_port_id_services(self, use_current=False):
     else:
         r = get_next_redis(InventoryTask.config)
     rp = r.pipeline()
-    for k in r.scan_iter('ims:circuit_hierarchy:*', count=2000):
+    for k in r.scan_iter('ims:circuit_hierarchy:*', count=1000):
         rp.delete(k)
-    for k in r.scan_iter('ims:interface_services:*', count=2000):
+    for k in r.scan_iter('ims:interface_services:*', count=1000):
         rp.delete(k)
     rp.execute()
     rp = r.pipeline()
     for circ in hierarchy.values():
         rp.set(f'ims:circuit_hierarchy:{circ["id"]}', json.dumps([circ]))
+    rp.execute()
+    rp = r.pipeline()
     for k, v in interface_services.items():
         rp.set(
             f'ims:interface_services:{k}',
-- 
GitLab