// eqpalg/utility/stat_collector.h #pragma once #include #include #include #include // 前向声明 namespace DAA { class STA; } using TimePoint = std::chrono::system_clock::time_point; /** * @brief 统计学习收集器 * * 管理 DAA::STA 分布统计的生命周期: * - 样本累积(dist_add) * - 分布初始化与 DB2 持久化 * - 置信区间更新 */ class StatCollector { public: StatCollector() = default; ~StatCollector(); /** * @brief 配置统计收集器 * @param ruleId 规则 ID * @param ruleName 规则名称 * @param distMode 分布模式(0=手动, 1=在线, 2=离线) * @param isLearning 是否启用自学习 */ void configure(const std::string& ruleId, const std::string& ruleName, int distMode, bool isLearning); /** * @brief cron 进程:从共享内存获取累积样本,更新分布并持久化 * @param statValues 累积的样本值 * @param now 当前时间 * @param cronDelayHours cron 更新延迟(小时) * @param lastLoadTime [in/out] 上次加载时间 * @return 处理的样本数量 */ int processCron(const std::vector& statValues, TimePoint now, int cronDelayHours, TimePoint& lastLoadTime); /** * @brief 加载置信区间到上下限 * @param limitDown [out] 新的下限 * @param limitUp [out] 新的上限 * @param now 当前时间 * @param lastLoadTime [out] 更新时间 * @return true 成功加载新区间 */ bool reloadCiDist(double& limitDown, double& limitUp, TimePoint now, TimePoint& lastLoadTime); /** * @brief 删除统计数据和重置 */ void reset(const std::string& ruleId); bool isConfigured() const { return configured_; } // ---- task 进程专用接口 ---- /** @brief 确保 STA 实例已初始化(惰性初始化) */ void ensureInitialized(); /** @brief 重置累积数据,供新 task 使用 */ void resetData(); /** @brief 向分布添加单个值 */ void distAdd(double value); /** @brief 将 task 结果存储到 DB2 (T_SAMPLE_STAT) */ bool taskStoreDb2(const std::string& sampleId); /** @brief 获取样本统计字符串,用于 T_SAMPLE_MAG */ std::string getSampleStatStr(); /** @brief 向运行统计中添加值 */ void runningStatAdd(double value); private: std::unique_ptr sta_ptr_; std::string rule_id_; std::string rule_name_; int dist_mode_ = 0; bool is_learning_ = true; bool configured_ = false; };