#include "mix_cc/ihyper_db.h" #include #include #include #include #include #include #include #include #include #include #include int GlitchDetection::init() { data_index_ = 0; int ret = 0; try { ret += AlgBase::init(); ret += this->load_exp(); this->error_content_ = rule_json_.at("output").at("error").at("value").get(); logger_->Debug() << "error_content_:" << error_content_ << std::endl; } catch (const std::exception &e) { std::throw_with_nested( mix_cc::Exception(-1, "load error", BOOST_CURRENT_LOCATION)); } py_param_["ruleid"] = this->rule_id_; py_param_["stime"] = mix_cc::mix_time_t(now_time_).to_milliseconds(); py_param_["etime"] = mix_cc::mix_time_t(now_time_).to_milliseconds(); py_param_["alarm_content"] = this->error_content_; py_param_["name"] = this->rule_name_; py_param_["glitch_per"] = this->glitch_per_; run_time_range_.set_left(this->now_time_); this->data_ = {0}; logger_->Debug() << rule_name_ << "--init 完成!" << std::endl; return ret; } GlitchDetection::GlitchDetection(const string name, const mix_cc::json &rule_json, const string ruleId) : AlgBase(name, rule_json, ruleId) { logger_.reset(new LOG("GlitchDetection:" + rule_name_, AUTO_CATCH_PID)); } GlitchDetection::~GlitchDetection() { } AlarmInfo GlitchDetection::exec_mon() { AlarmInfo out_alarm{}; logger_->Debug() << "-----心跳-----" << rule_name_ << std::endl; refresh_now_time(); if (data_index_ >= data_size_) { run_time_range_.set_right(this->now_time_); logger_->Debug() << "数据收集完成!" << std::endl; bool res = SingletonTemplate::GetInstance().insert( this->rule_id_, this->data_, data_index_); logger_->Info() << "data[0:10]:"; for (int i = 0; i < 10; i++) { logger_->Info() << data_[i] << ","; } logger_->Info() << std::endl; logger_->Info() << "data[-10:-1]:"; for (int i = data_size_ - 11; i < data_size_ - 1; i++) { logger_->Info() << data_[i] << ","; } logger_->Info() << std::endl; if (res) { py_param_["stime"] = mix_cc::mix_time_t(run_time_range_.get_left()).to_milliseconds(); py_param_["etime"] = mix_cc::mix_time_t(run_time_range_.get_right()).to_milliseconds(); bool res2 = SingletonTemplate::GetInstance().send2py( "glitch", py_param_.dump()); } data_index_ = 0; run_time_range_.set_left(this->now_time_); } data_[data_index_] = exp_mpdule_ptr_->get_value("dataX"); data_index_++; return out_alarm; } void GlitchDetection::save_rule_norm_data() { try { int64_t lt = mix_cc::mix_time_t(this->query_time_range_.get_left()) .to_milliseconds(); int64_t rt = mix_cc::mix_time_t(this->query_time_range_.get_right()) .to_milliseconds(); int64_t default_lt = rt - 1000 * 5; lt = std::min(lt, default_lt); data_info_.update(lt, rt); SingletonTemplate::GetInstance().insert(rule_id_, &data_info_); } catch (const std::exception &e) { logger_->Error() << e.what() << std::endl; } } std::vector GlitchDetection::exec_task(mix_cc::time_range_t time_range) { std::vector 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; } int GlitchDetection::load_exp() { int res = 0; if (rule_json_.at("function").contains("dataX")) { /*监控变量的表达式*/ auto tmp_exp = rule_json_.at("function").at("dataX").at("value").get(); exp_str_ = get_macro_replaced_exp(tmp_exp); exp_mpdule_ptr_->add_exp("dataX", exp_str_); /*数据长度*/ auto tmp_data_size = rule_json_.at("function") .at("dataX") .at("param") .at("lenth") .at("value") .get(); data_size_ = std::stoi(tmp_data_size); if (data_size_ < 100 || data_size_ > MAXLEN) { data_size_ = MAXLEN - 2; } glitch_per_ = 0.1; logger_->Debug() << "exp_str:" << exp_str_ << ",data_size_:" << data_size_ << ",glitch_per_:" << glitch_per_ << std::endl; return res; } } bool GlitchDetection::get_prr() { if (this->prr_ == 1) { exp_mpdule_ptr_->update(); bool prr_result = (bool)exp_mpdule_ptr_->get_value("pre_result"); this->now_prr_ = prr_result; logger_->Debug() << "ruleid:" << this->rule_id_ << ",rulename:" << this->rule_name_ << ",get_prr():" << prr_result << ",data_index_:" << data_index_ << std::endl; // exp_mpdule_ptr_->print_value(); if (!prr_result) { data_index_ = 0; run_time_range_.set_left(this->now_time_); logger_->Debug() << "!prr_result!!!!-----" << "ruleid:" << this->rule_id_ << ",rulename:" << this->rule_name_ << ",get_prr():" << prr_result << ",data_index_:" << data_index_ << std::endl; } return prr_result; } this->now_prr_ = true; return true; }