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

use parallel lookups for lg/routers

parent fc0ed2cf
Branches
Tags 0.53
No related merge requests found
import json import json
from flask import Blueprint, jsonify, Response from flask import Blueprint, jsonify, Response, current_app
from inventory_provider.routes import common from inventory_provider.routes import common
...@@ -25,29 +25,25 @@ def routers(access): ...@@ -25,29 +25,25 @@ def routers(access):
status=404, status=404,
mimetype='text/html') mimetype='text/html')
redis = common.get_current_redis()
def _visible(router): def _visible(router):
if access == ACCESS_INTERNAL: if access == ACCESS_INTERNAL:
return True return True
return router['type'] == 'CORE' return router['type'] == 'CORE'
def _routers(): def _routers():
i = 0 for doc in common.load_json_docs(
for k in redis.scan_iter('opsdb:lg:*'): config_params=current_app.config['INVENTORY_PROVIDER_CONFIG'],
rtr = redis.get(k.decode('utf-8')).decode('utf-8') key_pattern='opsdb:lg:*'):
rtr = json.loads(rtr) yield doc['value']
i += 1
if _visible(rtr):
yield rtr
redis = common.get_current_redis()
cache_key = f'classifier-cache:lg:{access}' cache_key = f'classifier-cache:lg:{access}'
result = redis.get(cache_key) result = redis.get(cache_key)
if result: if result:
result = json.loads(result.decode('utf-8')) result = json.loads(result.decode('utf-8'))
else: else:
result = list(_routers()) result = list(filter(_visible, _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'))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment