feat: Auto-Verify闭环 — verify API+定时验证+OKR联动+周报
This commit is contained in:
@@ -0,0 +1,299 @@
|
||||
"""
|
||||
自动验证引擎 API — 管理会计OS
|
||||
POST /api/cma/verify/{plan_id} 手动验证行动计划执行结果
|
||||
|
||||
数据流:
|
||||
ActionPlan.auto_verify_rule (JSON) → 条件判断 → passed/failed
|
||||
→ KPI值回填 (kpi_current_before/after + KPIValue source_type=verify)
|
||||
→ 验证通过 → 所属OKR progress +15%
|
||||
→ 通知任总 (send_wecom_message)
|
||||
|
||||
规则格式(新):
|
||||
{
|
||||
"kpi_code": "C_REBATE_RATE",
|
||||
"condition": "LESS_THAN", # LESS_THAN/GREATER_THAN/WITHIN_RANGE/NOT_NULL
|
||||
"target_value": 80,
|
||||
"baseline_value": 86.4,
|
||||
"verify_after_days": 7,
|
||||
"retry_max": 3,
|
||||
"escalate_to": "任富海",
|
||||
"notify": true
|
||||
}
|
||||
|
||||
兼容格式(旧):
|
||||
{"condition": "value < 75", "description": "..."} → 走 bot_bridge_v2._evaluate_condition
|
||||
"""
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
from datetime import datetime, timedelta
|
||||
import json
|
||||
import logging
|
||||
|
||||
from app.database import get_db
|
||||
from app.models import ActionPlan, KPIDefinition, KPIValue, Objective
|
||||
from app.utils.notifier import send_wecom_message
|
||||
from app.auth_middleware import require_role
|
||||
|
||||
logger = logging.getLogger("cma.verify")
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/api/cma/verify",
|
||||
tags=["自动验证"],
|
||||
dependencies=[Depends(require_role("ceo", "finance", "business", "it"))],
|
||||
)
|
||||
|
||||
CONDITION_FUNCS = {
|
||||
"LESS_THAN": lambda actual, target: actual is not None and actual < target,
|
||||
"GREATER_THAN": lambda actual, target: actual is not None and actual > target,
|
||||
"WITHIN_RANGE": lambda actual, target: target is not None and len(target) == 2 and float(target[0]) <= actual <= float(target[1]),
|
||||
"NOT_NULL": lambda actual, target: actual is not None,
|
||||
}
|
||||
|
||||
|
||||
def _parse_rule(rule) -> dict:
|
||||
"""解析auto_verify_rule(兼容JSON字符串)"""
|
||||
if rule is None:
|
||||
return {}
|
||||
if isinstance(rule, str):
|
||||
try:
|
||||
return json.loads(rule)
|
||||
except Exception:
|
||||
return {"condition": rule}
|
||||
return rule
|
||||
|
||||
|
||||
def evaluate_rule(rule: dict, actual, kpi_data: dict = None) -> bool:
|
||||
"""
|
||||
按规则判断actual是否达标
|
||||
- 新格式: condition为枚举 LESS_THAN/GREATER_THAN/WITHIN_RANGE/NOT_NULL
|
||||
- 旧格式: condition为表达式字符串(如 "value < 75")→ bot_bridge_v2引擎
|
||||
"""
|
||||
condition = (rule or {}).get("condition", "")
|
||||
if not condition:
|
||||
return actual is not None
|
||||
|
||||
if condition in CONDITION_FUNCS:
|
||||
target = rule.get("target_value")
|
||||
return CONDITION_FUNCS[condition](actual, target)
|
||||
|
||||
# 旧格式表达式(bot-bridge兼容)
|
||||
try:
|
||||
from app.api.bot_bridge_v2 import _evaluate_condition
|
||||
kd = kpi_data or {"value": actual, "target": rule.get("target_value"), "baseline": rule.get("baseline_value")}
|
||||
return _evaluate_condition(condition, kd)
|
||||
except Exception as e:
|
||||
logger.warning(f"旧格式条件评估失败({condition}): {e}")
|
||||
return False
|
||||
|
||||
|
||||
def update_okr_progress(db: Session, plan: ActionPlan) -> dict:
|
||||
"""验证通过 → 所属OKR progress +15%(每通过1个KR)"""
|
||||
if not plan.objective_id:
|
||||
return {"updated": False, "reason": "no_objective"}
|
||||
obj = db.query(Objective).filter(Objective.id == plan.objective_id).first()
|
||||
if not obj:
|
||||
return {"updated": False, "reason": "objective_not_found"}
|
||||
before = obj.progress or 0
|
||||
obj.progress = min(100, before + 15)
|
||||
db.flush()
|
||||
return {"updated": True, "objective_id": obj.id, "before": before, "after": obj.progress}
|
||||
|
||||
|
||||
def build_auto_verify_rule(kpi, baseline_value=None, verify_after_days: int = 7) -> dict:
|
||||
"""根据KPI阈值自动生成验证规则(验收#1: 创建ActionPlan自动带auto_verify_rule)
|
||||
|
||||
从KPI的绿灯阈值(green)推导达标方向:
|
||||
- 绿灯 '<=X' → 目标是把值压到 X 以下 → LESS_THAN X
|
||||
- 绿灯 '>=X' → 目标是把值抬到 X 以上 → GREATER_THAN X
|
||||
- 有绿色区间 'X~Y' → WITHIN_RANGE
|
||||
- 无阈值 → NOT_NULL
|
||||
"""
|
||||
expr = (kpi.threshold_green or "").strip()
|
||||
condition = "NOT_NULL"
|
||||
target = None
|
||||
import re
|
||||
m = re.match(r"^(<=|>=|<|>|=)\s*([\d.]+)$", expr)
|
||||
if m:
|
||||
op, val = m.group(1), float(m.group(2))
|
||||
if op in ("<", "<="):
|
||||
condition = "LESS_THAN"
|
||||
elif op in (">", ">="):
|
||||
condition = "GREATER_THAN"
|
||||
else:
|
||||
condition = "NOT_NULL"
|
||||
target = val
|
||||
elif "~" in expr:
|
||||
parts = expr.split("~")
|
||||
if len(parts) == 2:
|
||||
try:
|
||||
condition = "WITHIN_RANGE"
|
||||
target = [float(parts[0]), float(parts[1])]
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
# 无阈值表达式 → 用KPI目标值推方向
|
||||
condition = "NOT_NULL"
|
||||
target = None
|
||||
|
||||
return {
|
||||
"kpi_code": kpi.kpi_code,
|
||||
"condition": condition,
|
||||
"target_value": target,
|
||||
"baseline_value": float(baseline_value) if baseline_value is not None else None,
|
||||
"verify_after_days": verify_after_days,
|
||||
"retry_max": 3,
|
||||
"escalate_to": "任富海",
|
||||
"notify": True,
|
||||
}
|
||||
|
||||
|
||||
def backfill_kpi_value(db: Session, plan: ActionPlan, actual, rule: dict, source: str = "verify"):
|
||||
"""回填KPI当前值: 写入kpi_current_before/after + 新KPIValue记录"""
|
||||
kpi = None
|
||||
kpi_code = rule.get("kpi_code") if rule else None
|
||||
# 优先按验证规则指定的KPI编码查询;无规则时才回退到plan.kpi_id
|
||||
if kpi_code:
|
||||
kpi = db.query(KPIDefinition).filter(KPIDefinition.kpi_code == kpi_code).first()
|
||||
elif plan.kpi_id:
|
||||
kpi = db.query(KPIDefinition).filter(KPIDefinition.id == plan.kpi_id).first()
|
||||
|
||||
if actual is None:
|
||||
return None
|
||||
|
||||
plan.kpi_current_before = plan.kpi_current_before if plan.kpi_current_before is not None else rule.get("baseline_value")
|
||||
plan.kpi_current_after = actual
|
||||
|
||||
if kpi:
|
||||
new_val = KPIValue(
|
||||
kpi_id=kpi.id,
|
||||
period=datetime.now().strftime("%Y-%m"),
|
||||
actual_value=actual,
|
||||
source_type="verify",
|
||||
source_batch=f"verify-plan-{plan.id}",
|
||||
data_status="verified",
|
||||
calculated_at=datetime.now(),
|
||||
remark=f"行动计划#{plan.id}验证回填",
|
||||
)
|
||||
db.add(new_val)
|
||||
return {"kpi_id": kpi.id, "kpi_code": kpi.kpi_code}
|
||||
return None
|
||||
|
||||
|
||||
@router.post("/{plan_id}")
|
||||
def verify_action_plan(plan_id: int, payload: dict, db: Session = Depends(get_db)):
|
||||
"""验证行动计划执行结果(手动验证 / Bot回填)
|
||||
|
||||
请求体:
|
||||
{
|
||||
"actual_value": 78.5, # 可选,缺省时取KPI最新值
|
||||
"source": "财务Bot分析", # 来源
|
||||
"note": "渠补谈判后...", # 备注/验证结果详情
|
||||
"passed": true # 可选,无规则时手动指定
|
||||
}
|
||||
"""
|
||||
plan = db.query(ActionPlan).filter(ActionPlan.id == plan_id).first()
|
||||
if not plan:
|
||||
raise HTTPException(404, "行动计划不存在")
|
||||
|
||||
rule = _parse_rule(plan.auto_verify_rule)
|
||||
actual = payload.get("actual_value")
|
||||
note = payload.get("note", "")
|
||||
source = payload.get("source", "手动验证")
|
||||
|
||||
# 1. 无规则 → 按手动指定 passed 标记
|
||||
if not rule or not rule.get("condition"):
|
||||
passed = payload.get("passed", True)
|
||||
plan.verify_status = "passed" if passed else "failed"
|
||||
plan.verify_result = "pass" if passed else "fail"
|
||||
plan.verify_log = (plan.verify_log or []) + [{
|
||||
"timestamp": datetime.now().isoformat(),
|
||||
"passed": passed,
|
||||
"note": note,
|
||||
"source": source,
|
||||
}]
|
||||
plan.verified_at = datetime.now()
|
||||
db.commit()
|
||||
return {"plan_id": plan_id, "verify_status": plan.verify_status, "passed": passed}
|
||||
|
||||
# 2. 缺省actual → 取KPI最新值
|
||||
if actual is None:
|
||||
kpi = None
|
||||
kpi_code = rule.get("kpi_code")
|
||||
if plan.kpi_id:
|
||||
kpi = db.query(KPIDefinition).filter(KPIDefinition.id == plan.kpi_id).first()
|
||||
elif kpi_code:
|
||||
kpi = db.query(KPIDefinition).filter(KPIDefinition.kpi_code == kpi_code).first()
|
||||
if kpi:
|
||||
latest = db.query(KPIValue).filter(
|
||||
KPIValue.kpi_id == kpi.id,
|
||||
KPIValue.actual_value.isnot(None),
|
||||
).order_by(KPIValue.calculated_at.desc(), KPIValue.id.desc()).first()
|
||||
if latest:
|
||||
actual = latest.actual_value
|
||||
|
||||
# 3. 执行条件判断
|
||||
passed = evaluate_rule(rule, actual, kpi_data={"value": actual, "target": rule.get("target_value"), "baseline": rule.get("baseline_value")})
|
||||
|
||||
# 4. 回写KPI当前值
|
||||
backfill = backfill_kpi_value(db, plan, actual, rule, source=source)
|
||||
|
||||
# 5. 更新状态
|
||||
plan.verify_status = "passed" if passed else "failed"
|
||||
plan.verify_result = "pass" if passed else "fail"
|
||||
plan.verify_log = (plan.verify_log or []) + [{
|
||||
"timestamp": datetime.now().isoformat(),
|
||||
"condition": rule.get("condition"),
|
||||
"actual_value": actual,
|
||||
"target_value": rule.get("target_value"),
|
||||
"passed": passed,
|
||||
"source": source,
|
||||
"note": note,
|
||||
}]
|
||||
if passed:
|
||||
plan.status = "done"
|
||||
plan.progress = 100
|
||||
plan.verified_at = datetime.now()
|
||||
|
||||
# 6. OKR进度联动(验证通过 → +15%)
|
||||
okr_update = None
|
||||
if passed:
|
||||
okr_update = update_okr_progress(db, plan)
|
||||
|
||||
db.commit()
|
||||
|
||||
# 7. 通知任总
|
||||
if rule.get("notify", True):
|
||||
delta = ""
|
||||
if plan.kpi_current_before is not None and actual is not None:
|
||||
try:
|
||||
d = float(actual) - float(plan.kpi_current_before)
|
||||
delta = f"({d:+.1f})"
|
||||
except (TypeError, ValueError):
|
||||
delta = ""
|
||||
title = f"✅ 行动计划#{plan.id}验证通过" if passed else f"❌ 行动计划#{plan.id}验证失败"
|
||||
content = (
|
||||
f"{plan.title}\n"
|
||||
f"KPI: {rule.get('kpi_code', '')} {plan.kpi_current_before}→{actual} {delta}\n"
|
||||
f"规则: {rule.get('condition')} {rule.get('target_value')}\n"
|
||||
f"来源: {source}\n"
|
||||
f"{note}"
|
||||
)
|
||||
try:
|
||||
send_wecom_message(content=content, title=title, alert_level="green" if passed else "red")
|
||||
except Exception as e:
|
||||
logger.warning(f"验证通知发送失败: {e}")
|
||||
|
||||
return {
|
||||
"plan_id": plan_id,
|
||||
"verify_status": plan.verify_status,
|
||||
"kpi_current_before": plan.kpi_current_before,
|
||||
"kpi_current_after": plan.kpi_current_after,
|
||||
"improvement": f"{float(actual) - float(plan.kpi_current_before):+.1f}" if plan.kpi_current_before is not None and actual is not None else None,
|
||||
"passed": passed,
|
||||
"okr_progress": okr_update,
|
||||
"kpi_backfill": backfill,
|
||||
"message": (
|
||||
f"验证通过:{plan.title}" if passed
|
||||
else f"验证失败:{plan.title}(条件 {rule.get('condition')} {rule.get('target_value')} 未达成,当前值 {actual})"
|
||||
),
|
||||
}
|
||||
+2
-1
@@ -5,7 +5,7 @@ from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import JSONResponse
|
||||
from dotenv import load_dotenv
|
||||
from app.database import init_db
|
||||
from app.api import auth, kpis, templates, maps, dashboard, data, alerts, ai_analysis, alert_rules, users, thresholds, notifications, permissions, action_plans, alignment, org, objectives, versions, budget, cost, predict, reports, security, knowledge, bot_bridge, bot_bridge_v2, lead, tenant, customer_dashboard, deviation_push, budget_generate, knowledge_articles, kpi_causality, data_quality, bi_reports, entities, bsc_layers, okr, okr_templates, subjects, driver_budget, bot_kpis, bot_iron_law, analysis_results, expenses, cash, tax_compliance
|
||||
from app.api import auth, kpis, templates, maps, dashboard, data, alerts, ai_analysis, alert_rules, users, thresholds, notifications, permissions, action_plans, alignment, org, objectives, versions, budget, cost, predict, reports, security, knowledge, bot_bridge, bot_bridge_v2, lead, tenant, customer_dashboard, deviation_push, budget_generate, knowledge_articles, kpi_causality, data_quality, bi_reports, entities, bsc_layers, okr, okr_templates, subjects, driver_budget, bot_kpis, bot_iron_law, analysis_results, expenses, cash, tax_compliance, verify
|
||||
from app.utils.cache import clear_all as clear_cache, delete as delete_cache
|
||||
from scripts.erp_sync import run_sync as run_erp_sync
|
||||
from app.auth_middleware import require_auth
|
||||
@@ -77,6 +77,7 @@ app.include_router(analysis_results.router)
|
||||
app.include_router(expenses.router)
|
||||
app.include_router(cash.router)
|
||||
app.include_router(tax_compliance.router)
|
||||
app.include_router(verify.router)
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def global_exception_handler(request: Request, exc: Exception):
|
||||
|
||||
@@ -210,6 +210,11 @@ class ActionPlan(Base):
|
||||
auto_verify_rule = Column(JSON, nullable=True, comment="自动验证规则: {\"condition\": \"value > target\"}")
|
||||
verify_result = Column(String(20), nullable=True, comment="验证结果: pass/fail/pending")
|
||||
verify_log = Column(JSON, nullable=True, comment="验证历史日志")
|
||||
verify_status = Column(String(20), default="pending", comment="验证状态: pending/passed/failed/retrying/escalated")
|
||||
verify_attempts = Column(Integer, default=0, comment="验证尝试次数")
|
||||
verified_at = Column(DateTime, nullable=True, comment="验证完成时间")
|
||||
kpi_current_before = Column(Float, nullable=True, comment="执行前KPI值")
|
||||
kpi_current_after = Column(Float, nullable=True, comment="执行后KPI值")
|
||||
created_by = Column(String(100), nullable=True, comment="创建人")
|
||||
created_at = Column(DateTime, server_default=func.now())
|
||||
updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now())
|
||||
|
||||
Binary file not shown.
@@ -122,6 +122,58 @@ def send_mail(smtp_config: dict, to_addrs: list, title: str, content: str) -> di
|
||||
# 主推送函数
|
||||
# ============================================================
|
||||
|
||||
def send_wecom_message(content: str, title: str = "管理会计OS通知", alert_level: str = "green") -> dict:
|
||||
"""发送企业微信消息到所有已启用的通知渠道(验证结果/行动报告推送用)
|
||||
|
||||
- 读取 notification_channels 表中启用的 wecom_app / wecom 渠道
|
||||
- 无渠道时记日志并返回失败(不抛异常,保证主流程不中断)
|
||||
"""
|
||||
try:
|
||||
from app.database import get_session_local
|
||||
from app.models import NotificationChannel
|
||||
except Exception as e:
|
||||
logger.warning(f"send_wecom_message: 加载依赖失败: {e}")
|
||||
return {"success": False, "message": f"依赖加载失败: {e}"}
|
||||
|
||||
db = get_session_local()()
|
||||
try:
|
||||
channels = db.query(NotificationChannel).filter(
|
||||
NotificationChannel.enabled == True,
|
||||
NotificationChannel.channel_type.in_(["wecom_app", "wecom"]),
|
||||
).all()
|
||||
if not channels:
|
||||
logger.info(f"send_wecom_message: 无已启用的企微渠道,跳过推送: {title}")
|
||||
return {"success": False, "message": "无已启用的企微通知渠道"}
|
||||
|
||||
results = []
|
||||
for ch in channels:
|
||||
config = ch.config or {}
|
||||
if ch.channel_type == "wecom_app":
|
||||
r = send_wecom_app(
|
||||
corp_id=config.get("corp_id", ""),
|
||||
corp_secret=config.get("corp_secret", ""),
|
||||
agent_id=config.get("agent_id", ""),
|
||||
touser=config.get("touser", "@all"),
|
||||
title=title, content=content, alert_level=alert_level,
|
||||
)
|
||||
else:
|
||||
r = send_wecom_robot(
|
||||
webhook_url=config.get("webhook_url", ""),
|
||||
title=title, content=content, alert_level=alert_level,
|
||||
)
|
||||
r["channel"] = ch.channel_type
|
||||
results.append(r)
|
||||
return {
|
||||
"success": any(r.get("success") for r in results),
|
||||
"results": results,
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"send_wecom_message 异常: {e}")
|
||||
return {"success": False, "message": f"推送异常: {e}"}
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
def push_alert(alert: dict, channels: list[dict]) -> list[dict]:
|
||||
"""向所有已启用渠道推送一条预警"""
|
||||
results = []
|
||||
|
||||
Reference in New Issue
Block a user