/****************************************************************************************************************** * 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 * 1.1 2021-3 c@t add DTW ******************************************************************************************************************/ #include #include #include #include // add static modules extern std::map glob_items; int AlgExpFFTComp::init() { LOG d("AlgExpFFTComp::init", AUTO_CATCH_PID); int ret = 0; m_btime = 0; try { } catch (const std::exception &e) { d.Error() << mix_cc::get_nested_exception(e) << std::endl; ret = -1; } return ret; } AlgExpFFTComp::AlgExpFFTComp(const string &name, const Json::Value &rulejson, const string &ruleId) : AlgBase(name, rulejson, ruleId), AlgExp(name, rulejson, ruleId) { LOG d("AlgExpFFTComp::AlgExpFFTComp", AUTO_CATCH_PID); keep_mode_ = 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(); m_sampletag_freq = m_json_param["alarm_option"]["tag_freq"][1].asString(); m_sampletag_data = m_json_param["alarm_option"]["tag_data"][1].asString(); StringHelper::Trim(m_sampletag_freq); StringHelper::Trim(m_sampletag_data); 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 (%) 3: trend):" << test_mode_ << " samplediff:" << judge_diff_ << endl; this->init(); m_expstr = string(m_json_param["action_condition"]["action_start"][1].asString()); exp_act_ = new MathExpression(m_expstr.c_str(), mm_vars); d.Debug() << m_expstr << ":" << exp_act_->evaluate() << endl; m_expstr = string(m_json_param["action_condition"]["action_end"][1].asString()); exp_feedback_ = new MathExpression(m_expstr.c_str(), mm_vars); d.Debug() << m_expstr << ":" << exp_feedback_->evaluate() << endl; this->expire_time_ = m_json_alarm["action_condition"]["expire"].asUInt(); this->fft_stat_ = std::move(std::unique_ptr(new FFTStat(rule_id_))); if (exp_act_ == NULL) { exp_act_ = new MathExpression(m_expstr.c_str(), mm_vars); d.Debug() << m_expstr << ":" << exp_act_->evaluate() << endl; } if (m_modest == DATA_STAT::HISTORY) { this->malloc_ihd_mem(); } // this->dest_value_ = fft_stat_->LoadFromDB2(); d.Debug() << "Init Success" << std::endl; } AlgExpFFTComp::~AlgExpFFTComp() { delete exp_act_; delete exp_feedback_; } int AlgExpFFTComp::calculate(string &outjson) { LOG d("AlgExpFFTComp::calculate|" + rule_name_, AUTO_CATCH_PID); int ret = 0; outjson = ""; // d.Debug()<QuerySnapshot(m_tags[i], &mp_hdRec[i]) == 0) { 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 != 0) { return 0; } for (int i = 0; i < tag_count_; i++) { for (unsigned 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(); // records_queried_[j][i]. } ret = this->mon_proc(outjson, &records_queried_[0][i]); if (ret == 0 && 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 AlgExpFFTComp::mon_proc(string &outjson, HD3Record *hdRec) { LOG d("AlgExpFFTComp::mon_proc|" + rule_name_, AUTO_CATCH_PID); int ret = 0; outjson = ""; bool act_triggered_ = false; bool feedback_triggered_ = false; long long timediff = 0; try { act_triggered_ = (bool)exp_act_->evaluate(); feedback_triggered_ = (bool)exp_feedback_->evaluate(); // d.Debug()<<"act_triggered_:"<print_exp_vars(); if (m_btime == 0 && act_triggered_) { if (m_modest == DATA_STAT::ACTUAL && data_source_ == DataSource::MEMORY) { m_btime = this->mstime(); this->SetHDTime(&query_time_region_.left, m_btime); } else { // set left and begin time this->SetHDTime(&query_time_region_.left, hdRec); // btime = left = hdRec.time m_btime = this->ToMsTime(&query_time_region_.left); } d.Debug() << " action start " << m_btime << endl; this->print_exp_vars(); for (unsigned int i = 0; i < m_tags.size(); i++) { mm_vars["s" + std::to_string(i + 1)] = mm_vars["tag" + std::to_string(i + 1)]; } return 0; } if (m_btime > 0 && keep_mode_ && !act_triggered_) { // btime = 0, but should not be i n this one m_btime = 0; this->print_exp_vars(); d.Debug() << " action signal is not holding " << endl; return 0; } if (m_btime > 0 && (feedback_triggered_ || (mstime() - m_btime) > expire_time_)) { if (m_modest == DATA_STAT::ACTUAL && data_source_ == DataSource::MEMORY) { this->SetHDTime(&query_time_region_.left, m_btime); this->SetHDTime(&query_time_region_.right, this->mstime()); } else { // set right(end) time this->SetHDTime(&query_time_region_.left, m_btime); this->SetHDTime(&query_time_region_.right, hdRec); } timediff = this->ToMsTime(&query_time_region_.right) - m_btime; mm_vars["time"] = timediff; this->print_exp_vars(m_expstr); d.Debug() << " action time[" << m_btime << "," << this->ToMsTime(&query_time_region_.right) << "],timediff:" << timediff << endl; if (query_time_region_.right.nSec < query_time_region_.left.nSec) { std::swap(query_time_region_.right.nSec, query_time_region_.left.nSec); d.Error() << "Time Range is Error !" << std::endl; } // 得到查询的结果 auto sample_data = hd_com .GetQueryBatch( m_tags[atoi(m_sampletag_data.substr(3).c_str()) - 1], query_time_region_) .QueryRemainingAll(); auto sample_freq = hd_com .GetQueryBatch( m_tags[atoi(m_sampletag_freq.substr(3).c_str()) - 1], query_time_region_) .QueryRemainingAll(); d.Debug() << "Size1 :" << sample_data.size() << " Size2:" << sample_freq.size() << std::endl; fft_stat_->ParseData(sample_data, sample_freq); auto val = fft_stat_->GetComparevalue(fft_stat_->GetMaxMagnitudeIndex()); d.Debug() << "Value:" << val << std::endl; fft_stat_->StorageToDB2(val); // delete // ret = this->GetHDTrend(m_expstr); // if (dest_value_ * (1 - judge_diff_) > dest_value_ || // dest_value_ * (1 + judge_diff_) < val) { if (false) { msg = rule_name_ + " " + m_json_param["alarm_option"]["error"][1].asString(); d.Debug() << msg << endl; msg = this->build_alarm_info(MsgLevel::ERROR, rule_id_, rule_name_, "TREND_SMP", msg, query_time_region_); if (!msg.empty()) outjson = msg; } m_btime = 0; } } catch (const std::exception &e) { d.Error() << mix_cc::get_nested_exception(e) << std::endl; ret = -1; } return ret; }