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:
@@ -52,14 +52,25 @@ def run_auto_verify():
|
||||
continue
|
||||
|
||||
# 查最新KPI值
|
||||
kpi = db.query(KPIDefinition).filter(
|
||||
KPIDefinition.kpi_code == kpi_code
|
||||
).order_by(KPIDefinition.id.desc()).first()
|
||||
# 缺陷2残留修复(对齐 verify.py):从 plan 关联 KPI 向上取 entity_id,kpi_code 查询带 entity_id 过滤(防跨租户误匹配)
|
||||
entity_id = None
|
||||
if plan.kpi_id:
|
||||
pkpi = db.query(KPIDefinition).filter(KPIDefinition.id == plan.kpi_id).first()
|
||||
if pkpi:
|
||||
entity_id = pkpi.entity_id
|
||||
q = db.query(KPIDefinition).filter(KPIDefinition.kpi_code == kpi_code)
|
||||
if entity_id is not None:
|
||||
q = q.filter(KPIDefinition.entity_id == entity_id)
|
||||
kpi = q.order_by(KPIDefinition.id.desc()).first()
|
||||
if not kpi:
|
||||
continue
|
||||
# 缺陷3残留修复(对齐 verify.py):KPIValue 按 period <= 当前月过滤,跨月验证不取未来期间
|
||||
period_limit = now.strftime("%Y-%m")
|
||||
latest = db.query(KPIValue).filter(
|
||||
KPIValue.kpi_id == kpi.id
|
||||
).order_by(KPIValue.calculated_at.desc()).first()
|
||||
KPIValue.kpi_id == kpi.id,
|
||||
KPIValue.actual_value.isnot(None),
|
||||
KPIValue.period <= period_limit,
|
||||
).order_by(KPIValue.period.desc(), KPIValue.calculated_at.desc(), KPIValue.id.desc()).first()
|
||||
if not latest or latest.actual_value is None:
|
||||
continue
|
||||
|
||||
@@ -74,11 +85,14 @@ def run_auto_verify():
|
||||
plan.kpi_current_after = actual
|
||||
|
||||
if passed:
|
||||
# 缺陷1残留修复(对齐 verify.py):plan 已处于"验证通过"状态(passed + verified_at 非空)则跳过 OKR 累加,防重复累加
|
||||
already_verified = bool(plan.verify_status == "passed" and plan.verified_at is not None)
|
||||
plan.verify_status = "passed"
|
||||
plan.verified_at = now
|
||||
plan.status = "done"
|
||||
plan.status = "completed" # 缺陷4残留修复:"done" 不在枚举(pending/in_progress/completed/cancelled),改 completed
|
||||
plan.progress = 100
|
||||
# 阶段3: OKR progress更新
|
||||
update_okr_progress(db, plan, +15)
|
||||
update_okr_progress(db, plan, +15, already_verified=already_verified)
|
||||
messages.append(f"✅ 行动计划#{plan.id}验证通过: {plan.title} ({kpi_code}: {rule.get('baseline_value')}→{actual})")
|
||||
verified_count += 1
|
||||
else:
|
||||
@@ -107,16 +121,24 @@ def run_auto_verify():
|
||||
db.close()
|
||||
|
||||
|
||||
def update_okr_progress(db, plan, delta):
|
||||
"""验证通过→更新所属OKR progress"""
|
||||
def update_okr_progress(db, plan, delta, already_verified=False):
|
||||
"""验证通过→更新所属OKR progress
|
||||
|
||||
缺陷1残留修复(对齐 verify.py 2026-08-30):幂等防重复累加
|
||||
- already_verified=True(plan 已处于验证通过状态且 verified_at 非空)→ 跳过累加,保持原值
|
||||
"""
|
||||
okr_id = getattr(plan, "okr_id", None) or getattr(plan, "objective_id", None)
|
||||
if not okr_id:
|
||||
return
|
||||
return {"updated": False, "reason": "no_objective"}
|
||||
obj = db.query(Objective).filter(Objective.id == okr_id).first()
|
||||
if obj:
|
||||
current = obj.progress or 0
|
||||
obj.progress = min(current + delta, 100)
|
||||
db.add(obj)
|
||||
if not obj:
|
||||
return {"updated": False, "reason": "objective_not_found"}
|
||||
if already_verified:
|
||||
return {"updated": False, "reason": "already_verified", "objective_id": obj.id, "progress": obj.progress or 0}
|
||||
current = obj.progress or 0
|
||||
obj.progress = min(current + delta, 100)
|
||||
db.add(obj)
|
||||
return {"updated": True, "objective_id": obj.id, "progress": obj.progress}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user