diff --git a/etna/alerts/migrations/0002_alert_uid.py b/etna/alerts/migrations/0002_alert_uid.py new file mode 100644 index 000000000..6107b5d72 --- /dev/null +++ b/etna/alerts/migrations/0002_alert_uid.py @@ -0,0 +1,19 @@ +# Generated by Django 5.0.7 on 2024-07-25 09:53 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("alerts", "0001_initial"), + ] + + operations = [ + migrations.AddField( + model_name="alert", + name="uid", + field=models.BigIntegerField(blank=True, default=0, editable=False), + preserve_default=False, + ), + ] diff --git a/etna/alerts/models.py b/etna/alerts/models.py index f49a74b90..e83c015e5 100644 --- a/etna/alerts/models.py +++ b/etna/alerts/models.py @@ -1,3 +1,5 @@ +import time + from django.conf import settings from django.db import models @@ -57,9 +59,15 @@ class Alert(models.Model): FieldPanel("alert_level"), ] + uid = models.BigIntegerField(null=False, blank=True, editable=False) + def __str__(self): return self.title + def save(self, *args, **kwargs): + self.uid = round(time.time() * 1000) + super().save(*args, **kwargs) + class AlertMixin(models.Model): """Alert mixin. diff --git a/etna/alerts/serializers.py b/etna/alerts/serializers.py index 5c680297a..e4c744776 100644 --- a/etna/alerts/serializers.py +++ b/etna/alerts/serializers.py @@ -5,8 +5,13 @@ class AlertSerializer(serializers.Serializer): def to_representation(self, instance): - return { - "title": instance.title, - "message": expand_db_html(instance.message), - "alert_level": instance.alert_level, - } + if instance.active: + return { + "title": instance.title, + "message": expand_db_html(instance.message), + "alert_level": instance.alert_level, + "cascade": instance.cascade, + "uid": instance.uid, + } + else: + return None diff --git a/etna/api/tests/expected_results/author.json b/etna/api/tests/expected_results/author.json index 106a8b631..7db24d0df 100644 --- a/etna/api/tests/expected_results/author.json +++ b/etna/api/tests/expected_results/author.json @@ -27,7 +27,9 @@ "global_alert": { "title": "BETA", "message": "

Message

", - "alert_level": "high" + "alert_level": "high", + "cascade": true, + "uid": ALERT_UID }, "type_label": "Author", "teaser_text": "Teaser text", diff --git a/etna/api/tests/expected_results/authors.json b/etna/api/tests/expected_results/authors.json index c8eedef84..cd11cc93b 100644 --- a/etna/api/tests/expected_results/authors.json +++ b/etna/api/tests/expected_results/authors.json @@ -27,7 +27,9 @@ "global_alert": { "title": "BETA", "message": "

Message

", - "alert_level": "high" + "alert_level": "high", + "cascade": true, + "uid": ALERT_UID }, "type_label": "Author index", "teaser_text": "Teaser text", diff --git a/etna/api/tests/test_pages.py b/etna/api/tests/test_pages.py index 038879362..4332a15b7 100644 --- a/etna/api/tests/test_pages.py +++ b/etna/api/tests/test_pages.py @@ -335,6 +335,7 @@ def replace_placeholders(self, data: str): "HOME_PAGE_ID": str(self.root_page.id), "ARTICLE_INDEX_ID": str(self.article_index.id), "ARTICLE_ID": str(self.article.id), + "ALERT_UID": str(self.alert.uid), "FOCUSED_ID": str(self.focused_article.id), "ARTS_ID": str(self.arts.id), "EARLY_MODERN_ID": str(self.early_modern.id),