将 ExpBase::mon_proc() 中的 exp_type_ 分支逻辑提取为 4 个子类,通过 doMonProc() 虚函数实现多态分发: - LogicAlg (exp_type 1):实时逻辑判断 - BoundAlg (exp_type 2):监控变量上下限检测 - BoundHoldAlg (exp_type 5):持续超限检测(继承 BoundAlg) - FeedbackAlg (exp_type 3/4):动作反馈处理 ExpBase 新增纯虚函数 doMonProc() 和钩子函数 doInitExtend(), init() 和 mon_proc() 中所有类型分支替换为虚函数委托调用。
117 lines
4.6 KiB
C++
117 lines
4.6 KiB
C++
#include <eqpalg/algs/feedback_alg.h>
|
|
#include <eqpalg/feature_extraction/daa.h>
|
|
#include <eqpalg/utility/build_alarm_info.h>
|
|
#include <shm/RuleStatShm.h>
|
|
|
|
FeedbackAlg::FeedbackAlg(const std::string& name, const mix_cc::json& rule_json,
|
|
const std::string& ruleId, size_t exp_type)
|
|
: ExpBase(name, rule_json, ruleId, exp_type),
|
|
hasBoundCheck_(exp_type == ExpType::CondBound) {
|
|
logger_.reset(new LOG("FeedbackAlg:" + rule_name_, AUTO_CATCH_PID));
|
|
}
|
|
|
|
FeedbackAlg::~FeedbackAlg() = default;
|
|
|
|
void FeedbackAlg::doInitExtend() {
|
|
if (hasBoundCheck_) {
|
|
reload_config_up_down();
|
|
reload_ci_dist();
|
|
last_load_time_ = std::chrono::system_clock::now();
|
|
stat_collector_.configure(rule_id_, rule_name_, dist_mode_, is_learning_);
|
|
}
|
|
}
|
|
|
|
AlarmInfo FeedbackAlg::doMonProc() {
|
|
double result_value = expr_engine_->evaluate("act");
|
|
|
|
// CondBound: filter + statistics
|
|
if (hasBoundCheck_) {
|
|
bool filter_ok = true;
|
|
try {
|
|
filter_ok = expr_engine_->evaluateBool("feedback");
|
|
} catch (...) {}
|
|
if (is_learning_ && filter_ok) {
|
|
rule_stat_.current_value = result_value;
|
|
SingletonTemp<EqpStat>::GetInstance().add_stat_values(rule_id_, result_value);
|
|
}
|
|
}
|
|
|
|
bool triggered = static_cast<bool>(result_value);
|
|
|
|
// === FbStateMachine ===
|
|
auto [fbState, needFunReset] = fb_fsm_.update(
|
|
triggered, now_time_, expr_engine_->vars(), m_tags.size());
|
|
|
|
if (needFunReset) {
|
|
expr_engine_->markFunVarsNeedReset();
|
|
}
|
|
|
|
if (fbState == FbState::Started) {
|
|
query_time_range_.set_left(now_time_);
|
|
return AlarmInfo{};
|
|
}
|
|
|
|
if (fbState == FbState::NotHold) {
|
|
return AlarmInfo{};
|
|
}
|
|
|
|
if (fbState == FbState::InProgress) {
|
|
bool fbCond = expr_engine_->evaluateBool("feedback");
|
|
bool done = fb_fsm_.checkFeedback(fbCond, now_time_, expr_engine_->vars());
|
|
if (done) {
|
|
query_time_range_.set_left(
|
|
query_time_range_.get_right() -
|
|
std::chrono::milliseconds(static_cast<int>(expr_engine_->vars()["time"])));
|
|
result_value = expr_engine_->evaluate("result");
|
|
expr_engine_->markFunVarsNeedReset();
|
|
rule_stat_.limit_down = bound_checker_.limitDown();
|
|
rule_stat_.limit_up = bound_checker_.limitUp();
|
|
rule_stat_.current_value = result_value;
|
|
|
|
if (hasBoundCheck_) {
|
|
if (is_learning_) {
|
|
SingletonTemp<EqpStat>::GetInstance().add_stat_values(
|
|
rule_id_, result_value);
|
|
}
|
|
if (bound_checker_.isOutOfBounds(result_value)) {
|
|
rule_stat_.alarm_value = result_value;
|
|
std::string msg;
|
|
if (fb_fsm_.isTimeMode()) {
|
|
msg = error_str_ + ":" + DAA::double2str(result_value) +
|
|
"ms,时间范围:[0," + DAA::double2str(bound_checker_.limitUp()) + "] ms";
|
|
} else {
|
|
msg = error_str_ + ":" + DAA::double2str(result_value) + unit_ +
|
|
",合理区间:[" + DAA::double2strLimit(bound_checker_.limitDown()) +
|
|
"," + DAA::double2strLimit(bound_checker_.limitUp()) + "]" + unit_;
|
|
}
|
|
return utility::build_alarm_info(
|
|
utility::get_msg_level(bound_checker_.limitDown(),
|
|
bound_checker_.limitUp(), result_value),
|
|
rule_id_, rule_name_, "EXP4", msg, get_alarm_time());
|
|
}
|
|
} else {
|
|
if (static_cast<bool>(result_value)) {
|
|
rule_stat_.alarm_value = result_value;
|
|
auto msg = rule_name_ + " " + error_str_;
|
|
return utility::build_alarm_info(MsgLevel::ERROR, rule_id_,
|
|
rule_name_, "EXP3", msg,
|
|
get_alarm_time());
|
|
}
|
|
}
|
|
}
|
|
return AlarmInfo{};
|
|
}
|
|
|
|
if (fbState == FbState::Timeout) {
|
|
if (fb_fsm_.isTimeMode()) {
|
|
std::string msg = rule_name_ + " 反馈超时:" +
|
|
std::to_string(time_out_.count()) + " ms";
|
|
return utility::build_alarm_info(MsgLevel::ERROR, rule_id_, rule_name_,
|
|
"EXPACT", msg, query_time_range_);
|
|
}
|
|
return AlarmInfo{};
|
|
}
|
|
|
|
return AlarmInfo{};
|
|
}
|