-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #266 from grafana/dev
Merge dev to main
- Loading branch information
Showing
22 changed files
with
186 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
engine/apps/alerts/incident_appearance/renderers/classic_markdown_renderer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from apps.alerts.incident_appearance.renderers.base_renderer import AlertBaseRenderer, AlertGroupBaseRenderer | ||
from apps.alerts.incident_appearance.templaters import AlertClassicMarkdownTemplater | ||
from common.utils import str_or_backup | ||
|
||
|
||
class AlertClassicMarkdownRenderer(AlertBaseRenderer): | ||
@property | ||
def templater_class(self): | ||
return AlertClassicMarkdownTemplater | ||
|
||
def render(self): | ||
templated_alert = self.templated_alert | ||
rendered_alert = { | ||
"title": str_or_backup(templated_alert.title, "Alert"), | ||
"message": str_or_backup(templated_alert.message, ""), | ||
"image_url": str_or_backup(templated_alert.image_url, None), | ||
"source_link": str_or_backup(templated_alert.source_link, None), | ||
} | ||
return rendered_alert | ||
|
||
|
||
class AlertGroupClassicMarkdownRenderer(AlertGroupBaseRenderer): | ||
def __init__(self, alert_group, alert=None): | ||
if alert is None: | ||
alert = alert_group.alerts.last() | ||
|
||
super().__init__(alert_group, alert) | ||
|
||
@property | ||
def alert_renderer_class(self): | ||
return AlertClassicMarkdownRenderer | ||
|
||
def render(self): | ||
return self.alert_renderer.render() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
engine/apps/alerts/incident_appearance/templaters/classic_markdown_templater.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from apps.alerts.incident_appearance.templaters.alert_templater import AlertTemplater | ||
|
||
|
||
class AlertClassicMarkdownTemplater(AlertTemplater): | ||
RENDER_FOR = "web" | ||
|
||
def _render_for(self): | ||
return self.RENDER_FOR | ||
|
||
def _postformat(self, templated_alert): | ||
if templated_alert.title: | ||
templated_alert.title = self._slack_format(templated_alert.title) | ||
if templated_alert.message: | ||
templated_alert.message = self._slack_format(templated_alert.message) | ||
return templated_alert | ||
|
||
def _slack_format(self, data): | ||
sf = self.slack_formatter | ||
sf.hyperlink_mention_format = "[{title}]({url})" | ||
return sf.format(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
engine/apps/alerts/migrations/0005_alertgroup_cached_render_for_web.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Generated by Django 3.2.13 on 2022-07-20 09:04 | ||
|
||
from django.db import migrations, models, OperationalError | ||
|
||
|
||
class AddFieldIfNotExists(migrations.AddField): | ||
""" | ||
Adds a field and ignores "duplicate column" error in case the field already exists. | ||
When migrating back it will not delete the field. | ||
""" | ||
|
||
def database_forwards(self, app_label, schema_editor, from_state, to_state): | ||
try: | ||
super().database_forwards(app_label, schema_editor, from_state, to_state) | ||
except OperationalError: | ||
pass | ||
|
||
def database_backwards(self, app_label, schema_editor, from_state, to_state): | ||
pass | ||
|
||
|
||
class Migration(migrations.Migration): | ||
""" | ||
This migration tries to create two fields cached_render_for_web and active_cache_for_web_calculation_id. | ||
In case these fields already exist, this migration will do nothing. | ||
In case the database was already affected by the previous version of the 0004 migration, | ||
it will recreate these fields. | ||
""" | ||
|
||
dependencies = [ | ||
('alerts', '0004_auto_20220711_1106'), | ||
] | ||
|
||
operations = [ | ||
AddFieldIfNotExists( | ||
model_name='alertgroup', | ||
name='cached_render_for_web', | ||
field=models.JSONField(default=dict), | ||
), | ||
AddFieldIfNotExists( | ||
model_name='alertgroup', | ||
name='active_cache_for_web_calculation_id', | ||
field=models.CharField(default=None, max_length=100, null=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from common.jinja_templater.filters import regex_replace | ||
|
||
|
||
def test_regex_replace_drop_field(): | ||
original = "[ var='D0' metric='my_metric' labels={} value=140 ]" | ||
expected = "[ metric='my_metric' labels={} value=140 ]" | ||
assert regex_replace(original, "var='[a-zA-Z0-9]+' ", "") == expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.