From bc2e60cdc9c33820b4bba95de2b0fda856aa2304 Mon Sep 17 00:00:00 2001 From: Matthew Stratford Date: Wed, 22 Sep 2021 00:15:17 +0100 Subject: [PATCH] Update alerts to actually update/end, move list to macro. --- baps_types/alert.py | 20 +++++++++------ helpers/alert_manager.py | 39 +++++++++++++++++++++--------- ui-templates/alerts.html | 35 ++++----------------------- ui-templates/parts/alert_list.html | 31 ++++++++++++++++++++++++ web_server.py | 4 +-- 5 files changed, 77 insertions(+), 52 deletions(-) create mode 100644 ui-templates/parts/alert_list.html 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 %}

Current Alerts: {{ data.alert_count_current }}

-
- {% for alert in data.alerts_current %} -
-
-

- - {{ alert.severity }} -

- Since {{ alert.start_time }} - Last Seen {{ alert.last_time }} - {% if alert.end_time > -1 %} - Ended {{ alert.end_time }} - {% endif %} -
- -
-
- Module: {{ alert.module }} - {% autoescape false %} -

{{ alert.description | replace("\n\n", "

") | replace("\n", "
")}}

- {% endautoescape %} -
-
-
- {% endfor %} + {{ alert_list(data.alerts_current) }}

Previous Alerts: {{ data.alert_count_previous }}

-
- -
+
+ {{ alert_list(data.alerts_previous) }} +
{% endif %} {% endblock %} diff --git a/ui-templates/parts/alert_list.html b/ui-templates/parts/alert_list.html new file mode 100644 index 0000000..6c8ae48 --- /dev/null +++ b/ui-templates/parts/alert_list.html @@ -0,0 +1,31 @@ +{% macro alert_list(alerts) %} + {% for alert in alerts %} +
+
+

+ + {{ alert.severity }} +

+ Since {{ alert.start_time }} + Last Seen {{ alert.last_time }} + {% if alert.end_time %} + Ended {{ alert.end_time }} + {% endif %} +
+ +
+
+ Module: {{ alert.module }} + {% autoescape false %} +

{{ alert.description | replace("\n\n", "

") | replace("\n", "
")}}

+ {% endautoescape %} +
+
+
+ {% endfor %} + {% if not alerts %} + No alerts here. + {% endif %} +{% endmacro %} diff --git a/web_server.py b/web_server.py index 89d17f8..08252a7 100644 --- a/web_server.py +++ b/web_server.py @@ -122,7 +122,7 @@ def ui_index(request): data = { "ui_page": "index", "ui_title": "", - "alert_count": alerts.alert_count_current, + "alert_count": len(alerts.alerts_current), "server_version": config["server_version"], "server_build": config["server_build"], "server_name": config["server_name"], @@ -146,7 +146,7 @@ def ui_status(request): def ui_alerts(request): data = { "alerts_current": alerts.alerts_current, - "alerts_count_current": alerts.alert_count_current, + "alerts_previous": alerts.alerts_previous, "ui_page": "alerts", "ui_title": "Alerts" }