229 lines
4.8 KiB
C++
229 lines
4.8 KiB
C++
#pragma once
|
||
/**
|
||
* @file STA.h
|
||
* @brief 统计处理
|
||
* @author your name (you@domain.com)
|
||
* @version 0.1
|
||
* @date 2024-01-09
|
||
*
|
||
* Copyright: Baosight Co. Ltd.
|
||
* DO NOT COPY/USE WITHOUT PERMISSION
|
||
*
|
||
*/
|
||
#include <cmath>
|
||
#include <dlib/statistics.h>
|
||
#include <eqpalg/define/public.h>
|
||
#include <eqpalg/feature_extraction/distribution.h>
|
||
#include <log4cplus/LOG.h>
|
||
#include <map>
|
||
#include <memory>
|
||
#include <string>
|
||
#include <time.h>
|
||
#include <unordered_map>
|
||
#include <vector>
|
||
namespace DAA {
|
||
using RSD = dlib::running_stats<double>;
|
||
using std::map;
|
||
using std::string;
|
||
using std::unordered_map;
|
||
using std::vector;
|
||
const int STA_SIZE_MIN = 50;
|
||
|
||
string double2str(double data, int precision = 2);
|
||
|
||
/**
|
||
* @brief limit 识别 32768 为 ∞
|
||
* @param data My Param doc
|
||
* @param precision My Param doc
|
||
* @return string
|
||
*/
|
||
string double2strLimit(double data, int precision = 2);
|
||
|
||
/**
|
||
* @brief 数据精度控制
|
||
* @param data 原始数据
|
||
* @param precision 精度,默认2位
|
||
* @return double 控制精度后的数据
|
||
*/
|
||
double limit_precision(double data, int precision = 2);
|
||
|
||
int64_t double2int64_t(double data, bool is_need = true);
|
||
double int64_t2double(int64_t data, bool is_need = true);
|
||
|
||
/**
|
||
* @brief 范围数据,求中心数据
|
||
* eg: a0,a0+range,a0+2range,……,an,
|
||
* 返回data对应的 an
|
||
* @param a0 等差数列 默认首项
|
||
* @param range 等差数列 等差
|
||
* @param data 数据
|
||
* @return double an
|
||
*/
|
||
double arith_seq(double a0, double range, double data);
|
||
|
||
struct FreqDistData {
|
||
double X1;
|
||
size_t Count;
|
||
};
|
||
/**
|
||
* @brief 均匀分布 随机数 [min,max]
|
||
* @param _min My Param doc
|
||
* @param _max My Param doc
|
||
* @return double
|
||
*/
|
||
double RandMinMax(double _min, double _max);
|
||
|
||
/**
|
||
* @brief 一维统计
|
||
*/
|
||
class STA {
|
||
public:
|
||
STA(const string &ruleid, const string &rulename);
|
||
~STA();
|
||
|
||
/**
|
||
* @brief 分布信息 累加
|
||
* @param data My Param doc
|
||
*/
|
||
void dist_add(double data);
|
||
/**
|
||
* @brief 查看T_RULE_SAMPLE_1D_INFO 是否有记录
|
||
* @return true
|
||
* @return false
|
||
*/
|
||
bool is_init();
|
||
/**
|
||
* @brief 初始化T_RULE_SAMPLE_1D_INFO
|
||
* @param range My Param doc
|
||
* @param init_value My Param doc
|
||
* @return true
|
||
* @return false
|
||
*/
|
||
bool init(double range, double init_value);
|
||
/**
|
||
* @brief 更新T_RULE_SAMPLE_1D数据
|
||
* @return true
|
||
* @return false
|
||
*/
|
||
bool store_db2();
|
||
|
||
/**
|
||
* @brief 更新T_SAMPLE_STAT数据
|
||
* @return true
|
||
* @return false
|
||
*/
|
||
bool task_store_db2(string sampleid);
|
||
|
||
size_t size();
|
||
/**
|
||
* @brief mon接口 获取最新值信区间
|
||
* @return mix_cc::float_range_t
|
||
*/
|
||
static mix_cc::float_range_t select_from_t_rule_feature(std::string ruleid);
|
||
|
||
/**
|
||
* @brief mon接口 获取最新值信区间
|
||
* @return mix_cc::float_range_t
|
||
*/
|
||
static mix_cc::float_range_t select_from_t_sample_mag(std::string ruleid);
|
||
/**
|
||
* @brief 删除ruleid对应的统计信息
|
||
* T_RULE_SAMPLE_1D,
|
||
* T_RULE_SAMPLE_1D_INFO,
|
||
* T_SAMPLE_RECORD,
|
||
* T_RULE_SAMPLE_FEATURE
|
||
* @param ruleid My Param doc
|
||
* @return int
|
||
*/
|
||
static int delete_statistics_data(std::string ruleid);
|
||
|
||
/**
|
||
* @brief cron接口 更新 置信区间
|
||
* @return int
|
||
*/
|
||
int update_ci_dist();
|
||
/**
|
||
* @brief 重置统计信息
|
||
* 1.
|
||
* @return true
|
||
* @return false
|
||
*/
|
||
bool reset_data();
|
||
/**
|
||
* @brief task接口 更新 置信区间
|
||
* @return int
|
||
*/
|
||
int task_update_ci_dist();
|
||
|
||
/**
|
||
* @brief task接口
|
||
* 将统计数据 放进json,用于存T_SAMPLE_MAG
|
||
* cron内部接口
|
||
* 用于存T_SAMPLE_RECORD
|
||
* @return string
|
||
*/
|
||
string get_sample_stat_str();
|
||
|
||
void running_stat_add(double data);
|
||
|
||
private:
|
||
string rule_id_;
|
||
RSD running_stat_;
|
||
bool is_init_ = false;
|
||
std::unique_ptr<LOG> logger_;
|
||
Dist dist_1d_;
|
||
bool is_need_ = true;
|
||
double range_;
|
||
double init_value_;
|
||
map<int64_t, int64_t> dist_data_;
|
||
|
||
double scale_;
|
||
static constexpr size_t k_dest_dump_size = 10000;
|
||
|
||
vector<FreqDistData> sample_1d_data_;
|
||
|
||
mix_cc::float_range_t dist_range_ci_;
|
||
|
||
bool is_task_ = false;
|
||
|
||
SampleStat sample_stat_;
|
||
|
||
int seq_ = 0;
|
||
|
||
private:
|
||
/**
|
||
* @brief 默认初始化
|
||
*/
|
||
void init();
|
||
/**
|
||
* @brief 更新running_stat_
|
||
* @return int
|
||
*/
|
||
int update_running_stat();
|
||
/**
|
||
* @brief 更新 t_rule_sample_feature
|
||
* @return int
|
||
*/
|
||
int update_t_rule_sample_feature();
|
||
/**
|
||
* @brief 更新t_sample_record
|
||
* @return int
|
||
*/
|
||
int update_t_sample_record();
|
||
/**
|
||
* @brief 更新sample_stat_
|
||
* 统计样本 结果 json
|
||
* 包括 均值 标准差 偏度 斜度等
|
||
* @return int
|
||
*/
|
||
int update_t_sample_mag();
|
||
|
||
bool reset_data(double range, double init_value);
|
||
/**
|
||
* @brief Get the seq object
|
||
* @return int
|
||
*/
|
||
int get_seq();
|
||
};
|
||
|
||
} |