<?php
header("Content-Type:application/json;charset=utf-8");
// 改为宿主机真实内网IP，不要写localhost/mysql容器名
$host = "10.150.117.190";
$user = "root";
$pass = "hp93000";
$dbname = "alert_mail_stat";
$conn = mysqli_connect($host,$user,$pass,$dbname);
mysqli_set_charset($conn,"utf8mb4");
$act = $_GET['act'] ?? '';

// 1. 获取今日总统计：触发次数、恢复次数、故障实例数
if($act == "day_total"){
    $today = date("Y-m-d");
    $sql = "SELECT
        SUM(IF(alert_type=1,1,0)) as fire_count,
        SUM(IF(alert_type=2,1,0)) as resolve_count,
        COUNT(DISTINCT instance) as instance_num
        FROM alert_log WHERE DATE(receive_time) = '$today'";
    $res = mysqli_fetch_assoc(mysqli_query($conn,$sql));
    echo json_encode($res,JSON_UNESCAPED_UNICODE);
}

// 2. 今日告警TOP排行（按发生次数）
if($act == "top_alert"){
    $today = date("Y-m-d");
    $sql = "SELECT alert_name,COUNT(*) cnt FROM alert_log WHERE DATE(receive_time)='$today' GROUP BY alert_name ORDER BY cnt DESC LIMIT 10";
    $data = [];
    $q = mysqli_query($conn,$sql);
    while($r = mysqli_fetch_assoc($q)) $data[] = $r;
    echo json_encode($data,JSON_UNESCAPED_UNICODE);
}

// 3. 今日明细列表（前端表格）
if($act == "log_list"){
    $today = date("Y-m-d");
    $sql = "SELECT alert_type,alert_name,instance,severity,starts_at,ends_at,receive_time
    FROM alert_log WHERE DATE(receive_time)='$today' ORDER BY receive_time DESC";
    $data = [];
    $q = mysqli_query($conn,$sql);
    while($r = mysqli_fetch_assoc($q)) $data[] = $r;
    echo json_encode($data,JSON_UNESCAPED_UNICODE);
}

mysqli_close($conn);
?>
