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.
111 lines
3.2 KiB
Python
111 lines
3.2 KiB
Python
from apiflask import APIBlueprint
|
|
from iti.applications.extensions import db, sys_log
|
|
from iti.applications.common.utils import success, page_schema, page
|
|
from iti.applications.models import (
|
|
IotAlertRule,
|
|
IotAlertRuleSchema,
|
|
)
|
|
from .schemas.alert_rule import (
|
|
AlertRuleQuery,
|
|
AlertRuleAddRequest,
|
|
AlertRuleUpdateRequest,
|
|
)
|
|
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_rule", __name__, url_prefix="/alertRule", tag="告警规则")
|
|
|
|
@bp.get("/list")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@permission("iot:alertRule:list")
|
|
@bp.input(AlertRuleQuery.Schema(partial=True), location="query")
|
|
@bp.output(IotAlertRuleSchema(many=True))
|
|
def get_alert_rule_list(query_data: AlertRuleQuery):
|
|
"""
|
|
获取告警规则列表
|
|
"""
|
|
|
|
return get_list_or_page(query_data)
|
|
|
|
@bp.get("/page")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@permission("iot:alertRule:list")
|
|
@bp.input(AlertRuleQuery.Schema(partial=True), location="query")
|
|
@bp.output(page_schema(IotAlertRuleSchema))
|
|
def get_alert_rule_page(query_data: AlertRuleQuery):
|
|
"""
|
|
获取告警规则分页列表
|
|
"""
|
|
return page(get_list_or_page(query_data))
|
|
|
|
@bp.post("/add")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@permission("iot:alertRule:add")
|
|
@bp.input(AlertRuleAddRequest, location="json")
|
|
def add_alert_rule(json_data: dict):
|
|
"""
|
|
添加告警规则
|
|
"""
|
|
alert_rule = IotAlertRule(**json_data)
|
|
db.session.add(alert_rule)
|
|
db.session.commit()
|
|
return success()
|
|
|
|
@bp.put("/<int:id>")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@permission("iot:alertRule:update")
|
|
@bp.input(AlertRuleUpdateRequest(partial=True), location="json")
|
|
def update_alert_rule(id: int, json_data: dict):
|
|
"""
|
|
更新告警规则
|
|
"""
|
|
alert_rule = db.session.scalar(select(IotAlertRule).where(IotAlertRule.id == id))
|
|
if not alert_rule:
|
|
raise BizException("告警规则不存在")
|
|
|
|
for key, value in json_data.items():
|
|
setattr(alert_rule, key, value)
|
|
|
|
db.session.commit()
|
|
return success()
|
|
|
|
@bp.delete("/<int:id>")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@permission("iot:alertRule:delete")
|
|
def delete_alert_rule(id: int):
|
|
"""
|
|
删除告警规则
|
|
"""
|
|
alert_rule = db.session.scalar(select(IotAlertRule).where(IotAlertRule.id == id))
|
|
if not alert_rule:
|
|
raise BizException("告警规则不存在")
|
|
|
|
db.session.delete(alert_rule)
|
|
db.session.commit()
|
|
return success()
|
|
|
|
|
|
|
|
def get_list_or_page(query_data: AlertRuleQuery):
|
|
"""
|
|
获取告警规则列表
|
|
"""
|
|
query = select(IotAlertRule).order_by(IotAlertRule.created_at.desc())
|
|
if query_data.node_id:
|
|
query = query.filter(IotAlertRule.node_id == query_data.node_id)
|
|
if query_data.status is not None:
|
|
query = query.filter(IotAlertRule.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() |