package com.jay.monitor.data.server.report.alert.rule;

import com.jay.monitor.data.core.model.serializable.SQLMonitorDTO;
import com.jay.monitor.data.core.model.serializable.base.BaseDTO;
import com.jay.monitor.data.server.enums.AlertRuleEnums;
import com.jay.monitor.data.server.models.config.AlertRuleConfigModel;
import com.jay.monitor.data.server.store.ConfigStoreManager;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 影响行数规则处理
 *
 * @author liukx
 */
// @Component
@Deprecated
public class AffectedRowRule implements AlertRule {

    @Autowired
    private ConfigStoreManager configStoreManager;

    @Override
    public AlertRuleEnums ruleType() {
        return AlertRuleEnums.AFFECTED_ROW;
    }

    @Override
    public boolean isTrigger(BaseDTO baseDTO) {
        if (baseDTO instanceof SQLMonitorDTO) {
            SQLMonitorDTO sqlMonitor = (SQLMonitorDTO)baseDTO;
            AlertRuleConfigModel alertConfigModel = configStoreManager.getAlertRuleConfigModel(baseDTO.getType());
            if (alertConfigModel != null) {
                Integer maxResponseCount = alertConfigModel.getMaxResponseCount();
                Integer affectedRows = sqlMonitor.getAffectedRows();
                if (affectedRows != null && affectedRows > maxResponseCount) {
                    return true;
                }
            }
        }
        return false;
    }
}
