fix: auto-verify cron残留缺陷修复+第三阶段接入 verify-cron-fix (auto_verify_cron 4项对齐verify.py: entity_id过滤/period过滤/OKR幂等/status completed; action_plan_weekly done->completed; action_plans创建自动生成auto_verify_rule)

This commit is contained in:
Hermes CI Fix
2026-08-31 07:44:00 +08:00
parent 27d8269667
commit 136fe3811f
3 changed files with 54 additions and 16 deletions
+17 -1
View File
@@ -9,7 +9,8 @@ from calendar import monthrange
from app.database import get_db
from app.deps import get_entity_id
from app.auth_middleware import require_role, require_auth
from app.models import ActionPlan, KPIAlert, KPIDefinition, User, Objective, KR
from app.models import ActionPlan, KPIAlert, KPIDefinition, KPIValue, User, Objective, KR
from app.api.verify import build_auto_verify_rule
logger = logging.getLogger("cma.action_plans")
@@ -183,7 +184,22 @@ def create_plan(
status="pending",
progress=0,
created_by=current_user.name or current_user.username,
auto_verify_rule=data.get("auto_verify_rule"), # 显式规则原样保存;None 时下面自动生成
)
# 第三阶段接入(2026-08-31):创建 ActionPlan 自动生成验证规则
# 请求体未传 auto_verify_rule 且关联 KPI 存在 → 复用 verify.build_auto_verify_rule 生成默认规则(不改变现有创建行为;auto_close 默认 false
if not data.get("auto_verify_rule"):
baseline_value = data.get("baseline_value")
if baseline_value is None:
# 基线缺省时取 KPI 当前最新值(period <= 当前月),供 kpi_current_before 回填
latest_kpi = db.query(KPIValue).filter(
KPIValue.kpi_id == kpi_ent.id,
KPIValue.actual_value.isnot(None),
KPIValue.period <= datetime.now().strftime("%Y-%m"),
).order_by(KPIValue.period.desc(), KPIValue.calculated_at.desc(), KPIValue.id.desc()).first()
if latest_kpi:
baseline_value = latest_kpi.actual_value
plan.auto_verify_rule = build_auto_verify_rule(kpi_ent, baseline_value=baseline_value)
db.add(plan)
db.commit()
db.refresh(plan)