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

@@ -9,6 +9,7 @@ from app.modules.dashboard.routes import router as dashboard_router
from app.modules.events.routes import router as events_router
from app.modules.feishu.routes import router as feishu_router
from app.modules.legacy_mysql.routes import router as legacy_mysql_router
from app.modules.market.routes import router as market_router
from app.modules.observability.routes import router as observability_router
from app.modules.reports.routes import router as reports_router
from app.modules.risk.routes import router as risk_router
@@ -31,6 +32,7 @@ api_router.include_router(feishu_router, prefix="/integrations/feishu", tags=["f
api_router.include_router(ai_router, prefix="/ai", tags=["ai"])
api_router.include_router(ai_memory_router, prefix="/ai", tags=["ai-memory"])
api_router.include_router(reports_router, prefix="/reports", tags=["reports"])
api_router.include_router(market_router, prefix="/market", tags=["market"])
api_router.include_router(risk_router, prefix="/risks", tags=["risks"])
api_router.include_router(audit_router, prefix="/audit", tags=["audit"])
api_router.include_router(events_router, prefix="/events", tags=["events"])

View File

@@ -1,3 +1,4 @@
from datetime import date
from socket import gethostname
from typing import Any
@@ -39,6 +40,7 @@ def create_scheduler(app: FastAPI | None = None) -> Any:
enqueue_legacy_project_sync,
enqueue_legacy_task_sync,
enqueue_lifecycle_report,
enqueue_market_report,
enqueue_project_weekly_push,
)
from app.modules.observability.service import ObservabilityService
@@ -127,6 +129,18 @@ def create_scheduler(app: FastAPI | None = None) -> Any:
)
_set_state(app, "last_weekly_lifecycle_dispatch", dispatch)
def run_market_premarket() -> None:
dispatch = enqueue_market_report("premarket")
_set_state(app, "last_market_premarket_dispatch", dispatch)
def run_market_close() -> None:
dispatch = enqueue_market_report("close")
_set_state(app, "last_market_close_dispatch", dispatch)
def run_market_weekly() -> None:
dispatch = enqueue_market_report("weekly", date.today())
_set_state(app, "last_market_weekly_dispatch", dispatch)
def run_event_dispatch() -> None:
dispatch = enqueue_event_dispatch(
limit=settings.event_dispatch_batch_size,
@@ -190,6 +204,34 @@ def create_scheduler(app: FastAPI | None = None) -> Any:
id="event_dispatch",
replace_existing=True,
)
if settings.market_analysis_enabled:
scheduler.add_job(
run_market_premarket,
trigger="cron",
day_of_week="mon-fri",
hour=settings.market_premarket_cron_hour,
minute=settings.market_premarket_cron_minute,
id="market_premarket_analysis",
replace_existing=True,
)
scheduler.add_job(
run_market_close,
trigger="cron",
day_of_week="mon-fri",
hour=settings.market_close_cron_hour,
minute=settings.market_close_cron_minute,
id="market_close_analysis",
replace_existing=True,
)
scheduler.add_job(
run_market_weekly,
trigger="cron",
day_of_week=settings.market_weekly_day_of_week,
hour=settings.market_weekly_cron_hour,
minute=settings.market_weekly_cron_minute,
id="market_weekly_analysis",
replace_existing=True,
)
scheduler.add_job(
record_scheduler_heartbeat,
trigger="interval",

View File

@@ -6,12 +6,21 @@ from app.core.background.task_queue.constants import (
TASK_SYNC_LEGACY_PROJECTS,
TASK_SYNC_LEGACY_TASKS,
TASK_RUN_LIFECYCLE,
TASK_RUN_MARKET_CLOSE,
TASK_RUN_MARKET_REPORT,
)
from app.core.background.task_queue.dispatcher import dispatch_task
from app.core.background.task_queue.events import enqueue_event_dispatch
from app.core.background.task_queue.legacy import enqueue_legacy_project_sync, enqueue_legacy_task_sync
from app.core.background.task_queue.legacy import (
enqueue_legacy_project_sync,
enqueue_legacy_task_sync,
)
from app.core.background.task_queue.lifecycle import enqueue_lifecycle_report
from app.core.background.task_queue.reports import enqueue_daily_brief_push, enqueue_project_weekly_push
from app.core.background.task_queue.market import enqueue_market_close, enqueue_market_report
from app.core.background.task_queue.reports import (
enqueue_daily_brief_push,
enqueue_project_weekly_push,
)
from app.core.background.task_queue.risk import enqueue_risk_event_generation
@@ -23,12 +32,16 @@ __all__ = [
"TASK_SYNC_LEGACY_PROJECTS",
"TASK_SYNC_LEGACY_TASKS",
"TASK_RUN_LIFECYCLE",
"TASK_RUN_MARKET_CLOSE",
"TASK_RUN_MARKET_REPORT",
"dispatch_task",
"enqueue_daily_brief_push",
"enqueue_event_dispatch",
"enqueue_legacy_project_sync",
"enqueue_legacy_task_sync",
"enqueue_lifecycle_report",
"enqueue_market_close",
"enqueue_market_report",
"enqueue_project_weekly_push",
"enqueue_risk_event_generation",
]

View File

@@ -5,3 +5,5 @@ TASK_SYNC_LEGACY_PROJECTS = "legacy.sync_projects"
TASK_SYNC_LEGACY_TASKS = "legacy.sync_tasks"
TASK_DISPATCH_PENDING_EVENTS = "events.dispatch_pending"
TASK_RUN_LIFECYCLE = "reports.run_lifecycle"
TASK_RUN_MARKET_REPORT = "market.report.run"
TASK_RUN_MARKET_CLOSE = TASK_RUN_MARKET_REPORT

View File

@@ -45,6 +45,7 @@ def enqueue_legacy_project_sync(
inline,
)
def enqueue_legacy_task_sync(
source_query: str | None = None,
source_query_name: str | None = None,

View File

@@ -0,0 +1,26 @@
from datetime import date
from typing import Any
from app.core.background.task_queue.constants import TASK_RUN_MARKET_REPORT
from app.core.config import get_settings
def enqueue_market_report(
report_type: str, trade_date: date | None = None, force: bool = False
) -> dict[str, Any]:
value = trade_date.isoformat() if trade_date else None
if not get_settings().task_queue_enabled:
from app.tasks.market import run_market_report
return run_market_report.run(report_type, value, force)
from app.tasks import celery_app
result = celery_app.signature(
TASK_RUN_MARKET_REPORT,
kwargs={"report_type": report_type, "trade_date": value, "force": force},
).apply_async()
return {"task_id": result.id, "status": "queued"}
def enqueue_market_close(trade_date: date | None = None) -> dict[str, Any]:
return enqueue_market_report("close", trade_date)

View File

@@ -76,6 +76,7 @@ def enqueue_daily_brief_push(
inline,
)
def enqueue_project_weekly_push(
receive_id: str | None = None,
receive_id_type: str = FeishuReceiveIdType.CHAT_ID,

View File

@@ -71,6 +71,17 @@ class Settings(BaseSettings):
task_queue_always_eager: bool = False
lifecycle_pipeline_enabled: bool = False
finance_needs_enabled: bool = False
market_analysis_enabled: bool = False
market_data_provider: str = "tushare"
market_data_base_url: str = "https://api.tushare.pro"
market_data_token: str | None = None
market_premarket_cron_hour: int = 8
market_premarket_cron_minute: int = 30
market_close_cron_hour: int = 15
market_close_cron_minute: int = 30
market_weekly_day_of_week: str = "sun"
market_weekly_cron_hour: int = 20
market_weekly_cron_minute: int = 0
legacy_sync_enabled: bool = False
celery_result_backend_url: str | None = None
daily_brief_cron_hour: int = 9
@@ -106,6 +117,7 @@ class Settings(BaseSettings):
"openclaw_gateway_token",
"hermes_api_key",
"direct_llm_api_key",
"market_data_token",
"feishu_app_secret",
"feishu_verification_token",
]

View File

@@ -166,5 +166,6 @@ AI_AUDIT_SENSITIVE_KEYS = frozenset(
"openclaw_gateway_token",
"hermes_api_key",
"direct_llm_api_key",
"market_data_token",
}
)

View File

@@ -14,6 +14,8 @@ class AISkillId(StrEnum):
INVESTMENT_RESEARCH = "investment_research"
PROJECT_LIFECYCLE_ANALYSIS = "project_lifecycle_analysis"
PROJECT_FINANCE_NEEDS_ANALYSIS = "project_finance_needs_analysis"
MARKET_OVERVIEW_ANALYSIS = "market_overview_analysis"
STOCK_ANALYSIS = "stock_analysis"
HERMES_MEMORY_RECALL = "hermes_memory_recall"
HERMES_MEMORY_WRITE = "hermes_memory_write"
@@ -68,6 +70,16 @@ PROJECT_FINANCE_NEEDS_ANALYSIS_INSTRUCTIONS = (
"完成时限和验证指标;五、数据限制。所有安排必须标注需要人工确认。"
"不得把项目资金安排需求称为公司融资缺口,不得审批付款、融资或投资交易。"
)
MARKET_OVERVIEW_ANALYSIS_INSTRUCTIONS = (
"基于上下文中的A股指数、涨跌家数、成交额和行业数据生成中文市场研究。"
"输出总体判断、证据、前三项风险、关注行业、下一交易日观察指标和数据限制。"
"不得编造行情、保证收益或下交易指令。"
)
STOCK_ANALYSIS_INSTRUCTIONS = (
"基于上下文中的价格、收益、均线、波动率、回撤、估值和财务指标生成中文股票研究。"
"输出基本面、技术面、估值、三种情景、风险及后续跟踪指标。"
"不得给出保证性结论或自动交易指令,所有决策必须人工确认。"
)
HERMES_MEMORY_RECALL_INSTRUCTIONS = (
"Retrieve concise long-term memory, preferences, prior decisions, and relevant "
"business context for this request. Return only information useful to answer it."
@@ -98,6 +110,16 @@ AI_SKILLS: dict[AISkillId, AISkill] = {
source=AISkillSource.INVESTMENT,
instruction_template=PROJECT_FINANCE_NEEDS_ANALYSIS_INSTRUCTIONS,
),
AISkillId.MARKET_OVERVIEW_ANALYSIS: AISkill(
skill_id=AISkillId.MARKET_OVERVIEW_ANALYSIS,
source=AISkillSource.INVESTMENT,
instruction_template=MARKET_OVERVIEW_ANALYSIS_INSTRUCTIONS,
),
AISkillId.STOCK_ANALYSIS: AISkill(
skill_id=AISkillId.STOCK_ANALYSIS,
source=AISkillSource.INVESTMENT,
instruction_template=STOCK_ANALYSIS_INSTRUCTIONS,
),
AISkillId.HERMES_MEMORY_RECALL: AISkill(
skill_id=AISkillId.HERMES_MEMORY_RECALL,
source=AISkillSource.AI_MEMORY,

View File

@@ -22,6 +22,8 @@ class AuditAction(StrEnum):
ENTERPRISE_ANALYTICS = "enterprise_analytics"
EVENT_DISPATCH = "event.dispatch"
HEARTBEAT = "heartbeat"
MARKET_SYNC = "market.sync"
MARKET_WATCHLIST_UPDATE = "market.watchlist.update"
class AuditRiskLevel(StrEnum):
@@ -40,6 +42,7 @@ class AuditSource(StrEnum):
EVENTS = "events"
AI_MEMORY = "ai_memory"
OBSERVABILITY = "observability"
MARKET = "market"
class AuditTargetType(StrEnum):
@@ -51,6 +54,7 @@ class AuditTargetType(StrEnum):
AI_MEMORY = "ai-memory"
DOMAIN_EVENT = "domain-event"
HEARTBEAT = "heartbeat"
MARKET = "market"
class AuditStatus(StrEnum):
@@ -71,6 +75,7 @@ AUDIT_SENSITIVE_KEYS = frozenset(
"openclaw_gateway_token",
"hermes_api_key",
"direct_llm_api_key",
"market_data_token",
"feishu_app_secret",
"feishu_verification_token",
}

View File

@@ -2,6 +2,14 @@ from app.modules.business.models.attendance import AttendanceRecord
from app.modules.business.models.finance import Expense, FundAccount, Procurement
from app.modules.business.models.governance import PerformanceMetric, Policy, Standard
from app.modules.business.models.legacy import LegacySyncRun
from app.modules.business.models.market import (
MarketAnnouncement,
MarketDailyQuote,
MarketFinancialMetric,
MarketInstrument,
MarketMacroIndicator,
MarketWatchlist,
)
from app.modules.business.models.lifecycle import (
Employee,
ProjectCashFlow,
@@ -22,6 +30,12 @@ __all__ = [
"Employee",
"FundAccount",
"LegacySyncRun",
"MarketAnnouncement",
"MarketDailyQuote",
"MarketFinancialMetric",
"MarketInstrument",
"MarketMacroIndicator",
"MarketWatchlist",
"PerformanceMetric",
"Policy",
"Procurement",

View File

@@ -0,0 +1,87 @@
from datetime import date, datetime
from decimal import Decimal
from sqlalchemy import Boolean, Date, DateTime, Integer, Numeric, String, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from app.core.database import Base
from app.modules.business.models.common import TimestampMixin
class MarketInstrument(Base, TimestampMixin):
__tablename__ = "market_instruments"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
symbol: Mapped[str] = mapped_column(String(32), unique=True, index=True)
name: Mapped[str] = mapped_column(String(128), index=True)
exchange: Mapped[str | None] = mapped_column(String(16), nullable=True, index=True)
instrument_type: Mapped[str] = mapped_column(String(16), default="stock", index=True)
industry: Mapped[str | None] = mapped_column(String(128), nullable=True, index=True)
list_date: Mapped[date | None] = mapped_column(Date, nullable=True)
is_active: Mapped[bool] = mapped_column(Boolean, default=True, index=True)
source_updated_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
class MarketDailyQuote(Base, TimestampMixin):
__tablename__ = "market_daily_quotes"
__table_args__ = (UniqueConstraint("symbol", "trade_date", name="uq_market_quote_day"),)
id: Mapped[int] = mapped_column(Integer, primary_key=True)
symbol: Mapped[str] = mapped_column(String(32), index=True)
trade_date: Mapped[date] = mapped_column(Date, index=True)
open_price: Mapped[Decimal | None] = mapped_column(Numeric(18, 4), nullable=True)
high_price: Mapped[Decimal | None] = mapped_column(Numeric(18, 4), nullable=True)
low_price: Mapped[Decimal | None] = mapped_column(Numeric(18, 4), nullable=True)
close_price: Mapped[Decimal] = mapped_column(Numeric(18, 4))
pre_close: Mapped[Decimal | None] = mapped_column(Numeric(18, 4), nullable=True)
pct_change: Mapped[Decimal | None] = mapped_column(Numeric(12, 4), nullable=True)
volume: Mapped[Decimal | None] = mapped_column(Numeric(20, 2), nullable=True)
amount_cny: Mapped[Decimal | None] = mapped_column(Numeric(20, 2), nullable=True)
pe: Mapped[Decimal | None] = mapped_column(Numeric(18, 4), nullable=True)
pb: Mapped[Decimal | None] = mapped_column(Numeric(18, 4), nullable=True)
total_market_value: Mapped[Decimal | None] = mapped_column(Numeric(20, 2), nullable=True)
class MarketFinancialMetric(Base, TimestampMixin):
__tablename__ = "market_financial_metrics"
__table_args__ = (UniqueConstraint("symbol", "period_end", name="uq_market_financial_period"),)
id: Mapped[int] = mapped_column(Integer, primary_key=True)
symbol: Mapped[str] = mapped_column(String(32), index=True)
period_end: Mapped[date] = mapped_column(Date, index=True)
revenue_yoy: Mapped[Decimal | None] = mapped_column(Numeric(12, 4), nullable=True)
net_profit_yoy: Mapped[Decimal | None] = mapped_column(Numeric(12, 4), nullable=True)
roe: Mapped[Decimal | None] = mapped_column(Numeric(12, 4), nullable=True)
debt_to_assets: Mapped[Decimal | None] = mapped_column(Numeric(12, 4), nullable=True)
operating_cashflow_yoy: Mapped[Decimal | None] = mapped_column(Numeric(12, 4), nullable=True)
class MarketMacroIndicator(Base, TimestampMixin):
__tablename__ = "market_macro_indicators"
__table_args__ = (UniqueConstraint("code", "period_date", name="uq_market_macro_period"),)
id: Mapped[int] = mapped_column(Integer, primary_key=True)
code: Mapped[str] = mapped_column(String(64), index=True)
name: Mapped[str] = mapped_column(String(128))
period_date: Mapped[date] = mapped_column(Date, index=True)
value: Mapped[Decimal] = mapped_column(Numeric(20, 6))
unit: Mapped[str | None] = mapped_column(String(32), nullable=True)
source: Mapped[str] = mapped_column(String(64), default="tushare")
class MarketAnnouncement(Base, TimestampMixin):
__tablename__ = "market_announcements"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
source_key: Mapped[str] = mapped_column(String(64), unique=True, index=True)
symbol: Mapped[str | None] = mapped_column(String(32), nullable=True, index=True)
announcement_date: Mapped[date] = mapped_column(Date, index=True)
published_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
title: Mapped[str] = mapped_column(String(512))
category: Mapped[str | None] = mapped_column(String(128), nullable=True, index=True)
source_url: Mapped[str | None] = mapped_column(String(1024), nullable=True)
source: Mapped[str] = mapped_column(String(64), default="tushare")
class MarketWatchlist(Base, TimestampMixin):
__tablename__ = "market_watchlists"
__table_args__ = (UniqueConstraint("actor", "symbol", name="uq_market_watchlist_actor_symbol"),)
id: Mapped[int] = mapped_column(Integer, primary_key=True)
actor: Mapped[str] = mapped_column(String(128), index=True)
symbol: Mapped[str] = mapped_column(String(32), index=True)
enabled: Mapped[bool] = mapped_column(Boolean, default=True, index=True)

View File

@@ -26,6 +26,8 @@ from app.modules.reports.constants import ReportResponseKey
from app.modules.reports.chart import lifecycle_chart_alt, render_lifecycle_chart
from app.modules.feishu.service import FeishuService
from app.modules.reports.services import ReportService
from app.modules.market.chart import render_market_chart
from app.modules.market.service import MarketService
from app.modules.risk.constants import RiskSummaryKey
from app.modules.risk.services import RiskService
@@ -38,20 +40,48 @@ RISK_TITLE = "风险预警"
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_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:
@@ -59,9 +89,7 @@ def _parse_content_text(content: Any) -> str:
if isinstance(content, dict):
return str(
content.get(FeishuPayloadKey.TEXT)
or content.get(FeishuPayloadKey.CONTENT)
or ""
content.get(FeishuPayloadKey.TEXT) or content.get(FeishuPayloadKey.CONTENT) or ""
)
if not isinstance(content, str):
return ""
@@ -70,11 +98,7 @@ def _parse_content_text(content: Any) -> str:
except json.JSONDecodeError:
return content
if isinstance(data, dict):
return str(
data.get(FeishuPayloadKey.TEXT)
or data.get(FeishuPayloadKey.CONTENT)
or ""
)
return str(data.get(FeishuPayloadKey.TEXT) or data.get(FeishuPayloadKey.CONTENT) or "")
return content
@@ -163,6 +187,10 @@ class FeishuCommandService:
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:
@@ -284,9 +312,7 @@ class FeishuCommandService:
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
FeishuCommandName.PROJECT_FINANCE if project_match else FeishuCommandName.FINANCE_NEEDS
)
if not get_settings().finance_needs_enabled:
content = "项目资金需求分析尚未启用,请先配置并启用财务只读同步。"
@@ -362,6 +388,184 @@ class FeishuCommandService:
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,
@@ -401,13 +605,30 @@ class FeishuCommandService:
command = FeishuCommandName.RULE_DISABLE
elif command_text.startswith("启用规则"):
command = FeishuCommandName.RULE_ENABLE
elif command_text.startswith(("查看规则", "规则列表")):
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:
create_match = RULE_CREATE_PATTERN.fullmatch(command_text)
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)
@@ -422,10 +643,10 @@ class FeishuCommandService:
else:
rule = memory.create_rule(
content=rule_content,
scope="global",
subject="company",
scope="market" if market_create_match else "global",
subject="market" if market_create_match else "company",
priority=priority,
tags=["feishu"],
tags=["feishu", *(["market"] if market_create_match else [])],
actor=actor,
)
content = (
@@ -438,6 +659,7 @@ class FeishuCommandService:
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,
)
@@ -457,9 +679,7 @@ class FeishuCommandService:
elif disable_match or enable_match:
enabled = enable_match is not None
command = (
FeishuCommandName.RULE_ENABLE
if enabled
else FeishuCommandName.RULE_DISABLE
FeishuCommandName.RULE_ENABLE if enabled else FeishuCommandName.RULE_DISABLE
)
match = enable_match or disable_match
rule = memory.update_rule(

View File

@@ -91,6 +91,12 @@ class FeishuCommandName(StrEnum):
RULE_ENABLE = "rule_enable"
FINANCE_NEEDS = "finance_needs"
PROJECT_FINANCE = "project_finance"
MARKET_OVERVIEW = "market_overview"
MARKET_MACRO = "market_macro"
MARKET_ANNOUNCEMENTS = "market_announcements"
STOCK_ANALYSIS = "stock_analysis"
WATCHLIST_ADD = "watchlist_add"
WATCHLIST_LIST = "watchlist_list"
DAILY_BRIEF = "daily_brief"
PROJECT_WEEKLY = "project_weekly"
ATTENDANCE_SUMMARY = "attendance_summary"

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

View File

@@ -64,6 +64,12 @@ class LifecyclePipelineService:
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 {

View File

@@ -8,6 +8,7 @@ class WorkflowType(StrEnum):
ENTERPRISE_ANALYTICS = "enterprise_analytics"
AI_MEMORY_CAPTURE = "ai_memory_capture"
LIFECYCLE_REPORT = "lifecycle_report"
MARKET_ANALYSIS = "market_analysis"
class WorkflowStatus(StrEnum):

View File

@@ -2,6 +2,7 @@ from app.tasks.app import celery_app
from app.tasks import events as _events # noqa: F401
from app.tasks import legacy as _legacy # noqa: F401
from app.tasks import lifecycle as _lifecycle # noqa: F401
from app.tasks import market as _market # noqa: F401
from app.tasks import reports as _reports # noqa: F401
from app.tasks import risk as _risk # noqa: F401

31
app/tasks/market.py Normal file
View File

@@ -0,0 +1,31 @@
from datetime import date
from app.core.background.task_queue.constants import TASK_RUN_MARKET_CLOSE
from app.core.database import SessionLocal
from app.modules.market.pipeline import MarketPipelineService
from app.tasks.app import celery_app
@celery_app.task(
name=TASK_RUN_MARKET_CLOSE,
autoretry_for=(Exception,),
retry_backoff=True,
retry_kwargs={"max_retries": 3},
)
def run_market_report(
report_type: str = "close", trade_date: str | None = None, force: bool = False
) -> dict:
target = date.fromisoformat(trade_date) if trade_date else date.today()
db = SessionLocal()
try:
return MarketPipelineService(db).run(
report_type=report_type,
reference_date=target,
force=force,
actor="scheduler",
)
finally:
db.close()
run_market_close = run_market_report

View File

@@ -7,6 +7,12 @@ from app.modules.business.models import (
Employee,
FundAccount,
LegacySyncRun,
MarketAnnouncement,
MarketDailyQuote,
MarketFinancialMetric,
MarketInstrument,
MarketMacroIndicator,
MarketWatchlist,
PerformanceMetric,
Policy,
Procurement,
@@ -51,6 +57,12 @@ _MODELS = [
RiskEvent,
RiskEventAction,
LegacySyncRun,
MarketAnnouncement,
MarketInstrument,
MarketDailyQuote,
MarketFinancialMetric,
MarketMacroIndicator,
MarketWatchlist,
ReportPushRun,
DomainEvent,
WorkflowInstance,