From 1c5af166f0b8580a0ba97fba1ccb6af2d8ae7e7e Mon Sep 17 00:00:00 2001
From: Robert Latta <robert.latta@geant.org>
Date: Fri, 14 Jun 2024 10:27:02 +0100
Subject: [PATCH] small refactor

---
 inventory_provider/tasks/worker.py | 81 ++++++++++++++----------------
 1 file changed, 38 insertions(+), 43 deletions(-)

diff --git a/inventory_provider/tasks/worker.py b/inventory_provider/tasks/worker.py
index 8685a6b1..aea2a32a 100644
--- a/inventory_provider/tasks/worker.py
+++ b/inventory_provider/tasks/worker.py
@@ -1290,18 +1290,19 @@ def transform_ims_data(data):
     circuit_ids_to_monitor = data['circuit_ids_to_monitor']
     additional_circuit_customers = data['additional_circuit_customers']
 
-    # data in this gets modified
     # This is only used within this function and the populate_mic_with_third_party_data
     # The following data gets added to this
     #  contacts
     #  planned_work_contacts
     #  sid
     #  third_party_id - only if it exists for the circuit
-    hierarchy = data['hierarchy']
+    hierarchy = data['hierarchy']  # data in this gets modified
 
-    # This just gets the flex ILS data added to it
+    # These two just gets the flex ILS data added to them.
+    # They are also used for building interface_services
     port_id_details = data['port_id_details']  # data in this gets modified
     port_id_services = data['port_id_services']  # data in this gets modified
+
     circuit_ids_and_sids = data['circuit_ids_sids']
     circuit_ids_and_third_party_ids = data['circuit_ids_third_party_ids']
     geant_nodes = data['geant_nodes']
@@ -1553,46 +1554,40 @@ def transform_ims_data(data):
 
     _build_interface_services()
 
-    def _build_sid_services():
-        for circuits in interface_services.values():
-            for circuit in circuits:
-                if 'sid' in circuit:
-                    sid = circuit['sid']
-                    sid_info = {
-                        'circuit_id': circuit['id'],
-                        'sid': sid,
-                        'status': circuit['original_status'],
-                        'monitored': circuit['monitored'],
-                        'name': circuit['name'],
-                        'speed': circuit['calculated-speed'],
-                        'service_type': circuit['service_type'],
-                        'project': circuit['project'],
-                        'customer': circuit['customer'],
-                        'equipment': circuit['equipment'],
-                        'port': circuit['port'],
-                        'geant_equipment': circuit['equipment'] in geant_nodes
-                    }
-                    if sid_info not in sid_services.setdefault(sid, []):
-                        sid_services[sid].append(sid_info)
-
-    _build_sid_services()
-
-    def _build_services_by_type():
-        for circuits in interface_services.values():
-            for circuit in circuits:
-                type_key = ims_sorted_service_type_key(circuit['service_type'])
-                services_by_type.setdefault(type_key, {})[circuit['id']] = circuit
-
-    _build_services_by_type()
-
-    def _build_node_pair_services():
-        for circuits in interface_services.values():
-            for circuit in circuits:
-                if circuit['other_end_equipment']:
-                    node_pair_key = f"{circuit['equipment']}/{circuit['other_end_equipment']}"
-                    node_pair_services.setdefault(node_pair_key, {})[circuit['id']] = circuit
-
-    _build_node_pair_services()
+    def _add_to_sid_services(_circuit):
+        if 'sid' in _circuit:
+            sid = _circuit['sid']
+            sid_info = {
+                'circuit_id': _circuit['id'],
+                'sid': sid,
+                'status': _circuit['original_status'],
+                'monitored': _circuit['monitored'],
+                'name': _circuit['name'],
+                'speed': _circuit['calculated-speed'],
+                'service_type': _circuit['service_type'],
+                'project': _circuit['project'],
+                'customer': _circuit['customer'],
+                'equipment': _circuit['equipment'],
+                'port': _circuit['port'],
+                'geant_equipment': _circuit['equipment'] in geant_nodes
+            }
+            if sid_info not in sid_services.setdefault(sid, []):
+                sid_services[sid].append(sid_info)
+
+    def _add_to_services_by_type(_circuit):
+        type_key = ims_sorted_service_type_key(_circuit['service_type'])
+        services_by_type.setdefault(type_key, {})[_circuit['id']] = _circuit
+
+    def _add_to_node_pair_services(_circuit):
+        if _circuit['other_end_equipment']:
+            node_pair_key = f"{_circuit['equipment']}/{_circuit['other_end_equipment']}"
+            node_pair_services.setdefault(node_pair_key, {})[_circuit['id']] = _circuit
+
+    for circuits in interface_services.values():
+        for circuit in circuits:
+            _add_to_sid_services(circuit)
+            _add_to_services_by_type(circuit)
+            _add_to_node_pair_services(circuit)
 
     return {
         'hierarchy': hierarchy,
-- 
GitLab