diff --git a/asabiris/formatter/jinja/service.py b/asabiris/formatter/jinja/service.py index cde5b43a..bfa52767 100644 --- a/asabiris/formatter/jinja/service.py +++ b/asabiris/formatter/jinja/service.py @@ -2,6 +2,7 @@ import configparser import asab +import datetime import pathlib import json import jinja2 @@ -28,8 +29,18 @@ def __init__(self, app, service_name="JinjaService"): self.Variables = {} self.Environment = jinja2.Environment() + # Inject 'now' function and datetimeformat filter into the Jinja2 template's global namespace + self.Environment.globals['now'] = self._jinja_now + self.Environment.filters['datetimeformat'] = self.datetimeformat self._load_variables_from_json() + def _jinja_now(self): + current_time = datetime.datetime.utcnow() + return current_time + + def datetimeformat(self, value, format='%Y-%m-%d %H:%M:%S'): + return value.strftime(format) + def _load_variables_from_json(self): """ Load variables from a JSON file specified in the 'jinja' section of the configuration. @@ -74,8 +85,8 @@ async def format(self, template_path, template_params): if b is None: raise ASABIrisError( ErrorCode.TEMPLATE_NOT_FOUND, - tech_message="Failed to render. Reason: Template {} does not exist".format(template_path), - error_i18n_key="Template '{{incorrect_path}}' does not exist", + tech_message="Failed to render. Reason: Template {} does not exist.".format(template_path), + error_i18n_key="Template '{{incorrect_path}}' does not exist.", error_dict={ "incorrect_path": template_path, } @@ -86,7 +97,6 @@ async def format(self, template_path, template_params): # Prepare template variables (aka context) context = construct_context(dict(), self.Variables, template_params) - # Do the rendering return template.render(context) except jinja2.exceptions.UndefinedError as e: diff --git a/library/Templates/Email/render_now_variable.md b/library/Templates/Email/render_now_variable.md new file mode 100644 index 00000000..20bf3d77 --- /dev/null +++ b/library/Templates/Email/render_now_variable.md @@ -0,0 +1,19 @@ +SUBJECT: LogMan.io detected anomaly for {% if dimension %}'{{ dimension }}'{% endif %} + +## Anomaly detected + +Please find more information below: + +{% if name %} +Event reason: ***{{ name }}*** +{% endif %} + +{% if tenant %} +Tenant: ***{{ tenant }}*** +{% endif %} + +{% if dimension %} +User anomaly occurred for: ***{{ dimension }}*** {% endif %}{% if now %} +at ***{{ now() | datetimeformat('%d-%m-%Y %H:%M') }} UTC***{% endif %} + +For more information regarding this anomaly, please see this **[event]({{public_url}}/?tenant={{tenant}}#/discover/lmio-{{tenant}}-events-complex?aggby=day&filter=&ts=now-14d&te=now&refresh=off&size=40&selectedfields=%255B%255D&groupby=&filteredfields=%255B%257B%2522lmio.alert.dimension%2522%253A%2522{{ dimension }}%2522%257D%255D)** in your **[TeskaLabs LogMan.io]({{ public_url }}/?tenant={{ tenant }}#/discover)** diff --git a/library/Templates/Wrapper/markdown_wrapper.html b/library/Templates/Wrapper/Markdown wrapper.html similarity index 100% rename from library/Templates/Wrapper/markdown_wrapper.html rename to library/Templates/Wrapper/Markdown wrapper.html diff --git a/qa.md b/qa.md index ea4a8790..5656a4eb 100644 --- a/qa.md +++ b/qa.md @@ -30,7 +30,7 @@ "to": ["foo@example.com"], "body": { "template": "/Templates/Email/message.md", - "wrapper": "/Templates/Email/body_wrapper.html", + "wrapper": "/Templates/Email/Markdown wrapper.html", "params":{ "name":"Iris" } @@ -47,7 +47,7 @@ "to": ["foo@example.com"], "body": { "template": "/Templates/Email/message.md", - "wrapper": "/Templates/Emails/body_wrapper.html", + "wrapper": "/Templates/Emails/Markdown wrapper.html", "params":{ "name":"Iris" } @@ -63,8 +63,8 @@ { "to": ["foo@example.com"], "body": { - "template": "/Templates/Email/message.md", - "wrapper": "/Templates/Email/body_wrapper.md", + "template": "/Templates/Emails/message.md", + "wrapper": "/Templates/Email/Markdown wrapper.md", "params":{ "name":"Iris" } @@ -72,6 +72,22 @@ } ``` +## TSM001E: Send an email using Markdown template from a template that uses now from template global. + +`PUT /send_mail` + +``` +{ + "to": ["foo@example.com"], + "body": { + "template": "/Templates/Email/render_now_variable.md", + "params":{ + "name":"Iris", + "tenant":"Default", + } + } +} +``` ## TSM002: Send an email using HTML template @@ -1411,11 +1427,11 @@ EXPECTED RESPONSE: {"type":"email", "to": ["Shivashankar "], "from": "info@teskalabs.com", "body":{"template":"/Templates/Email/message.md", "params":{"name": "I am testing a template", "error": "None" }}} -{"type":"email", "to": ["Shivashankar "], "from": "info@teskalabs.com", "body":{"template":"/Templates/Email/message.md", "wrapper":"/Templates/Wrapper/markdown_wrapper.md", "params":{"name": "I am testing a template", "error": "None" }}} +{"type":"email", "to": ["Shivashankar "], "from": "info@teskalabs.com", "body":{"template":"/Templates/Email/message.md", "wrapper":"/Templates/Wrapper/Markdown wrapper.md", "params":{"name": "I am testing a template", "error": "None" }}} -{"type": "email", "to": ["Shivashankar "], "from": "info@teskalabs.com", "body":{"template":"/Templates/Email/message.md", "wrapper":"/Templates/Wrapper/markdown_wrapper.html", "params":{"name": "I am testing a template", "error": "None" }}} +{"type": "email", "to": ["Shivashankar "], "from": "info@teskalabs.com", "body":{"template":"/Templates/Email/message.md", "wrapper":"/Templates/Wrapper/Markdown wrapper.html", "params":{"name": "I am testing a template", "error": "None" }}} -{"type": "email", "to": ["Shivashankar "], "from": "info@teskalabs.com", "body":{"template":"/Templates/Email/message.md", "wrapper":"/Templates/Wrappers/markdown_wrapper.html", "params":{"name": "I am testing a template", "error": "None" }}} +{"type": "email", "to": ["Shivashankar "], "from": "info@teskalabs.com", "body":{"template":"/Templates/Email/message.md", "wrapper":"/Templates/Wrappers/Markdown wrapper.html", "params":{"name": "I am testing a template", "error": "None" }}} 'Missing from'