diff --git a/baps_types/alert.py b/baps_types/alert.py index 493a544..f6958fc 100644 --- a/baps_types/alert.py +++ b/baps_types/alert.py @@ -1,13 +1,13 @@ -from typing import Any, Dict +from typing import Any, Dict, Optional from datetime import datetime CRITICAL = "Critical" WARNING = "Warning" class Alert: - start_time: int = 0 - last_time: int = 0 - end_time: int = -1 + start_time: datetime + last_time: datetime + end_time: Optional[datetime] id: str title: str description: str @@ -23,11 +23,14 @@ class Alert: return "warning" return "info" - # return self._weight + # This alert has happened again. + def reoccured(self): + self.last_time = datetime.now() + self.end_time = None - # weight.setter - # def weight(self, value: int): - # self._weight = value + # This alert has finished, just update end time and keep last_time. + def cleared(self): + self.end_time = datetime.now() @property @@ -63,3 +66,4 @@ class Alert: setattr(self,key,new_data[key]) self.last_time = self.start_time + self.end_time = None diff --git a/helpers/alert_manager.py b/helpers/alert_manager.py index fea93ed..ab9c476 100644 --- a/helpers/alert_manager.py +++ b/helpers/alert_manager.py @@ -1,4 +1,4 @@ -from typing import Any, List +from typing import Any, List, Optional #Magic for importing alert providers from alerts directory. from pkgutil import iter_modules @@ -54,25 +54,40 @@ class AlertManager(): def poll_alerts(self): # Poll modules for any alerts. - alerts: List[Alert] = [] + new_alerts: List[Optional[Alert]] = [] for provider in self._providers: provider_alerts = provider.get_alerts() if provider_alerts: - alerts.extend(provider_alerts) + new_alerts.extend(provider_alerts) - self._alerts = alerts + # Here we replace new firing alerts with older ones, to keep any context. + # (This doesn't do anything yet really, for future use.) + for existing in self._alerts: + found = False + for new in new_alerts: + # given we're removing alerts, got to skip any we removed. + if not new: + continue + + if existing.id == new.id: + # Alert is continuing. Replace it with the old one. + index = new_alerts.index(new) + existing.reoccured() + new_alerts[index] = None # We're going to merge the existing and new, so clear the new one out. + found = True + break + if found == False: + # The existing alert is gone, mark it as ended. + existing.cleared() + + self._alerts.extend([value for value in new_alerts if value]) # Remove any nulled out new alerts @property def alerts_current(self): self.poll_alerts() - return self._alerts + return [alert for alert in self._alerts if not alert.end_time] @property - def alert_count_current(self): + def alerts_previous(self): self.poll_alerts() - return len(self._alerts) - - @property - def alert_count_previous(self): - self.poll_alerts() - return len(self._alerts) + return [alert for alert in self._alerts if alert.end_time] diff --git a/ui-templates/alerts.html b/ui-templates/alerts.html index ff100c0..36e5fdd 100644 --- a/ui-templates/alerts.html +++ b/ui-templates/alerts.html @@ -1,43 +1,18 @@ {% extends 'base.html' %} +{% from 'parts/alert_list.html' import alert_list %} {% block head %} {% endblock %} {% block content_inner %} {% if data %}
{{ alert.description | replace("\n\n", "
") | replace("\n", "
")}}
{{ alert.description | replace("\n\n", "
") | replace("\n", "
")}}