feat: 2026-04-15~05-02 累积变更基线 — AI 重构 + Runtime Context + DWS 修复
涵盖(每条对应已存的审计记录): - AI 模块拆分:apps/backend/app/ai/apps -> prompts/(8 个 APP + app2a 派生) audit: 2026-04-20__ai-module-complete.md - admin-web AI 管理套件:AIDashboard / AIOperations / AIRunLogs / AITriggers / TriggerManager audit: 2026-04-21__admin-web-ai-management-suite.md - App2 财务洞察 prompt v3 -> v5.1 + 小程序 AI 接入(chat / board-finance) audit: 2026-04-22__app2_prompt_v5_1_and_miniprogram_ai_insight.md - App2 prewarm 全过滤器 + AI 触发器 cron reschedule audit: 2026-04-21__app2-finance-prewarm-all-filters.md migration: 20260420_ai_trigger_jobs_and_app2_prewarm.sql / 20260421_app2_prewarm_cron_reschedule.sql - AppType 联合类型对齐 + adminAiAppTypes.test.ts audit: 2026-04-30__admin_web_ai_app_type_alignment.md - DashScope tokens_used 提取修复 audit: 2026-04-30__backend_dashscope_tokens_used_extraction.md - App3 线索完整详情 prompt audit: 2026-05-01__backend_app3_full_detail_prompt.md - Runtime Context 沙箱(5-1~5-2 主线): - 后端 schema/service + admin_runtime_context / xcx_runtime_clock 两个 router - admin-web RuntimeContext.tsx + miniprogram runtime-clock.ts - migration: 20260501__runtime_context_sandbox.sql - tools/db/verify_admin_web_sandbox.py + verify_sandbox_end_to_end.py - database/changes: 7 份 sandbox_* 验证报告 - 飞球 DWS 修复:finance_area_daily 区域汇总 + task_engine 调整 + RLS 视图业务日上界(migration 20260502 + scripts/ops/gen_rls_business_date_migration.py) 合规: - .gitignore 启用 tmp/ 排除 - 不入仓:apps/etl/connectors/feiqiu/.env(API_TOKEN secret,本地修改保留) 待验证清单: - docs/audit/changes/2026-05-04__cumulative_baseline_pending_verification.md 每个主题的功能完整性 / 上线验证几乎都未收口,按优先级 P0~P3 逐一处理
This commit is contained in:
@@ -37,6 +37,7 @@ from decimal import Decimal
|
||||
from fastapi import HTTPException
|
||||
|
||||
from app.services import fdw_queries
|
||||
from app.services.runtime_context import get_runtime_context, task_runtime_filter
|
||||
from app.services.task_generator import compute_heart_icon
|
||||
from app.trace.decorators import trace_service
|
||||
|
||||
@@ -114,15 +115,17 @@ def _verify_task_ownership(
|
||||
- 不属于当前助教 → 403
|
||||
- required_status 不匹配 → 409
|
||||
"""
|
||||
runtime_clause, runtime_params = task_runtime_filter(site_id, conn=conn)
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
f"""
|
||||
SELECT id, task_type, status, is_pinned, abandon_reason,
|
||||
assistant_id, site_id
|
||||
FROM biz.coach_tasks
|
||||
WHERE id = %s
|
||||
{runtime_clause}
|
||||
""",
|
||||
(task_id,),
|
||||
[task_id, *runtime_params],
|
||||
)
|
||||
row = cur.fetchone()
|
||||
|
||||
@@ -166,22 +169,24 @@ async def get_task_list(user_id: int, site_id: int) -> list[dict]:
|
||||
assistant_id = _get_assistant_id(conn, user_id, site_id)
|
||||
|
||||
# 查询有效 + 已放弃任务(abandoned 排最后)
|
||||
runtime_clause, runtime_params = task_runtime_filter(site_id, conn=conn)
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
f"""
|
||||
SELECT id, task_type, status, priority_score, is_pinned,
|
||||
expires_at, created_at, member_id, abandon_reason
|
||||
FROM biz.coach_tasks
|
||||
WHERE site_id = %s
|
||||
AND assistant_id = %s
|
||||
AND status IN ('active', 'abandoned')
|
||||
{runtime_clause}
|
||||
ORDER BY
|
||||
CASE WHEN status = 'abandoned' THEN 1 ELSE 0 END ASC,
|
||||
is_pinned DESC,
|
||||
priority_score DESC NULLS LAST,
|
||||
created_at ASC
|
||||
""",
|
||||
(site_id, assistant_id),
|
||||
[site_id, assistant_id, *runtime_params],
|
||||
)
|
||||
tasks = cur.fetchall()
|
||||
conn.commit()
|
||||
@@ -605,8 +610,9 @@ async def get_task_list_v2(
|
||||
# 构建排除条件:relationship_building + member_id 不在 RS 范围内
|
||||
# 当排除列表为空时不加额外条件
|
||||
exclude_clause = ""
|
||||
query_params_count: list = [site_id, assistant_id, db_status]
|
||||
query_params_page: list = [site_id, assistant_id, db_status]
|
||||
runtime_clause, runtime_params = task_runtime_filter(site_id, conn=conn)
|
||||
query_params_count: list = [site_id, assistant_id, db_status, *runtime_params]
|
||||
query_params_page: list = [site_id, assistant_id, db_status, *runtime_params]
|
||||
if rb_exclude_member_ids:
|
||||
exclude_clause = (
|
||||
" AND NOT (task_type = 'relationship_building' AND member_id = ANY(%s))"
|
||||
@@ -621,6 +627,7 @@ async def get_task_list_v2(
|
||||
SELECT COUNT(*)
|
||||
FROM biz.coach_tasks
|
||||
WHERE site_id = %s AND assistant_id = %s AND status = %s
|
||||
{runtime_clause}
|
||||
{exclude_clause}
|
||||
""",
|
||||
query_params_count,
|
||||
@@ -636,6 +643,7 @@ async def get_task_list_v2(
|
||||
expires_at, created_at, member_id, abandon_reason
|
||||
FROM biz.coach_tasks
|
||||
WHERE site_id = %s AND assistant_id = %s AND status = %s
|
||||
{runtime_clause}
|
||||
{exclude_clause}
|
||||
ORDER BY is_pinned DESC,
|
||||
priority_score DESC NULLS LAST,
|
||||
@@ -669,9 +677,11 @@ async def get_task_list_v2(
|
||||
recent60d_map: dict[int, dict] = {}
|
||||
batch_data: dict | None = None
|
||||
try:
|
||||
from app.services.runtime_context import as_runtime_today_param
|
||||
_ref_date = as_runtime_today_param(site_id, conn=conn)
|
||||
batch_data = fdw_queries.batch_query_for_task_list(
|
||||
conn, site_id, assistant_id, member_ids,
|
||||
datetime.now().year, datetime.now().month,
|
||||
_ref_date.year, _ref_date.month,
|
||||
)
|
||||
member_info_map = batch_data["member_info"]
|
||||
balance_map = batch_data["balance"]
|
||||
@@ -685,7 +695,11 @@ async def get_task_list_v2(
|
||||
# ── 6. 查询 ai_cache 获取 aiSuggestion(优雅降级) ──
|
||||
ai_suggestion_map: dict[int, str] = {}
|
||||
try:
|
||||
member_id_strs = [str(mid) for mid in member_ids]
|
||||
runtime_ctx = get_runtime_context(site_id, conn=conn)
|
||||
if runtime_ctx.is_sandbox and runtime_ctx.sandbox_instance_id:
|
||||
member_id_strs = [f"{runtime_ctx.sandbox_instance_id}:{mid}" for mid in member_ids]
|
||||
else:
|
||||
member_id_strs = [str(mid) for mid in member_ids]
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
@@ -706,7 +720,8 @@ async def get_task_list_v2(
|
||||
result = row[1] if isinstance(row[1], dict) else {}
|
||||
summary = result.get("summary", "")
|
||||
if summary:
|
||||
ai_suggestion_map[int(target_id_str)] = summary
|
||||
raw_target = target_id_str.split(":", 1)[-1]
|
||||
ai_suggestion_map[int(raw_target)] = summary
|
||||
conn.commit()
|
||||
except Exception:
|
||||
logger.warning("查询 ai_cache aiSuggestion 失败", exc_info=True)
|
||||
@@ -802,8 +817,11 @@ def build_performance_summary(
|
||||
当 batch_data 为 None 时(如无任务的空列表场景),回退到独立查询。
|
||||
课时/档位/客户数从 monthly_summary(每日更新)取实时数据,
|
||||
不再依赖月初结算的 salary_calc。收入仍从 salary_calc 取(如有)。
|
||||
|
||||
CHANGE 2026-05-02 | now 改用 RuntimeContext.business_date,沙箱不读「未来」月份。
|
||||
"""
|
||||
now = datetime.now()
|
||||
from app.services.runtime_context import as_runtime_today_param
|
||||
now = as_runtime_today_param(site_id, conn=conn)
|
||||
year, month = now.year, now.month
|
||||
|
||||
if batch_data:
|
||||
@@ -971,15 +989,17 @@ async def get_task_by_member(
|
||||
try:
|
||||
assistant_id = _get_assistant_id(conn, user_id, site_id)
|
||||
|
||||
runtime_clause, runtime_params = task_runtime_filter(site_id, conn=conn)
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
f"""
|
||||
SELECT id, task_type
|
||||
FROM biz.coach_tasks
|
||||
WHERE site_id = %s AND assistant_id = %s AND member_id = %s
|
||||
AND status = 'active'
|
||||
{runtime_clause}
|
||||
""",
|
||||
(site_id, assistant_id, member_id),
|
||||
[site_id, assistant_id, member_id, *runtime_params],
|
||||
)
|
||||
rows = cur.fetchall()
|
||||
|
||||
@@ -1020,16 +1040,18 @@ async def get_task_detail(
|
||||
assistant_id = _get_assistant_id(conn, user_id, site_id)
|
||||
|
||||
# ── 1. 查询任务基础信息 ──
|
||||
runtime_clause, runtime_params = task_runtime_filter(site_id, conn=conn)
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
f"""
|
||||
SELECT id, task_type, status, priority_score, is_pinned,
|
||||
expires_at, created_at, member_id, abandon_reason,
|
||||
assistant_id, site_id
|
||||
FROM biz.coach_tasks
|
||||
WHERE id = %s
|
||||
{runtime_clause}
|
||||
""",
|
||||
(task_id,),
|
||||
[task_id, *runtime_params],
|
||||
)
|
||||
row = cur.fetchone()
|
||||
|
||||
@@ -1090,6 +1112,12 @@ async def get_task_detail(
|
||||
# ── 3. 查询维客线索 ──
|
||||
retention_clues = []
|
||||
try:
|
||||
runtime_ctx = get_runtime_context(site_id, conn=conn)
|
||||
member_target_id = (
|
||||
f"{runtime_ctx.sandbox_instance_id}:{member_id}"
|
||||
if runtime_ctx.is_sandbox and runtime_ctx.sandbox_instance_id
|
||||
else str(member_id)
|
||||
)
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
@@ -1136,7 +1164,7 @@ async def get_task_detail(
|
||||
AND cache_type IN ('app4_analysis', 'app5_talking_points')
|
||||
ORDER BY created_at DESC
|
||||
""",
|
||||
(str(member_id), site_id),
|
||||
(member_target_id, site_id),
|
||||
)
|
||||
seen_types: set[str] = set()
|
||||
for cache_row in cur.fetchall():
|
||||
@@ -1173,8 +1201,10 @@ async def get_task_detail(
|
||||
|
||||
# CHANGE 2026-03-25 | 统计范围:近60天;列表不限
|
||||
# 预估规则:当月且日期 ≤ 5号
|
||||
from datetime import date, timedelta
|
||||
today = date.today()
|
||||
# CHANGE 2026-05-02 | today 改用 business_date,沙箱不读「未来」60 天
|
||||
from datetime import timedelta
|
||||
from app.services.runtime_context import as_runtime_today_param
|
||||
today = as_runtime_today_param(site_id, conn=conn)
|
||||
cutoff_60d = today - timedelta(days=60)
|
||||
is_estimate_month = today.day <= 5
|
||||
|
||||
|
||||
Reference in New Issue
Block a user