eis/eqpalg/utility/stat_collector.cpp
Huamonarch 4f8eecd828 refactor: 提取 StatCollector 统计学习组件
将 ExpBase::cron_proc() 中的 DAA::STA 统计学习逻辑提取到独立的
StatCollector 工具类,统一管理分布统计的生命周期(样本累积、分布
初始化、DB2 持久化、置信区间更新)。同时将 exec_task() 与
task_mon_pro() 中的任务相关 STA 操作也委托给 StatCollector。

新增:
- eqpalg/utility/stat_collector.h  -- StatCollector 接口
- eqpalg/utility/stat_collector.cpp -- StatCollector 实现

修改:
- eqpalg/algs/exp_base.h  -- 替换 sta_ptr_ 为 stat_collector_
- eqpalg/algs/exp_base.cpp -- cron_proc/reload_ci_dist/reset_dev_data/
                               exec_task/task_mon_pro 委托给 StatCollector
2026-05-15 14:21:36 +08:00

113 lines
3.3 KiB
C++

// eqpalg/utility/stat_collector.cpp
#include <eqpalg/utility/stat_collector.h>
#include <eqpalg/feature_extraction/daa.h>
#include <eqpalg/define/public.h>
#include <algorithm>
#include <log4cplus/LOG.h>
StatCollector::~StatCollector() = default;
void StatCollector::configure(const std::string& ruleId,
const std::string& ruleName,
int distMode, bool isLearning) {
rule_id_ = ruleId;
rule_name_ = ruleName;
dist_mode_ = distMode;
is_learning_ = isLearning;
configured_ = true;
}
int StatCollector::processCron(const std::vector<double>& statValues,
TimePoint now, int cronDelayHours,
TimePoint& lastLoadTime) {
if (statValues.empty()) return 0;
int size_data = static_cast<int>(statValues.size());
if (sta_ptr_ == nullptr) {
sta_ptr_ = std::make_unique<DAA::STA>(rule_id_, rule_name_);
sta_ptr_->update_ci_dist();
lastLoadTime = now;
}
if (now - lastLoadTime > std::chrono::hours(cronDelayHours)) {
sta_ptr_->update_ci_dist();
lastLoadTime = now;
}
if (sta_ptr_->is_init()) {
for (int i = 0; i < size_data; i++) {
sta_ptr_->dist_add(statValues[i]);
}
} else {
double max_data = *std::max_element(statValues.begin(), statValues.end());
double min_data = *std::min_element(statValues.begin(), statValues.end());
double range = (max_data - min_data) / static_cast<double>(DAA::STA_SIZE_MIN);
if (range < 0.1) range = 0.1;
if (sta_ptr_->init(range, min_data)) {
for (int i = 0; i < size_data; i++) {
sta_ptr_->dist_add(statValues[i]);
}
}
}
sta_ptr_->store_db2();
return size_data;
}
bool StatCollector::reloadCiDist(double& limitDown, double& limitUp,
TimePoint now, TimePoint& lastLoadTime) {
if (dist_mode_ == 0) return false; // 手动模式,不自动加载
lastLoadTime = now;
mix_cc::float_range_t dist_range;
if (dist_mode_ == DistMode::Online) {
dist_range = DAA::STA::select_from_t_rule_feature(rule_id_);
} else if (dist_mode_ == DistMode::Offline) {
dist_range = DAA::STA::select_from_t_sample_mag(rule_id_);
} else {
return false;
}
if (dist_range.get_distance() == 0 ||
dist_range.get_left() > dist_range.get_right()) {
return false;
}
limitDown = dist_range.get_left();
limitUp = dist_range.get_right();
return true;
}
void StatCollector::reset(const std::string& ruleId) {
DAA::STA::delete_statistics_data(ruleId);
sta_ptr_.reset();
}
void StatCollector::ensureInitialized() {
if (sta_ptr_ == nullptr) {
sta_ptr_ = std::make_unique<DAA::STA>(rule_id_, rule_name_);
}
}
void StatCollector::resetData() {
sta_ptr_->reset_data();
}
void StatCollector::distAdd(double value) {
sta_ptr_->dist_add(value);
}
bool StatCollector::taskStoreDb2(const std::string& sampleId) {
return sta_ptr_->task_store_db2(sampleId);
}
std::string StatCollector::getSampleStatStr() {
return sta_ptr_->get_sample_stat_str();
}
void StatCollector::runningStatAdd(double value) {
sta_ptr_->running_stat_add(value);
}