eis/eqpalg/alg_base.h

345 lines
8.0 KiB
C++
Raw 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.

#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/expression_engine.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;
const size_t pv_num_ = 6;
VarsCache var_cache_;
const string rule_id_;
const string rule_name_;
const mix_cc::json rule_json_;
std::unique_ptr<GbLogger> gb_logger_;
std::unique_ptr<LOG>
logger_;
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;
TimePoint last_alarm_time_;
TimeDur delay_time_;
TimeDur query_interval_time_ = hours(
CMemVar::Const()->eis_qihd_interval_time);
TimeDur ihd_min_time_particles_ = 20ms;
vector<string> m_tags;
bool is_usable_;
bool is_running_ = true;
utility::AlarmPoster alarm_poster_;
string remark_ =
"0";
std::mutex lm;
RuleStatShm::RuleStatLocal rule_stat_;
std::string error_message_str_;
std::vector<ErrorCodeType> error_code_list_;
int prr_ = PRR::None;
std::unique_ptr<ExpressionEngine> expr_engine_;
DataInfo data_info_;
ConditionMonitor con_monitor_;
bool now_prr_ = false;
int task_seq = 0;
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 是否打印心跳检测日志 固定 5分钟1条
* @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_;
mix_cc::time_range_t task_time_range_;
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>
queried_data_;
std::vector<TimePoint> queried_time_;
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
* @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();
};