Skip to content
Snippets Groups Projects
Commit 1708c1b3 authored by Robert Latta's avatar Robert Latta
Browse files

added ignore-cache to all routes that cache data

parent f0b23fa6
No related branches found
No related tags found
No related merge requests found
...@@ -2,13 +2,13 @@ import ipaddress ...@@ -2,13 +2,13 @@ import ipaddress
import json import json
import logging import logging
import re import re
from distutils.util import strtobool
from typing import Optional from typing import Optional
from flask import Blueprint, Response, request from flask import Blueprint, Response, request
from redis import Redis from redis import Redis
from inventory_provider.routes import common from inventory_provider.routes import common
from inventory_provider.routes.common import _ignore_cache_or_retrieve
routes = Blueprint("inventory-data-classifier-support-routes", __name__) routes = Blueprint("inventory-data-classifier-support-routes", __name__)
...@@ -261,19 +261,9 @@ def get_juniper_link_info(source_equipment: str, interface: str) -> Response: ...@@ -261,19 +261,9 @@ def get_juniper_link_info(source_equipment: str, interface: str) -> Response:
cache_key = \ cache_key = \
f'classifier-cache:juniper:{ims_source_equipment}:{ims_interface}' f'classifier-cache:juniper:{ims_source_equipment}:{ims_interface}'
ignore_cache = request.args.get('ignore-cache', default='false', type=str) result = _ignore_cache_or_retrieve(request, cache_key, r)
try:
ignore_cache = strtobool(ignore_cache)
except ValueError:
ignore_cache = False
if ignore_cache:
result = False
else:
result = r.get(cache_key)
if result: if not result:
result = result.decode('utf-8')
else:
result = { result = {
'interface': _link_interface_info(r, source_equipment, interface) 'interface': _link_interface_info(r, source_equipment, interface)
} }
...@@ -522,21 +512,9 @@ def peer_info(address_str: str) -> Response: ...@@ -522,21 +512,9 @@ def peer_info(address_str: str) -> Response:
r = common.get_current_redis() r = common.get_current_redis()
cache_key = f'classifier-cache:peer:{address_str}' cache_key = f'classifier-cache:peer:{address_str}'
result = _ignore_cache_or_retrieve(request, cache_key, r)
ignore_cache = request.args.get('ignore-cache', default='false', type=str) if not result:
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')
else:
result = { result = {
'interfaces': [], 'interfaces': [],
'locations': [], 'locations': [],
...@@ -626,19 +604,9 @@ def get_trap_metadata(source_equipment: str, interface: str, circuit_id: str) \ ...@@ -626,19 +604,9 @@ def get_trap_metadata(source_equipment: str, interface: str, circuit_id: str) \
r = common.get_current_redis() r = common.get_current_redis()
ignore_cache = request.args.get('ignore-cache', default='false', type=str) result = _ignore_cache_or_retrieve(request, cache_key, r)
try:
ignore_cache = strtobool(ignore_cache)
except ValueError:
ignore_cache = False
if ignore_cache:
result = False
else:
result = r.get(cache_key)
if result: if not result:
result = result.decode('utf-8')
else:
result = { result = {
'locations': [] 'locations': []
} }
...@@ -693,19 +661,9 @@ def get_fiberlink_trap_metadata(ne_name_str: str, object_name_str: str) \ ...@@ -693,19 +661,9 @@ def get_fiberlink_trap_metadata(ne_name_str: str, object_name_str: str) \
cache_key = \ cache_key = \
f'classifier-cache:fiberlink:{ne_name_str}:{object_name_str}' f'classifier-cache:fiberlink:{ne_name_str}:{object_name_str}'
ignore_cache = request.args.get('ignore-cache', default='false', type=str) result = _ignore_cache_or_retrieve(request, cache_key, r)
try:
ignore_cache = strtobool(ignore_cache)
except ValueError:
ignore_cache = False
if ignore_cache:
result = False
else:
result = r.get(cache_key)
if result: if not result:
result = result.decode('utf-8')
else:
equipment_a = matches[0][0] equipment_a = matches[0][0]
equipment_b = matches[1][0] equipment_b = matches[1][0]
interface_a = interfaces[0] interface_a = interfaces[0]
...@@ -828,19 +786,9 @@ def get_coriant_info(equipment_name: str, entity_string: str) -> Response: ...@@ -828,19 +786,9 @@ def get_coriant_info(equipment_name: str, entity_string: str) -> Response:
cache_key = 'classifier-cache:coriant:' \ cache_key = 'classifier-cache:coriant:' \
f'{ims_source_equipment}:{ims_interface}' f'{ims_source_equipment}:{ims_interface}'
ignore_cache = request.args.get('ignore-cache', default='false', type=str) result = _ignore_cache_or_retrieve(request, cache_key, r)
try:
ignore_cache = strtobool(ignore_cache)
except ValueError:
ignore_cache = False
if ignore_cache:
result = False
else:
result = r.get(cache_key)
if result: if not result:
result = result.decode('utf-8')
else:
m = re.match(r'^(\d+\-\d+)\.(\d+)', ims_interface) m = re.match(r'^(\d+\-\d+)\.(\d+)', ims_interface)
if not m: if not m:
......
...@@ -6,6 +6,7 @@ import queue ...@@ -6,6 +6,7 @@ import queue
import random import random
import threading import threading
from distutils.util import strtobool
from lxml import etree from lxml import etree
import requests import requests
from flask import request, Response, current_app, g from flask import request, Response, current_app, g
...@@ -16,6 +17,24 @@ _DECODE_TYPE_XML = 'xml' ...@@ -16,6 +17,24 @@ _DECODE_TYPE_XML = 'xml'
_DECODE_TYPE_JSON = 'json' _DECODE_TYPE_JSON = 'json'
def _ignore_cache_or_retrieve(request_, cache_key, r):
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
logger.debug('ignoring cache')
else:
result = r.get(cache_key)
if result:
result = result.decode('utf-8')
return result
def ims_hostname_decorator(field): def ims_hostname_decorator(field):
""" """
Decorator to convert host names to various formats to try to match what is Decorator to convert host names to various formats to try to match what is
......
...@@ -2,9 +2,10 @@ import json ...@@ -2,9 +2,10 @@ import json
import logging import logging
import re import re
from flask import Blueprint, jsonify, Response, current_app from flask import Blueprint, jsonify, Response, current_app, request
from inventory_provider.routes import common from inventory_provider.routes import common
from inventory_provider.routes.common import _ignore_cache_or_retrieve
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -139,11 +140,10 @@ def router_interfaces(hostname=None): ...@@ -139,11 +140,10 @@ def router_interfaces(hostname=None):
if hostname else 'classifier-cache:netconf-interfaces:all' if hostname else 'classifier-cache:netconf-interfaces:all'
r = common.get_current_redis() r = common.get_current_redis()
result = r.get(cache_key)
if result:
result = result.decode('utf-8')
else: result = _ignore_cache_or_retrieve(request, cache_key, r)
if not result:
key_pattern = f'netconf-interfaces:{hostname}:*' \ key_pattern = f'netconf-interfaces:{hostname}:*' \
if hostname else 'netconf-interfaces:*' if hostname else 'netconf-interfaces:*'
config = current_app.config['INVENTORY_PROVIDER_CONFIG'] config = current_app.config['INVENTORY_PROVIDER_CONFIG']
......
import json import json
import logging import logging
from flask import Blueprint, jsonify, Response from flask import Blueprint, jsonify, Response, request
from inventory_provider.routes import common from inventory_provider.routes import common
from inventory_provider.routes.common import _ignore_cache_or_retrieve
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -107,11 +108,10 @@ def routers(access): ...@@ -107,11 +108,10 @@ def routers(access):
yield rtr yield rtr
cache_key = f'classifier-cache:ims-lg:{access}' cache_key = f'classifier-cache:ims-lg:{access}'
result = redis.get(cache_key)
if result: result = _ignore_cache_or_retrieve(request, cache_key, redis)
result = json.loads(result.decode('utf-8'))
else: if not result:
result = list(_routers()) result = list(_routers())
# cache this data for the next call # cache this data for the next call
redis.set(cache_key, json.dumps(result).encode('utf-8')) redis.set(cache_key, json.dumps(result).encode('utf-8'))
......
import itertools import itertools
import json import json
from flask import Blueprint, jsonify, Response from flask import Blueprint, jsonify, Response, request
from inventory_provider.routes import common from inventory_provider.routes import common
from inventory_provider.routes.common import _ignore_cache_or_retrieve
routes = Blueprint("msr-query-routes", __name__) routes = Blueprint("msr-query-routes", __name__)
...@@ -104,11 +105,10 @@ def access_services(): ...@@ -104,11 +105,10 @@ def access_services():
yield json.loads(service) yield json.loads(service)
cache_key = 'classifier-cache:msr:access-services' cache_key = 'classifier-cache:msr:access-services'
result = redis.get(cache_key)
if result: result = _ignore_cache_or_retrieve(request, cache_key, redis)
result = json.loads(result.decode('utf-8'))
else: if not result:
result = list(_services()) result = list(_services())
# cache this data for the next call # cache this data for the next call
redis.set(cache_key, json.dumps(result).encode('utf-8')) redis.set(cache_key, json.dumps(result).encode('utf-8'))
...@@ -159,11 +159,9 @@ def _handle_peering_group_request(name, cache_key, group_key_base): ...@@ -159,11 +159,9 @@ def _handle_peering_group_request(name, cache_key, group_key_base):
if name: if name:
cache_key = f'{cache_key}:{name}' cache_key = f'{cache_key}:{name}'
items = r.get(cache_key) items = _ignore_cache_or_retrieve(request, cache_key, r)
if items: if not items:
items = json.loads(items.decode('utf-8'))
else:
if name: if name:
items = _load_list_items(f'{group_key_base}:{name}') items = _load_list_items(f'{group_key_base}:{name}')
else: else:
...@@ -265,11 +263,9 @@ def _handle_peering_group_list_request(cache_key, group_key_base): ...@@ -265,11 +263,9 @@ def _handle_peering_group_list_request(cache_key, group_key_base):
k = k.decode('utf-8') k = k.decode('utf-8')
yield k[len(group_key_base) + 1:] yield k[len(group_key_base) + 1:]
names = r.get(cache_key) names = _ignore_cache_or_retrieve(request, cache_key, r)
if names: if not names:
names = json.loads(names.decode('utf-8'))
else:
names = list(_get_all_subkeys()) names = list(_get_all_subkeys())
if not names: if not names:
return Response( return Response(
......
...@@ -2,11 +2,12 @@ import json ...@@ -2,11 +2,12 @@ import json
import logging import logging
import re import re
from flask import Blueprint, Response, current_app from flask import Blueprint, Response, current_app, request
from inventory_provider import juniper from inventory_provider import juniper
from inventory_provider.routes import common from inventory_provider.routes import common
from inventory_provider.routes.classifier import get_ims_equipment_name, \ from inventory_provider.routes.classifier import get_ims_equipment_name, \
get_ims_interface get_ims_interface
from inventory_provider.routes.common import _ignore_cache_or_retrieve
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
routes = Blueprint('poller-support-routes', __name__) routes = Blueprint('poller-support-routes', __name__)
...@@ -291,10 +292,9 @@ def interfaces(hostname=None): ...@@ -291,10 +292,9 @@ def interfaces(hostname=None):
r = common.get_current_redis() r = common.get_current_redis()
result = r.get(cache_key) result = _ignore_cache_or_retrieve(request, cache_key, r)
if result:
result = result.decode('utf-8') if not result:
else:
result = list(_load_interfaces_to_poll(hostname)) result = list(_load_interfaces_to_poll(hostname))
if not result: if not result:
return Response( return Response(
...@@ -387,10 +387,9 @@ def interface_speeds(hostname=None): ...@@ -387,10 +387,9 @@ def interface_speeds(hostname=None):
r = common.get_current_redis() r = common.get_current_redis()
result = r.get(cache_key) result = _ignore_cache_or_retrieve(request, cache_key, r)
if result:
result = result.decode('utf-8') if not result:
else:
result = list(_load_interfaces_and_speeds(hostname)) result = list(_load_interfaces_and_speeds(hostname))
if not result: if not result:
return Response( return Response(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment