Skip to content
Snippets Groups Projects
Commit 7f8be331 authored by Bjarke Madsen's avatar Bjarke Madsen
Browse files

cache jinja templates to speed provisioning

parent f9907a4a
No related branches found
No related tags found
No related merge requests found
......@@ -7,6 +7,63 @@ import json
import jinja2
def _read_template(filename):
"""
Reads the template from the given filename.
:param filename: path to the template file
:return: template
"""
with open(filename) as f:
return jinja2.Template(f.read())
dropdown_template_file = os.path.abspath(os.path.join(
os.path.dirname(__file__),
'templates',
'shared',
'dropdown.json.j2'))
yaxes_template_file = os.path.abspath(os.path.join(
os.path.dirname(__file__),
'templates',
'shared',
'yaxes.json.j2'))
panel_template_file = file = os.path.abspath(os.path.join(
os.path.dirname(__file__),
'templates',
'shared',
'panel.json.j2'))
panel_target_template_file = os.path.abspath(os.path.join(
os.path.dirname(__file__),
'templates',
'shared',
'panel_target.json.j2'))
nren_dashboard_template_file = os.path.abspath(os.path.join(
os.path.dirname(__file__),
'templates',
'nren_access',
'nren-dashboard.json.j2'))
dashboard_template_file = os.path.abspath(os.path.join(
os.path.dirname(__file__),
'templates',
'shared',
'dashboard.json.j2'))
DROPDOWN_TEMPLATE = _read_template(dropdown_template_file)
YAXES_TEMPLATE = _read_template(yaxes_template_file)
PANEL_TEMPLATE = _read_template(panel_template_file)
PANEL_TARGET_TEMPLATE = _read_template(panel_target_template_file)
NREN_DASHBOARD_TEMPLATE = _read_template(nren_dashboard_template_file)
DASHBOARD_TEMPLATE = _read_template(dashboard_template_file)
def create_dropdown_panel(title, **kwargs):
"""
Creates a dropdown panel from the given data.
......@@ -17,14 +74,7 @@ def create_dropdown_panel(title, **kwargs):
:return: rendered dropdown panel JSON
"""
TEMPLATE_FILENAME = os.path.abspath(os.path.join(
os.path.dirname(__file__),
'templates',
'shared',
'dropdown.json.j2'))
with open(TEMPLATE_FILENAME) as f:
template = jinja2.Template(f.read())
return template.render({**kwargs, 'title': title})
return DROPDOWN_TEMPLATE.render({**kwargs, 'title': title})
def create_yaxes(type):
......@@ -36,14 +86,7 @@ def create_yaxes(type):
:return: rendered yaxes JSON
"""
file = os.path.abspath(os.path.join(
os.path.dirname(__file__),
'templates',
'shared',
'yaxes.json.j2'))
with open(file) as f:
template = jinja2.Template(f.read())
return template.render({'type': type})
return YAXES_TEMPLATE.render({'type': type})
def create_panel_target(data):
......@@ -56,14 +99,7 @@ def create_panel_target(data):
:return: rendered panel target JSON
"""
file = os.path.abspath(os.path.join(
os.path.dirname(__file__),
'templates',
'shared',
'panel_target.json.j2'))
with open(file) as f:
template = jinja2.Template(f.read())
return template.render(data)
return PANEL_TARGET_TEMPLATE.render(data)
def create_panel(data):
......@@ -76,18 +112,11 @@ def create_panel(data):
:return: rendered panel JSON
"""
file = os.path.abspath(os.path.join(
os.path.dirname(__file__),
'templates',
'shared',
'panel.json.j2'))
with open(file) as f:
template = jinja2.Template(f.read())
yaxes = create_yaxes(data.get('y_axis_type', 'bits'))
targets = data.get('targets', [])
for target in data.get('panel_targets', []):
targets.append(create_panel_target(target))
return template.render({**data, 'yaxes': yaxes, 'targets': targets})
return PANEL_TEMPLATE.render({**data, 'yaxes': yaxes, 'targets': targets})
def render_dashboard(dashboard, nren=False):
......@@ -103,20 +132,10 @@ def render_dashboard(dashboard, nren=False):
"""
if nren:
file = os.path.abspath(os.path.join(
os.path.dirname(__file__),
'templates',
'nren_access',
'nren-dashboard.json.j2'))
template = NREN_DASHBOARD_TEMPLATE
else:
file = os.path.abspath(os.path.join(
os.path.dirname(__file__),
'templates',
'shared',
'dashboard.json.j2'))
with open(file) as f:
template = jinja2.Template(f.read())
template = DASHBOARD_TEMPLATE
rendered = template.render(dashboard)
rendered = json.loads(rendered)
rendered['uid'] = None
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment