eis/eqpalg/alg_base.h

364 lines
10 KiB
C
Raw Normal View History

#pragma once
/**
* @file eqpalg/alg_base.h
* @brief
* @author Cat (null.null.null@qq.com)
* @version 0.1
* @date 2021-09-17
*
* Copyright: Baosight Co. Ltd.
* DO NOT COPY/USE WITHOUT PERMISSION
*
*/
#include "mix_cc/ihyper_db.h"
#include "mix_cc/json.h"
#include "mix_cc/matheval/matheval.hpp"
#include <chrono>
#include <dsm/public.h>
#include <eqpalg/define/error_code.h>
#include <eqpalg/define/public.h>
#include <eqpalg/gb_logger.h>
#include <eqpalg/utility/ExpModule.h>
#include <eqpalg/utility/VarsCache.hpp>
#include <eqpalg/utility/XorShift128Plus.hpp>
#include <eqpalg/utility/alarm_poster.h>
#include <eqpalg/utility/condition_monitor.hpp>
#include <eqpalg/utility/eqp_stat.h>
#include <memory>
#include <mutex>
#include <shm/RuleStatShm.h>
#include <shm/SingletonTemp.hpp>
#include <string>
#include <unordered_map>
#include <vector>
#include <zlib/MemVar.h>
using std::string;
using namespace std::chrono;
using std::chrono::system_clock;
using TimePoint = system_clock::time_point;
using TimeDur = milliseconds;
/**
* @brief
*/
class AlgBase {
protected:
std::map<std::string, double> mm_vars; ///< 表达式系统变量map
const size_t pv_num_ = 6;
VarsCache var_cache_; ///< mmvar_变量明 预分配
const string rule_id_; ///< 算法实例id,在AlgBase基类内部初始化
const string rule_name_; ///< 算法实例名称在AlgBase内部初始化
const mix_cc::json rule_json_; ///< 规则json对象,在AlgBase基类内部初始化
std::unique_ptr<GbLogger> gb_logger_; ///< 全局logger指针
std::unique_ptr<LOG>
logger_; ///< 基类的logger指针,子类需要使用使用reset方法重置为子类的指针
TimePoint last_run_start_time_; ///< 上次运行的开始时间
TimePoint last_save_start_time_; ///< 上次调用保存数据函数的开始时间
TimePoint
last_rule_state_updae_start_time_; ///< 上次调用保存数据函数的开始时间
TimePoint last_heart_beat_start_time_; ///< 上次规则运行 活跃 心跳检测日志
TimePoint this_run_start_time_; ///< 本次运行的开始时间
TimePoint now_time_; ///< 当前正在执行时的时间
int data_source_ = 1; ///< 数据来源,0:iHyperDB,1:memory
TimePoint last_alarm_time_; ///< 上次报警的时间,用于外部类使用
TimeDur delay_time_; ///< 每次执行的时间间隔在AlgBase内部初始化
TimeDur query_interval_time_ = hours(
CMemVar::Const()->eis_qihd_interval_time); ///< cron task 查询最大时间区间
// TimeDur ihd_min_time_particles_ = 50ms; ///<
// iHyperDB数据查询最小时间颗粒
TimeDur ihd_min_time_particles_ = 20ms; ///< iHyperDB数据查询最小时间颗粒
vector<string> m_tags; ///< 保存的tag点名
bool is_usable_; ///< 算法是否可用
// bool is_cron_run = false; ///< cron是否已经完成
bool is_running_ = true; ///< 算法是否正在执行
// int smooth_type_ = 0; ///<
// 数据平滑方式0为不更改1为高斯,此部分暂时未完成
utility::AlarmPoster alarm_poster_; ///< 发送报警信息
string remark_ =
"0"; ///<通知方式 0-不通知1-短信+语音单次通知 2-短信单次+语音多次
std::mutex lm;
RuleStatShm::RuleStat rule_stat_; ///<规则的统计参数,给页面
std::string error_message_str_; ///<参数错误提示语
std::vector<ErrorCodeType> error_code_list_; ///<错误码{错误类型,错误位置}
int prr_ = PRR::None; ///<规则运行前提条件
// int spd_th_ = 1; ///<规则运行前提条件
// bool need_prr_ = false; ///<是否需要规则执行的前提条件
// std::string pre_item_; ///<前提条件 item
// double pre_min_ = 0; ///<前提条件最小值
std::unique_ptr<ExpModule> exp_mpdule_ptr_; ///<表达式模块
DataInfo data_info_; ///<运行数据保存信息 保存正常数据
ConditionMonitor con_monitor_; ///<保存正常数据 是否保存判断
bool now_prr_ = false; ///<前提条件是否满足
bool is_exp_alg_ = false; ///<是否是表达式类算法
int task_seq = 0; ///< task线程 线程id
TimeDur save_interval_ms_ = 5000ms; ///< 正常数据保存 间隔时间
TimeDur rule_state_update_interval_ms_ = 500ms; ///< 页面数据更新 间隔时间
public:
void logReset(int task_seq);
/**
* @brief map
* @return true
* @return false
*/
bool update_map_rule();
/**
* @brief
* @param name
* @param rulejson
* @param ruleId id
*/
AlgBase(const string &name, const mix_cc::json &rule_json,
const string &ruleId);
virtual ~AlgBase();
/**
* @brief
* 1.tag点
* 2.
* 3.
* @return int
*/
virtual int init();
/**
* @brief start end
*/
virtual void save_rule_norm_data();
/**
* @brief
* @return true
* @return false
*/
bool get_cycled();
/**
* @brief
* 线
* @return true
* @return false
*/
bool get_save_data_cycled();
/**
* @brief
* 线
* @return true
* @return false
*/
bool get_update_rule_stat_cycled();
/**
* @brief 51
* @return true
* @return false
*/
bool get_heart_beat_log_cycled();
/**
* @brief
* @param usable
*/
virtual void set_usable(bool usable);
/**
* @brief
* @return true
* @return false
*/
bool get_usable() { return this->is_usable_; }
/**
* @brief
* @return true
* @return false
*/
virtual bool get_prr();
/**
* @brief Get the rule id object
* @return std::string
*/
std::string get_rule_id() { return this->rule_id_; }
/**
* @brief Get the rule name object
* @return std::string
*/
std::string get_rule_name() { return this->rule_name_; }
/**
* @brief
* @param time_point My Param doc
*/
virtual void set_last_alarm_time(TimePoint time_point = system_clock::now());
/**
* @brief
* @return TimePoint
*/
TimePoint get_last_alarm_time();
/**
* @brief
* @return AlarmInfo
*/
virtual AlarmInfo exec_mon() = 0;
/**
* @brief
*
* @param time_range
* @return AlarmInfo
*/
virtual std::vector<AlarmInfo> exec_task(mix_cc::time_range_t time_range) = 0;
// normal 单次执行
virtual AlarmInfo exec_normal_task(mix_cc::time_range_t time_range) {
return AlarmInfo{};
};
/**
* @brief
* (json形式)
* @return mix_cc::json
*/
virtual mix_cc::json exec_cron();
/**
* @brief threads进行调用
* 使
*/
void exec_mon_call();
/**
* @brief
*/
void exec_task_call(const mix_cc::time_range_t &time_range);
void exec_normal_task_call(const mix_cc::time_range_t &time_range);
/**
* @brief
*/
void exec_cron_call();
/**
* @brief Get the is cron run object
* 线cron进程用
* @return true
* @return false
*/
// bool get_is_cron_run(){return this->is_cron_run};
protected:
mix_cc::time_range_t query_time_range_; ///< ihdb查询的时间周期
mix_cc::time_range_t task_time_range_; ////<task进程的时间范围
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>
queried_data_; ///< ihd查询后的数据缓存
std::vector<TimePoint> queried_time_; ///< ihdb查询后的时间缓存
protected:
/**
* @brief ihyperdb中刷新该算法所对应的数据
*
* queried_data_ queried_time_变量中
* @return int
*/
int refresh_ihd_cache();
/**
* @brief task用 ihyperdb中刷新该算法所对应的数据
* @param delay_time ihd间隔时间
* @return int
*/
int refresh_ihd_cache(TimeDur delay_time);
/**
* @brief ihd到 queried_data_
* @param time_range
* @return int 0--
*/
int refresh_ihd_cache(mix_cc::time_range_t time_range);
/**
* @brief tag点名称信息
* @return int
*/
int reload_config_tag();
/**
* @brief now_time_
* @return int
*/
int refresh_now_time();
/**
* @brief tag和时间范围ihd到 queried_data_
* @param tag tag点
* @param time_range
* @return int 0--
*/
int ihd_get_by_tag(string tag, mix_cc::time_range_t time_range);
public:
/**
* @brief
* @return int
*/
int get_data_source();
/**
* @brief
* ExpTimes用
*/
virtual void reset_dev_data(){};
/**
* @brief
*
* @return int
*/
// int update_remark();
/**
* @brief
* @param lb
* @param ub
* @param va
* @param stime
* @param etime
*/
virtual void update_limit_alarm(double lb, double ub, double va,
int64_t stime = 0, int64_t etime = 0);
protected:
/**
* @brief Get the alarm time object
* @return mix_cc::time_range_t
*/
virtual mix_cc::time_range_t get_alarm_time();
};