| |
| """ |
| 自动生成 README 中的统计 badge 和表格 |
| |
| 用法: |
| python gen_stats.py # 只打印统计 |
| python gen_stats.py --update-readme # 更新 README.md 中的统计部分 |
| """ |
|
|
| import json |
| import re |
| import argparse |
| from collections import Counter |
| from pathlib import Path |
|
|
| DATA_FILE = Path(__file__).parent / "data.jsonl" |
| README_FILE = Path(__file__).parent / "README.md" |
|
|
| LAYER_ORDER = ["混沌工程层", "基础设施层", "可观测层", "中间件层", "跨云与级联层", "应用层"] |
| LAYER_COLORS = { |
| "混沌工程层": "red", |
| "基础设施层": "blue", |
| "可观测层": "purple", |
| "中间件层": "orange", |
| "跨云与级联层": "yellow", |
| "应用层": "green", |
| } |
| DIFF_ORDER = ["easy", "medium", "hard"] |
| DIFF_COLORS = {"easy": "brightgreen", "medium": "orange", "hard": "red"} |
|
|
|
|
| def load_entries(): |
| entries = [] |
| with open(DATA_FILE) as f: |
| for line in f: |
| if line.strip(): |
| entries.append(json.loads(line)) |
| return entries |
|
|
|
|
| LAYERS = {"基础设施层", "应用层", "中间件层", "可观测层", "跨云与级联层", "混沌工程层"} |
| SUB_CATEGORIES = { |
| "容器与工作负载", "节点与系统资源", "存储卷与持久化", "网络与DNS", |
| "配置与密钥", "发布与回滚", "服务发现与入口", "探针与健康检查", "应用运行时", |
| "缓存与Redis", "数据库", "消息队列", "CDN与网关", |
| "监控与指标", "告警与路由", "日志与采集", "链路追踪与RUM", |
| "多云一致性", "级联故障", "灾备与切换", |
| "故障注入设计", "多故障与干扰", "题面与证据", |
| } |
| DIFFICULTIES = {"easy", "medium", "hard"} |
| SCOPES = {"single-cloud", "multi-cloud"} |
|
|
|
|
| def parse_tags(entry): |
| """Parse tags from list format: [layer, sub_category, faults..., difficulty, scope].""" |
| tags = entry["tags"] |
| if isinstance(tags, dict): |
| return tags |
| |
| layer = [t for t in tags if t in LAYERS] |
| sub_cat = [t for t in tags if t in SUB_CATEGORIES] |
| diff = [t for t in tags if t in DIFFICULTIES] |
| scope = [t for t in tags if t in SCOPES] |
| faults = [t for t in tags if t not in LAYERS and t not in SUB_CATEGORIES and t not in DIFFICULTIES and t not in SCOPES] |
| return { |
| "layer": layer[0] if layer else "", |
| "sub_category": sub_cat[0] if sub_cat else "", |
| "faults": faults, |
| "difficulty": diff[0] if diff else "", |
| "scope": scope[0] if scope else "", |
| } |
|
|
|
|
| def compute_stats(entries): |
| parsed = [parse_tags(e) for e in entries] |
| layer_counts = Counter(p["layer"] for p in parsed) |
| sub_counts = Counter(p["sub_category"] for p in parsed) |
| fault_counts = Counter() |
| for p in parsed: |
| for fault in p.get("faults", []): |
| fault_counts[fault] += 1 |
| diff_counts = Counter(p["difficulty"] for p in parsed) |
| return layer_counts, sub_counts, fault_counts, diff_counts |
|
|
|
|
| def build_stats_section(entries): |
| layer_counts, sub_counts, fault_counts, diff_counts = compute_stats(entries) |
| total = len(entries) |
|
|
| lines = [] |
| lines.append(f"## Dataset Statistics\n") |
| lines.append(f"**Total: {total} challenges**\n") |
|
|
| |
| lines.append(f"### By Layer\n") |
| badge_line = " ".join( |
| f"}-{LAYER_COLORS[layer]})" |
| for layer in LAYER_ORDER |
| if layer_counts.get(layer, 0) > 0 |
| ) |
| lines.append(badge_line + "\n") |
|
|
| |
| lines.append(f"\n### By Sub-category\n") |
| lines.append("| Sub-category | Count |") |
| lines.append("|---|---|") |
| for sub, count in sub_counts.most_common(): |
| lines.append(f"| {sub} | {count} |") |
|
|
| |
| lines.append(f"\n### By Difficulty\n") |
| badge_line = " ".join( |
| f"}-{DIFF_COLORS[diff]})" |
| for diff in DIFF_ORDER |
| if diff_counts.get(diff, 0) > 0 |
| ) |
| lines.append(badge_line + "\n") |
|
|
| |
| lines.append(f"\n### Top Faults\n") |
| lines.append("| Fault | Count |") |
| lines.append("|---|---|") |
| for fault, count in fault_counts.most_common(10): |
| lines.append(f"| {fault} | {count} |") |
|
|
| return "\n".join(lines) |
|
|
|
|
| def update_readme(): |
| readme = README_FILE.read_text() |
| stats_section = build_stats_section(load_entries()) |
|
|
| |
| start_marker = "<!-- STATS_START -->" |
| end_marker = "<!-- STATS_END -->" |
|
|
| if start_marker in readme and end_marker in readme: |
| pattern = re.compile( |
| re.escape(start_marker) + r".*?" + re.escape(end_marker), |
| re.DOTALL, |
| ) |
| new_content = f"{start_marker}\n{stats_section}\n{end_marker}" |
| readme = pattern.sub(new_content, readme) |
| else: |
| |
| usage_idx = readme.find("## Usage") |
| if usage_idx > 0: |
| readme = readme[:usage_idx] + f"{start_marker}\n{stats_section}\n{end_marker}\n\n" + readme[usage_idx:] |
| else: |
| readme += f"\n{start_marker}\n{stats_section}\n{end_marker}\n" |
|
|
| README_FILE.write_text(readme) |
| print(f"README.md updated with stats for {len(load_entries())} challenges") |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser() |
| parser.add_argument("--update-readme", action="store_true", help="Update README.md") |
| args = parser.parse_args() |
|
|
| if args.update_readme: |
| update_readme() |
| else: |
| entries = load_entries() |
| layer_counts, sub_counts, fault_counts, diff_counts = compute_stats(entries) |
| print(f"Total: {len(entries)} challenges\n") |
| print("Layer distribution:") |
| for l in LAYER_ORDER: |
| print(f" {l}: {layer_counts.get(l, 0)}") |
| print("\nSub-category distribution:") |
| for s, c in sub_counts.most_common(): |
| print(f" {s}: {c}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|