224 lines
8.0 KiB
C++
224 lines
8.0 KiB
C++
/******************************************************************************************************************
|
||
* Action instruction algorithm(study sample data online,compare with sample
|
||
*data)
|
||
*
|
||
* arg[0] action expression
|
||
* arg[1] feedback expression
|
||
* arg[2] expression of judgment result
|
||
*
|
||
* feedback expression
|
||
* arg[2] expression of judgment result
|
||
*
|
||
* 1.0 2020-12-17 zoufuzhou
|
||
******************************************************************************************************************/
|
||
#include <base/BitTool.h>
|
||
#include <dao/DBMag.h>
|
||
#include <eqpalg/algs/AlgExpSample.h>
|
||
#include <eqpalg/utility/sample_utilites.h>
|
||
#include <utility/StringHelper.h>
|
||
|
||
extern std::map<std::string, ITEM> glob_items;
|
||
|
||
int AlgExpSample::init() {
|
||
LOG d("AlgExpSample::init", AUTO_CATCH_PID);
|
||
|
||
int ret = 0;
|
||
m_btime = 0;
|
||
this->sample_analysis->Init("T_RULE_SAMPLE", test_mode_, archive_interval_day_, judge_diff_);
|
||
string dbwhere = " RULEID = '" + rule_id_ + "' and flag = '1' ";
|
||
if (m_json_param["sample"]["datestart"][1].asString() != "" &&
|
||
m_json_param["sample"]["dateend"][1].asString() != "") {
|
||
dbwhere += " AND SAMPLEDATE >= TO_DATE ('" +
|
||
m_json_param["sample"]["datestart"][1].asString() +
|
||
"','YYYY-MM-DD') and SAMPLEDATE <= TO_DATE ('" +
|
||
m_json_param["sample"]["dateend"][1].asString() +
|
||
"','YYYY-MM-DD')";
|
||
}
|
||
d.Debug() << "where:" << dbwhere << endl;
|
||
this->sample_analysis->Read(dbwhere);
|
||
d.Debug() << "abs_diff:"
|
||
<< this->sample_analysis->GetProperty()->data.abs_diff.tostring() << endl;
|
||
d.Debug() << "normal_cfdi:"
|
||
<< this->sample_analysis->GetProperty()->data.normal_cfdi.tostring()
|
||
<< endl;
|
||
return ret;
|
||
}
|
||
|
||
AlgExpSample::AlgExpSample(const string &name, const Json::Value &rulejson,
|
||
const string &ruleId, const bool down_limit)
|
||
: AlgBase(name, rulejson, ruleId), AlgExp(name, rulejson, ruleId),
|
||
_down_limit(down_limit) {
|
||
LOG d("AlgExpSample::AlgExpSample", AUTO_CATCH_PID);
|
||
try {
|
||
data_source_ = m_json_param["datasource"]["value"][1].asInt();
|
||
d.Debug() << "data source[0:iHyerDB,1:memory]:" << data_source_ << endl;
|
||
keep_mode_ = 1; // m_json_param["action_condition"]["action_hold"][1].asInt();
|
||
m_modest = m_json_param["action_condition"]["mode"][1].asInt();
|
||
d.Debug() << "keep:" << keep_mode_ << " mode stat_tools:" << m_modest << endl;
|
||
archive_interval_day_ = m_json_param["sample"]["archive"][1].asInt();
|
||
d.Debug() << "archive:" << archive_interval_day_ << endl;
|
||
test_mode_ = m_json_param["alarm_option"]["mode"][1].asInt();
|
||
judge_diff_ = m_json_param["alarm_option"]["value"][1].asDouble();
|
||
d.Debug()
|
||
<< "modejudge(0: absolute difference, 1: error percentage (%), 2: "
|
||
"normal value signal (%)):"
|
||
<< test_mode_ << " samplediff:" << judge_diff_ << endl;
|
||
|
||
this->init();
|
||
|
||
m_expstr = string(m_json_param["action_condition"]["action_start"][1].asString());
|
||
this->print_exp_vars(m_expstr);
|
||
exp_act_ = new MathExpression(m_expstr.c_str(), mm_vars);
|
||
d.Debug() << "act_triggered_:" << m_expstr << "=" << exp_act_->evaluate() << endl;
|
||
|
||
m_expstr = string(m_json_param["alarm_option"]["var"][1].asString());
|
||
StringHelper::Trim(m_expstr);
|
||
if (m_expstr == "time")
|
||
// if(m_expstr.find("time",0) != string::npos)
|
||
{
|
||
m_timemode = true;
|
||
} else {
|
||
m_timemode = false;
|
||
}
|
||
|
||
if (exp_act_ == NULL) {
|
||
exp_act_ = new MathExpression(m_expstr.c_str(), mm_vars);
|
||
d.Debug() << "act_triggered_:" << m_expstr << "=" << exp_act_->evaluate()
|
||
<< endl;
|
||
}
|
||
|
||
if (!m_modest) {
|
||
this->malloc_ihd_mem();
|
||
}
|
||
} catch (const std::exception &e) {
|
||
d.Debug() << mix_cc::get_nested_exception(e) << std::endl;
|
||
}
|
||
}
|
||
|
||
AlgExpSample::~AlgExpSample() { delete exp_act_; }
|
||
|
||
int AlgExpSample::calculate(string &outjson) {
|
||
LOG d("AlgExpSample::calculate|" + rule_name_, AUTO_CATCH_PID);
|
||
int ret = 0;
|
||
outjson = "";
|
||
|
||
try {
|
||
if (m_modest == DATA_STAT::ACTUAL) {
|
||
for (unsigned int i = 0; i < m_tags.size(); i++) {
|
||
mm_vars["p" + std::to_string(i + 1)] =
|
||
mm_vars["tag" + std::to_string(i + 1)];
|
||
if (data_source_ == DataSource::MEMORY) {
|
||
mm_vars["tag" + std::to_string(i + 1)] = glob_items[m_tags[i]].value;
|
||
} else {
|
||
if (ihd_tools_->QuerySnapshot(m_tags[i], &mp_hdRec[i]) == NULL) {
|
||
mm_vars["tag" + std::to_string(i + 1)] = mp_hdRec[i].NumberValue();
|
||
}
|
||
}
|
||
}
|
||
ret = this->mon_proc(outjson, mp_hdRec);
|
||
} else {
|
||
ret = this->QueryDB3Record();
|
||
if (ret != NULL) {
|
||
return 0;
|
||
}
|
||
|
||
for (int i = 0; i < tag_count_; i++) {
|
||
for (int j = 0; j < m_tags.size(); j++) {
|
||
mm_vars["p" + std::to_string(j + 1)] =
|
||
mm_vars["tag" + std::to_string(j + 1)];
|
||
mm_vars["tag" + std::to_string(j + 1)] =
|
||
records_queried_[j][i].NumberValue();
|
||
}
|
||
ret = this->mon_proc(outjson, &records_queried_[0][i]);
|
||
if (ret == NULL && outjson != "") {
|
||
break;
|
||
}
|
||
}
|
||
if (this->m_btime == 0) {
|
||
this->SetHDTime(&query_time_region_.right, this->mstime());
|
||
}
|
||
this->free_ihd_mem();
|
||
}
|
||
|
||
} catch (const std::exception &e) {
|
||
d.Error() << mix_cc::get_nested_exception(e) << std::endl;
|
||
ret = -1;
|
||
}
|
||
return ret;
|
||
}
|
||
|
||
int AlgExpSample::mon_proc(string &outjson, HD3Record *hdRec) {
|
||
LOG d("AlgExpSample::mon_proc|" + rule_name_, AUTO_CATCH_PID);
|
||
int ret = 0;
|
||
outjson = "";
|
||
bool act_triggered_ = false;
|
||
double act_triggered_ = 0.0;
|
||
long long timediff = 0;
|
||
|
||
try {
|
||
|
||
act_triggered_ = (bool)exp_act_->evaluate();
|
||
this->print_exp_vars(m_expstr);
|
||
// d.Debug()<<"act_triggered_:"<<setw(2)<<act_triggered_<<endl;
|
||
if (act_triggered_) {
|
||
if (m_modest == DATA_STAT::ACTUAL &&
|
||
data_source_ == DataSource::MEMORY) {
|
||
this->SetHDTime(&query_time_region_.left, this->mstime());
|
||
this->SetHDTime(&query_time_region_.right, this->mstime());
|
||
} else {
|
||
this->SetHDTime(&query_time_region_.left, hdRec);
|
||
this->SetHDTime(&query_time_region_.right, hdRec);
|
||
}
|
||
|
||
this->print_exp_vars(m_expstr);
|
||
d.Debug() << "act_triggered_:" << setw(2) << act_triggered_ << endl;
|
||
act_triggered_ = exp_act_->evaluate();
|
||
d.Debug() << "act_triggered_:" << m_expstr << "=" << act_triggered_ << endl;
|
||
|
||
bool is_alarmed = false;
|
||
if(this->_down_limit)
|
||
{
|
||
is_alarmed = utility::is_alarm_value(this->sample_analysis, act_triggered_, judge_diff_);
|
||
}else
|
||
{
|
||
d.Debug() << "check" << std::endl;
|
||
is_alarmed = is_alarm_value_no_down_lim(this->sample_analysis, act_triggered_, judge_diff_);
|
||
}
|
||
if (is_alarmed) {
|
||
d.Debug() << "abs_diff:"
|
||
<< this->sample_analysis->GetProperty()->data.abs_diff.tostring()
|
||
<< endl;
|
||
d.Debug() << "normal_cfdi:"
|
||
<< this->sample_analysis->GetProperty()->data.normal_cfdi.tostring()
|
||
<< endl;
|
||
// this->print_exp_vars(m_expstr);
|
||
auto value = generate_sample_describe(this->sample_analysis, act_triggered_, judge_diff_);
|
||
|
||
msg =
|
||
rule_name_ + " " + m_json_param["alarm_option"]["error"][1].asString() + value;
|
||
// msg = rule_name_ + " " +
|
||
// m_json_param["alarm_option"]["error"][1].asString();
|
||
d.Debug() << msg << endl;
|
||
query_time_region_.left.nSec = query_time_region_.right.nSec = time(0);
|
||
msg = this->build_alarm_info(MsgLevel::ERROR, rule_id_, rule_name_,
|
||
"EXPSAMPLE", msg, query_time_region_);
|
||
if (!msg.empty())
|
||
outjson = msg;
|
||
}
|
||
// else
|
||
{
|
||
if (m_timemode) {
|
||
this->sample_analysis->Learning(timediff);
|
||
} else {
|
||
this->sample_analysis->Learning(act_triggered_);
|
||
}
|
||
}
|
||
}
|
||
|
||
} catch (const std::exception &e) {
|
||
d.Error() << mix_cc::get_nested_exception(e) << std::endl;
|
||
ret = -1;
|
||
}
|
||
return ret;
|
||
}
|