#!/usr/bin/env bash
# CMA预警检查 — 通过API端点执行
set -euo pipefail

cd /root/cma-management/backend

# 通过HTTP API执行预警检查
RESULT=$(curl -s -X POST http://localhost:8010/api/cma/admin/alerts/check \
  -H "Content-Type: application/json" 2>&1)

echo "[$(date '+%Y-%m-%d %H:%M:%S')] 预警检查结果: $RESULT"

# 验证是否成功
if echo "$RESULT" | grep -q '"new_alerts"'; then
    NEW_ALERTS=$(echo "$RESULT" | python3 -c "import json,sys; print(json.load(sys.stdin).get('result',{}).get('new_alerts','unknown'))" 2>/dev/null || echo "parse_error")
    echo "新增预警数: $NEW_ALERTS"
    exit 0
else
    echo "预警检查失败"
    exit 1
fi
