diff --git a/core/forms.py b/core/forms.py index 11278f7..3c4008b 100644 --- a/core/forms.py +++ b/core/forms.py @@ -88,6 +88,7 @@ class NotificationRuleForm(RestrictedFormMixin, ModelForm): "url", "service", "policy", + "ingest", "enabled", ) help_texts = { @@ -102,6 +103,7 @@ class NotificationRuleForm(RestrictedFormMixin, ModelForm): "window": "Time window to search: 1d, 1h, 1m, 1s, etc.", "amount": "Amount of matches to be returned for scheduled queries. Cannot be used with on-demand queries.", "policy": "When to trigger this policy.", + "ingest": "Whether to ingest matches", } def clean(self): diff --git a/core/lib/rules.py b/core/lib/rules.py index c14c3cd..20faa42 100644 --- a/core/lib/rules.py +++ b/core/lib/rules.py @@ -357,7 +357,8 @@ class NotificationRuleData(object): :param matches: the matches to store """ # new_matches = self.reform_matches(index, matches, meta, mode) - await self.db.async_store_matches(matches) + if self.object.ingest: + await self.db.async_store_matches(matches) def ingest_matches_sync(self, index, matches, meta, mode): """ @@ -366,7 +367,8 @@ class NotificationRuleData(object): :param matches: the matches to store """ # new_matches = self.reform_matches(index, matches, meta, mode) - self.db.store_matches(matches) + if self.object.ingest: + self.db.store_matches(matches) async def rule_matched(self, index, message, meta, mode): """ diff --git a/core/migrations/0028_rename_send_empty_notificationrule_ingest_and_more.py b/core/migrations/0028_rename_send_empty_notificationrule_ingest_and_more.py new file mode 100644 index 0000000..16fb5a9 --- /dev/null +++ b/core/migrations/0028_rename_send_empty_notificationrule_ingest_and_more.py @@ -0,0 +1,33 @@ +# Generated by Django 4.1.6 on 2023-02-13 21:03 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0027_alter_notificationrule_policy_and_more'), + ] + + operations = [ + migrations.RenameField( + model_name='notificationrule', + old_name='send_empty', + new_name='ingest', + ), + migrations.AlterField( + model_name='notificationrule', + name='interval', + field=models.IntegerField(choices=[(0, 'On demand'), (5, 'Every 5 seconds'), (60, 'Every minute'), (900, 'Every 15 minutes'), (1800, 'Every 30 minutes'), (3600, 'Every hour'), (14400, 'Every 4 hours'), (86400, 'Every day')], default=60), + ), + migrations.AlterField( + model_name='notificationrule', + name='service', + field=models.CharField(choices=[('ntfy', 'NTFY'), ('webhook', 'Custom webhook'), ('none', 'Disabled')], default='webhook', max_length=255), + ), + migrations.AlterField( + model_name='notificationrule', + name='window', + field=models.CharField(blank=True, default='30d', max_length=255, null=True), + ), + ] diff --git a/core/models.py b/core/models.py index 9586fab..ffc40a3 100644 --- a/core/models.py +++ b/core/models.py @@ -194,14 +194,16 @@ class NotificationRule(models.Model): priority = models.IntegerField(choices=PRIORITY_CHOICES, default=1) topic = models.CharField(max_length=2048, null=True, blank=True) url = models.CharField(max_length=1024, null=True, blank=True) - interval = models.IntegerField(choices=INTERVAL_CHOICES, default=0) - window = models.CharField(max_length=255, null=True, blank=True) + interval = models.IntegerField(choices=INTERVAL_CHOICES, default=60) + window = models.CharField(max_length=255, default="30d", null=True, blank=True) amount = models.PositiveIntegerField(default=1, null=True, blank=True) enabled = models.BooleanField(default=True) data = models.TextField() match = models.JSONField(null=True, blank=True) - service = models.CharField(choices=SERVICE_CHOICES, max_length=255, default="ntfy") - send_empty = models.BooleanField(default=False) + service = models.CharField( + choices=SERVICE_CHOICES, max_length=255, default="webhook" + ) + ingest = models.BooleanField(default=False) policy = models.CharField(choices=POLICY_CHOICES, max_length=255, default="default") def __str__(self): @@ -238,8 +240,6 @@ class NotificationRule(models.Model): user_settings["url"] = self.url if self.service is not None: user_settings["service"] = self.service - if self.send_empty is not None: - user_settings["send_empty"] = self.send_empty if check: if user_settings["service"] == "ntfy" and user_settings["topic"] is None: