Document tojson jinja filter (#3432)

# What this PR does
Document tojson jinja filter
This commit is contained in:
Innokentii Konstantinov 2023-11-28 20:47:57 +08:00 committed by GitHub
parent d8f111f960
commit fb4bad21d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 16 deletions

View file

@ -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

View file

@ -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()}

View file

@ -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

View file

@ -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")

View file

@ -34,6 +34,7 @@ const PREDEFINED_TERMS = [
'grafana_oncall_incident_id',
'source_link',
'tojson_pretty',
'tojson',
];
const MonacoEditor: FC<MonacoEditorProps> = (props) => {