feat(market): 添加市场分析数据基础架构和功能模块

- 新增市场分析相关数据库表结构,包括市场工具、每日报价、财务指标、
  宏观指标和公告等数据模型
- 创建市场分析相关的Alembic迁移脚本,包含完整的up和downgrade逻辑
- 集成市场分析路由到主API路由器中
- 添加市场数据定时任务调度,支持盘前、收盘和周度市场分析报告
- 实现市场数据后台任务队列,包含报告生成和收盘分析功能
- 扩展系统配置设置,添加市场分析启用开关和相关参数配置
- 增加AI智能技能支持,包含市场概览分析和股票分析功能
- 添加审计日志记录,支持市场数据同步和自选股更新操作追踪
- 实现飞书命令集成,支持股票分析、自选股管理、公告查询等交互
- 提供市场数据服务层,包含行业分析、股票对比、市场概览等功能
```
This commit is contained in:
2026-07-12 22:05:23 +08:00
parent 9cf7c44393
commit f8020cab56
28 changed files with 2704 additions and 60 deletions

View File

@@ -0,0 +1,3 @@
from app.modules.market.service import MarketService
__all__ = ["MarketService"]

View File

@@ -0,0 +1,62 @@
from io import BytesIO
from typing import Any
from PIL import Image, ImageDraw, ImageFont
def render_market_chart(report: dict[str, Any]) -> bytes:
image = Image.new("RGB", (1200, 720), (248, 250, 252))
draw = ImageDraw.Draw(image)
title = _font(34)
label = _font(22)
chart = report.get("chart_data") or {}
if "quotes" in chart:
draw.text(
(55, 35),
f"Stock Analysis: {chart.get('symbol') or ''}",
fill=(17, 24, 39),
font=title,
)
quotes = chart["quotes"]
values = [float(item["close"]) for item in quotes]
if values:
low, high = min(values), max(values)
span = high - low or 1
points = []
for index, value in enumerate(values):
x = 70 + index * 1060 / max(len(values) - 1, 1)
y = 610 - (value - low) * 480 / span
points.append((x, y))
draw.line(points, fill=(37, 99, 235), width=5)
draw.text(
(70, 630),
f"最低 {low:.2f} 最高 {high:.2f} 最新 {values[-1]:.2f}",
fill=(75, 85, 99),
font=label,
)
else:
draw.text((55, 35), "A-Share Market Overview", fill=(17, 24, 39), font=title)
values = [
("Advances", int(chart.get("advances") or 0)),
("Declines", int(chart.get("declines") or 0)),
("Flat", int(chart.get("flat") or 0)),
]
maximum = max((value for _, value in values), default=1) or 1
for index, (name, value) in enumerate(values):
y = 170 + index * 150
draw.text((70, y), f"{name} {value:,}", fill=(75, 85, 99), font=label)
draw.rectangle((70, y + 45, 1120, y + 82), fill=(209, 213, 219))
draw.rectangle(
(70, y + 45, 70 + 1050 * value / maximum, y + 82),
fill=((37, 99, 235), (220, 38, 38), (245, 158, 11))[index],
)
output = BytesIO()
image.save(output, format="PNG", optimize=True)
return output.getvalue()
def _font(size: int) -> ImageFont.ImageFont:
try:
return ImageFont.truetype("DejaVuSans.ttf", size=size)
except OSError:
return ImageFont.load_default(size=size)

View File

@@ -0,0 +1,263 @@
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()

View File

@@ -0,0 +1,130 @@
from datetime import date
from typing import Literal
from fastapi import APIRouter, Depends
from pydantic import BaseModel
from sqlalchemy.orm import Session
from app.core.database import get_db
from app.core.background.task_queue.market import enqueue_market_report
from app.core.security import ApiPrincipal, require_api_key, require_operations_enabled
from app.modules.market.service import MarketService
router = APIRouter(dependencies=[Depends(require_api_key)])
class WatchlistRequest(BaseModel):
symbol: str
class MarketReportRequest(BaseModel):
report_type: Literal["premarket", "close", "weekly"]
reference_date: date | None = None
force: bool = False
@router.get("/overview")
def overview(
trade_date: date | None = None,
include_ai: bool = False,
db: Session = Depends(get_db),
principal: ApiPrincipal = Depends(require_api_key),
) -> dict:
return MarketService(db).market_overview(trade_date, include_ai, principal.actor)
@router.get("/stocks/{symbol}/analysis")
def stock_analysis(
symbol: str,
include_ai: bool = False,
db: Session = Depends(get_db),
principal: ApiPrincipal = Depends(require_api_key),
) -> dict:
return MarketService(db).stock_analysis(symbol, include_ai, principal.actor)
@router.get("/weekly")
def weekly_overview(
reference_date: date | None = None,
include_ai: bool = False,
db: Session = Depends(get_db),
principal: ApiPrincipal = Depends(require_api_key),
) -> dict:
return MarketService(db).weekly_overview(reference_date, include_ai, principal.actor)
@router.get("/industries/{industry}/analysis")
def industry_analysis(industry: str, db: Session = Depends(get_db)) -> dict:
return MarketService(db).industry_analysis(industry)
@router.get("/stocks/compare/{first}/{second}")
def compare_stocks(first: str, second: str, db: Session = Depends(get_db)) -> dict:
return MarketService(db).compare_stocks([first, second])
@router.get("/macro")
def macro_overview(
include_ai: bool = False,
db: Session = Depends(get_db),
principal: ApiPrincipal = Depends(require_api_key),
) -> dict:
service = MarketService(db)
return (
service.macro_analysis(include_ai=True, actor=principal.actor)
if include_ai
else service.macro_overview()
)
@router.get("/announcements")
def announcements(
symbol: str | None = None,
start_date: date | None = None,
limit: int = 50,
db: Session = Depends(get_db),
) -> dict:
return MarketService(db).announcements(symbol, start_date, limit)
@router.post("/sync/daily")
def sync_daily(trade_date: date, db: Session = Depends(get_db)) -> dict:
require_operations_enabled()
return MarketService(db).sync_daily(trade_date)
@router.post("/sync/macro")
def sync_macro(reference_date: date | None = None, db: Session = Depends(get_db)) -> dict:
require_operations_enabled()
return MarketService(db).sync_macro(reference_date)
@router.post("/sync/announcements")
def sync_announcements(
start_date: date, end_date: date, db: Session = Depends(get_db)
) -> dict:
require_operations_enabled()
return {"processed": MarketService(db).sync_announcements(start_date, end_date)}
@router.post("/reports/enqueue")
def enqueue_report(payload: MarketReportRequest) -> dict:
require_operations_enabled()
return enqueue_market_report(payload.report_type, payload.reference_date, payload.force)
@router.post("/watchlist")
def add_watchlist(
payload: WatchlistRequest,
db: Session = Depends(get_db),
principal: ApiPrincipal = Depends(require_api_key),
) -> dict:
require_operations_enabled()
return MarketService(db).add_watchlist(principal.actor, payload.symbol)
@router.get("/watchlist")
def watchlist(
db: Session = Depends(get_db), principal: ApiPrincipal = Depends(require_api_key)
) -> dict:
return {"items": MarketService(db).watchlist(principal.actor)}

File diff suppressed because it is too large Load Diff