Files
aiagent/backend/app/models/alert_rule.py
renjianbo 876789fac1 feat: multi-tenant workspace isolation, RBAC, sidebar nav, billing, and Android enhancements
- Backend: workspace_id isolation for 14 model tables + safe migration/backfill
- Backend: RBAC system with 4 roles and 23 permissions, seeded on startup
- Backend: workspace admin endpoints (list/manage all workspaces)
- Backend: admin user management API (CRUD, reset password)
- Backend: billing API with subscription plans, usage tracking, rate limiting
- Backend: fix system_logs.py UNION query and wrong column references
- Backend: WebSocket JWT auth and workspace enforcement
- Frontend: sidebar navigation replacing top dropdown menu
- Frontend: user management page (Users.vue) for admins
- Frontend: enhanced Workspaces.vue with admin table view
- Frontend: workspace RBAC computed properties in user store
- Android: agent marketplace, billing/subscription UI, onboarding wizard
- Android: phone login, analytics tracker, crash handler, network diagnostics
- Android: splash screen, encrypted token storage, app update enhancements
- Docs: multi-tenant RBAC guide with 8 sections and role-permission matrix

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-07-04 01:00:22 +08:00

100 lines
4.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
告警规则模型
"""
from sqlalchemy import Column, String, Text, Integer, Boolean, DateTime, ForeignKey, Index, JSON, func
from sqlalchemy.dialects.mysql import CHAR
from sqlalchemy.orm import relationship
from app.core.database import Base
import uuid
class AlertRule(Base):
"""告警规则表"""
__tablename__ = "alert_rules"
id = Column(CHAR(36), primary_key=True, default=lambda: str(uuid.uuid4()), comment="告警规则ID")
name = Column(String(100), nullable=False, comment="规则名称")
description = Column(Text, comment="规则描述")
# 告警类型execution_failed执行失败、execution_timeout执行超时、error_rate错误率、resource_usage资源使用
alert_type = Column(String(50), nullable=False, comment="告警类型")
# 监控目标workflow工作流、agent智能体、system系统
target_type = Column(String(50), nullable=False, comment="监控目标类型")
target_id = Column(CHAR(36), nullable=True, comment="监控目标ID为空则监控所有")
# 告警条件JSON格式
# 例如:{"threshold": 5, "time_window": 3600, "comparison": "gt"} 表示1小时内失败次数大于5
conditions = Column(JSON, nullable=False, comment="告警条件")
# 通知方式email邮件、webhookWebhook、internal站内通知
notification_type = Column(String(50), nullable=False, comment="通知方式")
notification_config = Column(JSON, nullable=True, comment="通知配置如邮箱地址、Webhook URL等")
# 状态
enabled = Column(Boolean, default=True, comment="是否启用")
# 统计信息
trigger_count = Column(Integer, default=0, comment="触发次数")
last_triggered_at = Column(DateTime, comment="最后触发时间")
# 用户关联
user_id = Column(CHAR(36), ForeignKey("users.id"), nullable=False, comment="创建者ID")
workspace_id = Column(CHAR(36), ForeignKey("workspaces.id"), nullable=True, index=True, comment="工作区ID")
# 时间戳
created_at = Column(DateTime, default=func.now(), comment="创建时间")
updated_at = Column(DateTime, default=func.now(), onupdate=func.now(), comment="更新时间")
__table_args__ = (
Index("idx_ar_alert_type", "alert_type"),
Index("idx_ar_enabled", "enabled"),
Index("idx_ar_target_type", "target_type"),
)
# 关系
user = relationship("User", backref="alert_rules")
def __repr__(self):
return f"<AlertRule(id={self.id}, name={self.name}, alert_type={self.alert_type})>"
class AlertLog(Base):
"""告警日志表"""
__tablename__ = "alert_logs"
id = Column(CHAR(36), primary_key=True, default=lambda: str(uuid.uuid4()), comment="告警日志ID")
rule_id = Column(CHAR(36), ForeignKey("alert_rules.id"), nullable=False, comment="告警规则ID")
# 告警信息
alert_type = Column(String(50), nullable=False, comment="告警类型")
severity = Column(String(20), default="warning", comment="严重程度info/warning/error/critical")
message = Column(Text, nullable=False, comment="告警消息")
details = Column(JSON, comment="详细信息")
# 状态
status = Column(String(20), default="pending", comment="状态pending/sent/failed/acknowledged")
# 通知信息
notification_type = Column(String(50), comment="通知方式")
notification_result = Column(Text, comment="通知结果")
# 时间戳
triggered_at = Column(DateTime, default=func.now(), comment="触发时间")
acknowledged_at = Column(DateTime, comment="确认时间")
acknowledged_by = Column(CHAR(36), ForeignKey("users.id"), nullable=True, comment="确认人ID")
workspace_id = Column(CHAR(36), ForeignKey("workspaces.id"), nullable=True, index=True, comment="工作区ID")
__table_args__ = (
Index("idx_al_rule_id", "rule_id"),
Index("idx_al_status", "status"),
Index("idx_al_triggered_at", "triggered_at"),
)
# 关系
rule = relationship("AlertRule", backref="logs")
acknowledged_user = relationship("User", foreign_keys=[acknowledged_by])
def __repr__(self):
return f"<AlertLog(id={self.id}, rule_id={self.rule_id}, severity={self.severity})>"