332 lines
12 KiB
C++
332 lines
12 KiB
C++
#include <dlib/serialize.h>
|
||
#include <dlib/statistics.h>
|
||
#include <eqpalg/algs/table_static.h>
|
||
#include <eqpalg/table_struct/t_rule_cfg.h>
|
||
#include <eqpalg/table_struct/t_rule_result.h>
|
||
#include <eqpalg/table_struct/t_rule_sample_1d.h>
|
||
#include <mix_cc/sql/database/db2_t.h>
|
||
// #include <filesystem>
|
||
// #include <fstream>
|
||
TableStatic::TableStatic(const string name, const mix_cc::json& rule_json,
|
||
const string ruleId)
|
||
: AlgBase(name, rule_json, ruleId) {
|
||
logger_.reset(new LOG("TableStatic:" + rule_name_, AUTO_CATCH_PID));
|
||
}
|
||
TableStatic::~TableStatic() {}
|
||
|
||
void TableStatic::init_config() {
|
||
is_usable_ = false;
|
||
this->reload_config_tag();
|
||
int d_min = rule_json_.at("interval").at("time").at(1).get<int64_t>();
|
||
if (d_min < 5) {
|
||
d_min = 5;
|
||
}
|
||
this->delay_time_proc_ = minutes(d_min);
|
||
this->delay_time_ = milliseconds(1000);
|
||
|
||
last_run_start_time_ = system_clock::now() - this->delay_time_;
|
||
last_run_start_time_proc_ = system_clock::now() - this->delay_time_proc_;
|
||
/// table_name获取
|
||
this->table_name_ =
|
||
rule_json_.at("table_name").at("name").at(1).get<string>();
|
||
this->table_key_ = rule_json_.at("table_key").at("name").at(1).get<string>();
|
||
}
|
||
|
||
int TableStatic::init() {
|
||
try {
|
||
this->init_config();
|
||
///标记 group 后缀
|
||
this->running_flag_ =
|
||
rule_json_.at("running_static").at("time").at(1).get<int>();
|
||
this->logger_->Debug() << "this->running_flag_:" << this->running_flag_
|
||
<< endl;
|
||
if (this->running_flag_ > 0) {
|
||
//运行时间 或 剪切次数
|
||
this->mp_table_data_[0] = TableData();
|
||
if (this->running_flag_ == 1) {
|
||
this->mp_table_data_[0].group_name_ = this->table_name_ + "-运行时间";
|
||
} else {
|
||
this->mp_table_data_[0].group_name_ = this->table_name_ + "-次数";
|
||
}
|
||
}
|
||
this->fault_flag_ =
|
||
rule_json_.at("fault_static").at("times").at(1).get<int>();
|
||
if (this->fault_flag_ > 0) {
|
||
//重故障 或仅故障
|
||
if (this->running_flag_ == 0) {
|
||
this->main_flag_ = 1;
|
||
}
|
||
this->mp_table_data_[1] = TableData();
|
||
}
|
||
this->alrm_flag_ =
|
||
rule_json_.at("alarm_static").at("times").at(1).get<int>();
|
||
if (this->alrm_flag_ > 0) {
|
||
//报警 轻故障
|
||
this->mp_table_data_[2] = TableData();
|
||
}
|
||
if (this->fault_flag_ && this->alrm_flag_) {
|
||
this->mp_table_data_[1].group_name_ = this->table_name_ + "-重故障";
|
||
this->mp_table_data_[2].group_name_ = this->table_name_ + "-轻故障";
|
||
}
|
||
if (this->fault_flag_ && !this->alrm_flag_) {
|
||
this->mp_table_data_[1].group_name_ = this->table_name_ + "-故障";
|
||
this->logger_->Debug() << "this->mp_table_data_[1].group_name_:"
|
||
<< this->mp_table_data_[1].group_name_ << endl;
|
||
}
|
||
get_ruleid(); ///<查cfg
|
||
this->Jkey_ += this->table_key_;
|
||
} catch (const std::exception& e) {
|
||
std::throw_with_nested(
|
||
mix_cc::Exception(-1, "init error", BOOST_CURRENT_LOCATION));
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
/**
|
||
* @brief
|
||
*/
|
||
AlarmInfo TableStatic::exec_mon() {
|
||
AlarmInfo out_alarm{};
|
||
// this->logger_->Debug() << " TableStatic::exec_mon: start!" << endl;
|
||
if (this->get_cycled_proc()) {
|
||
auto reSult = this->mon_proc();
|
||
this->logger_->Debug() << " update json" << endl;
|
||
} else if (this->get_clear_flag()) {
|
||
mix_cc::json js1 = this->table_data_.invert2json();
|
||
this->Jvalue_ = js1.dump();
|
||
// this->logger_->Debug() << "Jkey:" << this->Jkey_
|
||
// << " this->Jvalue_ :" << this->Jvalue_ << endl;
|
||
auto reSult = insert_memcache(this->Jkey_, this->Jvalue_);
|
||
}
|
||
|
||
return out_alarm;
|
||
}
|
||
std::vector<AlarmInfo> TableStatic::exec_task(mix_cc::time_range_t time_range) {
|
||
return {};
|
||
}
|
||
void TableStatic::set_usable(bool usable) { this->is_usable_ = usable; }
|
||
/**
|
||
* @brief 获取ruleid 从 cfg
|
||
* @return int
|
||
*/
|
||
void TableStatic::get_ruleid() {
|
||
for (int i = 0; i < 3; i++) {
|
||
auto reSult = get_ruleid_by_int(i);
|
||
}
|
||
this->logger_->Debug()
|
||
<< "this->main_flag_:" << this->main_flag_ << " main_table_ nums is "
|
||
<< this->mp_table_data_[this->main_flag_].rule_ids_.size() << endl;
|
||
this->table_data_.init_id(this->mp_table_data_[this->main_flag_].rule_ids_,
|
||
this->mp_table_data_[this->main_flag_].rule_names_);
|
||
this->is_rules_empty_ = !this->table_data_.rule_nums_;
|
||
}
|
||
int TableStatic::get_ruleid_by_int(int num) {
|
||
T_RULE_CFG trc;
|
||
if (this->mp_table_data_.find(num) == this->mp_table_data_.end()) {
|
||
this->logger_->Debug() << "no " << num
|
||
<< " [0-running_time;1-重故障;2-轻故障]" << endl;
|
||
return -3;
|
||
}
|
||
|
||
string grop_name = this->mp_table_data_[num].group_name_;
|
||
auto query_list_maybe =
|
||
exec<db2_t, T_RULE_CFG>(select(trc.ruleId(), trc.ruleName())
|
||
.from(trc)
|
||
.where(trc.ruleGroup() == grop_name));
|
||
if (query_list_maybe.is_just()) {
|
||
auto& query_list = query_list_maybe.unsafe_get_just();
|
||
if (!query_list.empty()) {
|
||
this->mp_table_data_[num].rule_nums_ = query_list.size();
|
||
logger_->Info() << query_list.size() << " rules in DB where group is:"
|
||
<< this->mp_table_data_[num].group_name_ << endl;
|
||
// this->mp_table_data_[num].rule_ids_ =
|
||
// vector<string>(this->table_data_.rule_nums_, "a");
|
||
// this->mp_table_data_[num].rule_names_ =
|
||
// vector<string>(this->table_data_.rule_nums_, "a");
|
||
for (int i = 0; i < this->mp_table_data_[num].rule_nums_; i++) {
|
||
this->mp_table_data_[num].rule_ids_.push_back(query_list[i].ruleId);
|
||
this->mp_table_data_[num].rule_names_.push_back(query_list[i].ruleName);
|
||
}
|
||
this->mp_table_data_[num].init_id(this->mp_table_data_[num].rule_ids_,
|
||
this->mp_table_data_[num].rule_names_);
|
||
} else {
|
||
this->gb_logger_->log_error("no rule in DB where group is:" +
|
||
this->mp_table_data_[num].group_name_);
|
||
return -2;
|
||
}
|
||
} else {
|
||
this->gb_logger_->log_error("db2查询失败" + this->rule_name_);
|
||
return -1;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
int TableStatic::select_alarm_by_ruleid(string ruleid) {
|
||
T_RULE_RESULT trr;
|
||
auto query_list_maybe = exec<db2_t, T_RULE_CFG>(
|
||
select(trr.ruleId())
|
||
.from(trr)
|
||
.where(trr.ruleId() == ruleid, trr.dealResult() == 0));
|
||
if (query_list_maybe.is_just()) {
|
||
auto& query_list = query_list_maybe.unsafe_get_just();
|
||
return query_list.size();
|
||
} else {
|
||
return 0;
|
||
}
|
||
}
|
||
double TableStatic::select_running_by_ruleid(string ruleid) {
|
||
T_RULE_SAMPLE_1D trs1a;
|
||
auto query_list_maybe = exec<db2_t, T_RULE_SAMPLE_1D>(
|
||
select(trs1a.X1()).from(trs1a).where(trs1a.RuleId() == ruleid));
|
||
if (query_list_maybe.is_just()) {
|
||
auto& query_list = query_list_maybe.unsafe_get_just();
|
||
if (query_list.empty()) {
|
||
this->logger_->Debug() << "query_list.empty() ruleid:" << ruleid << endl;
|
||
return 0;
|
||
}
|
||
// if (ruleid == "f600feef-b91e-45a5-9f78-4d87aeb53c1f") {
|
||
// this->logger_->Debug() << "query_list[0].X1:" << query_list[0].X1 <<
|
||
// endl;
|
||
// }
|
||
return query_list[0].X1;
|
||
} else {
|
||
this->logger_->Debug() << "!query_list_maybe.is_just() ruleid:" << ruleid
|
||
<< endl;
|
||
return 0;
|
||
}
|
||
}
|
||
unsigned long TableStatic::select_times_by_ruleid(string ruleid) {
|
||
T_RULE_SAMPLE_1D trs1a;
|
||
auto query_list_maybe = exec<db2_t, T_RULE_SAMPLE_1D>(
|
||
select(trs1a.Count()).from(trs1a).where(trs1a.RuleId() == ruleid));
|
||
if (query_list_maybe.is_just()) {
|
||
auto& query_list = query_list_maybe.unsafe_get_just();
|
||
if (query_list.empty()) {
|
||
return 0;
|
||
}
|
||
return query_list[0].Count;
|
||
} else {
|
||
return 0;
|
||
}
|
||
}
|
||
|
||
int TableStatic::insert_memcache(string Jkey, string Jvalue) {
|
||
auto reSult =
|
||
m_memclient.Insert(Jkey.c_str(),
|
||
Jvalue.c_str()); ///< memcache time_t expiration = 0
|
||
if (1 != reSult) {
|
||
this->logger_->Debug()
|
||
<< "m_memclient.Insert异常[0,set data fail;-1,key=null or value=null]:"
|
||
<< reSult << endl;
|
||
}
|
||
return 0;
|
||
}
|
||
int TableStatic::mon_proc() {
|
||
if (!is_rules_empty_) {
|
||
if (this->fault_flag_ && this->alrm_flag_) {
|
||
// 1 查 t_rule_result 后缀 _轻故障 2
|
||
for (int i = 0; i < this->table_data_.rule_nums_; i++) {
|
||
this->table_data_.alrm_times_[i] =
|
||
this->select_alarm_by_ruleid(this->mp_table_data_[2].rule_ids_[i]);
|
||
// 2查 t_rule_result 后缀 _重故障 1
|
||
this->table_data_.fault_times_[i] =
|
||
this->select_alarm_by_ruleid(this->mp_table_data_[1].rule_ids_[i]);
|
||
}
|
||
} else if (this->fault_flag_) {
|
||
// 2查 t_rule_result 后缀 _故障 1
|
||
for (int i = 0; i < this->table_data_.rule_nums_; i++) {
|
||
this->table_data_.fault_times_[i] =
|
||
this->select_alarm_by_ruleid(this->mp_table_data_[1].rule_ids_[i]);
|
||
}
|
||
//去掉轻故障
|
||
this->table_data_.clear_alrm_times();
|
||
} else {
|
||
this->table_data_.clear_fault_times();
|
||
this->table_data_.clear_alrm_times();
|
||
}
|
||
|
||
if (this->running_flag_ == 1) {
|
||
// 查 T_RULE_SAMPLE_1D x1字段 0 0
|
||
for (int i = 0; i < this->table_data_.rule_nums_; i++) {
|
||
this->table_data_.running_time_[i] = this->select_running_by_ruleid(
|
||
this->mp_table_data_[0].rule_ids_[i]);
|
||
}
|
||
this->table_data_.clear_used_times();
|
||
} else if (this->running_flag_ == 2) {
|
||
// 查 T_RULE_SAMPLE_1D count字段 2 3
|
||
for (int i = 0; i < this->table_data_.rule_nums_; i++) {
|
||
this->table_data_.used_times_[i] =
|
||
this->select_times_by_ruleid(this->mp_table_data_[0].rule_ids_[i]);
|
||
}
|
||
this->table_data_.clear_running_time();
|
||
} else {
|
||
this->table_data_.clear_running_time();
|
||
this->table_data_.clear_used_times();
|
||
}
|
||
|
||
mix_cc::json js1 = this->table_data_.invert2json();
|
||
this->Jvalue_ = js1.dump();
|
||
// this->logger_->Debug() << "Jkey:" << this->Jkey_
|
||
// << " this->Jvalue_ :" << this->Jvalue_ << endl;
|
||
auto reSult = insert_memcache(this->Jkey_, this->Jvalue_);
|
||
} else {
|
||
this->logger_->Debug() << this->table_data_.group_name_ << " 没有配置规则!"
|
||
<< endl;
|
||
return -1;
|
||
}
|
||
return 0;
|
||
}
|
||
bool TableStatic::get_cycled_proc() {
|
||
auto now = system_clock::now();
|
||
if ((now - last_run_start_time_proc_) > this->delay_time_proc_) {
|
||
last_run_start_time_proc_ = now;
|
||
} else {
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
bool TableStatic::get_clear_flag() {
|
||
string Jkey = string(CMemVar::Const()->UnitNo) + "_" + "clear_flag";
|
||
string Jkey2 = string(CMemVar::Const()->UnitNo) + "_" + "clear_flag_id";
|
||
auto reSult = m_memclient.Get(Jkey.c_str());
|
||
auto reSult2 = m_memclient.Get(Jkey2.c_str());
|
||
// this->logger_->Debug() << "Jkey:" << Jkey << " Jvalue:" << reSult << endl;
|
||
// this->logger_->Debug() << "Jkey2:" << Jkey2 << " Jvalue2:" << reSult2 <<
|
||
// endl;
|
||
string Jvalue = "0";
|
||
if (reSult == "no key" || reSult == "no value" || reSult == "1") {
|
||
auto find_r = std::find(this->table_data_.rule_ids_.begin(),
|
||
this->table_data_.rule_ids_.end(), reSult2);
|
||
if (find_r != this->table_data_.rule_ids_.end()) {
|
||
int sub_index = find_r - this->table_data_.rule_ids_.begin();
|
||
//运行时间
|
||
if (this->running_flag_ == 1) {
|
||
this->table_data_.running_time_[sub_index] =
|
||
this->select_running_by_ruleid(
|
||
this->mp_table_data_[0].rule_ids_[sub_index]);
|
||
}
|
||
//使用次数
|
||
else if (this->running_flag_ == 2) {
|
||
this->table_data_.used_times_[sub_index] = this->select_times_by_ruleid(
|
||
this->mp_table_data_[0].rule_ids_[sub_index]);
|
||
}
|
||
//重故障
|
||
if (this->fault_flag_) {
|
||
this->table_data_.fault_times_[sub_index] =
|
||
this->select_alarm_by_ruleid(
|
||
this->mp_table_data_[1].rule_ids_[sub_index]);
|
||
}
|
||
//轻故障
|
||
if (this->alrm_flag_) {
|
||
this->table_data_.alrm_times_[sub_index] = this->select_alarm_by_ruleid(
|
||
this->mp_table_data_[2].rule_ids_[sub_index]);
|
||
}
|
||
this->insert_memcache(Jkey, Jvalue);
|
||
return true;
|
||
} else {
|
||
// this->insert_memcache(Jkey, Jvalue);
|
||
this->logger_->Debug() << "ruleid: " << reSult2 << " 不存在!" << endl;
|
||
}
|
||
}
|
||
return false;
|
||
} |