#!/usr/bin/env python3
"""查询管理会计OS各模块数据量"""
import subprocess, json, sys

# 登录获取token
r = subprocess.run(
    "curl -s -X POST http://127.0.0.1:8010/api/cma/auth/login -H 'Content-Type: application/json' -d '{\"username\":\"admin\",\"password\":\"admin123\"}'",
    shell=True, capture_output=True, text=True, timeout=10
)
login = json.loads(r.stdout)
token = login['token']

endpoints = [
    ("KPI定义", "/api/cma/kpis"),
    ("战略地图", "/api/cma/maps"),
    ("预警记录", "/api/cma/alerts"),
    ("用户列表", "/api/cma/users"),
    ("数据源配置", "/api/cma/data/sources"),
    ("通知渠道", "/api/cma/notifications/channels"),
    ("预警规则", "/api/cma/alert-rules"),
    ("行动计划", "/api/cma/action-plans"),
    ("dashboard/KPI", "/api/cma/dashboard/kpis"),
    ("dashboard/财务分析", "/api/cma/dashboard/finance-analysis"),
    ("dashboard/总览", "/api/cma/dashboard/summary"),
    ("dashboard/预测", "/api/cma/dashboard/predict"),
    ("权限配置", "/api/cma/permissions/config"),
    ("权限模块", "/api/cma/permissions/modules"),
]

for name, path in endpoints:
    cmd = f"curl -s '{path}' -H 'Authorization: Bearer {token}'"
    r = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=15)
    try:
        data = json.loads(r.stdout)
        if isinstance(data, list):
            print(f"  {name:25s} → {len(data)} 条记录")
        elif isinstance(data, dict):
            if 'detail' in data:
                print(f"  {name:25s} → ❌ {str(data['detail'])[:50]}")
            else:
                # 尝试找列表字段
                found = False
                for k in ['data', 'items', 'records', 'modules', 'actions', 'roles', 'channels']:
                    if k in data and isinstance(data[k], list):
                        print(f"  {name:25s} → {len(data[k])} 条 ({k})")
                        found = True
                        break
                if not found:
                    preview = {k: str(v)[:60] for k, v in list(data.items())[:4]}
                    print(f"  {name:25s} → dict: {preview}")
    except:
        print(f"  {name:25s} → ⚠️ 解析失败: {r.stdout[:100]}")
