You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.0 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. #include "disinfection_logs_manager.hpp"
  2. #include <dirent.h>
  3. #include <errno.h>
  4. #include "iflytop/core/components/fileutils.hpp"
  5. #include "configs/project_setting.hpp"
  6. using namespace iflytop;
  7. using namespace core;
  8. using namespace std;
  9. #define LOG_STORGE_PATH "./disinfection_logs/"
  10. DisinfectionLogger::DisinfectionLogger() {}
  11. DisinfectionLogger::~DisinfectionLogger() {
  12. if (m_logfile.is_open()) {
  13. m_logfile.close();
  14. }
  15. }
  16. void DisinfectionLogger::initialize(string log_file_name) { //
  17. logger->info("create loggers:{}", log_file_name);
  18. m_logfile.open(log_file_name, ios::out | ios::binary | ios::trunc);
  19. if (!m_logfile.is_open()) {
  20. logger->error("create loggers:{} failed", log_file_name);
  21. }
  22. }
  23. void DisinfectionLogger::write(string log) {
  24. m_logfile.write(log.c_str(), log.size());
  25. if (m_logfile.fail()) {
  26. logger->error("write log failed:{}", strerror(errno));
  27. }
  28. m_logfile.flush();
  29. }
  30. DisinfectionLogsManager::DisinfectionLogsManager(/* args */) {}
  31. DisinfectionLogsManager::~DisinfectionLogsManager() {}
  32. shared_ptr<DisinfectionLogger> DisinfectionLogsManager::createNewLogger(string log_file_name) {
  33. system(fmt::format("mkdir -p {}", LOG_STORGE_PATH).c_str());
  34. shared_ptr<DisinfectionLogger> dslogger = make_shared<DisinfectionLogger>();
  35. dslogger->initialize(fmt::format("{}{}.csv", LOG_STORGE_PATH, log_file_name));
  36. vector<string> files;
  37. list_dir_csvfile(LOG_STORGE_PATH, files);
  38. if (files.size() > MAX_DISINFECTIONLOGGER_FILE_NUM) {
  39. logger->info("delete old loggers:{}", files.back());
  40. deleteReport(files.back());
  41. }
  42. return dslogger;
  43. }
  44. static void split(const string& s, vector<string>& sv, const char delim = ' ') {
  45. sv.clear();
  46. istringstream iss(s);
  47. string temp;
  48. while (getline(iss, temp, delim)) {
  49. sv.push_back(temp);
  50. }
  51. return;
  52. }
  53. void DisinfectionLogsManager::list_dir_csvfile(string path, vector<string>& sv) {
  54. sv.clear();
  55. #if 0
  56. DIR* dir;
  57. struct dirent* ptr;
  58. if ((dir = opendir(path.c_str())) == NULL) {
  59. logger->error("Open dir {} error...", path);
  60. return;
  61. }
  62. while ((ptr = readdir(dir)) != NULL) {
  63. if (ptr->d_name[0] == '.') continue;
  64. if (ptr->d_type == 8) {
  65. string filename = ptr->d_name;
  66. if (filename.find(".csv") != string::npos) {
  67. sv.push_back(filename);
  68. }
  69. }
  70. }
  71. closedir(dir);
  72. #endif
  73. /**
  74. * @brief
  75. */
  76. DIR* dir;
  77. struct dirent* ptr;
  78. if ((dir = opendir(path.c_str())) == NULL) {
  79. logger->error("Open dir {} error...", path);
  80. return;
  81. }
  82. vector<string> files;
  83. while ((ptr = readdir(dir)) != NULL) {
  84. if (ptr->d_name[0] == '.') continue;
  85. if (ptr->d_type == 8) {
  86. string filename = ptr->d_name;
  87. if (filename.find(".csv") != string::npos) {
  88. files.push_back(filename);
  89. }
  90. }
  91. }
  92. sort(files.begin(), files.end(), [](string a, string b) { return a > b; });
  93. for (auto& file : files) {
  94. sv.push_back(file);
  95. }
  96. return;
  97. }
  98. nlohmann::json DisinfectionLogsManager::getlogger(string log_file_name) {
  99. string content = FileUtils().readFileAsString(fmt::format("{}{}.csv", LOG_STORGE_PATH, log_file_name));
  100. nlohmann::json csvcontent;
  101. vector<string> lines;
  102. split(content, lines, '\n');
  103. for (auto& line : lines) {
  104. csvcontent["content"].push_back(line);
  105. }
  106. return csvcontent;
  107. }
  108. nlohmann::json DisinfectionLogsManager::getLoggerList() {
  109. //
  110. // 1. get all files in LOG_STORGE_PATH
  111. vector<string> files;
  112. list_dir_csvfile(LOG_STORGE_PATH, files);
  113. nlohmann::json loggerlist;
  114. for (auto& file : files) {
  115. // 获取文件名去掉.csv
  116. file = file.substr(0, file.find(".csv"));
  117. loggerlist.push_back(file);
  118. }
  119. return loggerlist;
  120. }
  121. void DisinfectionLogsManager::deleteReport(string log_file_name) { system(fmt::format("rm -f {}{}.csv", LOG_STORGE_PATH, log_file_name).c_str()); }
  122. void DisinfectionLogsManager::deleteReports(vector<string> log_file_names) {
  123. for (auto& log_file_name : log_file_names) {
  124. deleteReport(log_file_name);
  125. }
  126. }