From 1a753e060c3b6923c2069103b644e0e7c09f1b9b Mon Sep 17 00:00:00 2001
From: Robert Latta <robert.latta@geant.org>
Date: Fri, 18 Dec 2020 09:11:10 +0000
Subject: [PATCH] added ignore cache option

---
 inventory_provider/routes/ims_classifier.py | 58 +++++++++++++++++----
 inventory_provider/tasks/ims_worker.py      |  8 ++-
 2 files changed, 53 insertions(+), 13 deletions(-)

diff --git a/inventory_provider/routes/ims_classifier.py b/inventory_provider/routes/ims_classifier.py
index deee1741..393babe6 100644
--- a/inventory_provider/routes/ims_classifier.py
+++ b/inventory_provider/routes/ims_classifier.py
@@ -4,10 +4,11 @@ import json
 import logging
 import re
 from copy import copy
+from distutils.util import strtobool
 from functools import lru_cache
 from typing import Optional, Iterator, List
 
-from flask import Blueprint, Response, current_app
+from flask import Blueprint, Response, current_app, request
 from redis import Redis
 
 from inventory_provider.db.ims import IMS_SERVICE_NAMES
@@ -296,8 +297,15 @@ def get_juniper_link_info(source_equipment: str, interface: str) -> Response:
     cache_key = \
         f'ims-classifier-cache:juniper:{ims_source_equipment}:{ims_interface}'
 
-    # result = r.get(cache_key)
-    result = False
+    ignore_cache = request.args.get('ignore-cache', default='false', type=str)
+    try:
+        ignore_cache = strtobool(ignore_cache)
+    except ValueError:
+        ignore_cache = False
+    if ignore_cache:
+        result = False
+    else:
+        result = r.get(cache_key)
 
     if result:
         result = result.decode('utf-8')
@@ -420,8 +428,15 @@ def peer_info(address_str: str) -> Response:
 
     cache_key = f'ims-classifier-cache:peer:{address_str}'
 
-    # result = r.get(cache_key)
-    result = False
+    ignore_cache = request.args.get('ignore-cache', default='false', type=str)
+    try:
+        ignore_cache = strtobool(ignore_cache)
+    except ValueError:
+        ignore_cache = False
+    if ignore_cache:
+        result = False
+    else:
+        result = r.get(cache_key)
 
     if result:
         result = result.decode('utf-8')
@@ -494,8 +509,15 @@ def get_trap_metadata(source_equipment: str, interface: str, circuit_id: str) \
 
     r = common.get_current_redis()
 
-    result = r.get(cache_key)
-    result = False
+    ignore_cache = request.args.get('ignore-cache', default='false', type=str)
+    try:
+        ignore_cache = strtobool(ignore_cache)
+    except ValueError:
+        ignore_cache = False
+    if ignore_cache:
+        result = False
+    else:
+        result = r.get(cache_key)
 
     if result:
         result = result.decode('utf-8')
@@ -573,8 +595,15 @@ def get_fiberlink_trap_metadata(ne_name_str: str, object_name_str: str) \
     cache_key = \
         f'ims-classifier-cache:fiberlink:{ne_name_str}:{object_name_str}'
 
-    # result = r.get(cache_key)
-    result = False
+    ignore_cache = request.args.get('ignore-cache', default='false', type=str)
+    try:
+        ignore_cache = strtobool(ignore_cache)
+    except ValueError:
+        ignore_cache = False
+    if ignore_cache:
+        result = False
+    else:
+        result = r.get(cache_key)
 
     if result:
         result = result.decode('utf-8')
@@ -656,8 +685,15 @@ def get_coriant_info(equipment_name: str, entity_string: str) -> Response:
     cache_key = 'ims-classifier-cache:coriant:%s:%s' % (
         equipment_name, entity_string)
 
-    # result = r.get(cache_key)
-    result = False
+    ignore_cache = request.args.get('ignore-cache', default='false', type=str)
+    try:
+        ignore_cache = strtobool(ignore_cache)
+    except ValueError:
+        ignore_cache = False
+    if ignore_cache:
+        result = False
+    else:
+        result = r.get(cache_key)
 
     if result:
         result = result.decode('utf-8')
diff --git a/inventory_provider/tasks/ims_worker.py b/inventory_provider/tasks/ims_worker.py
index d792cabc..123872c2 100644
--- a/inventory_provider/tasks/ims_worker.py
+++ b/inventory_provider/tasks/ims_worker.py
@@ -25,7 +25,12 @@ logger = logging.getLogger(__name__)
 @app.task(base=InventoryTask, bind=True, name='update_fibre_spans_ims')
 @log_task_entry_and_exit
 def update_fibre_spans(self, use_current=False):
-    r = get_next_redis(InventoryTask.config)
+
+    if use_current:
+        r = get_current_redis(InventoryTask.config)
+        # scan with bigger batches, to mitigate network latency effects
+    else:
+        r = get_next_redis(InventoryTask.config)
     rp = r.pipeline()
     # scan with bigger batches, to mitigate network latency effects
     for key in r.scan_iter('ims:ne_fibre_spans:*', count=1000):
@@ -50,7 +55,6 @@ def update_interfaces_to_port_ids_ims(self, use_current=False):
 
     if use_current:
         r = get_current_redis(InventoryTask.config)
-        # scan with bigger batches, to mitigate network latency effects
     else:
         r = get_next_redis(InventoryTask.config)
 
-- 
GitLab