-- 告警规则表
CREATE TABLE IF NOT EXISTS tb_polling_alert_rule (
    id BIGSERIAL PRIMARY KEY,
    rule_name VARCHAR(100) NOT NULL,
    task_type VARCHAR(50) NOT NULL,
    metric_type VARCHAR(50) NOT NULL,
    operator VARCHAR(20) NOT NULL,
    threshold DECIMAL(10, 2) NOT NULL,
    duration_minutes INT NOT NULL DEFAULT 5,
    alert_level VARCHAR(20) NOT NULL DEFAULT 'warning',
    notification_channels TEXT,
    notification_config TEXT,
    status SMALLINT NOT NULL DEFAULT 1,
    cooldown_minutes INT NOT NULL DEFAULT 5,
    description TEXT,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    created_by BIGINT,
    updated_by BIGINT
);

-- 索引
CREATE INDEX idx_polling_alert_rule_status ON tb_polling_alert_rule(status);
CREATE INDEX idx_polling_alert_rule_task_type ON tb_polling_alert_rule(task_type);

-- 表注释
COMMENT ON TABLE tb_polling_alert_rule IS '告警规则表 - 定义轮询系统的告警条件和通知方式';

-- 列注释
COMMENT ON COLUMN tb_polling_alert_rule.rule_name IS '规则名称';
COMMENT ON COLUMN tb_polling_alert_rule.task_type IS '任务类型：realname/carddata/package';
COMMENT ON COLUMN tb_polling_alert_rule.metric_type IS '指标类型：queue_size/success_rate/avg_duration/concurrency';
COMMENT ON COLUMN tb_polling_alert_rule.operator IS '比较运算符：gt/lt/gte/lte/eq';
COMMENT ON COLUMN tb_polling_alert_rule.threshold IS '阈值';
COMMENT ON COLUMN tb_polling_alert_rule.duration_minutes IS '持续时长（分钟），避免短暂波动';
COMMENT ON COLUMN tb_polling_alert_rule.alert_level IS '告警级别：info/warning/error/critical';
COMMENT ON COLUMN tb_polling_alert_rule.notification_channels IS '通知渠道（JSON数组）：["email","sms","webhook"]';
COMMENT ON COLUMN tb_polling_alert_rule.notification_config IS '通知配置（JSON）';
COMMENT ON COLUMN tb_polling_alert_rule.status IS '状态：0-禁用，1-启用';
COMMENT ON COLUMN tb_polling_alert_rule.cooldown_minutes IS '冷却期（分钟），避免重复告警';
COMMENT ON COLUMN tb_polling_alert_rule.description IS '规则说明';
COMMENT ON COLUMN tb_polling_alert_rule.created_at IS '创建时间';
COMMENT ON COLUMN tb_polling_alert_rule.updated_at IS '更新时间';
COMMENT ON COLUMN tb_polling_alert_rule.created_by IS '创建人ID';
COMMENT ON COLUMN tb_polling_alert_rule.updated_by IS '更新人ID';
