eis/dsm/dsm_alg.cc

84 lines
2.8 KiB
C++

#include <dsm/dsm_alg.h>
#include <dsm/utility.h>
DsmAlg::DsmAlg() {
logger_ = std::make_unique<LOG>("DsmAlg");
ruleid_norminfo_ =
std::make_unique<CMemMap<std::string, DataInfo>>(DsmMapConfig::TableName);
}
DsmAlg::~DsmAlg() {}
void DsmAlg::dsm_handle(std::string ruleid) {
auto value = ruleid_norminfo_->operator[](ruleid);
logger_->Debug() << "map size:" << ruleid_norminfo_->size() << std::endl;
if (value != nullptr) {
try {
json js1 = (*value).invert2json();
int64_t start = js1.at("start").get<int64_t>();
int64_t end = js1.at("end").get<int64_t>();
std::string data_type = "RuleNormData";
if (js1.contains("data_type")) {
data_type = js1.at("data_type").get<std::string>();
}
std::string root_dir = get_root_dir(data_type);
string file_name =
mix_cc::mix_time_t(UtilityTools::millsecond_zero2time_point(start))
.to_formatted_time("%Y-%m-%d");
std::vector<std::string> tags =
js1.at("tags").get<std::vector<std::string>>();
logger_->Debug() << "value:" << js1.dump() << std::endl;
std::string full_path =
root_dir + "/" + ruleid + "/" + file_name + ".json";
if (UtilityTools::is_file_exists(full_path)) {
logger_->Debug() << full_path << " already exists!" << endl;
return;
}
auto js2 = data_query_(
tags,
mix_cc::time_range_t(UtilityTools::millsecond_zero2time_point(start),
UtilityTools::millsecond_zero2time_point(end)));
if (js2.empty()) {
logger_->Error() << ruleid << "|no data to save!" << std::endl;
return;
}
auto res = UtilityTools::save_json_file(ruleid, file_name, js2, root_dir);
if (res == 0) {
logger_->Debug() << ruleid << "|save file:" << file_name
<< " successfully!" << endl;
return;
} else if (res == 1) {
logger_->Debug() << ruleid << "|" << file_name << " already exists!"
<< endl;
return;
}
logger_->Error() << ruleid << "|Failed to save file!" << endl;
} catch (const std::exception &e) {
logger_->Error() << e.what() << std::endl;
return;
}
}
}
std::string DsmAlg::get_root_dir(std::string data_type) {
if (data_type == "RuleNormData") {
return this->RuleNormDataRootDir;
}
return this->RuleAlertDataRootDir;
}
void DsmAlg::timed_task() {
for (auto ruleid : need_ruleid_) {
this->dsm_handle(ruleid);
}
}
void DsmAlg::dispose(int event_no, const ::Ice::ByteSeq &seq) {
string str(seq.begin(), seq.end());
logger_->Debug() << "seq:" << str << std::endl;
// str -> json
mix_cc::json json_value = mix_cc::json::parse(str);
if (json_value.contains("ruleid")) {
need_ruleid_ = json_value["ruleid"].get<std::vector<std::string>>();
}
}