```
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响应模型以提供 更准确的数据类型定义。 ```
This commit is contained in:
@@ -1,235 +0,0 @@
|
||||
from datetime import date
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.config import get_settings
|
||||
from app.core.constants import ActorValue
|
||||
from app.core.utils.time import utc_now
|
||||
from app.modules.feishu.service import FeishuService
|
||||
from app.modules.legacy_mysql.intasect import IntasectSyncService
|
||||
from app.modules.reports.constants import ReportPushStatus, ReportType
|
||||
from app.modules.reports.services import ReportService
|
||||
from app.modules.workflows.constants import WorkflowStatus, WorkflowType
|
||||
from app.modules.workflows.models import WorkflowInstance
|
||||
from app.modules.workflows.service import WorkflowService
|
||||
|
||||
|
||||
class LifecyclePipelineService:
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
self.workflows = WorkflowService(db)
|
||||
|
||||
def period_key(self, report_type: str, reference_date: date | None = None) -> str:
|
||||
start, end = ReportService(self.db)._management_period(report_type, reference_date)
|
||||
period_value = end.isoformat() if report_type == ReportType.DAILY else start.isoformat()
|
||||
return f"{report_type}:{period_value}"
|
||||
|
||||
def find(self, period_key: str) -> WorkflowInstance | None:
|
||||
return self.db.execute(
|
||||
select(WorkflowInstance).where(
|
||||
WorkflowInstance.workflow_type == WorkflowType.LIFECYCLE_REPORT,
|
||||
WorkflowInstance.aggregate_type == "report_period",
|
||||
WorkflowInstance.aggregate_id == period_key,
|
||||
)
|
||||
).scalar_one_or_none()
|
||||
|
||||
def prepare(
|
||||
self,
|
||||
report_type: str,
|
||||
actor: str,
|
||||
force: bool = False,
|
||||
) -> tuple[WorkflowInstance, str, bool]:
|
||||
period_key = self.period_key(report_type)
|
||||
existing = self.find(period_key)
|
||||
if existing is not None and existing.status == WorkflowStatus.COMPLETED and not force:
|
||||
return existing, period_key, True
|
||||
workflow = self.workflows.start_or_update(
|
||||
workflow_type=WorkflowType.LIFECYCLE_REPORT,
|
||||
aggregate_type="report_period",
|
||||
aggregate_id=period_key,
|
||||
status_value=WorkflowStatus.RUNNING,
|
||||
action="queued",
|
||||
actor=actor,
|
||||
payload={"report_type": report_type, "period_key": period_key, "force": force},
|
||||
)
|
||||
return workflow, period_key, False
|
||||
|
||||
def run(
|
||||
self,
|
||||
report_type: str,
|
||||
receive_id: str | None = None,
|
||||
receive_id_type: str = "chat_id",
|
||||
force: bool = False,
|
||||
actor: str = ActorValue.SCHEDULER,
|
||||
) -> dict[str, Any]:
|
||||
if get_settings().read_only_mode:
|
||||
return {
|
||||
"period_key": self.period_key(report_type),
|
||||
"deduplicated": False,
|
||||
"status": "operations_disabled",
|
||||
}
|
||||
workflow, period_key, deduplicated = self.prepare(report_type, actor, force)
|
||||
if deduplicated:
|
||||
return {
|
||||
"workflow_code": workflow.code,
|
||||
"period_key": period_key,
|
||||
"deduplicated": True,
|
||||
"status": workflow.status,
|
||||
}
|
||||
try:
|
||||
workflow = self.workflows.start_or_update(
|
||||
workflow_type=WorkflowType.LIFECYCLE_REPORT,
|
||||
aggregate_type="report_period",
|
||||
aggregate_id=period_key,
|
||||
status_value=WorkflowStatus.RUNNING,
|
||||
action="source_sync",
|
||||
actor=actor,
|
||||
payload={"report_type": report_type},
|
||||
)
|
||||
sync_result = IntasectSyncService(self.db).sync_all(
|
||||
run_code=workflow.code,
|
||||
force_full=False,
|
||||
)
|
||||
self.workflows.start_or_update(
|
||||
workflow_type=WorkflowType.LIFECYCLE_REPORT,
|
||||
aggregate_type="report_period",
|
||||
aggregate_id=period_key,
|
||||
status_value=WorkflowStatus.RUNNING,
|
||||
action="analysis",
|
||||
actor=actor,
|
||||
payload={
|
||||
"datasets": {name: result["processed"] for name, result in sync_result.items()}
|
||||
},
|
||||
)
|
||||
report_service = ReportService(self.db)
|
||||
report = report_service.management_lifecycle_report(
|
||||
report_type=report_type,
|
||||
actor=actor,
|
||||
include_ai=True,
|
||||
)
|
||||
settings = get_settings()
|
||||
target_receive_id = receive_id or settings.feishu_default_chat_id
|
||||
ai_analysis = report.get("ai_analysis") or {}
|
||||
if not ai_analysis.get("ok"):
|
||||
notified = self._notify_ai_unavailable(
|
||||
target_receive_id,
|
||||
receive_id_type,
|
||||
period_key,
|
||||
actor,
|
||||
)
|
||||
failed = self.workflows.start_or_update(
|
||||
workflow_type=WorkflowType.LIFECYCLE_REPORT,
|
||||
aggregate_type="report_period",
|
||||
aggregate_id=period_key,
|
||||
status_value=WorkflowStatus.FAILED,
|
||||
action="ai_unavailable",
|
||||
actor=actor,
|
||||
payload={
|
||||
"ai_unavailable": True,
|
||||
"notified": notified,
|
||||
"error_type": ai_analysis.get("type") or "AIUnavailable",
|
||||
},
|
||||
)
|
||||
return {
|
||||
"workflow_code": failed.code,
|
||||
"period_key": period_key,
|
||||
"deduplicated": False,
|
||||
"status": failed.status,
|
||||
"ai_unavailable": True,
|
||||
"notified": notified,
|
||||
}
|
||||
idempotency_key = period_key
|
||||
if force:
|
||||
idempotency_key = f"{period_key}:force:{utc_now():%Y%m%d%H%M%S%f}"
|
||||
push_run = report_service.create_push_run(
|
||||
report_type=report_type,
|
||||
title=str(report["title"]),
|
||||
receive_id=target_receive_id,
|
||||
receive_id_type=receive_id_type,
|
||||
actor=actor,
|
||||
status=ReportPushStatus.PENDING,
|
||||
idempotency_key=idempotency_key,
|
||||
)
|
||||
if push_run.status != ReportPushStatus.SUCCESS:
|
||||
report_service.push_report(
|
||||
report,
|
||||
target_receive_id,
|
||||
receive_id_type,
|
||||
actor,
|
||||
push_run_code=push_run.code,
|
||||
)
|
||||
completed = self.workflows.start_or_update(
|
||||
workflow_type=WorkflowType.LIFECYCLE_REPORT,
|
||||
aggregate_type="report_period",
|
||||
aggregate_id=period_key,
|
||||
status_value=WorkflowStatus.COMPLETED,
|
||||
action="pushed",
|
||||
actor=actor,
|
||||
payload={"push_run_code": push_run.code, "idempotency_key": idempotency_key},
|
||||
)
|
||||
return {
|
||||
"workflow_code": completed.code,
|
||||
"period_key": period_key,
|
||||
"push_run_code": push_run.code,
|
||||
"deduplicated": False,
|
||||
"status": completed.status,
|
||||
}
|
||||
except Exception as exc:
|
||||
self.db.rollback()
|
||||
self.workflows.start_or_update(
|
||||
workflow_type=WorkflowType.LIFECYCLE_REPORT,
|
||||
aggregate_type="report_period",
|
||||
aggregate_id=period_key,
|
||||
status_value=WorkflowStatus.FAILED,
|
||||
action="failed",
|
||||
actor=actor,
|
||||
payload={"error": str(exc)[:2000]},
|
||||
)
|
||||
self._notify_failure(receive_id, receive_id_type, period_key, exc, actor)
|
||||
raise
|
||||
|
||||
def _notify_ai_unavailable(
|
||||
self,
|
||||
receive_id: str | None,
|
||||
receive_id_type: str,
|
||||
period_key: str,
|
||||
actor: str,
|
||||
) -> bool:
|
||||
settings = get_settings()
|
||||
if (
|
||||
not receive_id
|
||||
or not settings.feishu_app_id
|
||||
or not settings.feishu_app_secret
|
||||
):
|
||||
return False
|
||||
FeishuService(self.db).send_text(
|
||||
f"生命周期报告 {period_key}:AI 当前不可用,本次分析报告未发送。请检查模型服务。",
|
||||
receive_id,
|
||||
receive_id_type,
|
||||
actor,
|
||||
)
|
||||
return True
|
||||
|
||||
def _notify_failure(
|
||||
self,
|
||||
receive_id: str | None,
|
||||
receive_id_type: str,
|
||||
period_key: str,
|
||||
error: Exception,
|
||||
actor: str,
|
||||
) -> None:
|
||||
settings = get_settings()
|
||||
target = receive_id or settings.feishu_default_chat_id
|
||||
if not target or not settings.feishu_app_id or not settings.feishu_app_secret:
|
||||
return
|
||||
try:
|
||||
FeishuService(self.db).send_text(
|
||||
f"生命周期报告 {period_key} 执行失败:{type(error).__name__}",
|
||||
target,
|
||||
receive_id_type,
|
||||
actor,
|
||||
)
|
||||
except Exception:
|
||||
self.db.rollback()
|
||||
@@ -3,6 +3,7 @@ from datetime import date
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.application.delivery import ReportDeliveryService
|
||||
from app.core.background.task_queue import (
|
||||
enqueue_attendance_summary_push,
|
||||
enqueue_daily_brief_push,
|
||||
@@ -188,7 +189,7 @@ def push_daily_brief(
|
||||
) -> dict:
|
||||
service = ReportService(db)
|
||||
report = service.daily_brief()
|
||||
return service.push_report(
|
||||
return ReportDeliveryService(db).push_report(
|
||||
report,
|
||||
payload.receive_id,
|
||||
payload.receive_id_type,
|
||||
@@ -204,7 +205,7 @@ def push_project_weekly(
|
||||
) -> dict:
|
||||
service = ReportService(db)
|
||||
report = service.project_weekly()
|
||||
return service.push_report(
|
||||
return ReportDeliveryService(db).push_report(
|
||||
report,
|
||||
payload.receive_id,
|
||||
payload.receive_id_type,
|
||||
@@ -220,7 +221,7 @@ def push_attendance_summary(
|
||||
) -> dict:
|
||||
service = ReportService(db)
|
||||
report = service.attendance_summary()
|
||||
return service.push_report(
|
||||
return ReportDeliveryService(db).push_report(
|
||||
report,
|
||||
payload.receive_id,
|
||||
payload.receive_id_type,
|
||||
@@ -236,7 +237,7 @@ def push_risk_progress(
|
||||
) -> dict:
|
||||
service = ReportService(db)
|
||||
report = service.risk_progress()
|
||||
return service.push_report(
|
||||
return ReportDeliveryService(db).push_report(
|
||||
report,
|
||||
payload.receive_id,
|
||||
payload.receive_id_type,
|
||||
@@ -252,7 +253,7 @@ def push_work_daily(
|
||||
) -> dict:
|
||||
service = ReportService(db)
|
||||
report = service.work_daily_report(reporter=principal.actor, actor=principal.actor)
|
||||
return service.push_report(
|
||||
return ReportDeliveryService(db).push_report(
|
||||
report,
|
||||
payload.receive_id,
|
||||
payload.receive_id_type,
|
||||
@@ -268,7 +269,7 @@ def push_work_weekly(
|
||||
) -> dict:
|
||||
service = ReportService(db)
|
||||
report = service.work_weekly_report(reporter=principal.actor, actor=principal.actor)
|
||||
return service.push_report(
|
||||
return ReportDeliveryService(db).push_report(
|
||||
report,
|
||||
payload.receive_id,
|
||||
payload.receive_id_type,
|
||||
|
||||
@@ -1,108 +0,0 @@
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
class ReportDeliveryMixin:
|
||||
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._get_push_run(push_run_code)
|
||||
if push_run_code
|
||||
else self.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.update_push_run(
|
||||
push_run.code,
|
||||
ReportPushStatus.FAILED,
|
||||
error_message=str(exc),
|
||||
)
|
||||
EventService(self.db).emit(
|
||||
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}",
|
||||
)
|
||||
raise
|
||||
success_run = self.update_push_run(
|
||||
push_run.code,
|
||||
ReportPushStatus.SUCCESS,
|
||||
provider_response=result,
|
||||
sent=True,
|
||||
)
|
||||
EventService(self.db).emit(
|
||||
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).log(
|
||||
AuditLogCreate(
|
||||
actor=actor,
|
||||
source=AuditSource.REPORTS,
|
||||
action=AuditAction.REPORT_PUSH,
|
||||
target_id=push_run.code,
|
||||
response_payload={"status": ReportPushStatus.SUCCESS},
|
||||
)
|
||||
)
|
||||
return result
|
||||
@@ -111,7 +111,7 @@ class ReportEnterpriseAnalyticsMixin:
|
||||
EnterpriseAnalyticsKey.CONTENT: "\n".join(lines),
|
||||
}
|
||||
)
|
||||
AuditService(self.db).log(
|
||||
AuditService(self.db).record(
|
||||
AuditLogCreate(
|
||||
actor=actor,
|
||||
source=AuditSource.REPORTS,
|
||||
@@ -121,7 +121,7 @@ class ReportEnterpriseAnalyticsMixin:
|
||||
response_payload=report,
|
||||
)
|
||||
)
|
||||
EventService(self.db).emit(
|
||||
EventService(self.db).enqueue(
|
||||
event_type=EventType.ENTERPRISE_ANALYTICS_GENERATED,
|
||||
source=EventSource.ANALYTICS,
|
||||
aggregate_type=EventAggregateType.ENTERPRISE_ANALYTICS,
|
||||
@@ -132,8 +132,8 @@ class ReportEnterpriseAnalyticsMixin:
|
||||
EventPayloadKey.STATUS: ReportStatus.GENERATED,
|
||||
},
|
||||
idempotency_key=f"enterprise-analytics:{code}",
|
||||
dispatch=True,
|
||||
)
|
||||
self.db.commit()
|
||||
return report
|
||||
|
||||
def _enterprise_performance_stats(self) -> dict[str, Any]:
|
||||
|
||||
@@ -60,6 +60,7 @@ class ReportPushRunMixin:
|
||||
provider_response: dict[str, Any] | None = None,
|
||||
error_message: str | None = None,
|
||||
sent: bool = False,
|
||||
commit: bool = True,
|
||||
) -> ReportPushRun:
|
||||
record = self._get_push_run(code)
|
||||
record.status = status
|
||||
@@ -70,8 +71,11 @@ class ReportPushRunMixin:
|
||||
record.error_message = error_message
|
||||
if sent:
|
||||
record.sent_at = utc_now()
|
||||
self.db.commit()
|
||||
self.db.refresh(record)
|
||||
if commit:
|
||||
self.db.commit()
|
||||
self.db.refresh(record)
|
||||
else:
|
||||
self.db.flush()
|
||||
return record
|
||||
|
||||
def list_push_runs(
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.modules.reports.services.common import ReportQueryMixin
|
||||
from app.modules.reports.services.delivery import ReportDeliveryMixin
|
||||
from app.modules.reports.services.enterprise import ReportEnterpriseAnalyticsMixin
|
||||
from app.modules.reports.services.finance_needs import FinanceNeedsReportMixin
|
||||
from app.modules.reports.services.lifecycle import ReportLifecycleMixin
|
||||
@@ -13,7 +12,6 @@ from app.modules.risk.services import RiskService
|
||||
|
||||
|
||||
class ReportService(
|
||||
ReportDeliveryMixin,
|
||||
FinanceNeedsReportMixin,
|
||||
IntasectLifecycleReportMixin,
|
||||
ReportWorkReportMixin,
|
||||
|
||||
@@ -119,10 +119,9 @@ class ReportWorkReportMixin:
|
||||
risk_summary=risk_summary,
|
||||
)
|
||||
self.db.add(record)
|
||||
self.db.commit()
|
||||
self.db.refresh(record)
|
||||
self.db.flush()
|
||||
record_data = serialize_model(record)
|
||||
AuditService(self.db).log(
|
||||
AuditService(self.db).record(
|
||||
AuditLogCreate(
|
||||
actor=actor,
|
||||
source=AuditSource.REPORTS,
|
||||
@@ -132,7 +131,7 @@ class ReportWorkReportMixin:
|
||||
response_payload=record_data,
|
||||
)
|
||||
)
|
||||
EventService(self.db).emit(
|
||||
EventService(self.db).enqueue(
|
||||
event_type=EventType.REPORT_GENERATED,
|
||||
source=EventSource.REPORTS,
|
||||
aggregate_type=EventAggregateType.WORK_REPORT,
|
||||
@@ -143,8 +142,9 @@ class ReportWorkReportMixin:
|
||||
EventPayloadKey.STATUS: record.status,
|
||||
},
|
||||
idempotency_key=f"report-generated:{record.code}",
|
||||
dispatch=True,
|
||||
)
|
||||
self.db.commit()
|
||||
self.db.refresh(record)
|
||||
|
||||
return {ReportResponseKey.REPORT: report, ReportResponseKey.DATA: record_data}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user