refactor(Dockerfile): 使用requirements.txt替代硬编码依赖 将Dockerfile中的硬编码pip包列表替换为通过requirements.txt文件安装, 提高依赖管理的灵活性和可维护性。 feat(scheduling): 移除内置APScheduler,采用独立调度系统 移除app/core/background/scheduler.py中原来的APScheduler实现, 改为使用新的应用级调度系统app.application.scheduling。 refactor(task_queue): 调整任务队列模块结构和导入路径 将任务队列相关常量从app.core.background.task_queue.constants迁移至 app.tasks.constants,并更新所有相关导入路径和引用。 refactor(events): 将事件服务重构为独立的应用层组件 将事件分发逻辑从核心层迁移到应用层,使用app.application.events.EventDispatchService 替代原有的app.modules.events.services.EventService。 feat(ai_memory): 增强AI记忆自动写入的安全策略 新增ai_memory_blocked_content_terms配置项用于阻止敏感内容, 添加TTL过期机制控制自动写入条目的生命周期。 fix(security): 强化生产环境安全验证机制 增加model_validator确保生产环境中数据库连接、API密钥、CORS设置等 关键安全配置符合要求。 feat(risks): 优化风险事件操作动作的外键约束 为RiskEventAction模型的风险事件ID字段添加外键约束, 防止孤立记录并增强数据完整性。 refactor(audit): 优化审计服务方法命名和事务处理 将AuditService的log方法重命名为record以反映其阶段行为, 并调整事务提交时机以提高性能。 feat(events): 增强领域事件并发处理和响应模型 添加事件锁定机制防止重复处理,更新API响应模型以提供 更准确的数据类型定义。 ```
120 lines
4.4 KiB
Python
120 lines
4.4 KiB
Python
from sqlalchemy.orm import Session
|
|
|
|
from app.modules.audit.constants import AuditAction, AuditSource
|
|
from app.modules.audit.schemas import AuditLogCreate
|
|
from app.modules.audit.service import AuditService
|
|
from app.modules.events.constants import (
|
|
EventAggregateType,
|
|
EventPayloadKey,
|
|
EventSource,
|
|
EventType,
|
|
)
|
|
from app.modules.events.services import EventService
|
|
from app.modules.feishu.service import FeishuService
|
|
from app.modules.reports.constants import (
|
|
ReportPushStatus,
|
|
ReportResponseKey,
|
|
)
|
|
from app.modules.reports.chart import lifecycle_chart_alt, render_lifecycle_chart
|
|
from app.modules.reports.services import ReportService
|
|
|
|
|
|
class ReportDeliveryService:
|
|
"""Deliver generated reports without coupling report generation to Feishu."""
|
|
|
|
def __init__(self, db: Session):
|
|
self.db = db
|
|
self.reports = ReportService(db)
|
|
|
|
def push_report(
|
|
self,
|
|
report: dict,
|
|
receive_id: str | None,
|
|
receive_id_type: str,
|
|
actor: str,
|
|
push_run_code: str | None = None,
|
|
) -> dict:
|
|
report_type = str(report.get(ReportResponseKey.REPORT_TYPE) or report.get("type") or "report")
|
|
title = report.get(ReportResponseKey.TITLE)
|
|
push_run = (
|
|
self.reports._get_push_run(push_run_code)
|
|
if push_run_code
|
|
else self.reports.create_push_run(
|
|
report_type=report_type,
|
|
title=title,
|
|
receive_id=receive_id,
|
|
receive_id_type=receive_id_type,
|
|
actor=actor,
|
|
)
|
|
)
|
|
try:
|
|
feishu = FeishuService(self.db)
|
|
image_key = None
|
|
image_alt = None
|
|
chart_data = report.get("chart_data")
|
|
if chart_data:
|
|
image_result = feishu.upload_image(render_lifecycle_chart(chart_data), actor)
|
|
image_key = (image_result.get("data") or {}).get("image_key")
|
|
if not image_key:
|
|
raise ValueError("Feishu image upload did not return image_key")
|
|
image_alt = lifecycle_chart_alt(chart_data)
|
|
card = FeishuService.build_basic_card(
|
|
report[ReportResponseKey.TITLE],
|
|
report[ReportResponseKey.LINES],
|
|
image_key=image_key,
|
|
image_alt=image_alt,
|
|
)
|
|
result = feishu.send_card(card, receive_id, receive_id_type, actor)
|
|
except Exception as exc:
|
|
failed_run = self.reports.update_push_run(
|
|
push_run.code,
|
|
ReportPushStatus.FAILED,
|
|
error_message=str(exc),
|
|
commit=False,
|
|
)
|
|
EventService(self.db).enqueue(
|
|
event_type=EventType.REPORT_PUSH_FAILED,
|
|
source=EventSource.REPORTS,
|
|
aggregate_type=EventAggregateType.REPORT_PUSH_RUN,
|
|
aggregate_id=failed_run.code,
|
|
actor=actor,
|
|
payload={
|
|
EventPayloadKey.CODE: failed_run.code,
|
|
EventPayloadKey.STATUS: failed_run.status,
|
|
EventPayloadKey.ERROR_MESSAGE: failed_run.error_message,
|
|
},
|
|
idempotency_key=f"report-push:{failed_run.code}:{failed_run.status}",
|
|
)
|
|
self.db.commit()
|
|
raise
|
|
success_run = self.reports.update_push_run(
|
|
push_run.code,
|
|
ReportPushStatus.SUCCESS,
|
|
provider_response=result,
|
|
sent=True,
|
|
commit=False,
|
|
)
|
|
EventService(self.db).enqueue(
|
|
event_type=EventType.REPORT_PUSH_SUCCEEDED,
|
|
source=EventSource.REPORTS,
|
|
aggregate_type=EventAggregateType.REPORT_PUSH_RUN,
|
|
aggregate_id=success_run.code,
|
|
actor=actor,
|
|
payload={
|
|
EventPayloadKey.CODE: success_run.code,
|
|
EventPayloadKey.STATUS: success_run.status,
|
|
},
|
|
idempotency_key=f"report-push:{success_run.code}:{success_run.status}",
|
|
)
|
|
AuditService(self.db).record(
|
|
AuditLogCreate(
|
|
actor=actor,
|
|
source=AuditSource.REPORTS,
|
|
action=AuditAction.REPORT_PUSH,
|
|
target_id=push_run.code,
|
|
response_payload={"status": ReportPushStatus.SUCCESS},
|
|
)
|
|
)
|
|
self.db.commit()
|
|
return result
|