eis/eqpalg/.do_not_use/otheralg/times_stastics.cpp

314 lines
12 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <eqpalg/algs/times_stastics.h>
#include <eqpalg/table_struct/t_rule_sample_1d.h>
#include <eqpalg/utility/build_alarm_info.h>
#include <cstdlib>
#include <limits>
#include <string>
TimesStastics::TimesStastics(const string name, const mix_cc::json& rule_json,
const string ruleId)
: AlgBase(name, rule_json, ruleId) {
logger_.reset(new LOG("TimesStastics:" + rule_name_, AUTO_CATCH_PID));
}
TimesStastics::~TimesStastics() {}
int TimesStastics::init() {
int ret = 0;
last_load_time_ = system_clock::now();
AlgBase::init();
this->data_source_ = 1;
this->used_mode_ =
this->rule_json_.at("action_condition").at("used_mode").at(1).get<int>();
this->rw_time_ =
this->rule_json_.at("refresh_period").at("wr_time").at(1).get<int>();
this->logger_->Debug() << "监控模式:1剪切次数;2使用时间 " << this->used_mode_
<< " 读写周期" << this->rw_time_ << " mins"
<< std::endl;
// if (get_history_times() != 0) {
// this->logger_->Debug() << "暂无历史记录" << std::endl;
// }
std::string maxTimes_str = this->rule_json_.at("action_condition")
.at("maxTimes")
.at(1)
.get<std::string>();
if (this->used_mode_ == 1) {
mm_vars_["tag1"] =
SingletonTemplate<GlobaltemSharedMemory>::GetInstance()[m_tags[0]];
this->maxTimes_ = std::strtoul(maxTimes_str.c_str(), nullptr, 10);
this->logger_->Debug() << "剪切次数 "
<< "maxTimes:" << this->maxTimes_ << std::endl;
} else if (this->used_mode_ == 2) {
mm_vars_["tag1"] = 0; ///设置初始p1==0,防止遗漏程序启用时 使用状态的统计
this->maxUsedHours_ = std::stod(maxTimes_str);
this->logger_->Debug() << "使用时间 "
<< "maxUsedHours:" << this->maxUsedHours_
<< std::endl;
} else {
mm_vars_["tag1"] =
SingletonTemplate<GlobaltemSharedMemory>::GetInstance()[m_tags[0]];
}
return ret;
}
int TimesStastics::refresh_mm_vars() {
this->refresh_now_time();
mm_vars_["p1"] = mm_vars_["tag1"];
mm_vars_["tag1"] =
SingletonTemplate<GlobaltemSharedMemory>::GetInstance()[m_tags[0]];
return 0;
}
int TimesStastics::times_add() {
if (((this->used_mode_ == 1 || this->used_mode_ == 3) &&
this->now_times_ >= std::numeric_limits<unsigned long>::max() - 1) ||
(this->used_mode_ == 2 &&
this->now_used_hours_ >= std::numeric_limits<double>::max() - 1)) {
this->logger_->Debug() << "数据已超限! max(long):"
<< std::numeric_limits<unsigned long>::max()
<< " max(double):"
<< std::numeric_limits<double>::max() << std::endl;
return -1;
}
// test
// {
// if (this->used_mode_ == 1) {
// this->now_times_++;
// this->logger_->Debug()
// << "----------Test!----------:\n now_times:" << this->now_times_
// << std::endl;
// } else {
// this->now_used_hours_ = this->now_used_hours_ + 10;
// this->logger_->Debug() << "----------Test!----------:\n
// now_used_hours:"
// << this->now_used_hours_ << std::endl;
// }
// return 0;
// }
if (this->used_mode_ == 1) {
if (mm_vars_["p1"] == 0 && mm_vars_["tag1"] > 0.5) {
this->now_times_++;
this->logger_->Debug() << "now_times:" << this->now_times_ << std::endl;
}
} else if (this->used_mode_ == 2) {
///
if (!start_flag_) {
// if (mm_vars_["p1"] == 0 && mm_vars_["tag1"] > 0.5) {
if (mm_vars_["tag1"] > 0.5) {
this->act_start_ = system_clock::now();
start_flag_ = true;
this->logger_->Debug()
<< "开始计时:"
<< mix_cc::mix_time_t(this->act_start_).to_formatted_time()
<< std::endl;
}
} else {
std::chrono::duration<double, std::milli> duratime_ms =
system_clock::now() - this->act_start_;
int duratime = duratime_ms.count() / 60000;
if (mm_vars_["tag1"] == 0) {
start_flag_ = false;
this->now_used_hours_ += double(duratime) / 60.0;
this->logger_->Debug()
<< "计时结束:"
<< mix_cc::mix_time_t(system_clock::now()).to_formatted_time()
<< " 本次统计的使用时间:" << duratime << "mins " << std::endl;
} else {
if (start_flag_ && duratime >= this->rw_time_) {
// start_flag_ = false;
// std::chrono::duration<double, std::milli> duratime_ms =
// system_clock::now() - this->act_start_;
// int duratime = duratime_ms.count() / 60000;
this->now_used_hours_ += double(duratime) / 60.0;
this->logger_->Debug()
<< "本次使用已超过:" << this->rw_time_
<< " mins 暂存一次数据 当前时间:"
<< mix_cc::mix_time_t(system_clock::now()).to_formatted_time()
<< " 本次统计的使用时间:" << duratime << "mins " << std::endl;
this->act_start_ = system_clock::now();
}
}
}
} else {
if (mm_vars_["p1"] == 1 && mm_vars_["tag1"] == 0) {
this->now_times_++;
this->logger_->Debug()
<< this->rule_name_ << " 报警消除,报警次数:" << this->now_times_
<< std::endl;
}
}
return 0;
}
int TimesStastics::get_history_times() {
// HistoryTable trs1a;
T_RULE_SAMPLE_1D trs1a;
auto query_list_maybe = exec<db2_t, T_RULE_SAMPLE_1D>(
select(trs1a.Flag(), trs1a.Count(), trs1a.X1())
.from(trs1a)
.where(trs1a.RuleId() == this->rule_id_));
if (query_list_maybe.is_just()) {
auto& query_list = query_list_maybe.unsafe_get_just();
if (query_list.empty()) {
this->logger_->Error() << "db2 查询为空!" << std::endl;
return -2;
} else {
if (this->used_mode_ == 1 || this->used_mode_ == 3) {
this->now_times_ = query_list[0].Count;
} else {
this->now_used_hours_ = query_list[0].X1;
}
// return 0;
}
} else {
this->logger_->Error() << "db2 查询失败!" << std::endl;
return -1;
}
return 0;
}
int TimesStastics::update_history_times() {
this->logger_->Debug() << "-----update db2------"
<< " start_flag_:" << std::to_string(this->start_flag_)
<< std::endl;
auto now_times = this->now_times_;
auto now_used_time = this->now_used_hours_;
if (get_history_times() == -2) {
// insert into
this->insert_history_times(now_times, now_used_time);
} else {
T_RULE_SAMPLE_1D trs1a;
if (this->used_mode_ == 1 || this->used_mode_ == 3) {
exec<db2_t, size_t>(update(trs1a)
.set(trs1a.Count() = now_times)
.where(trs1a.RuleId() == this->rule_id_));
} else {
exec<db2_t, size_t>(update(trs1a)
.set(trs1a.X1() = now_used_time)
.where(trs1a.RuleId() == this->rule_id_));
}
}
this->now_times_ = now_times;
this->now_used_hours_ = now_used_time;
return 0;
}
int TimesStastics::insert_history_times(unsigned long now_times,
double now_used_time) {
T_RULE_SAMPLE_1D trs1a;
this->logger_->Debug() << "-----insert db2------" << std::endl;
if (this->used_mode_ == 1 || this->used_mode_ == 3) {
exec<db2_t, size_t>(
insert_into(trs1a).set(trs1a.RuleId() = this->rule_id_, trs1a.X1() = 1,
trs1a.Count() = now_times, trs1a.Flag() = 1));
this->logger_->Debug() << "当前次数:" << now_times << std::endl;
} else {
exec<db2_t, size_t>(insert_into(trs1a).set(
trs1a.RuleId() = this->rule_id_, trs1a.X1() = now_used_time,
trs1a.Count() = 1, trs1a.Flag() = 1));
this->logger_->Debug() << "当前使用时间:" << now_used_time << std::endl;
}
return 0;
}
AlarmInfo TimesStastics::exec_mon() {
AlarmInfo out_alarm{};
this->refresh_mm_vars();
/*从db2重置数据*/
if (!history_get_flag_) {
if (now_time_ - last_load_time_ > seconds(10)) {
if (get_history_times() != 0) {
this->logger_->Debug() << "暂无历史记录" << std::endl;
}
history_get_flag_ = true;
}
return out_alarm;
}
if (times_add() == -1) {
return out_alarm;
}
if (now_time_ - last_load_time_ > minutes(this->rw_time_)) {
this->update_history_times();
// this->logger_->Debug() << this->rule_name_
// << " start_flag_:" << this->start_flag_ << endl;
last_load_time_ = now_time_;
out_alarm = this->mon_proc();
}
// this->logger_->Debug() << "this->maxTimes_" << this->maxTimes_ <<
// std::endl;
return out_alarm;
}
AlarmInfo TimesStastics::mon_proc() {
AlarmInfo out_alarm{};
if (this->used_mode_ == 1) {
if (this->now_times_ > this->maxTimes_ &&
this->now_times_ > this->last_times_) {
this->last_times_ = this->now_times_;
query_time_range_.set_left(this->now_time_ - this->delay_time_);
query_time_range_.set_right(this->now_time_);
std::string msg = "当前次数:" + std::to_string(this->now_times_) +
"已达设定最大次数:" + std::to_string(this->maxTimes_) +
" ";
this->logger_->Debug() << "报警!" << msg << std::endl;
out_alarm =
utility::build_alarm_info(MsgLevel::ERROR, rule_id_, rule_name_,
"TimesStastics", msg, query_time_range_);
}
} else if (this->used_mode_ == 2) {
if (this->now_used_hours_ > this->maxUsedHours_ &&
this->now_used_hours_ > this->last_used_hours_) {
this->last_used_hours_ = this->now_used_hours_;
query_time_range_.set_left(this->now_time_ - this->delay_time_);
query_time_range_.set_right(this->now_time_);
std::string msg = "当前累积使用时间:" +
std::to_string(this->now_used_hours_) +
"小时 已达设定最大使用时间:" +
std::to_string(this->maxUsedHours_) + "小时 ";
this->logger_->Debug() << "报警!" << msg << std::endl;
out_alarm =
utility::build_alarm_info(MsgLevel::ERROR, rule_id_, rule_name_,
"TimesStastics", msg, query_time_range_);
}
} else {
///报警统计
return out_alarm;
}
return out_alarm;
}
std::vector<AlarmInfo> TimesStastics::exec_task(
mix_cc::time_range_t time_range) {
return {};
}
void TimesStastics::reset_dev_data() {
T_RULE_SAMPLE_1D trs1a;
last_load_time_ = now_time_; ///<防止刷新数据
exec<db2_t, size_t>(update(trs1a)
.set(trs1a.Count() = 0, trs1a.X1() = 0)
.where(trs1a.RuleId() == this->rule_id_));
auto reSult = m_memclient.Insert(this->Jkey_.c_str(), this->Jvalue_.c_str());
auto reSult2 = m_memclient.Insert(string(this->Jkey_ + "_id").c_str(),
this->rule_id_.c_str());
if (1 != reSult || 1 != reSult2) {
this->logger_->Debug() << "m_memclient.Insert异常[0,set data "
"fail;-1,key=null or value=null] Insert1:"
<< reSult << " Insert2:" << reSult2 << endl;
}
}
void TimesStastics::set_usable(bool usable) {
if (usable) {
this->get_history_times();
start_flag_ = false;
} else {
this->update_history_times();
}
this->is_usable_ = usable;
}