alarms: refactor the alarms subsystem
also include eotk alarms now
This commit is contained in:
parent
a935055083
commit
e2ce24bf3b
17 changed files with 288 additions and 152 deletions
|
@ -1,48 +1,40 @@
|
|||
import datetime
|
||||
from typing import Optional
|
||||
from typing import Optional, List
|
||||
|
||||
from app.extensions import db
|
||||
from app.models.alarms import Alarm
|
||||
|
||||
|
||||
def alarms_for(target: str) -> List[Alarm]:
|
||||
return list(Alarm.query.filter(
|
||||
Alarm.target == target
|
||||
).all())
|
||||
|
||||
|
||||
def _get_alarm(target: str,
|
||||
alarm_type: str,
|
||||
*,
|
||||
proxy_id: Optional[int] = None,
|
||||
origin_id: Optional[int] = None,
|
||||
aspect: str,
|
||||
create_if_missing: bool = True) -> Optional[Alarm]:
|
||||
alarm: Optional[Alarm]
|
||||
if target == "proxy":
|
||||
alarm = Alarm.query.filter(
|
||||
Alarm.target == "proxy",
|
||||
Alarm.alarm_type == alarm_type,
|
||||
Alarm.proxy_id == proxy_id
|
||||
).first()
|
||||
elif target == "origin":
|
||||
alarm = Alarm.query.filter(
|
||||
Alarm.target == "origin",
|
||||
Alarm.alarm_type == alarm_type,
|
||||
Alarm.proxy_id == origin_id
|
||||
).first()
|
||||
else:
|
||||
return None
|
||||
alarm: Optional[Alarm] = Alarm.query.filter(
|
||||
Alarm.aspect == aspect,
|
||||
Alarm.target == target
|
||||
).first()
|
||||
if create_if_missing and alarm is None:
|
||||
alarm = Alarm()
|
||||
alarm.aspect = aspect
|
||||
alarm.target = target
|
||||
alarm.alarm_type = alarm_type
|
||||
alarm.text = "New alarm"
|
||||
alarm.state_changed = datetime.datetime.utcnow()
|
||||
if target == "proxy":
|
||||
alarm.proxy_id = proxy_id
|
||||
if target == "origin":
|
||||
alarm.origin_id = origin_id
|
||||
alarm.last_updated = datetime.datetime.utcnow()
|
||||
db.session.add(alarm)
|
||||
db.session.commit()
|
||||
return alarm
|
||||
|
||||
|
||||
def get_proxy_alarm(proxy_id: int, alarm_type: str) -> Alarm:
|
||||
alarm = _get_alarm("proxy", alarm_type, proxy_id=proxy_id)
|
||||
def get_alarm(target: str, aspect: str) -> Optional[Alarm]:
|
||||
return _get_alarm(target, aspect, create_if_missing=False)
|
||||
|
||||
|
||||
def get_or_create_alarm(target: str, aspect: str) -> Alarm:
|
||||
alarm = _get_alarm(target, aspect, create_if_missing=True)
|
||||
if alarm is None:
|
||||
# mypy can't tell that this will never be reached
|
||||
raise RuntimeError("Creating an alarm must have failed.")
|
||||
raise RuntimeError("Asked for an alarm to be created but got None.")
|
||||
return alarm
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue