alarms: initialise state changed on create
This commit is contained in:
parent
5050ff5a28
commit
a33ef03607
1 changed files with 16 additions and 1 deletions
|
@ -1,10 +1,14 @@
|
||||||
|
import datetime
|
||||||
|
|
||||||
from app.extensions import db
|
from app.extensions import db
|
||||||
from app.models import Alarm
|
from app.models import Alarm
|
||||||
|
|
||||||
|
|
||||||
def _get_alarm(target: str,
|
def _get_alarm(target: str,
|
||||||
alarm_type: str,
|
alarm_type: str,
|
||||||
|
*,
|
||||||
proxy_id=None,
|
proxy_id=None,
|
||||||
|
origin_id=None,
|
||||||
create_if_missing=True):
|
create_if_missing=True):
|
||||||
if target == "proxy":
|
if target == "proxy":
|
||||||
alarm = Alarm.query.filter(
|
alarm = Alarm.query.filter(
|
||||||
|
@ -12,16 +16,27 @@ def _get_alarm(target: str,
|
||||||
Alarm.alarm_type == alarm_type,
|
Alarm.alarm_type == alarm_type,
|
||||||
Alarm.proxy_id == proxy_id
|
Alarm.proxy_id == proxy_id
|
||||||
).first()
|
).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
|
||||||
if create_if_missing and alarm is None:
|
if create_if_missing and alarm is None:
|
||||||
alarm = Alarm()
|
alarm = Alarm()
|
||||||
alarm.target = target
|
alarm.target = target
|
||||||
alarm.alarm_type = alarm_type
|
alarm.alarm_type = alarm_type
|
||||||
|
alarm.state_changed = datetime.datetime.utcnow()
|
||||||
if target == "proxy":
|
if target == "proxy":
|
||||||
alarm.proxy_id = proxy_id
|
alarm.proxy_id = proxy_id
|
||||||
|
if target == "origin":
|
||||||
|
alarm.origin_id = origin_id
|
||||||
db.session.add(alarm)
|
db.session.add(alarm)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return alarm
|
return alarm
|
||||||
|
|
||||||
|
|
||||||
def get_proxy_alarm(proxy_id: int, alarm_type: str):
|
def get_proxy_alarm(proxy_id: int, alarm_type: str):
|
||||||
return _get_alarm("proxy", "alarm_type", proxy_id=proxy_id)
|
return _get_alarm("proxy", alarm_type, proxy_id=proxy_id)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue