feat: detect-batch adds --json output and --algo single-algo mode
This commit is contained in:
+35
-1
@@ -35,13 +35,44 @@ def cmd_detect_batch(args):
|
|||||||
"""detect-batch 子命令:多算法 ANOMALY_WINDOW。"""
|
"""detect-batch 子命令:多算法 ANOMALY_WINDOW。"""
|
||||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
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)
|
conn = TDConnection(args.url)
|
||||||
try:
|
try:
|
||||||
db = args.db or "ts_anomaly"
|
db = args.db or "ts_anomaly"
|
||||||
conn.execute_no_result(f"USE {db}")
|
conn.execute_no_result(f"USE {db}")
|
||||||
results = detect_all_algos(conn, args.stable, algos=algos)
|
results = detect_all_algos(conn, args.stable, algos=algos)
|
||||||
|
|
||||||
|
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:
|
||||||
|
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():
|
for algo, r in results.items():
|
||||||
if r["error"]:
|
if r["error"]:
|
||||||
print(f" {algo}: ERROR - {r['error']}")
|
print(f" {algo}: ERROR - {r['error']}")
|
||||||
@@ -134,6 +165,7 @@ def build_parser() -> argparse.ArgumentParser:
|
|||||||
p_inj.add_argument("--csv", required=True, help="CSV 文件路径")
|
p_inj.add_argument("--csv", required=True, help="CSV 文件路径")
|
||||||
p_inj.add_argument("--stable", required=True, help="supertable 名称")
|
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("--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")
|
p_inj.add_argument("--url", default="ws://root:taosdata@localhost:6041", help="TDengine WebSocket URL")
|
||||||
|
|
||||||
# detect-batch
|
# detect-batch
|
||||||
@@ -141,6 +173,8 @@ def build_parser() -> argparse.ArgumentParser:
|
|||||||
p_det.add_argument("--stable", required=True, help="supertable 名称")
|
p_det.add_argument("--stable", required=True, help="supertable 名称")
|
||||||
p_det.add_argument("--db", default="ts_anomaly", help="数据库名")
|
p_det.add_argument("--db", default="ts_anomaly", help="数据库名")
|
||||||
p_det.add_argument("--algos", default=None, help="逗号分隔算法列表 (默认全 6 个)")
|
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")
|
p_det.add_argument("--url", default="ws://root:taosdata@localhost:6041", help="TDengine WebSocket URL")
|
||||||
|
|
||||||
# forecast-anomaly
|
# forecast-anomaly
|
||||||
|
|||||||
Reference in New Issue
Block a user