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 ...@@ -7,6 +7,63 @@ import json
import jinja2 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): def create_dropdown_panel(title, **kwargs):
""" """
Creates a dropdown panel from the given data. Creates a dropdown panel from the given data.
...@@ -17,14 +74,7 @@ def create_dropdown_panel(title, **kwargs): ...@@ -17,14 +74,7 @@ def create_dropdown_panel(title, **kwargs):
:return: rendered dropdown panel JSON :return: rendered dropdown panel JSON
""" """
TEMPLATE_FILENAME = os.path.abspath(os.path.join( return DROPDOWN_TEMPLATE.render({**kwargs, 'title': title})
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})
def create_yaxes(type): def create_yaxes(type):
...@@ -36,14 +86,7 @@ def create_yaxes(type): ...@@ -36,14 +86,7 @@ def create_yaxes(type):
:return: rendered yaxes JSON :return: rendered yaxes JSON
""" """
file = os.path.abspath(os.path.join( return YAXES_TEMPLATE.render({'type': type})
os.path.dirname(__file__),
'templates',
'shared',
'yaxes.json.j2'))
with open(file) as f:
template = jinja2.Template(f.read())
return template.render({'type': type})
def create_panel_target(data): def create_panel_target(data):
...@@ -56,14 +99,7 @@ def create_panel_target(data): ...@@ -56,14 +99,7 @@ def create_panel_target(data):
:return: rendered panel target JSON :return: rendered panel target JSON
""" """
file = os.path.abspath(os.path.join( return PANEL_TARGET_TEMPLATE.render(data)
os.path.dirname(__file__),
'templates',
'shared',
'panel_target.json.j2'))
with open(file) as f:
template = jinja2.Template(f.read())
return template.render(data)
def create_panel(data): def create_panel(data):
...@@ -76,18 +112,11 @@ def create_panel(data): ...@@ -76,18 +112,11 @@ def create_panel(data):
:return: rendered panel JSON :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')) yaxes = create_yaxes(data.get('y_axis_type', 'bits'))
targets = data.get('targets', []) targets = data.get('targets', [])
for target in data.get('panel_targets', []): for target in data.get('panel_targets', []):
targets.append(create_panel_target(target)) 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): def render_dashboard(dashboard, nren=False):
...@@ -103,20 +132,10 @@ def render_dashboard(dashboard, nren=False): ...@@ -103,20 +132,10 @@ def render_dashboard(dashboard, nren=False):
""" """
if nren: if nren:
file = os.path.abspath(os.path.join( template = NREN_DASHBOARD_TEMPLATE
os.path.dirname(__file__),
'templates',
'nren_access',
'nren-dashboard.json.j2'))
else: else:
file = os.path.abspath(os.path.join( template = DASHBOARD_TEMPLATE
os.path.dirname(__file__),
'templates',
'shared',
'dashboard.json.j2'))
with open(file) as f:
template = jinja2.Template(f.read())
rendered = template.render(dashboard) rendered = template.render(dashboard)
rendered = json.loads(rendered) rendered = json.loads(rendered)
rendered['uid'] = None 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