refactor: 提取 BoundChecker 上下限检测组件
从 ExpBase 提取 detect_up_down() 逻辑和哨兵值处理至独立的 BoundChecker 类。 将 DetectMode 从 struct 升级为 enum class。
This commit is contained in:
parent
c4bcb6610b
commit
b9cf5f4e9e
@ -336,19 +336,19 @@ AlarmInfo ExpBase::mon_proc() {
|
||||
SingletonTemp<EqpStat>::GetInstance().add_stat_values(
|
||||
rule_id_, result_value);
|
||||
}
|
||||
if (detect_up_down(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(limit_up_) + "] ms";
|
||||
"ms,时间范围:[0," + DAA::double2str(bound_checker_.limitUp()) + "] ms";
|
||||
} else {
|
||||
msg = error_str_ + ":" + DAA::double2str(result_value) + unit_ +
|
||||
",合理区间:[" + DAA::double2strLimit(limit_down_) + "," +
|
||||
DAA::double2strLimit(limit_up_) + "]" + unit_;
|
||||
",合理区间:[" + DAA::double2strLimit(bound_checker_.limitDown()) + "," +
|
||||
DAA::double2strLimit(bound_checker_.limitUp()) + "]" + unit_;
|
||||
}
|
||||
return utility::build_alarm_info(
|
||||
utility::get_msg_level(limit_down_, limit_up_, result_value),
|
||||
utility::get_msg_level(bound_checker_.limitDown(), bound_checker_.limitUp(), result_value),
|
||||
rule_id_, rule_name_, "EXP4", msg, get_alarm_time());
|
||||
}
|
||||
} else {
|
||||
@ -382,11 +382,11 @@ AlarmInfo ExpBase::mon_proc() {
|
||||
// 不是动作反馈
|
||||
else {
|
||||
if (exp_type_ == ExpType::Bound) {
|
||||
if (filter_flag_ == true && this->detect_up_down(result_value)) {
|
||||
if (filter_flag_ == true && bound_checker_.isOutOfBounds(result_value)) {
|
||||
rule_stat_.alarm_value = result_value;
|
||||
auto msg = error_str_ + ":" + DAA::double2str(result_value) + unit_ +
|
||||
",合理区间:[" + DAA::double2strLimit(limit_down_) + "," +
|
||||
DAA::double2strLimit(limit_up_) + "]" + unit_;
|
||||
",合理区间:[" + DAA::double2strLimit(bound_checker_.limitDown()) + "," +
|
||||
DAA::double2strLimit(bound_checker_.limitUp()) + "]" + unit_;
|
||||
logger_->Debug() << msg << endl;
|
||||
this->query_time_range_.set_left(query_time_range_.get_right() -
|
||||
delay_time_);
|
||||
@ -399,7 +399,7 @@ AlarmInfo ExpBase::mon_proc() {
|
||||
else if (exp_type_ == ExpType::BoundHoldTime) {
|
||||
|
||||
bool is_over_up_down =
|
||||
this->detect_up_down(result_value);
|
||||
bound_checker_.isOutOfBounds(result_value);
|
||||
if (!filter_flag_) {
|
||||
/*前提条件不满足*/
|
||||
act_start_time_ = this->now_time_;
|
||||
@ -420,8 +420,8 @@ AlarmInfo ExpBase::mon_proc() {
|
||||
rule_stat_.alarm_value = result_value;
|
||||
auto msg = error_str_ + ":" + DAA::double2str(result_value) +
|
||||
unit_ + ",合理区间:[" +
|
||||
DAA::double2strLimit(limit_down_) + "," +
|
||||
DAA::double2strLimit(limit_up_) + "]" + unit_;
|
||||
DAA::double2strLimit(bound_checker_.limitDown()) + "," +
|
||||
DAA::double2strLimit(bound_checker_.limitUp()) + "]" + unit_;
|
||||
expr_engine_->printVars();
|
||||
this->query_time_range_.set_left(query_time_range_.get_right() -
|
||||
delay_time_);
|
||||
@ -430,7 +430,7 @@ AlarmInfo ExpBase::mon_proc() {
|
||||
this->now_time_;
|
||||
act_started_ = false;
|
||||
return utility::build_alarm_info(
|
||||
utility::get_msg_level(limit_down_, limit_up_, result_value),
|
||||
utility::get_msg_level(bound_checker_.limitDown(), bound_checker_.limitUp(), result_value),
|
||||
rule_id_, rule_name_, "EXP5", msg, this->get_alarm_time());
|
||||
}
|
||||
} else {
|
||||
@ -735,10 +735,11 @@ int ExpBase::reload_config_up_down() {
|
||||
(int)limit_up_ == (int)limit_down_) {
|
||||
this->detect_mode_ = DetectMode::ErrorMode;
|
||||
}
|
||||
bound_checker_.setLimits(limit_down_, limit_up_);
|
||||
///共享内存参数
|
||||
logger_->Info() << rule_name_
|
||||
<< ",detect_mode_[0-双侧,1-仅left;2-仅right;3-错误]:"
|
||||
<< detect_mode_ << ",limit_down:" << limit_down_ << ","
|
||||
<< static_cast<int>(bound_checker_.detectMode()) << ",limit_down:" << limit_down_ << ","
|
||||
<< "limit_up:" << limit_up_ << std::endl;
|
||||
this->rule_stat_.limit_down = limit_down_;
|
||||
this->rule_stat_.limit_up = limit_up_;
|
||||
@ -819,6 +820,7 @@ int ExpBase::reload_ci_dist() {
|
||||
}
|
||||
this->limit_down_ = dist_range.get_left();
|
||||
this->limit_up_ = dist_range.get_right();
|
||||
bound_checker_.setLimits(limit_down_, limit_up_);
|
||||
this->rule_stat_.limit_down = limit_down_;
|
||||
this->rule_stat_.limit_up = limit_up_;
|
||||
this->rule_stat_.current_value =
|
||||
|
||||
@ -21,6 +21,7 @@
|
||||
#include <eqpalg/utility/HoldTime.h>
|
||||
#include <eqpalg/utility/StatExp.hpp>
|
||||
#include <eqpalg/utility/item2chinese.hpp>
|
||||
#include <eqpalg/utility/bound_checker.h>
|
||||
#include <eqpalg/utility/fb_state_machine.h>
|
||||
#include <glob/SingletonTemplate.h>
|
||||
#include <iomanip>
|
||||
@ -193,7 +194,9 @@ protected:
|
||||
|
||||
std::string sample_result_;
|
||||
|
||||
int detect_mode_ = DetectMode::Default;
|
||||
DetectMode detect_mode_ = DetectMode::Default;
|
||||
|
||||
BoundChecker bound_checker_;
|
||||
|
||||
bool filter_flag_ = false;
|
||||
|
||||
|
||||
@ -57,15 +57,7 @@ struct DistMode {
|
||||
static const int Online = 1;
|
||||
static const int Offline = 2;
|
||||
};
|
||||
/**
|
||||
* @brief 检测模式
|
||||
*/
|
||||
struct DetectMode {
|
||||
static const int Default = 0;
|
||||
static const int OnlyLeft = 1;
|
||||
static const int OnlyRight = 2;
|
||||
static const int ErrorMode = 3;
|
||||
};
|
||||
// DetectMode 已提取至 eqpalg/utility/bound_checker.h(enum class)
|
||||
/**
|
||||
* @brief 规则运行前提条件
|
||||
*/
|
||||
|
||||
34
eqpalg/utility/bound_checker.cpp
Normal file
34
eqpalg/utility/bound_checker.cpp
Normal file
@ -0,0 +1,34 @@
|
||||
// eqpalg/utility/bound_checker.cpp
|
||||
#include <eqpalg/utility/bound_checker.h>
|
||||
|
||||
void BoundChecker::setLimits(double down, double up) {
|
||||
limit_down_ = down;
|
||||
limit_up_ = up;
|
||||
|
||||
// 哨兵值推导检测模式(与 ExpBase::reload_config_up_down 逻辑一致)
|
||||
int idown = static_cast<int>(down);
|
||||
int iup = static_cast<int>(up);
|
||||
|
||||
if (idown == -32768 && iup != idown) {
|
||||
detect_mode_ = DetectMode::OnlyRight; // 仅右边界
|
||||
} else if ((iup == -32768 || iup == 32768 || iup == 32767) && iup != idown) {
|
||||
detect_mode_ = DetectMode::OnlyLeft; // 仅左边界
|
||||
} else if (iup == -32768 && iup == idown) {
|
||||
detect_mode_ = DetectMode::ErrorMode; // 配置错误
|
||||
} else {
|
||||
detect_mode_ = DetectMode::Default; // 双侧
|
||||
}
|
||||
}
|
||||
|
||||
bool BoundChecker::isOutOfBounds(double value) const {
|
||||
switch (detect_mode_) {
|
||||
case DetectMode::Default:
|
||||
return value < limit_down_ || value > limit_up_;
|
||||
case DetectMode::OnlyLeft:
|
||||
return value < limit_down_;
|
||||
case DetectMode::OnlyRight:
|
||||
return value > limit_up_;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
56
eqpalg/utility/bound_checker.h
Normal file
56
eqpalg/utility/bound_checker.h
Normal file
@ -0,0 +1,56 @@
|
||||
// eqpalg/utility/bound_checker.h
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* @brief 检测模式:双侧、仅左侧、仅右侧
|
||||
*/
|
||||
enum class DetectMode {
|
||||
Default = 0, // 双侧检测 (value < down || value > up)
|
||||
OnlyLeft = 1, // 仅检测左边界 (value < down)
|
||||
OnlyRight = 2, // 仅检测右边界 (value > up)
|
||||
ErrorMode = 3 // 错误模式
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 上下限检测器
|
||||
*
|
||||
* 从 ExpBase 提取,封装哨兵值 -32768/32767 的处理逻辑。
|
||||
*/
|
||||
class BoundChecker {
|
||||
public:
|
||||
BoundChecker() = default;
|
||||
|
||||
/**
|
||||
* @brief 设置上下限,自动推导检测模式
|
||||
* @param down 下限(-32768 表示无下限)
|
||||
* @param up 上限(32767/32768 表示无上限)
|
||||
*/
|
||||
void setLimits(double down, double up);
|
||||
|
||||
/**
|
||||
* @brief 手动设置检测模式(覆盖自动推导)
|
||||
*/
|
||||
void setDetectMode(DetectMode mode) { detect_mode_ = mode; }
|
||||
|
||||
/**
|
||||
* @brief 判断 value 是否超出限值
|
||||
*/
|
||||
bool isOutOfBounds(double value) const;
|
||||
|
||||
// 访问器
|
||||
double limitDown() const { return limit_down_; }
|
||||
double limitUp() const { return limit_up_; }
|
||||
DetectMode detectMode() const { return detect_mode_; }
|
||||
|
||||
/**
|
||||
* @brief 是否已配置有效限值
|
||||
*/
|
||||
bool isValid() const { return detect_mode_ != DetectMode::ErrorMode; }
|
||||
|
||||
private:
|
||||
double limit_down_ = -32768;
|
||||
double limit_up_ = -32768;
|
||||
DetectMode detect_mode_ = DetectMode::Default;
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user