akg-factor-bridge/plan_health.py

38 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""桥机容器:python plan_health.py。只读本地发布文件,不连库,不调用模型。"""
import json
import datetime as dt
import config
import plan_versions as pv
def inspect(root, now=None):
try:
p = pv.load(root)
state = pv.health(root, p['date'])
result = {'data_date': p['date'], 'decision': p.get('decision'),
'generated_at': p.get('generated_at'), 'refresh': state,
'rows': sum(len(p.get(k) or []) for k in ('main', 'observe'))}
if not p.get('decision'):
return 1, dict(result, error='旧快照,追溯不完整')
archived = pv.load(root, decision_id=p['decision']['id'])
if archived != p:
return 2, dict(result, error='最新入口与不可变版本不一致')
from plan_refresh import in_window, BJ
current = now or dt.datetime.now(dt.timezone.utc)
if in_window(current):
cutoff = dt.datetime.fromisoformat(p['decision']['cutoff_at'])
checked = dt.datetime.fromisoformat(state.get('checked_at') or p['generated_at'])
if cutoff.astimezone(BJ).date() != current.astimezone(BJ).date():
return 1, dict(result, error='今日晨版尚未发布')
if (current - checked).total_seconds() > 12 * 60:
return 1, dict(result, error='修订检查超过十二分钟未更新')
return (1 if state.get('error') or state.get('pending_codes') else 0), result
except (OSError, ValueError, KeyError, ImportError, TypeError) as e:
return 2, {'error': str(e)}
if __name__ == '__main__':
status, result = inspect(config.PLAN_SNAPSHOT_DIR)
print(json.dumps(result, ensure_ascii=False, indent=2))
raise SystemExit(status)