/****************************************************************************************************************** * 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 AlgExpFFT::init() { LOG d("AlgExpFFT::init", AUTO_CATCH_PID); int ret = 0; m_btime = 0; try { } catch (const std::exception &e) { logger_->Error() <Debug() << "keep:" << keep_mode_ << " mode stat_tools:" << m_modest << endl; archive_interval_day_ = m_json_param["sample"]["archive"][1].asInt(); freq_exp_str_ = m_json_param["alarm_option"]["freq"][1].asString(); m_sampletag_data = m_json_param["alarm_option"]["tag_data"][1].asString(); StringHelper::Trim(freq_exp_str_); 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(); logger_->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); logger_->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); logger_->Debug() << m_expstr << ":" << exp_feedback_->evaluate() << endl; mp_exp_freq = new MathExpression(freq_exp_str_.c_str(), mm_vars); logger_->Debug() << m_expstr << ":" << mp_exp_freq->evaluate() << endl; 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); logger_->Debug() << m_expstr << ":" << exp_act_->evaluate() << endl; } if (m_modest == DATA_STAT::HISTORY) { this->malloc_ihd_mem(); } logger_->Debug() << "Init Success" << std::endl; } AlgExpFFT::~AlgExpFFT() { delete exp_act_; delete exp_feedback_; delete mp_exp_freq; } int AlgExpFFT::calculate(string &outjson) { LOG d("AlgExpFFT::calculate|" + rule_name_, AUTO_CATCH_PID); int ret = 0; outjson = ""; // logger_->Debug()<Debug()<<"m_tags.size():"<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(); // records_queried_[j][i]. } 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) { logger_->Error() <evaluate(); feedback_triggered_ = (bool)exp_feedback_->evaluate(); // logger_->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); } logger_->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(); logger_->Debug() << " action signal is not holding " << endl; return 0; } if (m_btime > 0 && feedback_triggered_) { 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); logger_->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); logger_->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(); logger_->Debug() << "Size:" << sample_data.size() << std::endl; fft_stat_->ParseData(sample_data); auto val = fft_stat_->GetComparevalue(mp_exp_freq->evaluate()); fft_stat_->StorageToDB2(val); // delete // ret = this->GetHDTrend(m_expstr); if (false) { msg = rule_name_ + " " + m_json_param["alarm_option"]["error"][1].asString(); logger_->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) { logger_->Error() <