tradingSystem/scripts/check_business_evidence.py

136 lines
6.4 KiB
Python
Raw Permalink 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.

"""离线核对历史准备覆盖或任务子结果;不连接服务、不触发补算或交易。"""
import argparse
import datetime as dt
import json
from pathlib import Path
import re
from zoneinfo import ZoneInfo
TZ = ZoneInfo("Asia/Shanghai")
PARTS = ("minute_archive", "minute_fund", "daily_fund")
def count(value):
if type(value) is not int or value < 0:
raise ValueError("覆盖数和历史天数必须是非负整数")
return value
def timestamp(value):
result = dt.datetime.fromisoformat(value)
if result.tzinfo is None:
raise ValueError("时间缺少时区")
return result.astimezone(TZ)
def failure_paths(value, path="$", depth=0):
"""检查明确失败字段,保留路径,不输出可能含凭据的错误正文。"""
if depth > 40:
return [path + ":too_deep"]
failures = []
if isinstance(value, dict):
for key, child in value.items():
where = path + "." + str(key)
if ((key in ("ok", "success") and child is False)
or ((key in ("error", "errors", "invalid", "failed") or str(key).endswith(("_error", "_errors"))) and bool(child))
or (path.endswith(".sources") and child is False)
or (key == "status" and child in ("error", "failed", "FAILURE"))):
failures.append(where)
failures.extend(failure_paths(child, where, depth + 1))
elif isinstance(value, list):
for i, child in enumerate(value):
failures.extend(failure_paths(child, f"{path}[{i}]", depth + 1))
return sorted(set(failures))
def check_task(value):
if not isinstance(value, (dict, list)) or not value:
return {"status": "INCOMPLETE", "failure_paths": ["$:empty_or_invalid_result"]}
paths = failure_paths(value)
return {"status": "FAILED" if paths else "NO_REPORTED_FAILURE",
"failure_paths": paths,
"limit": "无显式失败不等于业务完成;仍须核对预期输出、覆盖与自然回执。"}
def check_preparation(doc, now, max_age=300):
"""只判断历史准备层,绝不把它提升为实时信号或交易可用。"""
if not isinstance(doc, dict) or doc.get("schema") != "itd-preparation-evidence-v1":
raise ValueError("不支持的证据格式")
if now.tzinfo is None or not 0 < max_age <= 86400:
raise ValueError("核验时间须带时区,证据有效秒数须在1至86400之间")
now = now.astimezone(TZ)
issues = []
for field, age_limit in (("collected_at", max_age), ("universe_at", max_age)):
age = (now - timestamp(doc[field])).total_seconds()
if not 0 <= age <= age_limit:
issues.append(field + ":stale_or_future")
if doc.get("day") != now.date().isoformat():
issues.append("day:mismatch")
if doc.get("collection_errors"):
issues.append("collection:incomplete")
codes = doc.get("expected_codes")
if not isinstance(codes, list) or not codes:
raise ValueError("缺少独立的应覆盖名单,不能把空集当作全部通过")
if any(not isinstance(c, str) or not re.fullmatch(r"(?:60\d{4}\.SH|68\d{4}\.SH|00\d{4}\.SZ|30\d{4}\.SZ)", c) for c in codes):
raise ValueError("应覆盖名单必须在入口规范化并限定沪深股票")
if len(codes) != len(set(codes)):
raise ValueError("应覆盖名单存在重复代码")
stocks = doc.get("stocks")
if not isinstance(stocks, dict):
raise ValueError("缺少逐股证据")
lanes = {p: {"expected": len(codes), "ready": 0, "unready": {}} for p in PARTS}
for code in codes:
row = stocks.get(code, {})
if not isinstance(row, dict):
raise ValueError("逐股证据类型错误")
for part in PARTS:
p = row.get("parts", {}).get(part, {})
e = row.get("effective", {})
reasons = []
if p.get("state") != "ready":
reasons.append("preparation_not_ready")
if part in row.get("errors", {}) or "build" in row.get("errors", {}):
reasons.append("preparation_error")
if row.get("day") != doc["day"] or row.get("loaded") is not True:
reasons.append("cache_not_accepted")
if part == "minute_fund":
if count(p.get("days", 0)) < 30:
reasons.append("history_days_below_30")
if count(e.get("money_flow_minutes", 0)) < 200:
reasons.append("effective_money_flow_minutes_below_200")
elif part == "minute_archive":
if count(e.get("volume_minutes", 0)) < 200 or count(e.get("qrs_minutes", 0)) < 180:
reasons.append("effective_price_volume_coverage_insufficient")
elif count(p.get("days", 0)) < 30 or e.get("daily_fund_present") is not True:
reasons.append("effective_daily_fund_missing")
if reasons:
lanes[part]["unready"][code] = reasons
else:
lanes[part]["ready"] += 1
return {"status": "READY" if not issues and all(x["ready"] == x["expected"] for x in lanes.values()) else "INCOMPLETE",
"scope": "historical_preparation_only", "day": doc["day"],
"issues": issues, "lanes": lanes,
"limit": "只读重建的历史准备资格;未验证进程内存、当日实时输入、实际计算、发布和消费。"}
def main(argv=None):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("kind", choices=("preparation", "task"))
parser.add_argument("file", type=Path)
parser.add_argument("--at", help="历史重放时间,必须带时区;省略则核验当前证据")
parser.add_argument("--max-age", type=int, default=300)
args = parser.parse_args(argv)
try:
value = json.loads(args.file.read_text())
result = (check_task(value) if args.kind == "task" else check_preparation(
value, timestamp(args.at) if args.at else dt.datetime.now(TZ), args.max_age))
except (OSError, ValueError, TypeError, KeyError, AttributeError):
print(json.dumps({"status": "INVALID_EVIDENCE", "reason": "输入文件、字段类型或时间格式不符合契约"}, ensure_ascii=False))
return 2
print(json.dumps(result, ensure_ascii=False, indent=2))
return 0 if result["status"] in ("READY", "NO_REPORTED_FAILURE") else 1
if __name__ == "__main__":
raise SystemExit(main())