88 lines
3.3 KiB
Python
88 lines
3.3 KiB
Python
#!/usr/bin/env python3
|
|
"""按预期流程检查长跑日志中的 FLOW_FLAG 命中情况。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import re
|
|
from pathlib import Path
|
|
|
|
|
|
FLAG_PATTERNS = {
|
|
"run_once_start": r"\[FLOW_FLAG\] run_once_basic:start",
|
|
"room_map_start": r"\[FLOW_FLAG\] room_map_record:start",
|
|
"room_map_written": r"\[FLOW_FLAG\] room_map_record:written",
|
|
"stage_dispatch_enter": r"\[FLOW_FLAG\] stage_dispatch:enter",
|
|
"fudai_find_open_start": r"\[FLOW_FLAG\] fudai_find_open:start",
|
|
"fudai_contain_start": r"\[FLOW_FLAG\] fudai_contain:start",
|
|
"fudai_ocr_parallel_start": r"\[FLOW_FLAG\] fudai_contain_ocr:parallel_start",
|
|
"fudai_ocr_parallel_done": r"\[FLOW_FLAG\] fudai_contain_ocr:parallel_done",
|
|
}
|
|
|
|
OPTIONAL_PATTERNS = {
|
|
"find_open_success": r"\[FLOW_FLAG\] fudai_find_open:open_success",
|
|
"find_open_not_found": r"\[FLOW_FLAG\] fudai_find_open:not_found",
|
|
"skip_filter_skip": r"\[FLOW_FLAG\] skip_filter:skip",
|
|
"skip_filter_pass": r"\[FLOW_FLAG\] skip_filter:pass",
|
|
"join_decision_start": r"\[FLOW_FLAG\] join_decision:start",
|
|
"join_decision_reservation_switch_done": r"\[FLOW_FLAG\] join_decision:reservation_switch_done",
|
|
"same_room_streak_trigger_reset_to_top": r"\[FLOW_FLAG\] same_room_streak:trigger_reset_to_top",
|
|
"room_map_merge_duplicates": r"\[FLOW_FLAG\] room_map_record:merge_duplicates",
|
|
}
|
|
|
|
|
|
def count_hits(text: str, pattern: str) -> int:
|
|
return len(re.findall(pattern, text))
|
|
|
|
|
|
def main() -> int:
|
|
ap = argparse.ArgumentParser(description="检查长跑日志中的 FLOW_FLAG 命中情况")
|
|
ap.add_argument("logfile", type=Path, help="日志文件路径")
|
|
ap.add_argument("--min-run-once", type=int, default=2, help="run_once_basic:start 最少命中次数")
|
|
args = ap.parse_args()
|
|
|
|
if not args.logfile.exists():
|
|
print(json.dumps({"ok": False, "error": f"logfile not found: {args.logfile}"}, ensure_ascii=False))
|
|
return 2
|
|
|
|
text = args.logfile.read_text(encoding="utf-8", errors="replace")
|
|
|
|
required_counts = {k: count_hits(text, v) for k, v in FLAG_PATTERNS.items()}
|
|
optional_counts = {k: count_hits(text, v) for k, v in OPTIONAL_PATTERNS.items()}
|
|
|
|
failures: list[str] = []
|
|
if required_counts["run_once_start"] < int(args.min_run_once):
|
|
failures.append(
|
|
f"run_once_start<{args.min_run_once} (actual={required_counts['run_once_start']})"
|
|
)
|
|
for k, n in required_counts.items():
|
|
if k == "run_once_start":
|
|
continue
|
|
if n <= 0:
|
|
failures.append(f"{k}=0")
|
|
|
|
# 至少出现一种 find_open 结果
|
|
if optional_counts["find_open_success"] <= 0 and optional_counts["find_open_not_found"] <= 0:
|
|
failures.append("no find_open outcome (success/not_found)")
|
|
|
|
# 至少出现一种 skip_filter 结果
|
|
if optional_counts["skip_filter_skip"] <= 0 and optional_counts["skip_filter_pass"] <= 0:
|
|
failures.append("no skip_filter outcome (skip/pass)")
|
|
|
|
ok = len(failures) == 0
|
|
payload = {
|
|
"ok": ok,
|
|
"logfile": str(args.logfile),
|
|
"required_counts": required_counts,
|
|
"optional_counts": optional_counts,
|
|
"failures": failures,
|
|
}
|
|
print(json.dumps(payload, ensure_ascii=False, indent=2))
|
|
return 0 if ok else 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|
|
|