Skip to content

Commit

Permalink
Merge pull request #75 from TeskaLabs/Enhancement/Jinjaservice-suppor…
Browse files Browse the repository at this point in the history
…t-now-variable

Enhancement: Jinja service support's now global namespace
  • Loading branch information
mithunbharadwaj authored Jun 14, 2024
2 parents 3a87f4b + 779f2f1 commit 5ec2ad4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
16 changes: 13 additions & 3 deletions asabiris/formatter/jinja/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import configparser

import asab
import datetime
import pathlib
import json
import jinja2
Expand All @@ -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.
Expand Down Expand Up @@ -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,
}
Expand All @@ -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:
Expand Down
19 changes: 19 additions & 0 deletions library/Templates/Email/render_now_variable.md
Original file line number Diff line number Diff line change
@@ -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)**
30 changes: 23 additions & 7 deletions qa.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"to": ["[email protected]"],
"body": {
"template": "/Templates/Email/message.md",
"wrapper": "/Templates/Email/body_wrapper.html",
"wrapper": "/Templates/Email/Markdown wrapper.html",
"params":{
"name":"Iris"
}
Expand All @@ -47,7 +47,7 @@
"to": ["[email protected]"],
"body": {
"template": "/Templates/Email/message.md",
"wrapper": "/Templates/Emails/body_wrapper.html",
"wrapper": "/Templates/Emails/Markdown wrapper.html",
"params":{
"name":"Iris"
}
Expand All @@ -63,15 +63,31 @@
{
"to": ["[email protected]"],
"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"
}
}
}
```

## TSM001E: Send an email using Markdown template from a template that uses now from template global.

`PUT /send_mail`

```
{
"to": ["[email protected]"],
"body": {
"template": "/Templates/Email/render_now_variable.md",
"params":{
"name":"Iris",
"tenant":"Default",
}
}
}
```


## TSM002: Send an email using HTML template
Expand Down Expand Up @@ -1411,11 +1427,11 @@ EXPECTED RESPONSE:
{"type":"email", "to": ["Shivashankar <[email protected]>"], "from": "[email protected]", "body":{"template":"/Templates/Email/message.md", "params":{"name": "I am testing a template", "error": "None" }}}
{"type":"email", "to": ["Shivashankar <[email protected]>"], "from": "[email protected]", "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 <[email protected]>"], "from": "[email protected]", "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 <[email protected]>"], "from": "[email protected]", "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 <[email protected]>"], "from": "[email protected]", "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 <[email protected]>"], "from": "[email protected]", "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 <[email protected]>"], "from": "[email protected]", "body":{"template":"/Templates/Email/message.md", "wrapper":"/Templates/Wrappers/Markdown wrapper.html", "params":{"name": "I am testing a template", "error": "None" }}}
'Missing from'
Expand Down

0 comments on commit 5ec2ad4

Please sign in to comment.