From 710f7755c045a65d2987410cdbc0dc1f3659d79a Mon Sep 17 00:00:00 2001 From: Ildar Iskhakov Date: Fri, 3 Feb 2023 19:53:35 +0800 Subject: [PATCH] Fix bug with root/dependant alert groups list api endpoint (#1284) # What this PR does ## Which issue(s) this PR fixes ## Checklist - [ ] Tests updated - [ ] Documentation added - [ ] `CHANGELOG.md` updated --- CHANGELOG.md | 1 + engine/apps/api/serializers/alert_group.py | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51c6984a..8532b8bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Fix bug with root/dependant alert groups list api endpoint([1284](https://github.com/grafana/oncall/pull/1284)) - Fixed NPE on teams switch ### Added diff --git a/engine/apps/api/serializers/alert_group.py b/engine/apps/api/serializers/alert_group.py index 58c58fdd..d171cd7b 100644 --- a/engine/apps/api/serializers/alert_group.py +++ b/engine/apps/api/serializers/alert_group.py @@ -23,6 +23,7 @@ class AlertGroupFieldsCacheSerializerMixin: def get_or_set_web_template_field( cls, obj, + last_alert, field_name, renderer_class, cache_lifetime=60 * 60 * 24, @@ -31,7 +32,7 @@ class AlertGroupFieldsCacheSerializerMixin: cached_field = cache.get(CACHE_KEY, None) web_templates_modified_at = obj.channel.web_templates_modified_at - last_alert_created_at = obj.last_alert.created_at + last_alert_created_at = last_alert.created_at # use cache only if cache exists # and cache was created after the last alert created @@ -44,7 +45,7 @@ class AlertGroupFieldsCacheSerializerMixin: ): field = cached_field.get(field_name) else: - field = renderer_class(obj, obj.last_alert).render() + field = renderer_class(obj, last_alert).render() cache.set(CACHE_KEY, {"cache_created_at": timezone.now(), field_name: field}, cache_lifetime) return field @@ -60,10 +61,12 @@ class ShortAlertGroupSerializer(AlertGroupFieldsCacheSerializerMixin, serializer fields = ["pk", "render_for_web", "alert_receive_channel", "inside_organization_number"] def get_render_for_web(self, obj): - if not obj.last_alert: + last_alert = obj.alerts.last() + if last_alert is None: return {} return AlertGroupFieldsCacheSerializerMixin.get_or_set_web_template_field( obj, + last_alert, "render_for_web", AlertGroupWebRenderer, ) @@ -133,6 +136,7 @@ class AlertGroupListSerializer(EagerLoadingMixin, AlertGroupFieldsCacheSerialize return {} return AlertGroupFieldsCacheSerializerMixin.get_or_set_web_template_field( obj, + obj.last_alert, "render_for_web", AlertGroupWebRenderer, ) @@ -142,6 +146,7 @@ class AlertGroupListSerializer(EagerLoadingMixin, AlertGroupFieldsCacheSerialize return {} return AlertGroupFieldsCacheSerializerMixin.get_or_set_web_template_field( obj, + obj.last_alert, "render_for_classic_markdown", AlertGroupClassicMarkdownRenderer, )