forked from iti-framework/iTi-Flask
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
3.3 KiB
Python
118 lines
3.3 KiB
Python
from apiflask import APIBlueprint
|
|
from iti.applications.service.iot.alert import (
|
|
add_endpoint_alert_log,
|
|
add_node_alert_log
|
|
)
|
|
from iti.applications.extensions import db
|
|
from iti.applications.common.utils import success, page_schema, page
|
|
from iti.applications.models import (
|
|
IotAlertLog,
|
|
IotAlertLogSchema,
|
|
)
|
|
from .schemas.alert_log import (
|
|
AlertLogQuery,
|
|
AlertLogAddRequest,
|
|
AlertLogUpdateRequest,
|
|
NodeAlertLogAddRequest,
|
|
)
|
|
from iti.applications.common import ModelFilter
|
|
from iti.applications.common.exceptions.biz_exp import BizException
|
|
from flask_jwt_extended import jwt_required
|
|
from sqlalchemy import select, delete, exists
|
|
from sqlalchemy.sql.functions import func
|
|
from sqlalchemy.orm import noload
|
|
from iti.applications.common import permission
|
|
|
|
bp = APIBlueprint("iot_alert_log", __name__, url_prefix="/alertLog", tag="告警中心")
|
|
|
|
@bp.get("/list")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@permission("iot:alertLog:list")
|
|
@bp.input(AlertLogQuery.Schema(partial=True), location="query")
|
|
@bp.output(IotAlertLogSchema(many=True))
|
|
def list_alert_log(query_data: AlertLogQuery):
|
|
"""
|
|
获取告警日志列表
|
|
"""
|
|
|
|
return success(get_list_or_page(query_data))
|
|
|
|
@bp.get("/page")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@permission("iot:alertLog:list")
|
|
@bp.input(AlertLogQuery.Schema(partial=True), location="query")
|
|
@bp.output(page_schema(IotAlertLogSchema))
|
|
def page_alert_log(query_data: AlertLogQuery):
|
|
"""
|
|
获取告警日志分页列表
|
|
"""
|
|
|
|
return success(get_list_or_page(query_data))
|
|
|
|
|
|
@bp.post("/add/<int:endpoint_id>")
|
|
# @jwt_required()
|
|
# @bp.doc(security="JWT")
|
|
# @permission("iot:alertLog:add")
|
|
def add_alert_log(endpoint_id: int):
|
|
"""
|
|
添加采集端网络不可达告警日志
|
|
"""
|
|
|
|
result = add_endpoint_alert_log(endpoint_id)
|
|
if len(result) > 0:
|
|
raise BizException(result)
|
|
|
|
return success()
|
|
|
|
@bp.post("/addNodeValue")
|
|
# @jwt_required()
|
|
# @bp.doc(security="JWT")
|
|
# @permission("iot:alertLog:add")
|
|
@bp.input(NodeAlertLogAddRequest, location="json")
|
|
def add_node_alert(json_data: dict):
|
|
"""
|
|
添加节点值异常告警日志
|
|
"""
|
|
node_id = json_data["node_id"]
|
|
alert_value = json_data["alert_value"]
|
|
result = add_node_alert_log(node_id, alert_value)
|
|
if len(result) > 0:
|
|
raise BizException(result)
|
|
|
|
return success();
|
|
|
|
|
|
@bp.post("/recover/<int:id>")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@permission("iot:alertLog:update")
|
|
def recover_alert_log(id: int):
|
|
"""
|
|
恢复告警日志
|
|
"""
|
|
|
|
alert_log = db.session.scalar(select(IotAlertLog).filter_by(id=id))
|
|
if not alert_log:
|
|
raise BizException("告警日志不存在")
|
|
|
|
alert_log.trigger_count =0
|
|
alert_log.status = 0
|
|
db.session.commit()
|
|
return success()
|
|
|
|
def get_list_or_page(query_data: AlertLogQuery):
|
|
"""
|
|
获取告警日志列表
|
|
"""
|
|
query = select(IotAlertLog).order_by(IotAlertLog.created_at.desc())
|
|
if query_data.alert_tag is not None:
|
|
query = query.filter(IotAlertLog.alert_tag == query_data.alert_tag)
|
|
if query_data.status is not None:
|
|
query = query.filter(IotAlertLog.status == query_data.status)
|
|
if query_data.page and query_data.size:
|
|
return db.paginate(query, page=query_data.page, per_page=query_data.size)
|
|
else:
|
|
return db.session.scalars(query).all() |