feat: 新增告警、成本、配额、微信模块及缓存服务
All checks were successful
continuous-integration/drone/push Build is passing

- 新增告警模块 (alerts): 告警规则配置与触发
- 新增成本管理模块 (cost): 成本统计与分析
- 新增配额模块 (quota): 配额管理与限制
- 新增微信模块 (wechat): 微信相关功能接口
- 新增缓存服务 (cache): Redis 缓存封装
- 新增请求日志中间件 (request_logger)
- 新增异常处理和链路追踪中间件
- 更新 dashboard 前端展示
- 更新 SDK stats_client 功能
This commit is contained in:
111
2026-01-24 16:53:47 +08:00
parent eab2533c36
commit 6c6c48cf71
29 changed files with 4607 additions and 41 deletions

108
backend/app/models/alert.py Normal file
View File

@@ -0,0 +1,108 @@
"""告警相关模型"""
from datetime import datetime
from sqlalchemy import Column, Integer, BigInteger, String, Text, Enum, SmallInteger, JSON, TIMESTAMP
from ..database import Base
class AlertRule(Base):
"""告警规则表"""
__tablename__ = "platform_alert_rules"
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(100), nullable=False) # 规则名称
description = Column(Text) # 规则描述
# 规则类型
rule_type = Column(Enum(
'error_rate', # 错误率告警
'call_count', # 调用次数告警
'token_usage', # Token使用量告警
'cost_threshold', # 费用阈值告警
'latency', # 延迟告警
'custom' # 自定义告警
), nullable=False)
# 作用范围
scope_type = Column(Enum('global', 'tenant', 'app'), default='global') # 作用范围类型
scope_value = Column(String(100)) # 作用范围值如租户ID或应用代码
# 告警条件
condition = Column(JSON, nullable=False) # 告警条件配置
# 示例: {"metric": "error_count", "operator": ">", "threshold": 10, "window": "5m"}
# 通知配置
notification_channels = Column(JSON) # 通知渠道列表
# 示例: [{"type": "wechat_bot", "webhook": "https://..."}, {"type": "email", "to": ["a@b.com"]}]
# 告警限制
cooldown_minutes = Column(Integer, default=30) # 冷却时间(分钟),避免重复告警
max_alerts_per_day = Column(Integer, default=10) # 每天最大告警次数
# 状态
status = Column(SmallInteger, default=1) # 0-禁用 1-启用
priority = Column(Enum('low', 'medium', 'high', 'critical'), default='medium') # 优先级
created_at = Column(TIMESTAMP, default=datetime.now)
updated_at = Column(TIMESTAMP, default=datetime.now, onupdate=datetime.now)
class AlertRecord(Base):
"""告警记录表"""
__tablename__ = "platform_alert_records"
id = Column(BigInteger, primary_key=True, autoincrement=True)
rule_id = Column(Integer, nullable=False, index=True) # 关联的规则ID
rule_name = Column(String(100)) # 规则名称(冗余,便于查询)
# 告警信息
alert_type = Column(String(50), nullable=False) # 告警类型
severity = Column(Enum('info', 'warning', 'error', 'critical'), default='warning') # 严重程度
title = Column(String(200), nullable=False) # 告警标题
message = Column(Text) # 告警详情
# 上下文
tenant_id = Column(String(50), index=True) # 相关租户
app_code = Column(String(50)) # 相关应用
metric_value = Column(String(100)) # 触发告警的指标值
threshold_value = Column(String(100)) # 阈值
# 通知状态
notification_status = Column(Enum('pending', 'sent', 'failed', 'skipped'), default='pending')
notification_result = Column(JSON) # 通知结果
notified_at = Column(TIMESTAMP) # 通知时间
# 处理状态
status = Column(Enum('active', 'acknowledged', 'resolved', 'ignored'), default='active')
acknowledged_by = Column(String(100)) # 确认人
acknowledged_at = Column(TIMESTAMP) # 确认时间
resolved_at = Column(TIMESTAMP) # 解决时间
created_at = Column(TIMESTAMP, default=datetime.now, index=True)
class NotificationChannel(Base):
"""通知渠道配置表"""
__tablename__ = "platform_notification_channels"
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(100), nullable=False) # 渠道名称
channel_type = Column(Enum(
'wechat_bot', # 企微机器人
'email', # 邮件
'sms', # 短信
'webhook', # Webhook
'dingtalk' # 钉钉
), nullable=False)
# 渠道配置
config = Column(JSON, nullable=False)
# wechat_bot: {"webhook": "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx"}
# email: {"smtp_host": "...", "smtp_port": 465, "username": "...", "password_encrypted": "..."}
# webhook: {"url": "https://...", "method": "POST", "headers": {...}}
# 状态
status = Column(SmallInteger, default=1) # 0-禁用 1-启用
created_at = Column(TIMESTAMP, default=datetime.now)
updated_at = Column(TIMESTAMP, default=datetime.now, onupdate=datetime.now)