eis/dsm/public.h

127 lines
2.9 KiB
C
Raw Normal View History

#pragma once
/**
* @file public.h
* @brief data save handle
*
* @author your name (you@domain.com)
* @version 0.1
* @date 2026-01-29
*
* Copyright: Baosight Co. Ltd.
* DO NOT COPY/USE WITHOUT PERMISSION
*
*/
#include "mix_cc/json.h"
#include <chrono>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <zlib/MemMap.hpp>
/**
* @brief dsm用MemMap的配置
*/
struct DsmMapConfig {
static inline constexpr char TableName[] = "ZONE10"; ///<表名
static constexpr int MapMaxSize = 5000; ///< map 最大容量
};
typedef char TagName[50]; ///<数据项(tag点英文item长度限制50字节以内)
typedef TagName Tags[30]; ///<单个规则tag点个数限制30个以内
using std::vector;
/**
* @brief
*/
struct DataInfo {
int64_t start;
int64_t end;
Tags tags;
int flag = 0; ///< 0--RuleNormData1----RuleAlertData
int tag_size = 0;
void init(vector<std::string> tag_s) {
tag_size = 0;
int size_n = tag_s.size();
if (size_n == 0) {
return;
}
for (int i = 0; i < size_n; i++) {
std::strcpy(tags[i], tag_s[i].c_str());
tag_size++;
}
}
/**
* @brief
* @param _start
* @param _end
* @param _flag 0--1--
* default 0
*/
void update(int64_t _start, int64_t _end, int _flag = 0) {
start = _start;
end = _end;
flag = _flag;
}
DataInfo() {
tag_size = 0;
start = 0;
end = 0;
flag = 0;
}
DataInfo(nlohmann::json js1) {
tag_size = 0;
start = js1["start"];
end = js1["end"];
vector<std::string> tag_s = js1["tags"];
for (int i = 0; i < tag_s.size(); i++) {
std::strcpy(tags[i], tag_s[i].c_str());
tag_size++;
}
if (js1.contains("data_type")) {
std::string data_type = js1["data_type"].get<std::string>();
flag = 0;
if (data_type == "RuleAlertData") {
flag = 1;
}
}
}
nlohmann::json invert2json() {
nlohmann::json js1;
js1["start"] = start;
js1["end"] = end;
vector<std::string> tag_s;
for (int i = 0; i < tag_size; i++) {
tag_s.push_back(std::string(tags[i]));
}
js1["tags"] = tag_s;
js1["data_type"] = "RuleNormData";
if (flag == 1) {
js1["data_type"] = "RuleAlertData";
}
return js1;
}
};
class DsmHandle {
private:
std::unique_ptr<CMemMap<std::string, DataInfo>> ruleid_CMemMap_;
public:
DsmHandle() {
if (ruleid_CMemMap_ == nullptr) {
ruleid_CMemMap_ = std::make_unique<CMemMap<std::string, DataInfo>>(
DsmMapConfig::TableName, DsmMapConfig::MapMaxSize);
}
}
~DsmHandle() {}
bool insert(const std::string& ruleid, DataInfo *data_info) {
try {
ruleid_CMemMap_->insert(ruleid, data_info);
} catch (const std::exception &e) {
return false;
}
}
};