Skip to content
Snippets Groups Projects
Commit 35e2c0bb authored by Erik Reid's avatar Erik Reid
Browse files

Finished feature better-service-category-endpoint-performance.

parents c1a8159d 09c1a18a
No related branches found
No related tags found
No related merge requests found
import json
from flask import Blueprint, Response, jsonify
from flask import Blueprint, Response, jsonify, current_app
from lxml import etree
from inventory_provider import juniper
from inventory_provider.routes import common
......@@ -88,10 +88,12 @@ def poller_interface_oids(hostname):
@common.require_accepts_json
def service_category_interfaces(category):
def _interfaces(rc, cat):
for k in rc.scan_iter(f'interface-services:{cat}:*'):
cached_ifc = rc.get(k.decode('utf-8')).decode('utf-8')
cached_ifc = json.loads(cached_ifc)
def _interfaces(cat):
config_params = current_app.config['INVENTORY_PROVIDER_CONFIG']
for doc in common.load_json_docs(
config_params=config_params,
key_pattern=f'interface-services:{cat}:*'):
cached_ifc = doc['value']
basic_ifc_info = dict()
for k in ['description', 'interface', 'router']:
basic_ifc_info[k] = cached_ifc[k]
......@@ -110,15 +112,17 @@ def service_category_interfaces(category):
result = r.get(cache_key)
if result:
result = json.loads(result.decode('utf-8'))
result = result.decode('utf-8')
else:
result = list(_interfaces(r, category))
if result:
r.set(cache_key, json.dumps(result).encode('utf-8'))
else:
result = list(_interfaces(category))
if not result:
return Response(
response=f'no info available for service category {category}',
status=404,
mimetype="text/html")
return jsonify(result)
result = json.dumps(result)
# cache this data for the next call
r.set(cache_key, result.encode('utf-8'))
return Response(result, mimetype="application/json")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment