feat: detect-batch adds --json output and --algo single-algo mode
This commit is contained in:
+39
-5
@@ -35,18 +35,49 @@ def cmd_detect_batch(args):
|
||||
"""detect-batch 子命令:多算法 ANOMALY_WINDOW。"""
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
||||
|
||||
algos = args.algos.split(",") if args.algos else ALL_ALGOS
|
||||
# 确定算法列表(--algo 优先于 --algos)
|
||||
if args.algo:
|
||||
algos = [args.algo]
|
||||
elif args.algos:
|
||||
algos = args.algos.split(",")
|
||||
else:
|
||||
algos = ALL_ALGOS
|
||||
|
||||
conn = TDConnection(args.url)
|
||||
try:
|
||||
db = args.db or "ts_anomaly"
|
||||
conn.execute_no_result(f"USE {db}")
|
||||
results = detect_all_algos(conn, args.stable, algos=algos)
|
||||
for algo, r in results.items():
|
||||
if r["error"]:
|
||||
print(f" {algo}: ERROR - {r['error']}")
|
||||
|
||||
if getattr(args, 'json_output', False):
|
||||
import json
|
||||
if len(algos) == 1:
|
||||
algo_name = algos[0]
|
||||
r = results[algo_name]
|
||||
output = {
|
||||
"algo": algo_name,
|
||||
"stable": args.stable,
|
||||
"windows": r["windows"],
|
||||
"window_count": len(r["windows"]),
|
||||
"error": r["error"],
|
||||
}
|
||||
else:
|
||||
print(f" {algo}: {len(r['windows'])} windows")
|
||||
output = []
|
||||
for algo_name, r in results.items():
|
||||
output.append({
|
||||
"algo": algo_name,
|
||||
"stable": args.stable,
|
||||
"windows": r["windows"],
|
||||
"window_count": len(r["windows"]),
|
||||
"error": r["error"],
|
||||
})
|
||||
print(json.dumps(output, ensure_ascii=False))
|
||||
else:
|
||||
for algo, r in results.items():
|
||||
if r["error"]:
|
||||
print(f" {algo}: ERROR - {r['error']}")
|
||||
else:
|
||||
print(f" {algo}: {len(r['windows'])} windows")
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
@@ -134,6 +165,7 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
p_inj.add_argument("--csv", required=True, help="CSV 文件路径")
|
||||
p_inj.add_argument("--stable", required=True, help="supertable 名称")
|
||||
p_inj.add_argument("--db", default="ts_anomaly", help="数据库名 (default: ts_anomaly)")
|
||||
p_inj.add_argument("--value-col", default="value", help="数值列名 (default: value)")
|
||||
p_inj.add_argument("--url", default="ws://root:taosdata@localhost:6041", help="TDengine WebSocket URL")
|
||||
|
||||
# detect-batch
|
||||
@@ -141,6 +173,8 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
p_det.add_argument("--stable", required=True, help="supertable 名称")
|
||||
p_det.add_argument("--db", default="ts_anomaly", help="数据库名")
|
||||
p_det.add_argument("--algos", default=None, help="逗号分隔算法列表 (默认全 6 个)")
|
||||
p_det.add_argument("--algo", default=None, help="单算法模式(优先于 --algos)")
|
||||
p_det.add_argument("--json", action="store_true", dest="json_output", help="输出 JSON 到 stdout")
|
||||
p_det.add_argument("--url", default="ws://root:taosdata@localhost:6041", help="TDengine WebSocket URL")
|
||||
|
||||
# forecast-anomaly
|
||||
|
||||
Reference in New Issue
Block a user