// eqpalg/utility/stat_collector.cpp #include #include #include #include #include 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& statValues, TimePoint now, int cronDelayHours, TimePoint& lastLoadTime) { if (statValues.empty()) return 0; int size_data = static_cast(statValues.size()); if (sta_ptr_ == nullptr) { sta_ptr_ = std::make_unique(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(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(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); }