Add AsyncDbWorker: a persistent background thread with dedup queue that executes DB2 writes asynchronously, keeping the mon 20ms cycle free of blocking I/O. Changes: - async_db_worker.h/.cc: singleton worker, submit() with rule_id dedup, drain_and_stop() for clean shutdown - eqp_stat.h/.cc: new update_static(ruleid, shear_times, running_time) overload that skips redundant DB reads for known values (reduces 5 SELECTs to 3 per persist cycle) - exp_times.cc: extract persist_exp_times() as a standalone function, update_history_times() snapshots values and submits to worker (returns immediately), reset_dev_data() uses direct SHM update - eqpalg_icei.cpp: alg_mgr_.reset() → drain_and_stop() in destructor ensures all algorithm threads are stopped before draining the worker Risk: re-run cmake .. to pick up the new async_db_worker.cc file.
112 lines
3.4 KiB
C++
112 lines
3.4 KiB
C++
#pragma once
|
||
/**
|
||
* @file eqp_stat.h
|
||
* @brief 处理与页面交互的展示数据 + mon↔cron 冷数据交换
|
||
*
|
||
* 展示数据:本地缓存 → get_stat_json() 拼 JSON(无共享内存锁)
|
||
* 冷数据:共享内存 map,boost::interprocess::interprocess_mutex 同步
|
||
*
|
||
* @author your name (you@domain.com)
|
||
* @version 0.2
|
||
* @date 2023-12-21
|
||
*
|
||
* Copyright: Baosight Co. Ltd.
|
||
* DO NOT COPY/USE WITHOUT PERMISSION
|
||
*/
|
||
#include <log4cplus/LOG.h>
|
||
#include <shm/RuleStatShm.h>
|
||
#include <map>
|
||
#include <memory>
|
||
#include <mutex>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
/// 展示数据条目(本地缓存用)
|
||
struct DisplayEntry {
|
||
double alarm_value = 0;
|
||
double current_value = 0;
|
||
double limit_up = 0;
|
||
double limit_down = 0;
|
||
std::string unit;
|
||
std::vector<std::string> items;
|
||
|
||
double limit_precision(double data, int precision = 2) const {
|
||
double factor = std::pow(10, precision);
|
||
return std::round(data * factor) / factor;
|
||
}
|
||
|
||
mix_cc::json to_json() const {
|
||
mix_cc::json js;
|
||
js["alarm_value"] = limit_precision(alarm_value);
|
||
js["current_value"] = limit_precision(current_value);
|
||
js["limit_up"] = limit_precision(limit_up);
|
||
js["limit_down"] = limit_precision(limit_down);
|
||
js["unit"] = unit;
|
||
js["items"] = items;
|
||
return js;
|
||
}
|
||
};
|
||
|
||
class DisplayCache {
|
||
public:
|
||
void update(const std::string &ruleid, const RuleStatShm::RuleStatLocal &stat);
|
||
void update_static(const std::string &ruleid, const RuleStatShm::RuleStatCold &cold);
|
||
void remove(const std::string &ruleid);
|
||
std::string get_json();
|
||
|
||
private:
|
||
std::map<std::string, DisplayEntry> cache_;
|
||
std::map<std::string, std::string> static_fields_;
|
||
std::mutex mtx_;
|
||
};
|
||
|
||
class EqpStat {
|
||
public:
|
||
EqpStat();
|
||
~EqpStat();
|
||
|
||
public:
|
||
void init();
|
||
|
||
/// 写展示数据到本地缓存(mon 高频调用,线程锁,无共享内存操作)
|
||
bool update_display(const std::string &ruleid,
|
||
const RuleStatShm::RuleStatLocal &rule_stat);
|
||
|
||
/// 写冷数据到共享内存(mon 高频调用,进程间锁)
|
||
bool update_cold(const std::string &ruleid,
|
||
const RuleStatShm::RuleStatLocal &rule_stat);
|
||
|
||
/// 写静态数据到共享内存 + 更新本地展示缓存(cron/ExpTimes 调用)
|
||
bool update_static(std::string ruleid, bool is_times);
|
||
/// 同上,但 running_time / shear_times 由调用方直接传入,跳过 DB 查询
|
||
bool update_static(const std::string &ruleid, int64_t shear_times,
|
||
double running_time);
|
||
bool update_static();
|
||
|
||
/// mon 攒样本到共享内存
|
||
bool add_stat_values(std::string ruleid, const double &value);
|
||
|
||
/// cron 从共享内存取样本
|
||
bool get_stat_values(std::string ruleid, RuleStatShm::RuleStatLocal &local);
|
||
|
||
bool delete_stat(std::string ruleid);
|
||
std::string get_stat_json();
|
||
std::vector<std::string> stat_find_no_ruleid();
|
||
int get_stat_size();
|
||
void get_cfg_rules();
|
||
std::string get_ruleid_json();
|
||
|
||
private:
|
||
std::unique_ptr<LOG> logger_;
|
||
DisplayCache display_cache_;
|
||
std::vector<std::string> cfg_rules_;
|
||
bool cfg_flag = false;
|
||
std::chrono::system_clock::time_point last_update_static_time_;
|
||
|
||
double select_running_by_ruleid(std::string ruleid);
|
||
unsigned long select_times_by_ruleid(std::string ruleid);
|
||
int select_alarm_by_ruleid(std::string ruleid);
|
||
std::string select_dev_coder_by_ruleid(std::string ruleid);
|
||
std::string select_latest_alarm_by_ruleid(std::string ruleid);
|
||
};
|