BAPSicle/baps_types/alert.py

69 lines
1.9 KiB
Python
Raw Normal View History

from typing import Any, Dict, Optional
2021-09-11 22:44:15 +00:00
from datetime import datetime
CRITICAL = "Critical"
WARNING = "Warning"
2021-09-22 18:49:24 +00:00
2021-09-11 22:44:15 +00:00
class Alert:
start_time: datetime
last_time: datetime
end_time: Optional[datetime]
2021-09-11 22:44:15 +00:00
id: str
title: str
description: str
module: str
severity: str
@property
def ui_class(self) -> str:
2021-09-22 18:49:24 +00:00
if self.severity == CRITICAL:
return "danger"
if self.severity == WARNING:
return "warning"
return "info"
2021-09-11 22:44:15 +00:00
# This alert has happened again.
def reoccured(self):
2021-09-22 18:49:24 +00:00
self.last_time = datetime.now()
self.end_time = None
2021-09-11 22:44:15 +00:00
# This alert has finished, just update end time and keep last_time.
def cleared(self):
2021-09-22 18:49:24 +00:00
self.end_time = datetime.now()
2021-09-11 22:44:15 +00:00
@property
def __dict__(self):
attrs = ["start_time", "last_time", "end_time", "id", "title", "description", "module", "severity"]
out = {}
for attr in attrs:
2021-09-22 18:49:24 +00:00
out[attr] = self.__getattribute__(attr)
2021-09-11 22:44:15 +00:00
return out
2021-09-22 18:49:24 +00:00
def __init__(self, new_data: Dict[str, Any]):
required_vars = [
"start_time", # Just in case an alert wants to show starting earlier than it is reported.
"id",
"title",
"description",
"module",
"severity"
]
2021-09-11 22:44:15 +00:00
2021-09-22 18:49:24 +00:00
for key in required_vars:
if key not in new_data.keys():
raise KeyError("Key {} is missing from data to create Alert.".format(key))
2021-09-11 22:44:15 +00:00
2021-09-22 18:49:24 +00:00
# if type(new_data[key]) != type(getattr(self,key)):
# raise TypeError("Key {} has type {}, was expecting {}.".format(key, type(new_data[key]), type(getattr(self,key))))
2021-09-11 22:44:15 +00:00
2021-09-22 18:49:24 +00:00
# Account for if the creator didn't want to set a custom time.
if key == "start_time" and new_data[key] == -1:
new_data[key] = datetime.now()
2021-09-11 22:44:15 +00:00
2021-09-22 18:49:24 +00:00
setattr(self, key, new_data[key])
2021-09-11 22:44:15 +00:00
2021-09-22 18:49:24 +00:00
self.last_time = self.start_time
self.end_time = None