eis/eqpalg/utility/eqp_stat.h
Huamonarch 973921fc4b Split RuleStat display from cold data paths to reduce lock contention
Display data (alarm_value, current_value, limit_up/down, items, unit) now
goes to a local-memory DisplayCache and is serialized to JSON without any
shared memory lock. Cold data (stat_values, running_time, shear_times, etc.)
stays in shared memory for mon-cron IPC, protected by a real interprocess
mutex (boost::interprocess::interprocess_mutex) instead of the broken
process-local std::mutex. AlgBase::rule_stat_ is now RuleStatLocal with
standard types — zero changes to algorithm subclass code.
2026-05-12 15:46:01 +08:00

109 lines
3.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
/**
* @file eqp_stat.h
* @brief 处理与页面交互的展示数据 + mon↔cron 冷数据交换
*
* 展示数据:本地缓存 → get_stat_json() 拼 JSON无共享内存锁
* 冷数据:共享内存 mapboost::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);
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);
};