From 7f8be331c03c0c2e8ae45c8edc5651ad670aa4fd Mon Sep 17 00:00:00 2001 From: Bjarke Madsen <bjarke@nordu.net> Date: Wed, 12 Apr 2023 14:44:23 +0200 Subject: [PATCH] cache jinja templates to speed provisioning --- brian_dashboard_manager/templating/render.py | 109 +++++++++++-------- 1 file changed, 64 insertions(+), 45 deletions(-) diff --git a/brian_dashboard_manager/templating/render.py b/brian_dashboard_manager/templating/render.py index 09454db..1885f3e 100644 --- a/brian_dashboard_manager/templating/render.py +++ b/brian_dashboard_manager/templating/render.py @@ -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 -- GitLab