From fb4bad21d22bc9410faa4914d06cc14fd2974bd4 Mon Sep 17 00:00:00 2001 From: Innokentii Konstantinov Date: Tue, 28 Nov 2023 20:47:57 +0800 Subject: [PATCH] Document tojson jinja filter (#3432) # What this PR does Document tojson jinja filter --- docs/sources/jinja2-templating/_index.md | 3 ++- engine/apps/labels/utils.py | 14 ++++++++++---- engine/apps/webhooks/utils.py | 8 ++++---- engine/common/jinja_templater/filters.py | 15 ++++++++------- .../src/components/MonacoEditor/MonacoEditor.tsx | 1 + 5 files changed, 25 insertions(+), 16 deletions(-) diff --git a/docs/sources/jinja2-templating/_index.md b/docs/sources/jinja2-templating/_index.md index 887689f7..b3e1d311 100644 --- a/docs/sources/jinja2-templating/_index.md +++ b/docs/sources/jinja2-templating/_index.md @@ -201,7 +201,8 @@ Built-in functions: ### Functions added by Grafana OnCall - `time` - current time -- `tojson_pretty` - JSON prettified +- `tojson` - dumps a structure to JSON +- `tojson_pretty` - same as tojson, but prettified - `iso8601_to_time` - converts time from iso8601 (`2015-02-17T18:30:20.000Z`) to datetime - `datetimeformat` - converts time from datetime to the given format (`%H:%M / %d-%m-%Y` by default) - `regex_replace` - performs a regex find and replace diff --git a/engine/apps/labels/utils.py b/engine/apps/labels/utils.py index bacf86c0..e0369b0b 100644 --- a/engine/apps/labels/utils.py +++ b/engine/apps/labels/utils.py @@ -57,10 +57,16 @@ def is_labels_feature_enabled(organization: "Organization") -> bool: ) -def get_label_verbal(obj: typing.Any) -> dict[str, str]: - return {label.key.name: label.value.name for label in obj.labels.all().select_related("key", "value")} +def get_labels_dict(labelable) -> dict[str, str]: + """ + get_labels_dict returns dict of labels' key and values names for the given object + """ + return {label.key.name: label.value.name for label in labelable.labels.all().select_related("key", "value")} -def get_alert_group_label_verbal(alert_group: "AlertGroup") -> dict[str, str]: - """This is different from get_label_verbal because alert group labels store key/value names, not IDs""" +def get_alert_group_labels_dict(alert_group: "AlertGroup") -> dict[str, str]: + """ + get_alert_group_labels_dict returns dict of labels' key and values names for the given alert group. + It's different from get_labels_dict, because AlertGroupAssociated labels store key/value_name, not key/value_id + """ return {label.key_name: label.value_name for label in alert_group.labels.all()} diff --git a/engine/apps/webhooks/utils.py b/engine/apps/webhooks/utils.py index feafe93e..fc8b01be 100644 --- a/engine/apps/webhooks/utils.py +++ b/engine/apps/webhooks/utils.py @@ -7,7 +7,7 @@ from urllib.parse import urlparse from django.conf import settings from apps.base.utils import live_settings -from apps.labels.utils import get_alert_group_label_verbal, get_label_verbal, is_labels_feature_enabled +from apps.labels.utils import get_alert_group_labels_dict, get_labels_dict, is_labels_feature_enabled from apps.schedules.ical_utils import list_users_to_notify_from_ical from common.jinja_templater import apply_jinja_template @@ -183,7 +183,7 @@ def serialize_event(event, alert_group, user, webhook, responses=None): # Enrich webhook data with labels payloads if labels feature is enabled # TODO: once feature flag will be removed this code should go to the 'data' dict declaration if is_labels_feature_enabled(alert_group.channel.organization): - data["webhook"] = {"id": webhook.public_primary_key, "name": webhook.name, "labels": get_label_verbal(webhook)} - data["integration"]["labels"] = get_label_verbal(alert_group.channel) - data["alert_group"]["labels"] = get_alert_group_label_verbal(alert_group) + data["webhook"] = {"id": webhook.public_primary_key, "name": webhook.name, "labels": get_labels_dict(webhook)} + data["integration"]["labels"] = get_labels_dict(alert_group.channel) + data["alert_group"]["labels"] = get_alert_group_labels_dict(alert_group) return data diff --git a/engine/common/jinja_templater/filters.py b/engine/common/jinja_templater/filters.py index 59618eb5..e24d01ee 100644 --- a/engine/common/jinja_templater/filters.py +++ b/engine/common/jinja_templater/filters.py @@ -26,6 +26,14 @@ def to_pretty_json(value): return None +# json_dumps is deprecated in favour of built-in tojson filter and left for backward-compatibility. +def json_dumps(value): + try: + return json.dumps(value) + except (ValueError, AttributeError, TypeError): + return None + + def regex_replace(value, find, replace): try: return re.sub(find, replace, value) @@ -47,13 +55,6 @@ def regex_search(pattern, value): return None -def json_dumps(value): - try: - return json.dumps(value) - except (ValueError, AttributeError, TypeError): - return None - - def b64decode(value): try: return base64.b64decode(value).decode("utf-8") diff --git a/grafana-plugin/src/components/MonacoEditor/MonacoEditor.tsx b/grafana-plugin/src/components/MonacoEditor/MonacoEditor.tsx index 2118404b..bcfbcb9b 100644 --- a/grafana-plugin/src/components/MonacoEditor/MonacoEditor.tsx +++ b/grafana-plugin/src/components/MonacoEditor/MonacoEditor.tsx @@ -34,6 +34,7 @@ const PREDEFINED_TERMS = [ 'grafana_oncall_incident_id', 'source_link', 'tojson_pretty', + 'tojson', ]; const MonacoEditor: FC = (props) => {