```
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:
4
app/application/pipelines/__init__.py
Normal file
4
app/application/pipelines/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from app.application.pipelines.lifecycle import LifecyclePipelineService
|
||||
from app.application.pipelines.market import MarketPipelineService
|
||||
|
||||
__all__ = ["LifecyclePipelineService", "MarketPipelineService"]
|
||||
237
app/application/pipelines/lifecycle.py
Normal file
237
app/application/pipelines/lifecycle.py
Normal file
@@ -0,0 +1,237 @@
|
||||
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.security import business_mutations_enabled
|
||||
from app.core.constants import ActorValue
|
||||
from app.core.utils.time import utc_now
|
||||
from app.application.delivery import ReportDeliveryService
|
||||
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 not business_mutations_enabled():
|
||||
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:
|
||||
ReportDeliveryService(self.db).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()
|
||||
264
app/application/pipelines/market.py
Normal file
264
app/application/pipelines/market.py
Normal file
@@ -0,0 +1,264 @@
|
||||
from datetime import date, timedelta
|
||||
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.security import business_mutations_enabled
|
||||
from app.core.utils.time import utc_now
|
||||
from app.modules.feishu.service import FeishuService
|
||||
from app.modules.market.chart import render_market_chart
|
||||
from app.modules.market.service import MarketService
|
||||
from app.modules.reports.constants import ReportPushStatus
|
||||
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
|
||||
|
||||
MARKET_REPORT_TYPES = {"premarket", "close", "weekly"}
|
||||
|
||||
|
||||
class MarketPipelineService:
|
||||
def __init__(self, db: Session, market: MarketService | None = None):
|
||||
self.db = db
|
||||
self.market = market or MarketService(db)
|
||||
self.workflows = WorkflowService(db)
|
||||
|
||||
def period_key(self, report_type: str, reference_date: date) -> str:
|
||||
self._validate_type(report_type)
|
||||
period = reference_date
|
||||
if report_type == "weekly":
|
||||
period = reference_date - timedelta(days=reference_date.weekday())
|
||||
return f"market:{report_type}:{period.isoformat()}"
|
||||
|
||||
def find(self, period_key: str) -> WorkflowInstance | None:
|
||||
return self.db.execute(
|
||||
select(WorkflowInstance).where(
|
||||
WorkflowInstance.workflow_type == WorkflowType.MARKET_ANALYSIS,
|
||||
WorkflowInstance.aggregate_type == "market_period",
|
||||
WorkflowInstance.aggregate_id == period_key,
|
||||
)
|
||||
).scalar_one_or_none()
|
||||
|
||||
def run(
|
||||
self,
|
||||
report_type: str,
|
||||
reference_date: date | None = None,
|
||||
force: bool = False,
|
||||
actor: str = ActorValue.SCHEDULER,
|
||||
) -> dict[str, Any]:
|
||||
target = reference_date or date.today()
|
||||
period_key = self.period_key(report_type, target)
|
||||
if not business_mutations_enabled():
|
||||
return {
|
||||
"period_key": period_key,
|
||||
"status": "operations_disabled",
|
||||
"deduplicated": False,
|
||||
}
|
||||
existing = self.find(period_key)
|
||||
if existing is not None and existing.status == WorkflowStatus.COMPLETED and not force:
|
||||
return {
|
||||
"workflow_code": existing.code,
|
||||
"period_key": period_key,
|
||||
"status": existing.status,
|
||||
"deduplicated": True,
|
||||
}
|
||||
self._step(period_key, report_type, "source_sync", WorkflowStatus.RUNNING, actor)
|
||||
try:
|
||||
sync_result = self._sync(report_type, target)
|
||||
if sync_result.get("market_closed"):
|
||||
workflow = self._step(
|
||||
period_key,
|
||||
report_type,
|
||||
"market_closed",
|
||||
WorkflowStatus.COMPLETED,
|
||||
actor,
|
||||
sync_result,
|
||||
)
|
||||
return {
|
||||
"workflow_code": workflow.code,
|
||||
"period_key": period_key,
|
||||
"status": "market_closed",
|
||||
"deduplicated": False,
|
||||
}
|
||||
self._step(
|
||||
period_key,
|
||||
report_type,
|
||||
"ai_analysis",
|
||||
WorkflowStatus.RUNNING,
|
||||
actor,
|
||||
sync_result,
|
||||
)
|
||||
report = (
|
||||
self.market.weekly_overview(target, include_ai=True, actor=actor)
|
||||
if report_type == "weekly"
|
||||
else self.market.market_overview(
|
||||
target if report_type == "close" else None,
|
||||
include_ai=True,
|
||||
actor=actor,
|
||||
)
|
||||
)
|
||||
ai = report.get("ai_analysis") or {}
|
||||
if not report.get("data_available"):
|
||||
return self._fail(period_key, report_type, "market_data_unavailable", actor)
|
||||
if not ai.get("ok"):
|
||||
self._notify("AI 当前不可用,本次市场分析报告未发送。", actor)
|
||||
return self._fail(period_key, report_type, "ai_unavailable", actor)
|
||||
return self._deliver(report_type, period_key, report, force, actor, sync_result)
|
||||
except Exception as exc:
|
||||
self.db.rollback()
|
||||
self._step(
|
||||
period_key,
|
||||
report_type,
|
||||
"failed",
|
||||
WorkflowStatus.FAILED,
|
||||
actor,
|
||||
{"error_type": type(exc).__name__, "error": str(exc)[:1000]},
|
||||
)
|
||||
self._notify(f"市场分析 {period_key} 执行失败:{type(exc).__name__}", actor)
|
||||
raise
|
||||
|
||||
def _sync(self, report_type: str, target: date) -> dict[str, Any]:
|
||||
result: dict[str, Any] = {}
|
||||
if report_type == "premarket" and not self.market.is_trading_day(target):
|
||||
return {"market_closed": True}
|
||||
if report_type == "close":
|
||||
result["daily"] = self.market.sync_daily(target)
|
||||
if result["daily"].get("market_closed"):
|
||||
result["market_closed"] = True
|
||||
return result
|
||||
result["macro"] = self.market.sync_macro(target)
|
||||
start = target - timedelta(days=6 if report_type == "weekly" else 1)
|
||||
result["announcements"] = self.market.sync_announcements(start, target)
|
||||
result["financials"] = self.market.sync_watchlist_financials()
|
||||
return result
|
||||
|
||||
def _deliver(
|
||||
self,
|
||||
report_type: str,
|
||||
period_key: str,
|
||||
report: dict[str, Any],
|
||||
force: bool,
|
||||
actor: str,
|
||||
sync_result: dict[str, Any],
|
||||
) -> dict[str, Any]:
|
||||
settings = get_settings()
|
||||
if not (
|
||||
settings.feishu_default_chat_id
|
||||
and settings.feishu_app_id
|
||||
and settings.feishu_app_secret
|
||||
):
|
||||
return self._fail(period_key, report_type, "delivery_not_configured", actor)
|
||||
idempotency_key = (
|
||||
period_key if not force else f"{period_key}:force:{utc_now():%Y%m%d%H%M%S%f}"
|
||||
)
|
||||
reports = ReportService(self.db)
|
||||
push_run = reports.create_push_run(
|
||||
report_type=f"market_{report_type}",
|
||||
title=report["title"],
|
||||
receive_id=settings.feishu_default_chat_id,
|
||||
receive_id_type="chat_id",
|
||||
actor=actor,
|
||||
idempotency_key=idempotency_key,
|
||||
)
|
||||
if push_run.status != ReportPushStatus.SUCCESS:
|
||||
try:
|
||||
feishu = FeishuService(self.db)
|
||||
image = feishu.upload_image(render_market_chart(report), actor)
|
||||
image_key = (image.get("data") or {}).get("image_key")
|
||||
if not image_key:
|
||||
raise ValueError("Feishu image upload did not return image_key")
|
||||
card = FeishuService.build_basic_card(
|
||||
report["title"],
|
||||
report["lines"],
|
||||
image_key=image_key,
|
||||
image_alt=report["title"],
|
||||
)
|
||||
response = feishu.send_card(
|
||||
card,
|
||||
settings.feishu_default_chat_id,
|
||||
receive_id_type="chat_id",
|
||||
actor=actor,
|
||||
)
|
||||
reports.update_push_run(
|
||||
push_run.code,
|
||||
ReportPushStatus.SUCCESS,
|
||||
provider_response=response,
|
||||
sent=True,
|
||||
)
|
||||
except Exception as exc:
|
||||
reports.update_push_run(
|
||||
push_run.code, ReportPushStatus.FAILED, error_message=str(exc)[:2000]
|
||||
)
|
||||
raise
|
||||
workflow = self._step(
|
||||
period_key,
|
||||
report_type,
|
||||
"pushed",
|
||||
WorkflowStatus.COMPLETED,
|
||||
actor,
|
||||
{"push_run_code": push_run.code, "sync": sync_result},
|
||||
)
|
||||
return {
|
||||
"workflow_code": workflow.code,
|
||||
"period_key": period_key,
|
||||
"push_run_code": push_run.code,
|
||||
"status": workflow.status,
|
||||
"deduplicated": False,
|
||||
}
|
||||
|
||||
def _fail(self, period_key: str, report_type: str, action: str, actor: str) -> dict[str, Any]:
|
||||
workflow = self._step(
|
||||
period_key, report_type, action, WorkflowStatus.FAILED, actor
|
||||
)
|
||||
return {
|
||||
"workflow_code": workflow.code,
|
||||
"period_key": period_key,
|
||||
"status": workflow.status,
|
||||
"reason": action,
|
||||
"deduplicated": False,
|
||||
}
|
||||
|
||||
def _step(
|
||||
self,
|
||||
period_key: str,
|
||||
report_type: str,
|
||||
action: str,
|
||||
status_value: str,
|
||||
actor: str,
|
||||
payload: dict[str, Any] | None = None,
|
||||
) -> WorkflowInstance:
|
||||
return self.workflows.start_or_update(
|
||||
workflow_type=WorkflowType.MARKET_ANALYSIS,
|
||||
aggregate_type="market_period",
|
||||
aggregate_id=period_key,
|
||||
status_value=status_value,
|
||||
action=action,
|
||||
actor=actor,
|
||||
payload={"report_type": report_type, **(payload or {})},
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _validate_type(report_type: str) -> None:
|
||||
if report_type not in MARKET_REPORT_TYPES:
|
||||
raise ValueError("Market report type must be premarket, close or weekly")
|
||||
|
||||
def _notify(self, content: str, actor: str) -> None:
|
||||
settings = get_settings()
|
||||
if not (
|
||||
settings.feishu_default_chat_id
|
||||
and settings.feishu_app_id
|
||||
and settings.feishu_app_secret
|
||||
):
|
||||
return
|
||||
try:
|
||||
FeishuService(self.db).send_text(
|
||||
content,
|
||||
settings.feishu_default_chat_id,
|
||||
receive_id_type="chat_id",
|
||||
actor=actor,
|
||||
)
|
||||
except Exception:
|
||||
self.db.rollback()
|
||||
Reference in New Issue
Block a user