Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add timestamp_to_datetime jinja template filter #5303

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Grafana OnCall enhances Jinja with additional functions:
- `datetimeparse`: Converts string to datetime according to strftime format codes (`%H:%M / %d-%m-%Y` by default)
- `timedeltaparse`: Converts a time range (e.g., `5s`, `2m`, `6h`, `3d`) to a timedelta that can be added to or subtracted from a datetime
- Usage example: `{% set delta = alert.window | timedeltaparse %}{{ alert.startsAt | iso8601_to_time - delta | datetimeformat }}`
- `timestamp_to_datetime`: Converts a Unix/Epoch time to a datetime object
- `regex_replace`: Performs a regex find and replace
- `regex_match`: Performs a regex match, returns `True` or `False`
- Usage example: `{{ payload.ruleName | regex_match(".*") }}`
Expand Down
7 changes: 7 additions & 0 deletions engine/common/jinja_templater/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ def datetimeformat_as_timezone(value, format="%H:%M / %d-%m-%Y", tz="UTC"):
return None


def timestamp_to_datetime(value):
try:
return datetime.fromtimestamp(value)
except (ValueError, AttributeError, TypeError):
return None


def iso8601_to_time(value):
try:
return parse_datetime(value)
Expand Down
2 changes: 2 additions & 0 deletions engine/common/jinja_templater/jinja_template_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
regex_replace,
regex_search,
timedeltaparse,
timestamp_to_datetime,
to_pretty_json,
)

Expand All @@ -39,3 +40,4 @@ def raise_security_exception(name):
jinja_template_env.filters["json_dumps"] = json_dumps
jinja_template_env.filters["b64decode"] = b64decode
jinja_template_env.filters["parse_json"] = parse_json
jinja_template_env.filters["timestamp_to_datetime"] = timestamp_to_datetime
11 changes: 11 additions & 0 deletions engine/common/tests/test_apply_jinja_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ def test_apply_jinja_template_iso8601_to_time():
assert result == expected


def test_apply_jinja_template_timestamp_to_datetime():
payload = {"sometime": 1730893740}

result = apply_jinja_template(
"{{ payload.sometime | timestamp_to_datetime }}",
payload,
)
expected = str(datetime.fromtimestamp(payload["sometime"]))
assert result == expected


def test_apply_jinja_template_datetimeformat():
payload = {"aware": "2023-05-28 23:11:12+0000", "naive": "2023-05-28 23:11:12"}

Expand Down
Loading