Skip to content

Commit

Permalink
Increase debugability for tristates
Browse files Browse the repository at this point in the history
  • Loading branch information
hmpf committed Sep 21, 2023
1 parent 535c733 commit 7a84141
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
19 changes: 13 additions & 6 deletions src/argus/notificationprofile/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,19 @@ def is_empty(self):

def incident_fits_tristates(self, incident):
if self.are_tristates_empty():
return None
fits_tristates = []
return {}
fits_tristates = {}
incident.refresh_from_db()
for tristate in self.TRINARY_FILTERS:
filter_tristate = self._get_tristate(tristate)
if filter_tristate is None:
LOG.debug('Tristates: "%s" not in filter, ignoring', tristate)
fits_tristates[tristate] = None
continue
incident_tristate = getattr(incident, tristate, None)
fits_tristates.append(filter_tristate == incident_tristate)
return all(fits_tristates)
LOG.debug('Tristates: "%s": filter = %s, incident = %s', tristate, filter_tristate, incident_tristate)
fits_tristates[tristate] = filter_tristate == incident_tristate
return fits_tristates

def incident_fits_maxlevel(self, incident):
if self.is_maxlevel_empty():
Expand Down Expand Up @@ -279,7 +283,9 @@ def incident_fits(self, incident: Incident):
checks = {}
checks["source"] = self.source_system_fits(incident, data)
checks["tags"] = self.tags_fit(incident, data)
checks["tristates"] = self.filter_wrapper.incident_fits_tristates(incident)
tristate_checks = self.filter_wrapper.incident_fits_tristates(incident)
for tristate, result in tristate_checks.items():
checks[tristate] = result
checks["max_level"] = self.filter_wrapper.incident_fits_maxlevel(incident)
any_failed = False in checks.values()
if any_failed:
Expand Down Expand Up @@ -374,7 +380,8 @@ def incident_fits(self, incident: Incident):
if not self.active:
return False
is_selected_by_time = self.timeslot.timestamp_is_within_time_recurrences(incident.start_time)
is_selected_by_filters = any(f.incident_fits(incident) for f in self.filters.all())
checks = {f: f.incident_fits(incident) for f in self.filters.all()}
is_selected_by_filters = False not in checks.values()
return is_selected_by_time and is_selected_by_filters

def event_fits(self, event: Event):
Expand Down
22 changes: 12 additions & 10 deletions tests/notificationprofile/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_incident_fits_tristates_no_tristates_set(self):
incident = Mock()
empty_filter = FilterWrapper({})
result = empty_filter.incident_fits_tristates(incident)
self.assertEqual(result, None)
self.assertEqual(result, {})

@override_settings(ARGUS_FALLBACK_FILTER={"acked": True})
def test_incident_fits_tristates_no_tristates_set_with_fallback(self):
Expand All @@ -92,30 +92,31 @@ def test_incident_fits_tristates_no_tristates_set_with_fallback(self):
incident.acked = False
empty_filter = FilterWrapper({})
result = empty_filter.incident_fits_tristates(incident)
self.assertEqual(result, False)
self.assertEqual(result["acked"], False)
# Should match
incident.acked = True
empty_filter = FilterWrapper({})
result = empty_filter.incident_fits_tristates(incident)
self.assertEqual(result, True)
self.assertEqual(result["acked"], True)
self.assertNotIn(False, result.values())

def test_incident_fits_tristates_is_true(self):
incident = Mock()
incident.open = True
incident.acked = False
incident.stateful = True
empty_filter = FilterWrapper({"open": True, "acked": False})
result = empty_filter.incident_fits_tristates(incident)
self.assertTrue(result)
filter = FilterWrapper({"open": True, "acked": False})
result = filter.incident_fits_tristates(incident)
self.assertTrue(set(result.values())) # all True!

def test_incident_fits_tristates_is_false(self):
incident = Mock()
incident.open = True
incident.acked = False
incident.stateful = True
empty_filter = FilterWrapper({"open": False, "acked": False})
result = empty_filter.incident_fits_tristates(incident)
self.assertFalse(result)
filter = FilterWrapper({"open": False, "acked": False})
result = filter.incident_fits_tristates(incident)
self.assertIn(False, result.values())

@override_settings(ARGUS_FALLBACK_FILTER={"acked": True})
def test_incident_fits_tristates_fallback_should_not_override(self):
Expand All @@ -124,7 +125,8 @@ def test_incident_fits_tristates_fallback_should_not_override(self):
incident.acked = False
filter = FilterWrapper({"acked": False})
result = filter.incident_fits_tristates(incident)
self.assertEqual(result, True)
self.assertEqual(result["acked"], True)
self.assertNotIn(False, result.values())


@tag("unittest")
Expand Down

0 comments on commit 7a84141

Please sign in to comment.