diff --git a/api/cmds/record_mgr_cmd_impl.cpp b/api/cmds/record_mgr_cmd_impl.cpp index 0e06255..84c8afd 100644 --- a/api/cmds/record_mgr_cmd_impl.cpp +++ b/api/cmds/record_mgr_cmd_impl.cpp @@ -19,14 +19,30 @@ void RecordMgrCmdImpl::exportUserBehaviorRecord(json& cmd, json& receipt) { } } void RecordMgrCmdImpl::exportDisinfectionRecord(json& cmd, json& receipt) { - auto errcode = m_dataExportService->exportDisinfectionData(); + // "keys":["2024-0516-094233","2024-0516-092446","2024-0516-092339"] + vector files; + for (auto& key : cmd["keys"]) { + files.push_back(key); + } + auto errcode = m_dataExportService->exportDisinfectionData(files); if (errcode != err::ksucc) { receipt["ackcode"] = err::zecode(errcode); receipt["ackcodeInfo"] = err::zecode2str(errcode); return; } } -void RecordMgrCmdImpl::cleanDisinfectionRecord(json& cmd, json& receipt) { dosystem("rm -rf /app/disinfection_logs/*"); } +void RecordMgrCmdImpl::cleanDisinfectionRecord(json& cmd, json& receipt) { + vector files; + for (auto& key : cmd["keys"]) { + files.push_back(key); + } + + for (auto& file : files) { + m_disinfectionLogsManager->deleteReport(file); + } + + // dosystem("rm -rf /app/disinfection_logs/*"); +} void RecordMgrCmdImpl::cleanUserBehaviorRecord(json& cmd, json& receipt) { m_dbService->cleanUserBehaviorRecord(); return; diff --git a/db/db_service.hpp b/db/db_service.hpp index 74b4da7..6c2c2f0 100644 --- a/db/db_service.hpp +++ b/db/db_service.hpp @@ -18,6 +18,7 @@ #include "iflytop/core/spdlogfactory/logger.hpp" #include "iflytop/core/thread/thread.hpp" #include "user_behavior_des.hpp" +#include "configs/project_setting.hpp" #define USER_DB "user.db" #define SETTING_DB "setting.db" @@ -25,7 +26,6 @@ #define FORMULA_DB "formula.db" #define USER_BEHAVIOR_RECORD_DB "user_behavior_record.db" -#define USER_BEHAVIOR_RECORD_DB_MAX_RECORDS 30000 /** * @brief * diff --git a/service/data_export_service.cpp b/service/data_export_service.cpp index ca7c83f..5ce8a95 100644 --- a/service/data_export_service.cpp +++ b/service/data_export_service.cpp @@ -57,7 +57,7 @@ static string getTime() { if (!x) { \ return err::ksys_copy_file_error; \ } -err::error_t DataExportService::exportDisinfectionData() { +err::error_t DataExportService::exportDisinfectionData(vector files) { lock_guard lock(lock_); string diskpath; @@ -65,7 +65,6 @@ err::error_t DataExportService::exportDisinfectionData() { logger->error("no disk detected"); return err::kharde_unfound; } - logger->info("diskpath: {}", diskpath); // 创建目录 @@ -74,7 +73,14 @@ err::error_t DataExportService::exportDisinfectionData() { // 挂载目录 IF_ERROR_RETURN(dosystem(fmt::format("mount {} /mnt/exportdata", diskpath))); // 拷贝文件 - IF_ERROR_RETURN(dosystem(fmt::format("cp -rf /app/disinfection_logs /mnt/exportdata/disinfection_logs{}", getTime()))); + // IF_ERROR_RETURN(dosystem(fmt::format("cp -rf /app/disinfection_logs /mnt/exportdata/disinfection_logs{}", getTime()))); + IF_ERROR_RETURN(dosystem(fmt::format("mkdir -p /mnt/exportdata/disinfection_logs/"))); + + for (auto& file : files) { + logger->info("copy file: {}", file); + IF_ERROR_RETURN(dosystem(fmt::format("cp -rf /app/disinfection_logs/{} /mnt/exportdata/disinfection_logs/", file))); + } + // 卸载目录 IF_ERROR_RETURN(dosystem(fmt::format("umount /mnt/exportdata"))); // 删除目录 diff --git a/service/data_export_service.hpp b/service/data_export_service.hpp index a2de7f4..de59823 100644 --- a/service/data_export_service.hpp +++ b/service/data_export_service.hpp @@ -49,7 +49,7 @@ class DataExportService { void mountDisk(); void unmountDisk(); - err::error_t exportDisinfectionData(); + err::error_t exportDisinfectionData(vector files); err::error_t exportAuditData(); private: diff --git a/service/disinfection_logs_manager.cpp b/service/disinfection_logs_manager.cpp index 8738d4a..9a08f36 100644 --- a/service/disinfection_logs_manager.cpp +++ b/service/disinfection_logs_manager.cpp @@ -4,12 +4,14 @@ #include #include "iflytop/core/components/fileutils.hpp" +#include "configs/project_setting.hpp" using namespace iflytop; using namespace core; using namespace std; #define LOG_STORGE_PATH "./disinfection_logs/" + DisinfectionLogger::DisinfectionLogger() {} DisinfectionLogger::~DisinfectionLogger() { if (m_logfile.is_open()) { @@ -37,9 +39,18 @@ DisinfectionLogsManager::~DisinfectionLogsManager() {} shared_ptr DisinfectionLogsManager::createNewLogger(string log_file_name) { system(fmt::format("mkdir -p {}", LOG_STORGE_PATH).c_str()); - shared_ptr logger = make_shared(); - logger->initialize(fmt::format("{}{}.csv", LOG_STORGE_PATH, log_file_name)); - return logger; + shared_ptr dslogger = make_shared(); + dslogger->initialize(fmt::format("{}{}.csv", LOG_STORGE_PATH, log_file_name)); + + vector files; + list_dir_csvfile(LOG_STORGE_PATH, files); + + if (files.size() > MAX_DISINFECTIONLOGGER_FILE_NUM) { + logger->info("delete old loggers:{}", files.back()); + deleteReport(files.back()); + } + + return dslogger; } static void split(const string& s, vector& sv, const char delim = ' ') { @@ -54,6 +65,7 @@ static void split(const string& s, vector& sv, const char delim = ' ') { } void DisinfectionLogsManager::list_dir_csvfile(string path, vector& sv) { sv.clear(); +#if 0 DIR* dir; struct dirent* ptr; if ((dir = opendir(path.c_str())) == NULL) { @@ -70,6 +82,32 @@ void DisinfectionLogsManager::list_dir_csvfile(string path, vector& sv) } } closedir(dir); +#endif + + /** + * @brief 读取文件夹下所有文件,并按照时间排序 + */ + + DIR* dir; + struct dirent* ptr; + if ((dir = opendir(path.c_str())) == NULL) { + logger->error("Open dir {} error...", path); + return; + } + + vector files; + while ((ptr = readdir(dir)) != NULL) { + if (ptr->d_name[0] == '.') continue; + if (ptr->d_type == 8) { + string filename = ptr->d_name; + if (filename.find(".csv") != string::npos) { + files.push_back(filename); + } + } + } + + sort(files.begin(), files.end(), [](string a, string b) { return a > b; }); + return; } @@ -96,4 +134,11 @@ nlohmann::json DisinfectionLogsManager::getLoggerList() { loggerlist.push_back(file); } return loggerlist; -} \ No newline at end of file +} + +void DisinfectionLogsManager::deleteReport(string log_file_name) { system(fmt::format("rm -f {}{}.csv", LOG_STORGE_PATH, log_file_name).c_str()); } +void DisinfectionLogsManager::deleteReports(vector log_file_names) { + for (auto& log_file_name : log_file_names) { + deleteReport(log_file_name); + } +} diff --git a/service/disinfection_logs_manager.hpp b/service/disinfection_logs_manager.hpp index cba71b0..fed5420 100644 --- a/service/disinfection_logs_manager.hpp +++ b/service/disinfection_logs_manager.hpp @@ -34,13 +34,15 @@ class DisinfectionLogsManager { DisinfectionLogsManager(/* args */); ~DisinfectionLogsManager(); - void initialize(){}; + void initialize() {}; shared_ptr createNewLogger(string log_file_name); nlohmann::json getlogger(string log_file_name); nlohmann::json getLoggerList(); + void deleteReport(string log_file_name); + void deleteReports(vector log_file_names); private: void list_dir_csvfile(string path, vector& sv);