From 1964ab62ec79735059b4ad16196eaf12d46dfdbd Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Mon, 16 Jan 2023 01:17:19 +0000 Subject: [PATCH] Check the specified window --- core/db/elastic.py | 10 ++++++++++ core/forms.py | 3 ++- core/lib/rules.py | 1 + 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/core/db/elastic.py b/core/db/elastic.py index 21f10db..3311ef3 100644 --- a/core/db/elastic.py +++ b/core/db/elastic.py @@ -256,6 +256,16 @@ class ElasticsearchBackend(StorageBackend): search_query = self.parse_query( data, tags, None, False, add_bool, bypass_check=True ) + if rule_object.window is not None: + range_query = { + "range": { + "ts": { + "gt": f"now-{rule_object.window}/d", + "lt": "now/d", + } + } + } + add_top.append(range_query) self.add_bool(search_query, add_bool) self.add_top(search_query, add_top) if "sentiment" in data: diff --git a/core/forms.py b/core/forms.py index e4b53c5..a38b812 100644 --- a/core/forms.py +++ b/core/forms.py @@ -137,7 +137,8 @@ class NotificationRuleForm(RestrictedFormMixin, ModelForm): try: # Passing db to avoid circular import parsed_data = NotificationRuleData(self.request.user, cleaned_data, db=db) - parsed_data.test_schedule() + if cleaned_data["enabled"]: + parsed_data.test_schedule() except RuleParseError as e: self.add_error(e.field, f"Parsing error: {e}") return diff --git a/core/lib/rules.py b/core/lib/rules.py index 5518817..e01be5d 100644 --- a/core/lib/rules.py +++ b/core/lib/rules.py @@ -126,6 +126,7 @@ class NotificationRuleData(object): self.cleaned_data = cleaned_data self.db = db self.data = self.cleaned_data.get("data") + self.window = self.cleaned_data.get("window") self.parsed = None self.aggs = {}