eis/eqpalg/algs/roller2.cpp

185 lines
6.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <base/BitTool.h>
#include <eqpalg/algs/roller2.h>
#include <eqpalg/exp_macro/get_macro_replaced_exp.h>
#include <eqpalg/feature_extraction/daa.h>
#include <eqpalg/utility/build_alarm_info.h>
#include <algorithm>
#include <limits>
#include <string>
#include <utility>
#include <vector>
#include "mix_cc/ihyper_db.h"
int Roller2::init() {
int res = 0;
try {
res += AlgBase::init();
if (rule_json_.at("output").contains("error")) {
try {
error_str_ =
rule_json_.at("output").at("error").at("value").get<std::string>();
} catch (const std::exception& e) {
logger_->Error() << "output,error出错" << e.what()
<< ",location:" << BOOST_CURRENT_LOCATION << endl;
error_message_str_ += "报警内容格式异常!";
return -1;
}
}
res += expr_engine_->firstFill(data_source_, now_time_, query_time_range_);
res += this->reload_config_data_source(); /*3.数据源*/
res += init_X_exp();
print_load_content();
} catch (const std::exception& e) {
std::throw_with_nested(
mix_cc::Exception(-1, "load error", BOOST_CURRENT_LOCATION));
}
if (res == 0) {
this->exp_is_wrong_ = false;
}
return res;
}
Roller2::Roller2(const string& name, const mix_cc::json& rule_json,
const string& ruleId, size_t exp_type)
: ExpBase(name, rule_json, ruleId, exp_type) {
logger_.reset(new LOG("Roller2:" + rule_name_, AUTO_CATCH_PID));
}
Roller2::~Roller2() {
}
AlarmInfo Roller2::doMonProc() {
return AlarmInfo{}; // 此类重写 mon_proc(),不使用 doMonProc()
}
AlarmInfo Roller2::mon_proc() {
refresh_var_result();
if (!pre_exp_flag_) {
return AlarmInfo{};
}
int Xsize = var_Xdouble_.size();
double avg = 0;
for (auto& exp_value : var_Xdouble_) {
avg += exp_value.second;
}
avg = avg / double(Xsize);
limit_down_ = avg - std::fabs(avg) * limit_over_;
limit_up_ = avg + std::fabs(avg) * limit_over_;
this->rule_stat_.limit_down = limit_down_;
this->rule_stat_.limit_up = limit_up_;
for (auto& exp_value : var_Xdouble_) {
rule_stat_.current_value = exp_value.second;
logger_->Debug() << var_name_[exp_value.first] + ":" +
DAA::double2str(exp_value.second, 3) +
",合理区间:[" + DAA::double2str(limit_down_, 3) +
"," + DAA::double2str(limit_up_, 3) + "]"
<< std::endl;
if (exp_value.second > this->rule_stat_.limit_up ||
exp_value.second < this->rule_stat_.limit_down) {
std::string msg = "";
msg = error_str_ + "," + var_name_[exp_value.first] + ":" +
DAA::double2str(exp_value.second, 3) + ",合理区间:[" +
DAA::double2str(limit_down_, 3) + "," +
DAA::double2str(limit_up_, 3) + "]";
auto alarm_time = query_time_range_;
alarm_time.set_left(alarm_time.get_right() - minutes(2));
return utility::build_alarm_info(
utility::get_msg_level(limit_down_, limit_up_, exp_value.second),
rule_id_, rule_name_, "EXP9", msg, alarm_time);
}
}
return AlarmInfo{};
}
std::vector<AlarmInfo> Roller2::exec_task(mix_cc::time_range_t time_range) {
std::vector<AlarmInfo> result;
for (auto now_time = time_range.get_left();
now_time <= time_range.get_right(); now_time += delay_time_) {
this->now_time_ = now_time;
auto rr1 = this->exec_mon();
result.push_back(rr1);
}
return result;
}
void Roller2::print_load_content() {
int exp_size = var_exp_str_.size();
for (auto& exp_str : var_exp_str_) {
logger_->Debug() << exp_str.first << ":" << exp_str.second << ",";
}
for (auto& name : var_name_) {
logger_->Debug() << name.first << ":" << name.second << ",";
}
logger_->Debug() << std::endl;
}
int Roller2::init_X_exp() {
int res = 0;
for (auto& exp_stri : ExpStr) {
if (rule_json_.at("function").contains(exp_stri)) {
auto tmp_exp =
rule_json_.at("function").at(exp_stri).at("value").get<std::string>();
exp_str_ = get_macro_replaced_exp(tmp_exp);
var_exp_str_[exp_stri] = exp_str_;
feedback_mode_ = true;
int reg_ret = expr_engine_->registerExpression(exp_stri, exp_str_);
if (reg_ret != 0) {
this->error_code_list_.push_back(
{ErrorType::CalError, ErrorLocation::FBExp});
return -1;
}
logger_->Debug() << "exp_stri:" << exp_str_ << "="
<< expr_engine_->evaluate(exp_stri) << endl;
if (rule_json_.at("function").at(exp_stri).at("param").contains("name")) {
var_name_[exp_stri] = rule_json_.at("function")
.at(exp_stri)
.at("param")
.at("name")
.at("value")
.get<std::string>();
} else if (rule_json_.at("function")
.at(exp_stri)
.at("param")
.contains("limit_over")) {
try {
limit_over_ = std::stod(rule_json_.at("function")
.at(exp_stri)
.at("param")
.at("limit_over")
.at("value")
.get<std::string>()) /
100.00;
} catch (const std::exception& e) {
logger_->Error() << "exp_stri:" << exp_stri
<< "param limit_over ERROR" << e.what()
<< ",location:" << BOOST_CURRENT_LOCATION << endl;
return -1;
}
}
} else {
logger_->Debug() << "init_X_exp() 完成!" << std::endl;
break;
}
}
return res;
}
void Roller2::refresh_var_result() {
try {
for (auto& expi : var_exp_str_) {
if (expi.first == "pre_exp") {
pre_exp_flag_ = expr_engine_->evaluateBool(expi.first);
} else {
var_Xdouble_[expi.first] = expr_engine_->evaluate(expi.first);
}
}
} catch (const std::exception& e) {
logger_->Error() << "refresh_var_result ERROR" << e.what()
<< ",location:" << BOOST_CURRENT_LOCATION << endl;
}
}