```
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:
@@ -48,6 +48,7 @@ class AIMemoryText(StrEnum):
|
||||
DEFAULT_SUBJECT = "company"
|
||||
AUTO_TAG = "auto"
|
||||
REJECTED_SECRET = "secret-like content rejected"
|
||||
REJECTED_SENSITIVE_FACT = "sensitive business fact rejected"
|
||||
|
||||
|
||||
AI_MEMORY_CODE_PREFIX = "MEM"
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
@@ -99,9 +100,8 @@ class AIMemoryService:
|
||||
items = items[:limit_value]
|
||||
for item in items:
|
||||
item.last_used_at = now
|
||||
self.db.commit()
|
||||
result = [serialize_model(item) for item in items]
|
||||
self.audit.log(
|
||||
self.audit.record(
|
||||
AuditLogCreate(
|
||||
actor=actor,
|
||||
source=AuditSource.AI_MEMORY,
|
||||
@@ -117,6 +117,7 @@ class AIMemoryService:
|
||||
response_payload={AIMemoryPayloadKey.COUNT: len(result)},
|
||||
)
|
||||
)
|
||||
self.db.commit()
|
||||
return result
|
||||
|
||||
def auto_write(
|
||||
@@ -152,8 +153,24 @@ class AIMemoryService:
|
||||
importance=0,
|
||||
status_value=AIMemoryStatus.REJECTED,
|
||||
actor=actor,
|
||||
expires_at=utc_now()
|
||||
+ timedelta(days=settings.ai_memory_auto_write_ttl_days),
|
||||
)
|
||||
return record
|
||||
if _contains_blocked_content(content, settings.ai_memory_blocked_content_terms):
|
||||
return self._create_entry(
|
||||
scope=scope,
|
||||
subject=subject,
|
||||
content=str(AIMemoryText.REJECTED_SENSITIVE_FACT),
|
||||
summary=str(AIMemoryText.REJECTED_SENSITIVE_FACT),
|
||||
tags=[str(AIMemoryText.AUTO_TAG)],
|
||||
source=AIMemorySource.AUTO,
|
||||
importance=0,
|
||||
status_value=AIMemoryStatus.REJECTED,
|
||||
actor=actor,
|
||||
expires_at=utc_now()
|
||||
+ timedelta(days=settings.ai_memory_auto_write_ttl_days),
|
||||
)
|
||||
summary = _truncate(answer, AI_MEMORY_MAX_SUMMARY_LENGTH)
|
||||
record = self._create_entry(
|
||||
scope=scope,
|
||||
@@ -165,6 +182,7 @@ class AIMemoryService:
|
||||
importance=1,
|
||||
status_value=AIMemoryStatus.ACTIVE,
|
||||
actor=actor,
|
||||
expires_at=utc_now() + timedelta(days=settings.ai_memory_auto_write_ttl_days),
|
||||
)
|
||||
return record
|
||||
|
||||
@@ -275,9 +293,7 @@ class AIMemoryService:
|
||||
record.tags = ["user-rule", *tags]
|
||||
if enabled is not None:
|
||||
record.status = AIMemoryStatus.ACTIVE if enabled else AIMemoryStatus.ARCHIVED
|
||||
self.db.commit()
|
||||
self.db.refresh(record)
|
||||
self.audit.log(
|
||||
self.audit.record(
|
||||
AuditLogCreate(
|
||||
actor=actor,
|
||||
source=AuditSource.AI_MEMORY,
|
||||
@@ -292,6 +308,8 @@ class AIMemoryService:
|
||||
},
|
||||
)
|
||||
)
|
||||
self.db.commit()
|
||||
self.db.refresh(record)
|
||||
return serialize_model(record)
|
||||
|
||||
def _validate_rule(self, content: str, priority: int) -> None:
|
||||
@@ -324,6 +342,7 @@ class AIMemoryService:
|
||||
status_value: str,
|
||||
actor: str,
|
||||
audit_action: str = AuditAction.AI_MEMORY_WRITE,
|
||||
expires_at: datetime | None = None,
|
||||
) -> AIMemoryEntry:
|
||||
record = AIMemoryEntry(
|
||||
code=(
|
||||
@@ -339,11 +358,11 @@ class AIMemoryService:
|
||||
importance=importance,
|
||||
status=status_value,
|
||||
actor=actor,
|
||||
expires_at=expires_at,
|
||||
)
|
||||
self.db.add(record)
|
||||
self.db.commit()
|
||||
self.db.refresh(record)
|
||||
self.audit.log(
|
||||
self.db.flush()
|
||||
self.audit.record(
|
||||
AuditLogCreate(
|
||||
actor=actor,
|
||||
source=AuditSource.AI_MEMORY,
|
||||
@@ -360,7 +379,7 @@ class AIMemoryService:
|
||||
response_payload={AIMemoryPayloadKey.CODE: record.code},
|
||||
)
|
||||
)
|
||||
EventService(self.db).emit(
|
||||
EventService(self.db).enqueue(
|
||||
event_type=EventType.AI_MEMORY_WRITTEN,
|
||||
source=EventSource.AI_MEMORY,
|
||||
aggregate_type=EventAggregateType.AI_MEMORY_ENTRY,
|
||||
@@ -373,8 +392,9 @@ class AIMemoryService:
|
||||
AIMemoryPayloadKey.STATUS: status_value,
|
||||
},
|
||||
idempotency_key=f"ai-memory:{record.code}",
|
||||
dispatch=True,
|
||||
)
|
||||
self.db.commit()
|
||||
self.db.refresh(record)
|
||||
return record
|
||||
|
||||
|
||||
@@ -402,6 +422,11 @@ def _contains_forbidden_value(value: Any, forbidden_keys: list[str]) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def _contains_blocked_content(value: str, blocked_terms: list[str]) -> bool:
|
||||
lowered = value.lower()
|
||||
return any(term.lower() in lowered for term in blocked_terms if term.strip())
|
||||
|
||||
|
||||
def _matches_query(entry: AIMemoryEntry, query: str) -> bool:
|
||||
query_text = query.lower().strip()
|
||||
if not query_text:
|
||||
|
||||
@@ -44,7 +44,9 @@ class AuditService:
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
|
||||
def log(self, payload: AuditLogCreate) -> AuditLog:
|
||||
def record(self, payload: AuditLogCreate) -> AuditLog:
|
||||
"""Stage an audit record in the caller's transaction."""
|
||||
|
||||
record = AuditLog(
|
||||
actor=payload.actor,
|
||||
source=payload.source,
|
||||
@@ -58,6 +60,13 @@ class AuditService:
|
||||
request_id=payload.request_id or get_request_id(),
|
||||
)
|
||||
self.db.add(record)
|
||||
self.db.flush()
|
||||
return record
|
||||
|
||||
def log(self, payload: AuditLogCreate) -> AuditLog:
|
||||
"""Persist an audit record as a standalone transaction."""
|
||||
|
||||
record = self.record(payload)
|
||||
self.db.commit()
|
||||
self.db.refresh(record)
|
||||
return record
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from datetime import date, datetime
|
||||
|
||||
from sqlalchemy import JSON, Date, DateTime, Integer, String, Text
|
||||
from sqlalchemy import JSON, Date, DateTime, ForeignKey, Integer, String, Text
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from app.core.constants import ActorValue
|
||||
@@ -42,7 +42,10 @@ class RiskEventAction(Base):
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
code: Mapped[str] = mapped_column(String(64), unique=True, index=True)
|
||||
risk_event_id: Mapped[int] = mapped_column(Integer, index=True)
|
||||
risk_event_id: Mapped[int] = mapped_column(
|
||||
ForeignKey("risk_events.id", ondelete="RESTRICT"),
|
||||
index=True,
|
||||
)
|
||||
action: Mapped[str] = mapped_column(String(64), index=True)
|
||||
actor: Mapped[str] = mapped_column(String(128), default=ActorValue.SYSTEM, index=True)
|
||||
from_status: Mapped[str | None] = mapped_column(String(32), nullable=True)
|
||||
|
||||
@@ -60,6 +60,7 @@ class EventPayloadKey(StrEnum):
|
||||
class EventErrorDetail(StrEnum):
|
||||
EVENT_NOT_FOUND = "Domain event not found"
|
||||
EVENT_NOT_RETRYABLE = "Domain event is not retryable"
|
||||
EVENT_LOCKED = "Domain event is already being processed"
|
||||
|
||||
|
||||
EVENT_CODE_PREFIX = "EVT"
|
||||
|
||||
@@ -36,6 +36,6 @@ class DomainEvent(Base):
|
||||
)
|
||||
locked_until: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, index=True)
|
||||
locked_by: Mapped[str | None] = mapped_column(String(128), nullable=True, index=True)
|
||||
max_attempts: Mapped[int] = mapped_column(Integer, default=3)
|
||||
max_attempts: Mapped[int] = mapped_column(Integer, default=3, server_default="3")
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=utc_now, index=True)
|
||||
processed_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, index=True)
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.application.events import EventDispatchService
|
||||
from app.core.database import get_db
|
||||
from app.core.security import ApiPrincipal, require_api_key
|
||||
from app.modules.events.constants import EventResponseKey
|
||||
from app.modules.events.schemas import DomainEventListRead, DomainEventRead
|
||||
from app.modules.events.services import EventService, _serialize_event
|
||||
|
||||
router = APIRouter(dependencies=[Depends(require_api_key)])
|
||||
|
||||
|
||||
@router.get("")
|
||||
@router.get("", response_model=DomainEventListRead)
|
||||
def list_events(
|
||||
status: str | None = None,
|
||||
event_type: str | None = None,
|
||||
@@ -25,15 +27,19 @@ def list_events(
|
||||
}
|
||||
|
||||
|
||||
@router.post("/{event_id}/dispatch")
|
||||
@router.post("/{event_id}/dispatch", response_model=dict[str, DomainEventRead])
|
||||
def dispatch_event(
|
||||
event_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
) -> dict:
|
||||
return {EventResponseKey.EVENT: _serialize_event(EventService(db).dispatch_event(event_id))}
|
||||
return {
|
||||
EventResponseKey.EVENT: _serialize_event(
|
||||
EventDispatchService(db).dispatch_event(event_id)
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@router.post("/{event_id}/retry")
|
||||
@router.post("/{event_id}/retry", response_model=dict[str, DomainEventRead])
|
||||
def retry_event(
|
||||
event_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
@@ -41,14 +47,16 @@ def retry_event(
|
||||
) -> dict:
|
||||
return {
|
||||
EventResponseKey.EVENT: _serialize_event(
|
||||
EventService(db).retry_event(event_id, actor=principal.actor)
|
||||
EventDispatchService(db).retry_event(event_id, actor=principal.actor)
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@router.post("/dispatch-pending")
|
||||
@router.post("/dispatch-pending", response_model=DomainEventListRead)
|
||||
def dispatch_pending(
|
||||
limit: int = Query(default=100, ge=1, le=500),
|
||||
db: Session = Depends(get_db),
|
||||
) -> dict:
|
||||
return {EventResponseKey.ITEMS: EventService(db).dispatch_pending(limit=limit)}
|
||||
return {
|
||||
EventResponseKey.ITEMS: EventDispatchService(db).dispatch_pending(limit=limit)
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
from pydantic import BaseModel
|
||||
@@ -15,9 +16,9 @@ class DomainEventRead(BaseModel):
|
||||
attempts: int
|
||||
idempotency_key: str | None
|
||||
last_error: str | None
|
||||
created_at: str
|
||||
processed_at: str | None
|
||||
created_at: datetime
|
||||
processed_at: datetime | None
|
||||
|
||||
|
||||
class DomainEventListRead(BaseModel):
|
||||
items: list[dict[str, Any]]
|
||||
items: list[DomainEventRead]
|
||||
|
||||
@@ -1,145 +0,0 @@
|
||||
from datetime import timedelta
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
from fastapi import HTTPException, status
|
||||
from sqlalchemy import or_, select
|
||||
|
||||
from app.core.config import get_settings
|
||||
from app.core.constants import ActorValue
|
||||
from app.core.http.pagination import bounded_limit
|
||||
from app.core.utils.time import utc_now
|
||||
from app.modules.audit.constants import (
|
||||
AuditAction,
|
||||
AuditRiskLevel,
|
||||
AuditSource,
|
||||
AuditTargetType,
|
||||
)
|
||||
from app.modules.audit.schemas import AuditLogCreate
|
||||
from app.modules.audit.service import AuditService
|
||||
from app.modules.events.constants import (
|
||||
EventErrorDetail,
|
||||
EventPayloadKey,
|
||||
EventStatus,
|
||||
)
|
||||
from app.modules.events.models import DomainEvent
|
||||
from app.modules.events.services.serialization import _serialize_event
|
||||
|
||||
|
||||
class EventDispatchMixin:
|
||||
def dispatch_event(self, event_id: str, worker_id: str | None = None) -> DomainEvent:
|
||||
record = self.get_event(event_id)
|
||||
if record.status == EventStatus.PROCESSED:
|
||||
return record
|
||||
if not self._can_attempt(record):
|
||||
return record
|
||||
settings = get_settings()
|
||||
now = utc_now()
|
||||
lock_owner = worker_id or f"api:{uuid4().hex}"
|
||||
record.locked_by = lock_owner
|
||||
record.locked_until = now + timedelta(seconds=settings.event_dispatch_lock_seconds)
|
||||
record.status = EventStatus.PENDING
|
||||
record.attempts += 1
|
||||
try:
|
||||
self._handle_event(record)
|
||||
except Exception as exc:
|
||||
retryable = record.attempts < self._max_attempts(record)
|
||||
record.status = EventStatus.PENDING if retryable else EventStatus.FAILED
|
||||
record.last_error = str(exc)
|
||||
record.next_attempt_at = (
|
||||
utc_now() + timedelta(seconds=settings.event_dispatch_retry_delay_seconds)
|
||||
if retryable
|
||||
else None
|
||||
)
|
||||
record.locked_by = None
|
||||
record.locked_until = None
|
||||
self.db.commit()
|
||||
self.db.refresh(record)
|
||||
self._audit_dispatch(record)
|
||||
return record
|
||||
record.status = EventStatus.PROCESSED
|
||||
record.last_error = None
|
||||
record.processed_at = utc_now()
|
||||
record.next_attempt_at = None
|
||||
record.locked_by = None
|
||||
record.locked_until = None
|
||||
self.db.commit()
|
||||
self.db.refresh(record)
|
||||
self._audit_dispatch(record)
|
||||
return record
|
||||
|
||||
def dispatch_pending(
|
||||
self,
|
||||
limit: int = 100,
|
||||
worker_id: str | None = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
now = utc_now()
|
||||
stmt = (
|
||||
select(DomainEvent)
|
||||
.where(
|
||||
DomainEvent.status == EventStatus.PENDING,
|
||||
or_(
|
||||
DomainEvent.next_attempt_at.is_(None),
|
||||
DomainEvent.next_attempt_at <= now,
|
||||
),
|
||||
or_(
|
||||
DomainEvent.locked_until.is_(None),
|
||||
DomainEvent.locked_until <= now,
|
||||
),
|
||||
or_(
|
||||
DomainEvent.max_attempts.is_(None),
|
||||
DomainEvent.attempts < DomainEvent.max_attempts,
|
||||
),
|
||||
)
|
||||
.order_by(DomainEvent.id.asc())
|
||||
.limit(bounded_limit(limit))
|
||||
)
|
||||
records = list(self.db.execute(stmt).scalars())
|
||||
lock_owner = worker_id or f"worker:{uuid4().hex}"
|
||||
return [
|
||||
_serialize_event(self.dispatch_event(record.event_id, worker_id=lock_owner))
|
||||
for record in records
|
||||
]
|
||||
|
||||
def retry_event(self, event_id: str, actor: str = ActorValue.API) -> DomainEvent:
|
||||
record = self.get_event(event_id)
|
||||
if record.status == EventStatus.PROCESSED:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail=EventErrorDetail.EVENT_NOT_RETRYABLE,
|
||||
)
|
||||
record.status = EventStatus.PENDING
|
||||
record.actor = actor
|
||||
record.attempts = 0
|
||||
record.max_attempts = record.max_attempts or get_settings().event_dispatch_max_attempts
|
||||
record.last_error = None
|
||||
record.locked_by = None
|
||||
record.locked_until = None
|
||||
record.next_attempt_at = utc_now()
|
||||
self.db.commit()
|
||||
self.db.refresh(record)
|
||||
return record
|
||||
|
||||
def _audit_dispatch(self, record: DomainEvent) -> None:
|
||||
AuditService(self.db).log(
|
||||
AuditLogCreate(
|
||||
actor=record.actor,
|
||||
source=AuditSource.EVENTS,
|
||||
action=AuditAction.EVENT_DISPATCH,
|
||||
target_type=AuditTargetType.DOMAIN_EVENT,
|
||||
target_id=record.event_id,
|
||||
risk_level=AuditRiskLevel.LOW,
|
||||
response_payload={
|
||||
EventPayloadKey.STATUS: record.status,
|
||||
EventPayloadKey.ATTEMPTS: record.attempts,
|
||||
EventPayloadKey.ERROR_MESSAGE: record.last_error,
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
def _can_attempt(self, record: DomainEvent) -> bool:
|
||||
return record.attempts < self._max_attempts(record)
|
||||
|
||||
@staticmethod
|
||||
def _max_attempts(record: DomainEvent) -> int:
|
||||
return record.max_attempts or get_settings().event_dispatch_max_attempts
|
||||
@@ -1,128 +0,0 @@
|
||||
|
||||
|
||||
from app.modules.events.constants import (
|
||||
EventAggregateType,
|
||||
EventPayloadKey,
|
||||
EventType,
|
||||
)
|
||||
from app.modules.events.models import DomainEvent
|
||||
|
||||
|
||||
class EventHandlerMixin:
|
||||
def _handle_event(self, record: DomainEvent) -> None:
|
||||
if record.event_type == EventType.RISK_ACTION_RECORDED:
|
||||
self._handle_risk_action(record)
|
||||
return
|
||||
if record.event_type in {
|
||||
EventType.REPORT_PUSH_SUCCEEDED,
|
||||
EventType.REPORT_PUSH_FAILED,
|
||||
EventType.REPORT_GENERATED,
|
||||
}:
|
||||
self._handle_report_event(record)
|
||||
return
|
||||
if record.event_type in {
|
||||
EventType.LEGACY_SYNC_COMPLETED,
|
||||
EventType.LEGACY_SYNC_FAILED,
|
||||
}:
|
||||
self._handle_legacy_sync_event(record)
|
||||
return
|
||||
if record.event_type == EventType.AI_MEMORY_WRITTEN:
|
||||
self._handle_ai_memory_event(record)
|
||||
return
|
||||
if record.event_type == EventType.ENTERPRISE_ANALYTICS_GENERATED:
|
||||
self._handle_enterprise_analytics_event(record)
|
||||
return
|
||||
|
||||
def _handle_risk_action(self, record: DomainEvent) -> None:
|
||||
from app.modules.risk.constants import RiskEventActionValue
|
||||
from app.modules.workflows.service import WorkflowService
|
||||
from app.modules.workflows.constants import WorkflowStatus, WorkflowType
|
||||
|
||||
payload = record.payload or {}
|
||||
action = str(payload.get(EventPayloadKey.ACTION) or "")
|
||||
if action == RiskEventActionValue.CLOSE:
|
||||
workflow_status = WorkflowStatus.COMPLETED
|
||||
elif action == RiskEventActionValue.RESOLVE:
|
||||
workflow_status = WorkflowStatus.WAITING_REVIEW
|
||||
else:
|
||||
workflow_status = WorkflowStatus.RUNNING
|
||||
WorkflowService(self.db).start_or_update(
|
||||
workflow_type=WorkflowType.RISK_EVENT_REVIEW,
|
||||
aggregate_type=EventAggregateType.RISK_EVENT,
|
||||
aggregate_id=record.aggregate_id,
|
||||
status_value=workflow_status,
|
||||
action=action or record.event_type,
|
||||
actor=record.actor,
|
||||
payload=payload,
|
||||
)
|
||||
|
||||
def _handle_report_event(self, record: DomainEvent) -> None:
|
||||
from app.modules.workflows.constants import WorkflowStatus, WorkflowType
|
||||
|
||||
workflow_status = (
|
||||
WorkflowStatus.FAILED
|
||||
if record.event_type == EventType.REPORT_PUSH_FAILED
|
||||
else WorkflowStatus.COMPLETED
|
||||
)
|
||||
self._track_operational_workflow(
|
||||
record,
|
||||
workflow_type=WorkflowType.REPORT_DELIVERY,
|
||||
workflow_status=workflow_status,
|
||||
)
|
||||
|
||||
def _handle_legacy_sync_event(self, record: DomainEvent) -> None:
|
||||
from app.modules.workflows.constants import WorkflowStatus, WorkflowType
|
||||
|
||||
workflow_status = (
|
||||
WorkflowStatus.FAILED
|
||||
if record.event_type == EventType.LEGACY_SYNC_FAILED
|
||||
else WorkflowStatus.COMPLETED
|
||||
)
|
||||
self._track_operational_workflow(
|
||||
record,
|
||||
workflow_type=WorkflowType.LEGACY_SYNC_MONITOR,
|
||||
workflow_status=workflow_status,
|
||||
)
|
||||
|
||||
def _handle_ai_memory_event(self, record: DomainEvent) -> None:
|
||||
from app.modules.ai_memory.constants import AIMemoryPayloadKey, AIMemoryStatus
|
||||
from app.modules.workflows.constants import WorkflowStatus, WorkflowType
|
||||
|
||||
payload = record.payload or {}
|
||||
workflow_status = (
|
||||
WorkflowStatus.BLOCKED
|
||||
if payload.get(AIMemoryPayloadKey.STATUS) == AIMemoryStatus.REJECTED
|
||||
else WorkflowStatus.COMPLETED
|
||||
)
|
||||
self._track_operational_workflow(
|
||||
record,
|
||||
workflow_type=WorkflowType.AI_MEMORY_CAPTURE,
|
||||
workflow_status=workflow_status,
|
||||
)
|
||||
|
||||
def _handle_enterprise_analytics_event(self, record: DomainEvent) -> None:
|
||||
from app.modules.workflows.constants import WorkflowStatus, WorkflowType
|
||||
|
||||
self._track_operational_workflow(
|
||||
record,
|
||||
workflow_type=WorkflowType.ENTERPRISE_ANALYTICS,
|
||||
workflow_status=WorkflowStatus.COMPLETED,
|
||||
)
|
||||
|
||||
def _track_operational_workflow(
|
||||
self,
|
||||
record: DomainEvent,
|
||||
workflow_type: str,
|
||||
workflow_status: str,
|
||||
) -> None:
|
||||
from app.modules.workflows.service import WorkflowService
|
||||
|
||||
WorkflowService(self.db).start_or_update(
|
||||
workflow_type=workflow_type,
|
||||
aggregate_type=record.aggregate_type,
|
||||
aggregate_id=record.aggregate_id,
|
||||
status_value=workflow_status,
|
||||
action=record.event_type,
|
||||
actor=record.actor,
|
||||
payload=record.payload or {},
|
||||
)
|
||||
@@ -10,14 +10,13 @@ from app.core.utils.time import utc_now
|
||||
from app.modules.events.constants import (
|
||||
EVENT_CODE_PREFIX,
|
||||
EventErrorDetail,
|
||||
EventStatus,
|
||||
)
|
||||
from app.modules.events.models import DomainEvent
|
||||
from app.modules.events.services.serialization import _serialize_event
|
||||
|
||||
|
||||
class EventQueryMixin:
|
||||
def emit(
|
||||
def enqueue(
|
||||
self,
|
||||
event_type: str,
|
||||
source: str,
|
||||
@@ -26,16 +25,12 @@ class EventQueryMixin:
|
||||
actor: str = ActorValue.SYSTEM,
|
||||
payload: dict[str, Any] | None = None,
|
||||
idempotency_key: str | None = None,
|
||||
dispatch: bool = False,
|
||||
) -> DomainEvent:
|
||||
if idempotency_key:
|
||||
existing = self.db.execute(
|
||||
select(DomainEvent).where(DomainEvent.idempotency_key == idempotency_key)
|
||||
).scalar_one_or_none()
|
||||
if existing is not None:
|
||||
if dispatch and existing.status == EventStatus.PENDING:
|
||||
return self.dispatch_event(existing.event_id)
|
||||
return existing
|
||||
"""Stage an outbox event in the caller's transaction."""
|
||||
|
||||
existing = self._find_idempotent_event(idempotency_key)
|
||||
if existing is not None:
|
||||
return existing
|
||||
|
||||
settings = get_settings()
|
||||
now = utc_now()
|
||||
@@ -52,12 +47,43 @@ class EventQueryMixin:
|
||||
max_attempts=settings.event_dispatch_max_attempts,
|
||||
)
|
||||
self.db.add(record)
|
||||
self.db.flush()
|
||||
return record
|
||||
|
||||
def emit(
|
||||
self,
|
||||
event_type: str,
|
||||
source: str,
|
||||
aggregate_type: str,
|
||||
aggregate_id: str | int | None,
|
||||
actor: str = ActorValue.SYSTEM,
|
||||
payload: dict[str, Any] | None = None,
|
||||
idempotency_key: str | None = None,
|
||||
) -> DomainEvent:
|
||||
existing = self._find_idempotent_event(idempotency_key)
|
||||
if existing is not None:
|
||||
return existing
|
||||
|
||||
record = self.enqueue(
|
||||
event_type=event_type,
|
||||
source=source,
|
||||
aggregate_type=aggregate_type,
|
||||
aggregate_id=aggregate_id,
|
||||
actor=actor,
|
||||
payload=payload,
|
||||
idempotency_key=idempotency_key,
|
||||
)
|
||||
self.db.commit()
|
||||
self.db.refresh(record)
|
||||
if dispatch:
|
||||
return self.dispatch_event(record.event_id)
|
||||
return record
|
||||
|
||||
def _find_idempotent_event(self, idempotency_key: str | None) -> DomainEvent | None:
|
||||
if not idempotency_key:
|
||||
return None
|
||||
return self.db.execute(
|
||||
select(DomainEvent).where(DomainEvent.idempotency_key == idempotency_key)
|
||||
).scalar_one_or_none()
|
||||
|
||||
def list_events(
|
||||
self,
|
||||
status_filter: str | None = None,
|
||||
|
||||
@@ -1,16 +1,12 @@
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.modules.events.services.dispatch import EventDispatchMixin
|
||||
from app.modules.events.services.handlers import EventHandlerMixin
|
||||
from app.modules.events.services.query import EventQueryMixin
|
||||
|
||||
|
||||
class EventService(
|
||||
EventDispatchMixin,
|
||||
EventHandlerMixin,
|
||||
EventQueryMixin,
|
||||
):
|
||||
"""Persist outbox events and dispatch the V3 internal handlers."""
|
||||
"""Persist and query transactional outbox events."""
|
||||
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
|
||||
@@ -1,732 +0,0 @@
|
||||
import json
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.constants import ActorValue
|
||||
from app.core.config import get_settings
|
||||
from app.modules.ai_agent.service import AIService
|
||||
from app.modules.ai_agent.constants import AIResponseKey
|
||||
from app.modules.audit.constants import AuditSource
|
||||
from app.modules.ai_memory.constants import AIMemoryStatus
|
||||
from app.modules.ai_memory.service import AIMemoryService
|
||||
from app.modules.feishu.constants import (
|
||||
FEISHU_AI_REPLY_TITLE,
|
||||
FEISHU_MENTION_PATTERN,
|
||||
FEISHU_ZERO_WIDTH_SPACE,
|
||||
FeishuCommandKey,
|
||||
FeishuCommandName,
|
||||
FeishuCommandResultKey,
|
||||
FeishuPayloadKey,
|
||||
FeishuReplyType,
|
||||
)
|
||||
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.chart import lifecycle_chart_alt, render_lifecycle_chart
|
||||
from app.modules.reports.constants import ReportResponseKey
|
||||
from app.modules.reports.services import ReportService
|
||||
|
||||
DAILY_REPORT_KEYWORDS = ("日报", "晨报", "经营日报", "经营晨报")
|
||||
PROJECT_WEEKLY_KEYWORDS = ("周报", "项目周报")
|
||||
ATTENDANCE_KEYWORDS = ("打卡", "考勤", "attendance")
|
||||
RISK_KEYWORDS = ("风险", "预警", "risk")
|
||||
AI_COMMAND_PREFIXES = ("问 ", "ai ", "AI ", "/ask ")
|
||||
DEFAULT_AI_PROMPT = "请说明你能做什么。"
|
||||
RULE_TITLE = "AI 学习规则"
|
||||
RULE_CREATE_PATTERN = re.compile(r"^学习规则(?:\s+(\d{1,3}))?\s*[::]\s*(.*)$")
|
||||
MARKET_RULE_CREATE_PATTERN = re.compile(
|
||||
r"^学习市场规则(?:\s+(\d{1,3}))?\s*[::]\s*(.*)$"
|
||||
)
|
||||
RULE_DISABLE_PATTERN = re.compile(r"^停用规则\s+(MEM-[A-Za-z0-9-]+)$", re.IGNORECASE)
|
||||
RULE_ENABLE_PATTERN = re.compile(r"^启用规则\s+(MEM-[A-Za-z0-9-]+)$", re.IGNORECASE)
|
||||
RULE_LIST_COMMANDS = {"查看规则", "规则列表", "查看市场规则"}
|
||||
RULE_COMMAND_PREFIXES = (
|
||||
"学习市场规则",
|
||||
"学习规则",
|
||||
"查看市场规则",
|
||||
"查看规则",
|
||||
"规则列表",
|
||||
"停用规则",
|
||||
"启用规则",
|
||||
)
|
||||
RULE_COMMAND_HELP = (
|
||||
"规则指令格式:\n"
|
||||
"学习规则:<规则内容>\n"
|
||||
"学习规则 80:<规则内容>\n"
|
||||
"学习市场规则 80:<仅用于市场分析的规则内容>\n"
|
||||
"查看规则\n"
|
||||
"停用规则 <规则编号>\n"
|
||||
"启用规则 <规则编号>"
|
||||
)
|
||||
PROJECT_FINANCE_PATTERN = re.compile(r"^项目资金\s+(.+)$")
|
||||
FINANCE_COMMANDS = {"资金需求", "未来30天资金需求"}
|
||||
STOCK_ANALYSIS_PATTERN = re.compile(
|
||||
r"^(?:股票分析|估值分析|财报分析)\s+([0-9]{6}(?:\.(?:SH|SZ|BJ))?)$", re.IGNORECASE
|
||||
)
|
||||
WATCHLIST_ADD_PATTERN = re.compile(r"^加入自选\s+([0-9]{6}(?:\.(?:SH|SZ|BJ))?)$", re.IGNORECASE)
|
||||
MARKET_COMMANDS = {
|
||||
"市场分析",
|
||||
"今日收盘分析",
|
||||
"本周市场分析",
|
||||
"宏观金融分析",
|
||||
"最新公告",
|
||||
}
|
||||
INDUSTRY_ANALYSIS_PATTERN = re.compile(r"^行业分析\s+(.+)$")
|
||||
STOCK_COMPARE_PATTERN = re.compile(
|
||||
r"^股票对比\s+([0-9]{6}(?:\.(?:SH|SZ|BJ))?)\s+" r"([0-9]{6}(?:\.(?:SH|SZ|BJ))?)$",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
|
||||
|
||||
def _parse_content_text(content: Any) -> str:
|
||||
"""Extract plain command text from a Feishu message content payload."""
|
||||
|
||||
if isinstance(content, dict):
|
||||
return str(
|
||||
content.get(FeishuPayloadKey.TEXT) or content.get(FeishuPayloadKey.CONTENT) or ""
|
||||
)
|
||||
if not isinstance(content, str):
|
||||
return ""
|
||||
try:
|
||||
data = json.loads(content)
|
||||
except json.JSONDecodeError:
|
||||
return content
|
||||
if isinstance(data, dict):
|
||||
return str(data.get(FeishuPayloadKey.TEXT) or data.get(FeishuPayloadKey.CONTENT) or "")
|
||||
return content
|
||||
|
||||
|
||||
def _clean_command_text(text: str) -> str:
|
||||
"""Remove mentions and invisible characters from Feishu command text."""
|
||||
|
||||
text = re.sub(FEISHU_MENTION_PATTERN, "", text or "")
|
||||
text = text.replace(FEISHU_ZERO_WIDTH_SPACE, "")
|
||||
return text.strip()
|
||||
|
||||
|
||||
def _command_result(
|
||||
command: FeishuCommandName,
|
||||
reply_type: FeishuReplyType,
|
||||
title: str,
|
||||
content: str,
|
||||
provider_response: dict[str, Any] | None = None,
|
||||
lines: list[str] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
result: dict[str, Any] = {
|
||||
FeishuCommandResultKey.COMMAND: command,
|
||||
FeishuCommandResultKey.REPLY_TYPE: reply_type,
|
||||
FeishuCommandResultKey.TITLE: title,
|
||||
FeishuCommandResultKey.CONTENT: content,
|
||||
FeishuCommandResultKey.PROVIDER_RESPONSE: provider_response,
|
||||
}
|
||||
if lines is not None:
|
||||
result[FeishuCommandResultKey.LINES] = lines
|
||||
return result
|
||||
|
||||
|
||||
class FeishuCommandService:
|
||||
"""Route Feishu text commands to reports, risk summaries, or AI replies."""
|
||||
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
self.feishu = FeishuService(db)
|
||||
|
||||
def extract_event_command(self, payload: dict[str, Any]) -> dict[str, Any] | None:
|
||||
event = payload.get(FeishuPayloadKey.EVENT) or {}
|
||||
message = event.get(FeishuPayloadKey.MESSAGE) or {}
|
||||
if not message:
|
||||
return None
|
||||
text = _clean_command_text(_parse_content_text(message.get(FeishuPayloadKey.CONTENT)))
|
||||
if not text:
|
||||
return None
|
||||
sender = event.get(FeishuPayloadKey.SENDER) or {}
|
||||
sender_id = sender.get(FeishuPayloadKey.SENDER_ID) or {}
|
||||
actor = (
|
||||
sender_id.get(FeishuPayloadKey.OPEN_ID)
|
||||
or sender_id.get(FeishuPayloadKey.USER_ID)
|
||||
or ActorValue.FEISHU
|
||||
)
|
||||
return {
|
||||
FeishuCommandKey.TEXT: text,
|
||||
FeishuCommandKey.CHAT_ID: message.get(FeishuCommandKey.CHAT_ID),
|
||||
FeishuCommandKey.ACTOR: actor,
|
||||
}
|
||||
|
||||
def handle_text(
|
||||
self,
|
||||
text: str,
|
||||
chat_id: str | None = None,
|
||||
actor: str = ActorValue.FEISHU,
|
||||
auto_reply: bool = True,
|
||||
) -> dict[str, Any]:
|
||||
command_text = _clean_command_text(text)
|
||||
lowered = command_text.lower()
|
||||
provider_response: dict[str, Any] | None = None
|
||||
|
||||
rule_result = self._handle_rule_command(
|
||||
command_text,
|
||||
chat_id=chat_id,
|
||||
actor=actor,
|
||||
auto_reply=auto_reply,
|
||||
)
|
||||
if rule_result is not None:
|
||||
return rule_result
|
||||
|
||||
finance_result = self._handle_finance_command(
|
||||
command_text,
|
||||
chat_id=chat_id,
|
||||
actor=actor,
|
||||
auto_reply=auto_reply,
|
||||
)
|
||||
if finance_result is not None:
|
||||
return finance_result
|
||||
|
||||
market_result = self._handle_market_command(command_text, chat_id, actor, auto_reply)
|
||||
if market_result is not None:
|
||||
return market_result
|
||||
|
||||
if any(keyword in command_text for keyword in DAILY_REPORT_KEYWORDS):
|
||||
report = ReportService(self.db).daily_brief()
|
||||
if auto_reply:
|
||||
provider_response = self._send_card_if_configured(
|
||||
chat_id,
|
||||
report[ReportResponseKey.TITLE],
|
||||
report[ReportResponseKey.LINES],
|
||||
actor,
|
||||
)
|
||||
return _command_result(
|
||||
FeishuCommandName.DAILY_BRIEF,
|
||||
FeishuReplyType.CARD,
|
||||
report[ReportResponseKey.TITLE],
|
||||
report[ReportResponseKey.CONTENT],
|
||||
provider_response,
|
||||
report[ReportResponseKey.LINES],
|
||||
)
|
||||
|
||||
if any(keyword in command_text for keyword in PROJECT_WEEKLY_KEYWORDS):
|
||||
report = ReportService(self.db).project_weekly()
|
||||
if auto_reply:
|
||||
provider_response = self._send_card_if_configured(
|
||||
chat_id,
|
||||
report[ReportResponseKey.TITLE],
|
||||
report[ReportResponseKey.LINES],
|
||||
actor,
|
||||
)
|
||||
return _command_result(
|
||||
FeishuCommandName.PROJECT_WEEKLY,
|
||||
FeishuReplyType.CARD,
|
||||
report[ReportResponseKey.TITLE],
|
||||
report[ReportResponseKey.CONTENT],
|
||||
provider_response,
|
||||
report[ReportResponseKey.LINES],
|
||||
)
|
||||
|
||||
if any(keyword in command_text for keyword in ATTENDANCE_KEYWORDS):
|
||||
report = ReportService(self.db).attendance_summary()
|
||||
if auto_reply:
|
||||
provider_response = self._send_card_if_configured(
|
||||
chat_id,
|
||||
report[ReportResponseKey.TITLE],
|
||||
report[ReportResponseKey.LINES],
|
||||
actor,
|
||||
)
|
||||
return _command_result(
|
||||
FeishuCommandName.ATTENDANCE_SUMMARY,
|
||||
FeishuReplyType.CARD,
|
||||
report[ReportResponseKey.TITLE],
|
||||
report[ReportResponseKey.CONTENT],
|
||||
provider_response,
|
||||
report[ReportResponseKey.LINES],
|
||||
)
|
||||
|
||||
if any(keyword in command_text for keyword in RISK_KEYWORDS):
|
||||
report = ReportService(self.db).risk_progress()
|
||||
if auto_reply:
|
||||
provider_response = self._send_card_if_configured(
|
||||
chat_id,
|
||||
report[ReportResponseKey.TITLE],
|
||||
report[ReportResponseKey.LINES],
|
||||
actor,
|
||||
)
|
||||
return _command_result(
|
||||
FeishuCommandName.RISK_SUMMARY,
|
||||
FeishuReplyType.CARD,
|
||||
report[ReportResponseKey.TITLE],
|
||||
report[ReportResponseKey.CONTENT],
|
||||
provider_response,
|
||||
report[ReportResponseKey.LINES],
|
||||
)
|
||||
|
||||
prompt = command_text
|
||||
for prefix in AI_COMMAND_PREFIXES:
|
||||
if command_text.startswith(prefix):
|
||||
prompt = command_text[len(prefix) :].strip()
|
||||
break
|
||||
if not prompt:
|
||||
prompt = DEFAULT_AI_PROMPT
|
||||
ai_result = AIService(self.db).ask(
|
||||
prompt,
|
||||
context={},
|
||||
actor=actor,
|
||||
source=AuditSource.FEISHU,
|
||||
)
|
||||
content = ai_result[AIResponseKey.ANSWER]
|
||||
is_explicit_ai = any(
|
||||
command_text.startswith(prefix) or lowered.startswith(prefix)
|
||||
for prefix in AI_COMMAND_PREFIXES
|
||||
)
|
||||
if auto_reply:
|
||||
provider_response = self._send_text_if_configured(chat_id, content, actor)
|
||||
return _command_result(
|
||||
FeishuCommandName.AI_ASK if is_explicit_ai else FeishuCommandName.FALLBACK_AI,
|
||||
FeishuReplyType.TEXT,
|
||||
FEISHU_AI_REPLY_TITLE,
|
||||
content,
|
||||
provider_response,
|
||||
)
|
||||
|
||||
def _handle_finance_command(
|
||||
self,
|
||||
command_text: str,
|
||||
chat_id: str | None,
|
||||
actor: str,
|
||||
auto_reply: bool,
|
||||
) -> dict[str, Any] | None:
|
||||
project_match = PROJECT_FINANCE_PATTERN.fullmatch(command_text)
|
||||
if command_text not in FINANCE_COMMANDS and project_match is None:
|
||||
return None
|
||||
command = (
|
||||
FeishuCommandName.PROJECT_FINANCE if project_match else FeishuCommandName.FINANCE_NEEDS
|
||||
)
|
||||
if not get_settings().finance_needs_enabled:
|
||||
content = "项目资金需求分析尚未启用,请先配置并启用财务只读同步。"
|
||||
provider_response = (
|
||||
self._send_text_if_configured(chat_id, content, actor) if auto_reply else None
|
||||
)
|
||||
return _command_result(
|
||||
command,
|
||||
FeishuReplyType.TEXT,
|
||||
"项目资金需求分析",
|
||||
content,
|
||||
provider_response,
|
||||
)
|
||||
|
||||
project_code = project_match.group(1).strip() if project_match else None
|
||||
service = ReportService(self.db)
|
||||
preview = service.project_finance_needs_report(
|
||||
project_code=project_code,
|
||||
include_ai=False,
|
||||
actor=actor,
|
||||
)
|
||||
if project_code and not preview["items"]:
|
||||
content = f"未找到项目“{project_code}”,请使用稳定项目编号或展示编号。"
|
||||
provider_response = (
|
||||
self._send_text_if_configured(chat_id, content, actor) if auto_reply else None
|
||||
)
|
||||
return _command_result(
|
||||
command,
|
||||
FeishuReplyType.TEXT,
|
||||
"项目资金需求分析",
|
||||
content,
|
||||
provider_response,
|
||||
)
|
||||
if not preview["summary"]["data_available"]:
|
||||
content = "项目财务数据未接入或无有效记录,暂不生成资金分析报告。"
|
||||
provider_response = (
|
||||
self._send_text_if_configured(chat_id, content, actor) if auto_reply else None
|
||||
)
|
||||
return _command_result(
|
||||
command,
|
||||
FeishuReplyType.TEXT,
|
||||
"项目资金需求分析",
|
||||
content,
|
||||
provider_response,
|
||||
)
|
||||
report = service.project_finance_needs_report(
|
||||
project_code=project_code,
|
||||
include_ai=True,
|
||||
actor=actor,
|
||||
)
|
||||
ai_analysis = report.get("ai_analysis") or {}
|
||||
if not ai_analysis.get(AIResponseKey.OK):
|
||||
content = "AI 当前不可用,本次项目资金分析报告未发送。请检查模型服务。"
|
||||
provider_response = (
|
||||
self._send_text_if_configured(chat_id, content, actor) if auto_reply else None
|
||||
)
|
||||
return _command_result(
|
||||
command,
|
||||
FeishuReplyType.TEXT,
|
||||
"AI 暂不可用",
|
||||
content,
|
||||
provider_response,
|
||||
)
|
||||
provider_response = None
|
||||
if auto_reply:
|
||||
provider_response = self._send_finance_card_if_configured(chat_id, report, actor)
|
||||
return _command_result(
|
||||
command,
|
||||
FeishuReplyType.CARD,
|
||||
report[ReportResponseKey.TITLE],
|
||||
report[ReportResponseKey.CONTENT],
|
||||
provider_response,
|
||||
report[ReportResponseKey.LINES],
|
||||
)
|
||||
|
||||
def _handle_market_command(
|
||||
self, text: str, chat_id: str | None, actor: str, auto_reply: bool
|
||||
) -> dict[str, Any] | None:
|
||||
stock = STOCK_ANALYSIS_PATTERN.fullmatch(text)
|
||||
add = WATCHLIST_ADD_PATTERN.fullmatch(text)
|
||||
industry = INDUSTRY_ANALYSIS_PATTERN.fullmatch(text)
|
||||
comparison = STOCK_COMPARE_PATTERN.fullmatch(text)
|
||||
if (
|
||||
text not in MARKET_COMMANDS
|
||||
and text != "查看自选"
|
||||
and not stock
|
||||
and not add
|
||||
and not industry
|
||||
and not comparison
|
||||
):
|
||||
return None
|
||||
if not get_settings().market_analysis_enabled:
|
||||
content = "市场分析尚未启用,请配置市场数据源后启用。"
|
||||
response = (
|
||||
self._send_text_if_configured(chat_id, content, actor) if auto_reply else None
|
||||
)
|
||||
return _command_result(
|
||||
FeishuCommandName.MARKET_OVERVIEW,
|
||||
FeishuReplyType.TEXT,
|
||||
"市场分析",
|
||||
content,
|
||||
response,
|
||||
)
|
||||
service = MarketService(self.db)
|
||||
if add:
|
||||
if get_settings().read_only_mode:
|
||||
content = "当前为只读模式,不能修改自选股。请由管理员启用操作后重试。"
|
||||
response = (
|
||||
self._send_text_if_configured(chat_id, content, actor)
|
||||
if auto_reply
|
||||
else None
|
||||
)
|
||||
return _command_result(
|
||||
FeishuCommandName.WATCHLIST_ADD,
|
||||
FeishuReplyType.TEXT,
|
||||
"自选股",
|
||||
content,
|
||||
response,
|
||||
)
|
||||
item = service.add_watchlist(actor, add.group(1))
|
||||
content = f"已加入自选:{item['symbol']}"
|
||||
response = (
|
||||
self._send_text_if_configured(chat_id, content, actor) if auto_reply else None
|
||||
)
|
||||
return _command_result(
|
||||
FeishuCommandName.WATCHLIST_ADD, FeishuReplyType.TEXT, "自选股", content, response
|
||||
)
|
||||
if text == "查看自选":
|
||||
items = service.watchlist(actor)
|
||||
content = "自选股:" + ("、".join(item["symbol"] for item in items) or "暂无")
|
||||
response = (
|
||||
self._send_text_if_configured(chat_id, content, actor) if auto_reply else None
|
||||
)
|
||||
return _command_result(
|
||||
FeishuCommandName.WATCHLIST_LIST, FeishuReplyType.TEXT, "自选股", content, response
|
||||
)
|
||||
if text == "最新公告":
|
||||
items = service.announcements(limit=10)["items"]
|
||||
content = (
|
||||
"最新公告:\n"
|
||||
+ "\n".join(
|
||||
f"- {item['announcement_date']} {item['symbol'] or '市场'}:{item['title']}"
|
||||
for item in items
|
||||
)
|
||||
if items
|
||||
else "公告元数据尚未接入。"
|
||||
)
|
||||
response = (
|
||||
self._send_text_if_configured(chat_id, content, actor) if auto_reply else None
|
||||
)
|
||||
return _command_result(
|
||||
FeishuCommandName.MARKET_ANNOUNCEMENTS,
|
||||
FeishuReplyType.TEXT,
|
||||
"最新公告",
|
||||
content,
|
||||
response,
|
||||
)
|
||||
if industry:
|
||||
try:
|
||||
data = service.industry_analysis(industry.group(1).strip())
|
||||
content = (
|
||||
f"{data['industry']} 平均涨跌 {data['average_pct_change']}%\n"
|
||||
+ "\n".join(
|
||||
f"- {item['name']}({item['symbol']}):{item['pct_change']}%"
|
||||
for item in data["items"][:10]
|
||||
)
|
||||
)
|
||||
except HTTPException:
|
||||
content = "未找到该行业的最新市场数据。"
|
||||
response = (
|
||||
self._send_text_if_configured(chat_id, content, actor) if auto_reply else None
|
||||
)
|
||||
return _command_result(
|
||||
FeishuCommandName.MARKET_OVERVIEW,
|
||||
FeishuReplyType.TEXT,
|
||||
"行业分析",
|
||||
content,
|
||||
response,
|
||||
)
|
||||
if comparison:
|
||||
try:
|
||||
content = service.compare_stocks([comparison.group(1), comparison.group(2)])[
|
||||
"content"
|
||||
]
|
||||
except HTTPException:
|
||||
content = "至少一只股票缺少可用行情,暂时无法比较。"
|
||||
response = (
|
||||
self._send_text_if_configured(chat_id, content, actor) if auto_reply else None
|
||||
)
|
||||
return _command_result(
|
||||
FeishuCommandName.STOCK_ANALYSIS,
|
||||
FeishuReplyType.TEXT,
|
||||
"股票对比",
|
||||
content,
|
||||
response,
|
||||
)
|
||||
command = FeishuCommandName.STOCK_ANALYSIS if stock else FeishuCommandName.MARKET_OVERVIEW
|
||||
if text == "宏观金融分析":
|
||||
command = FeishuCommandName.MARKET_MACRO
|
||||
try:
|
||||
if stock:
|
||||
report = service.stock_analysis(stock.group(1), True, actor)
|
||||
elif text == "本周市场分析":
|
||||
report = service.weekly_overview(include_ai=True, actor=actor)
|
||||
elif text == "宏观金融分析":
|
||||
report = service.macro_analysis(include_ai=True, actor=actor)
|
||||
else:
|
||||
report = service.market_overview(include_ai=True, actor=actor)
|
||||
except HTTPException:
|
||||
content = "未找到该股票的可用行情,请确认代码或先执行行情同步。"
|
||||
response = (
|
||||
self._send_text_if_configured(chat_id, content, actor) if auto_reply else None
|
||||
)
|
||||
return _command_result(command, FeishuReplyType.TEXT, "股票分析", content, response)
|
||||
ai = report.get("ai_analysis") or {}
|
||||
if not report.get("data_available"):
|
||||
content = "市场数据未接入,暂不生成分析报告。"
|
||||
elif not ai.get("ok"):
|
||||
content = "AI 当前不可用,本次市场分析报告未发送。"
|
||||
else:
|
||||
content = report["content"]
|
||||
response = None
|
||||
if auto_reply:
|
||||
if ai.get("ok"):
|
||||
if text == "宏观金融分析":
|
||||
response = self._send_text_if_configured(chat_id, content, actor)
|
||||
else:
|
||||
image = self.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 = self.feishu.send_card(card, receive_id=chat_id, actor=actor)
|
||||
else:
|
||||
response = self._send_text_if_configured(chat_id, content, actor)
|
||||
return _command_result(
|
||||
command,
|
||||
(
|
||||
FeishuReplyType.CARD
|
||||
if ai.get("ok") and text != "宏观金融分析"
|
||||
else FeishuReplyType.TEXT
|
||||
),
|
||||
report["title"],
|
||||
content,
|
||||
response,
|
||||
report["lines"] if ai.get("ok") else None,
|
||||
)
|
||||
|
||||
def _send_finance_card_if_configured(
|
||||
self,
|
||||
chat_id: str | None,
|
||||
report: dict[str, Any],
|
||||
actor: str,
|
||||
) -> dict[str, Any] | None:
|
||||
settings = get_settings()
|
||||
if not (settings.feishu_app_id and settings.feishu_app_secret):
|
||||
return None
|
||||
chart_data = {
|
||||
"period": report.get("as_of"),
|
||||
"finance": report.get("finance_chart_data"),
|
||||
}
|
||||
image_result = self.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")
|
||||
card = FeishuService.build_basic_card(
|
||||
report[ReportResponseKey.TITLE],
|
||||
report[ReportResponseKey.LINES],
|
||||
image_key=image_key,
|
||||
image_alt=lifecycle_chart_alt(chart_data),
|
||||
)
|
||||
return self.feishu.send_card(card, receive_id=chat_id, actor=actor)
|
||||
|
||||
def _handle_rule_command(
|
||||
self,
|
||||
command_text: str,
|
||||
chat_id: str | None,
|
||||
actor: str,
|
||||
auto_reply: bool,
|
||||
) -> dict[str, Any] | None:
|
||||
if not command_text.startswith(RULE_COMMAND_PREFIXES):
|
||||
return None
|
||||
|
||||
if command_text.startswith("停用规则"):
|
||||
command = FeishuCommandName.RULE_DISABLE
|
||||
elif command_text.startswith("启用规则"):
|
||||
command = FeishuCommandName.RULE_ENABLE
|
||||
elif command_text.startswith(("查看市场规则", "查看规则", "规则列表")):
|
||||
command = FeishuCommandName.RULE_LIST
|
||||
else:
|
||||
command = FeishuCommandName.RULE_CREATE
|
||||
if command in {
|
||||
FeishuCommandName.RULE_CREATE,
|
||||
FeishuCommandName.RULE_DISABLE,
|
||||
FeishuCommandName.RULE_ENABLE,
|
||||
} and get_settings().read_only_mode:
|
||||
content = "当前为只读模式,不能新增或修改学习规则。请由管理员启用操作后重试。"
|
||||
provider_response = (
|
||||
self._send_text_if_configured(chat_id, content, actor) if auto_reply else None
|
||||
)
|
||||
return _command_result(
|
||||
command,
|
||||
FeishuReplyType.TEXT,
|
||||
RULE_TITLE,
|
||||
content,
|
||||
provider_response,
|
||||
)
|
||||
content = RULE_COMMAND_HELP
|
||||
try:
|
||||
market_create_match = MARKET_RULE_CREATE_PATTERN.fullmatch(command_text)
|
||||
create_match = market_create_match or RULE_CREATE_PATTERN.fullmatch(command_text)
|
||||
disable_match = RULE_DISABLE_PATTERN.fullmatch(command_text)
|
||||
enable_match = RULE_ENABLE_PATTERN.fullmatch(command_text)
|
||||
memory = AIMemoryService(self.db)
|
||||
|
||||
if create_match:
|
||||
priority = int(create_match.group(1) or 50)
|
||||
rule_content = create_match.group(2).strip()
|
||||
if not rule_content:
|
||||
content = f"规则内容不能为空。\n\n{RULE_COMMAND_HELP}"
|
||||
elif not 1 <= priority <= 100:
|
||||
content = "规则优先级必须在 1 到 100 之间。"
|
||||
else:
|
||||
rule = memory.create_rule(
|
||||
content=rule_content,
|
||||
scope="market" if market_create_match else "global",
|
||||
subject="market" if market_create_match else "company",
|
||||
priority=priority,
|
||||
tags=["feishu", *(["market"] if market_create_match else [])],
|
||||
actor=actor,
|
||||
)
|
||||
content = (
|
||||
"规则已学习。\n"
|
||||
f"编号:{rule['code']}\n"
|
||||
f"优先级:{rule['importance']}\n"
|
||||
f"范围:{rule['scope']} / {rule['subject']}\n"
|
||||
"状态:已启用"
|
||||
)
|
||||
elif command_text in RULE_LIST_COMMANDS:
|
||||
command = FeishuCommandName.RULE_LIST
|
||||
rules = memory.list_rules(
|
||||
scope="market" if command_text == "查看市场规则" else None,
|
||||
status_filter=AIMemoryStatus.ACTIVE,
|
||||
limit=20,
|
||||
)
|
||||
if not rules:
|
||||
content = "当前没有已启用的学习规则。"
|
||||
else:
|
||||
lines = ["当前已启用的学习规则:"]
|
||||
for rule in rules:
|
||||
rule_text = str(rule["content"])
|
||||
if len(rule_text) > 80:
|
||||
rule_text = f"{rule_text[:80]}…"
|
||||
lines.append(
|
||||
f"{rule['code']}|优先级 {rule['importance']}|"
|
||||
f"{rule['scope']}/{rule['subject']}\n{rule_text}"
|
||||
)
|
||||
content = "\n\n".join(lines)
|
||||
elif disable_match or enable_match:
|
||||
enabled = enable_match is not None
|
||||
command = (
|
||||
FeishuCommandName.RULE_ENABLE if enabled else FeishuCommandName.RULE_DISABLE
|
||||
)
|
||||
match = enable_match or disable_match
|
||||
rule = memory.update_rule(
|
||||
code=match.group(1),
|
||||
content=None,
|
||||
priority=None,
|
||||
tags=None,
|
||||
enabled=enabled,
|
||||
actor=actor,
|
||||
)
|
||||
state = "已启用" if enabled else "已停用"
|
||||
content = (
|
||||
f"规则{state}。\n"
|
||||
f"编号:{rule['code']}\n"
|
||||
f"优先级:{rule['importance']}\n"
|
||||
f"范围:{rule['scope']} / {rule['subject']}\n"
|
||||
f"状态:{state}"
|
||||
)
|
||||
except HTTPException as exc:
|
||||
detail = str(exc.detail)
|
||||
if "secret-like" in detail:
|
||||
content = "规则疑似包含密码、令牌或其他密钥信息,已拒绝学习。"
|
||||
elif exc.status_code == 404:
|
||||
content = "没有找到该规则,请先发送“查看规则”确认规则编号。"
|
||||
elif "priority" in detail:
|
||||
content = "规则优先级必须在 1 到 100 之间。"
|
||||
else:
|
||||
content = "规则未保存,请检查指令内容后重试。"
|
||||
|
||||
provider_response = None
|
||||
if auto_reply:
|
||||
provider_response = self._send_text_if_configured(chat_id, content, actor)
|
||||
return _command_result(
|
||||
command,
|
||||
FeishuReplyType.TEXT,
|
||||
RULE_TITLE,
|
||||
content,
|
||||
provider_response,
|
||||
)
|
||||
|
||||
def _send_card_if_configured(
|
||||
self,
|
||||
chat_id: str | None,
|
||||
title: str,
|
||||
lines: list[str],
|
||||
actor: str,
|
||||
) -> dict[str, Any] | None:
|
||||
settings = get_settings()
|
||||
if not (settings.feishu_app_id and settings.feishu_app_secret):
|
||||
return None
|
||||
card = FeishuService.build_basic_card(title, lines)
|
||||
return self.feishu.send_card(card, receive_id=chat_id, actor=actor)
|
||||
|
||||
def _send_text_if_configured(
|
||||
self,
|
||||
chat_id: str | None,
|
||||
text: str,
|
||||
actor: str,
|
||||
) -> dict[str, Any] | None:
|
||||
settings = get_settings()
|
||||
if not (settings.feishu_app_id and settings.feishu_app_secret):
|
||||
return None
|
||||
return self.feishu.send_text(text, receive_id=chat_id, actor=actor)
|
||||
@@ -1,125 +0,0 @@
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.constants import ActorValue
|
||||
from app.modules.audit.constants import AuditAction, AuditSource
|
||||
from app.modules.audit.schemas import AuditLogCreate
|
||||
from app.modules.feishu.commands import FeishuCommandService
|
||||
from app.modules.feishu.constants import (
|
||||
FeishuCommandKey,
|
||||
FeishuEventReceiptKey,
|
||||
FeishuEventSource,
|
||||
FeishuPayloadKey,
|
||||
FeishuResponseKey,
|
||||
)
|
||||
from app.modules.feishu.models import FeishuEventReceipt
|
||||
from app.modules.feishu.service import FeishuService
|
||||
|
||||
FEISHU_EVENT_ACTIONS = {
|
||||
FeishuEventSource.WEBHOOK: AuditAction.FEISHU_WEBHOOK_EVENT,
|
||||
FeishuEventSource.LONG_CONNECTION: AuditAction.FEISHU_LONG_CONNECTION_EVENT,
|
||||
}
|
||||
|
||||
|
||||
class FeishuEventService:
|
||||
"""Handle Feishu message events from webhook or long connection."""
|
||||
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
self.feishu = FeishuService(db)
|
||||
self.commands = FeishuCommandService(db)
|
||||
|
||||
def handle_event(
|
||||
self,
|
||||
payload: dict[str, Any],
|
||||
source: str | FeishuEventSource,
|
||||
auto_reply: bool = True,
|
||||
) -> dict[str, Any]:
|
||||
self.feishu.verify_event(payload)
|
||||
challenge = payload.get(FeishuPayloadKey.CHALLENGE)
|
||||
if challenge:
|
||||
return {FeishuResponseKey.CHALLENGE: challenge}
|
||||
source_value = _normalize_source(source)
|
||||
event_identity = _event_identity(payload, source)
|
||||
if event_identity and not self._register_event(event_identity):
|
||||
return {
|
||||
FeishuResponseKey.OK: True,
|
||||
FeishuResponseKey.HANDLED: False,
|
||||
FeishuResponseKey.DUPLICATE: True,
|
||||
}
|
||||
self.feishu.audit.log(
|
||||
AuditLogCreate(
|
||||
actor=ActorValue.FEISHU,
|
||||
source=AuditSource.FEISHU,
|
||||
action=FEISHU_EVENT_ACTIONS[source_value],
|
||||
target_type=source_value,
|
||||
target_id=(
|
||||
event_identity.get(FeishuEventReceiptKey.EVENT_KEY)
|
||||
if event_identity
|
||||
else None
|
||||
),
|
||||
request_payload=payload,
|
||||
response_payload={FeishuResponseKey.ACCEPTED: True},
|
||||
)
|
||||
)
|
||||
command = self.commands.extract_event_command(payload)
|
||||
if not command:
|
||||
return {FeishuResponseKey.OK: True, FeishuResponseKey.HANDLED: False}
|
||||
result = self.commands.handle_text(
|
||||
command[FeishuCommandKey.TEXT],
|
||||
chat_id=command[FeishuCommandKey.CHAT_ID],
|
||||
actor=command[FeishuCommandKey.ACTOR],
|
||||
auto_reply=auto_reply,
|
||||
)
|
||||
return {
|
||||
FeishuResponseKey.OK: True,
|
||||
FeishuResponseKey.HANDLED: True,
|
||||
FeishuResponseKey.RESULT: result,
|
||||
}
|
||||
|
||||
def _register_event(self, event_identity: dict[str, str | None]) -> bool:
|
||||
receipt = FeishuEventReceipt(
|
||||
event_key=str(event_identity[FeishuEventReceiptKey.EVENT_KEY]),
|
||||
source=str(event_identity[FeishuEventReceiptKey.SOURCE]),
|
||||
event_id=event_identity.get(FeishuEventReceiptKey.EVENT_ID),
|
||||
message_id=event_identity.get(FeishuEventReceiptKey.MESSAGE_ID),
|
||||
)
|
||||
self.db.add(receipt)
|
||||
try:
|
||||
self.db.flush()
|
||||
except IntegrityError:
|
||||
self.db.rollback()
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def _normalize_source(source: str | FeishuEventSource) -> FeishuEventSource:
|
||||
return FeishuEventSource(source)
|
||||
|
||||
|
||||
def _event_identity(
|
||||
payload: dict[str, Any],
|
||||
source: str | FeishuEventSource,
|
||||
) -> dict[str, str | None] | None:
|
||||
source_value = _normalize_source(source)
|
||||
header = payload.get(FeishuPayloadKey.HEADER) or {}
|
||||
event = payload.get(FeishuPayloadKey.EVENT) or {}
|
||||
message = event.get(FeishuPayloadKey.MESSAGE) or {}
|
||||
event_id = header.get(FeishuPayloadKey.EVENT_ID)
|
||||
message_id = message.get(FeishuPayloadKey.MESSAGE_ID)
|
||||
stable_id = event_id or message_id
|
||||
if not stable_id:
|
||||
return None
|
||||
event_type = header.get(FeishuPayloadKey.EVENT_TYPE)
|
||||
event_key = ":".join(
|
||||
str(part)
|
||||
for part in (source_value, event_type or FeishuPayloadKey.EVENT, stable_id)
|
||||
)
|
||||
return {
|
||||
FeishuEventReceiptKey.EVENT_KEY: event_key,
|
||||
FeishuEventReceiptKey.SOURCE: source_value,
|
||||
FeishuEventReceiptKey.EVENT_ID: str(event_id) if event_id else None,
|
||||
FeishuEventReceiptKey.MESSAGE_ID: str(message_id) if message_id else None,
|
||||
}
|
||||
@@ -6,7 +6,7 @@ from urllib.parse import urlsplit
|
||||
from app.core.config import get_settings
|
||||
from app.core.database import SessionLocal
|
||||
from app.modules.feishu.constants import FEISHU_DEFAULT_OPEN_API_DOMAIN, FeishuEventSource
|
||||
from app.modules.feishu.events import FeishuEventService
|
||||
from app.application.feishu import FeishuEventService
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.core.security import ApiPrincipal, require_api_key
|
||||
from app.modules.feishu.commands import FeishuCommandService
|
||||
from app.application.feishu import FeishuCommandService, FeishuEventService
|
||||
from app.modules.feishu.constants import FeishuEventSource, FeishuPayloadKey, FeishuResponseKey
|
||||
from app.modules.feishu.events import FeishuEventService
|
||||
from app.modules.feishu.schemas import (
|
||||
FeishuCardMessage,
|
||||
FeishuCommandRequest,
|
||||
@@ -19,10 +20,9 @@ router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/webhook")
|
||||
async def feishu_webhook(request: Request, db: Session = Depends(get_db)) -> dict:
|
||||
def feishu_webhook(payload: dict[str, Any], db: Session = Depends(get_db)) -> dict:
|
||||
"""Handle Feishu webhook challenge and text command events."""
|
||||
|
||||
payload = await request.json()
|
||||
return FeishuEventService(db).handle_event(
|
||||
payload,
|
||||
source=FeishuEventSource.WEBHOOK,
|
||||
|
||||
@@ -4,6 +4,7 @@ from fastapi import HTTPException, status
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.core.constants import ActorValue
|
||||
from app.core.security import ensure_business_mutations_enabled
|
||||
from app.core.utils.time import utc_now
|
||||
from app.modules.audit.constants import AuditAction, AuditRiskLevel, AuditSource, AuditStatus
|
||||
from app.modules.audit.schemas import AuditLogCreate
|
||||
@@ -43,6 +44,7 @@ class LegacyProjectSyncMixin:
|
||||
dry_run: bool = True,
|
||||
actor: str = ActorValue.API,
|
||||
) -> dict[str, Any]:
|
||||
ensure_business_mutations_enabled()
|
||||
if self.db is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
@@ -120,9 +122,6 @@ class LegacyProjectSyncMixin:
|
||||
}
|
||||
)
|
||||
|
||||
if not dry_run:
|
||||
self.db.commit()
|
||||
|
||||
result = {
|
||||
LegacyResponseKey.DRY_RUN: dry_run,
|
||||
LegacyResponseKey.CREATED: created,
|
||||
@@ -142,11 +141,10 @@ class LegacyProjectSyncMixin:
|
||||
note=LEGACY_PROJECT_SYNC_NOTE,
|
||||
)
|
||||
self.db.add(sync_run)
|
||||
self.db.commit()
|
||||
self.db.refresh(sync_run)
|
||||
self.db.flush()
|
||||
result[LegacyResponseKey.SYNC_RUN_CODE] = sync_run.code
|
||||
|
||||
AuditService(self.db).log(
|
||||
AuditService(self.db).record(
|
||||
AuditLogCreate(
|
||||
actor=actor,
|
||||
source=AuditSource.LEGACY_MYSQL,
|
||||
@@ -170,7 +168,7 @@ class LegacyProjectSyncMixin:
|
||||
},
|
||||
)
|
||||
)
|
||||
EventService(self.db).emit(
|
||||
EventService(self.db).enqueue(
|
||||
event_type=EventType.LEGACY_SYNC_COMPLETED,
|
||||
source=EventSource.LEGACY_MYSQL,
|
||||
aggregate_type=EventAggregateType.LEGACY_SYNC_RUN,
|
||||
@@ -186,4 +184,6 @@ class LegacyProjectSyncMixin:
|
||||
},
|
||||
idempotency_key=f"legacy-sync:{sync_run.code}",
|
||||
)
|
||||
self.db.commit()
|
||||
self.db.refresh(sync_run)
|
||||
return result
|
||||
|
||||
@@ -4,6 +4,7 @@ from fastapi import HTTPException, status
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.core.constants import ActorValue
|
||||
from app.core.security import ensure_business_mutations_enabled
|
||||
from app.core.utils.time import utc_now
|
||||
from app.modules.audit.constants import AuditAction, AuditRiskLevel, AuditSource, AuditStatus
|
||||
from app.modules.audit.schemas import AuditLogCreate
|
||||
@@ -43,6 +44,7 @@ class LegacyTaskSyncMixin:
|
||||
dry_run: bool = True,
|
||||
actor: str = ActorValue.API,
|
||||
) -> dict[str, Any]:
|
||||
ensure_business_mutations_enabled()
|
||||
if self.db is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
@@ -120,9 +122,6 @@ class LegacyTaskSyncMixin:
|
||||
}
|
||||
)
|
||||
|
||||
if not dry_run:
|
||||
self.db.commit()
|
||||
|
||||
result = {
|
||||
LegacyResponseKey.DRY_RUN: dry_run,
|
||||
LegacyResponseKey.CREATED: created,
|
||||
@@ -142,11 +141,10 @@ class LegacyTaskSyncMixin:
|
||||
note=LEGACY_TASK_SYNC_NOTE,
|
||||
)
|
||||
self.db.add(sync_run)
|
||||
self.db.commit()
|
||||
self.db.refresh(sync_run)
|
||||
self.db.flush()
|
||||
result[LegacyResponseKey.SYNC_RUN_CODE] = sync_run.code
|
||||
|
||||
AuditService(self.db).log(
|
||||
AuditService(self.db).record(
|
||||
AuditLogCreate(
|
||||
actor=actor,
|
||||
source=AuditSource.LEGACY_MYSQL,
|
||||
@@ -170,7 +168,7 @@ class LegacyTaskSyncMixin:
|
||||
},
|
||||
)
|
||||
)
|
||||
EventService(self.db).emit(
|
||||
EventService(self.db).enqueue(
|
||||
event_type=EventType.LEGACY_SYNC_COMPLETED,
|
||||
source=EventSource.LEGACY_MYSQL,
|
||||
aggregate_type=EventAggregateType.LEGACY_SYNC_RUN,
|
||||
@@ -186,4 +184,6 @@ class LegacyTaskSyncMixin:
|
||||
},
|
||||
idempotency_key=f"legacy-sync:{sync_run.code}",
|
||||
)
|
||||
self.db.commit()
|
||||
self.db.refresh(sync_run)
|
||||
return result
|
||||
|
||||
@@ -1,263 +0,0 @@
|
||||
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.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 get_settings().read_only_mode:
|
||||
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()
|
||||
@@ -1,8 +1,9 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
from fastapi import APIRouter, Depends, Response, status
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.core.security import require_api_key
|
||||
from app.modules.observability.constants import ObservabilityKey, ObservabilityStatus
|
||||
from app.modules.observability.service import ObservabilityService
|
||||
|
||||
router = APIRouter()
|
||||
@@ -18,9 +19,13 @@ def live(
|
||||
|
||||
@router.get("/health/ready")
|
||||
def ready(
|
||||
response: Response,
|
||||
db: Session = Depends(get_db),
|
||||
) -> dict:
|
||||
return ObservabilityService(db).ready()
|
||||
result = ObservabilityService(db).ready()
|
||||
if result[ObservabilityKey.STATUS] != ObservabilityStatus.OK:
|
||||
response.status_code = status.HTTP_503_SERVICE_UNAVAILABLE
|
||||
return result
|
||||
|
||||
|
||||
@router.get("/metrics", dependencies=[Depends(require_api_key)])
|
||||
|
||||
@@ -94,9 +94,7 @@ class ObservabilityService:
|
||||
record.status = status_value
|
||||
record.last_seen_at = now
|
||||
record.updated_at = now
|
||||
self.db.commit()
|
||||
self.db.refresh(record)
|
||||
AuditService(self.db).log(
|
||||
AuditService(self.db).record(
|
||||
AuditLogCreate(
|
||||
actor=actor,
|
||||
source=AuditSource.OBSERVABILITY,
|
||||
@@ -112,6 +110,8 @@ class ObservabilityService:
|
||||
},
|
||||
)
|
||||
)
|
||||
self.db.commit()
|
||||
self.db.refresh(record)
|
||||
return self._serialize_heartbeat(record)
|
||||
|
||||
def heartbeat_summary(self) -> dict[str, Any]:
|
||||
|
||||
@@ -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}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ from typing import Any
|
||||
|
||||
|
||||
from app.core.constants import ActorValue
|
||||
from app.core.security import ensure_business_mutations_enabled
|
||||
from app.core.utils.time import utc_now
|
||||
from app.modules.audit.constants import (
|
||||
AuditAction,
|
||||
@@ -181,6 +182,7 @@ class RiskActionMixin:
|
||||
comment: str | None,
|
||||
payload: dict[str, Any],
|
||||
) -> RiskEventAction:
|
||||
ensure_business_mutations_enabled()
|
||||
action_record = RiskEventAction(
|
||||
code=f"RISK-ACTION-{utc_now():%Y%m%d%H%M%S%f}",
|
||||
risk_event_id=record.id,
|
||||
@@ -193,7 +195,8 @@ class RiskActionMixin:
|
||||
payload=payload,
|
||||
)
|
||||
self.db.add(action_record)
|
||||
AuditService(self.db).log(
|
||||
self.db.flush()
|
||||
AuditService(self.db).record(
|
||||
AuditLogCreate(
|
||||
actor=actor,
|
||||
source=AuditSource.RISK,
|
||||
@@ -210,7 +213,7 @@ class RiskActionMixin:
|
||||
},
|
||||
)
|
||||
)
|
||||
EventService(self.db).emit(
|
||||
EventService(self.db).enqueue(
|
||||
event_type=EventType.RISK_ACTION_RECORDED,
|
||||
source=EventSource.RISK,
|
||||
aggregate_type=EventAggregateType.RISK_EVENT,
|
||||
@@ -226,7 +229,6 @@ class RiskActionMixin:
|
||||
RiskEventActionKey.PAYLOAD: payload,
|
||||
},
|
||||
idempotency_key=f"risk:{record.id}:{action_record.code}",
|
||||
dispatch=True,
|
||||
)
|
||||
return action_record
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ from typing import Any
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.core.constants import ActorValue
|
||||
from app.core.security import ensure_business_mutations_enabled
|
||||
from app.modules.audit.constants import (
|
||||
AuditAction,
|
||||
AuditRiskLevel,
|
||||
@@ -29,6 +30,7 @@ class RiskGenerationMixin:
|
||||
def generate_events(self, actor: str = ActorValue.API) -> dict[str, Any]:
|
||||
"""Generate or refresh risk-event ledger entries from current signals."""
|
||||
|
||||
ensure_business_mutations_enabled()
|
||||
payloads = self._build_event_payloads()
|
||||
created = 0
|
||||
updated = 0
|
||||
@@ -67,8 +69,7 @@ class RiskGenerationMixin:
|
||||
}
|
||||
)
|
||||
|
||||
self.db.commit()
|
||||
AuditService(self.db).log(
|
||||
AuditService(self.db).record(
|
||||
AuditLogCreate(
|
||||
actor=actor,
|
||||
source=AuditSource.RISK,
|
||||
@@ -82,6 +83,7 @@ class RiskGenerationMixin:
|
||||
},
|
||||
)
|
||||
)
|
||||
self.db.commit()
|
||||
return {
|
||||
RiskGenerationResultKey.CREATED: created,
|
||||
RiskGenerationResultKey.UPDATED: updated,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import JSON, DateTime, Integer, String
|
||||
from sqlalchemy import JSON, DateTime, ForeignKey, Integer, String, UniqueConstraint
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from app.core.constants import ActorValue
|
||||
@@ -11,6 +11,14 @@ from app.modules.workflows.constants import WorkflowStatus
|
||||
|
||||
class WorkflowInstance(Base):
|
||||
__tablename__ = "workflow_instances"
|
||||
__table_args__ = (
|
||||
UniqueConstraint(
|
||||
"workflow_type",
|
||||
"aggregate_type",
|
||||
"aggregate_id",
|
||||
name="uq_workflow_aggregate",
|
||||
),
|
||||
)
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
code: Mapped[str] = mapped_column(String(64), unique=True, index=True)
|
||||
@@ -35,7 +43,10 @@ class WorkflowAction(Base):
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
code: Mapped[str] = mapped_column(String(64), unique=True, index=True)
|
||||
workflow_code: Mapped[str] = mapped_column(String(64), index=True)
|
||||
workflow_code: Mapped[str] = mapped_column(
|
||||
ForeignKey("workflow_instances.code", ondelete="RESTRICT"),
|
||||
index=True,
|
||||
)
|
||||
action: Mapped[str] = mapped_column(String(128), index=True)
|
||||
actor: Mapped[str] = mapped_column(String(128), default=ActorValue.SYSTEM, index=True)
|
||||
from_status: Mapped[str | None] = mapped_column(String(32), nullable=True)
|
||||
|
||||
@@ -4,12 +4,13 @@ from sqlalchemy.orm import Session
|
||||
from app.core.database import get_db
|
||||
from app.core.security import require_api_key
|
||||
from app.modules.workflows.constants import WorkflowResponseKey
|
||||
from app.modules.workflows.schemas import WorkflowListRead, WorkflowRead
|
||||
from app.modules.workflows.service import WorkflowService
|
||||
|
||||
router = APIRouter(dependencies=[Depends(require_api_key)])
|
||||
|
||||
|
||||
@router.get("")
|
||||
@router.get("", response_model=WorkflowListRead)
|
||||
def list_workflows(
|
||||
status: str | None = None,
|
||||
workflow_type: str | None = None,
|
||||
@@ -25,7 +26,7 @@ def list_workflows(
|
||||
}
|
||||
|
||||
|
||||
@router.get("/{code}")
|
||||
@router.get("/{code}", response_model=dict[str, WorkflowRead])
|
||||
def get_workflow(
|
||||
code: str,
|
||||
db: Session = Depends(get_db),
|
||||
|
||||
@@ -15,7 +15,8 @@ class WorkflowRead(BaseModel):
|
||||
created_at: str
|
||||
updated_at: str
|
||||
completed_at: str | None
|
||||
actions: list[dict[str, Any]] | None = None
|
||||
|
||||
|
||||
class WorkflowListRead(BaseModel):
|
||||
items: list[dict[str, Any]]
|
||||
items: list[WorkflowRead]
|
||||
|
||||
@@ -33,6 +33,7 @@ class WorkflowService:
|
||||
action: str,
|
||||
actor: str = ActorValue.SYSTEM,
|
||||
payload: dict[str, Any] | None = None,
|
||||
commit: bool = True,
|
||||
) -> WorkflowInstance:
|
||||
aggregate_id_text = str(aggregate_id) if aggregate_id is not None else None
|
||||
record = self.db.execute(
|
||||
@@ -92,8 +93,11 @@ class WorkflowService:
|
||||
payload=payload or {},
|
||||
)
|
||||
)
|
||||
self.db.commit()
|
||||
self.db.refresh(record)
|
||||
if commit:
|
||||
self.db.commit()
|
||||
self.db.refresh(record)
|
||||
else:
|
||||
self.db.flush()
|
||||
return record
|
||||
|
||||
def list_workflows(
|
||||
|
||||
Reference in New Issue
Block a user