From 0345b08efded2112720d02364d026b0365532432 Mon Sep 17 00:00:00 2001 From: fanxiaotong Date: Tue, 11 Mar 2025 14:34:47 +0800 Subject: [PATCH] utils --- .../Histogram/server/src/utils/FileUtils.h | 38 +++++ .../server/src/utils/HistogramProtocolUtil.h | 154 ++++++++++++++++++ 2 files changed, 192 insertions(+) create mode 100644 plugins/mindstudio-insight-plugins/Histogram/server/src/utils/FileUtils.h create mode 100644 plugins/mindstudio-insight-plugins/Histogram/server/src/utils/HistogramProtocolUtil.h diff --git a/plugins/mindstudio-insight-plugins/Histogram/server/src/utils/FileUtils.h b/plugins/mindstudio-insight-plugins/Histogram/server/src/utils/FileUtils.h new file mode 100644 index 00000000000..77640f845be --- /dev/null +++ b/plugins/mindstudio-insight-plugins/Histogram/server/src/utils/FileUtils.h @@ -0,0 +1,38 @@ +/* +* Copyright (c), Huawei Technologies Co., Ltd. 2024-2024.All rights reserved. +*/ +#ifndef FILEUTILS_H +#define FILEUTILS_H + +#include +#include +#include "iostream" +#ifdef _WIN32 +#include +namespace fs = std::filesystem; +#else + +#include + +namespace fs = std::experimental::filesystem; +#endif + +namespace Insight::Histogram::util { + + inline static std::vector GetListFilesInDirectory(const fs::path& dirPath) { + std::vector fileList; + if (!fs::exists(dirPath) || !fs::is_directory(dirPath)) { + std::cerr << "Error: path does not exist or is not a directory." << std::endl; + return fileList; + } + for (const auto& entry : fs::recursive_directory_iterator(dirPath)) { + const auto& path = entry.path(); + if (fs::is_regular_file(path)) { + fileList.push_back(path.string()); + } + } + return fileList; + } +} + +#endif //FILEUTILS_H diff --git a/plugins/mindstudio-insight-plugins/Histogram/server/src/utils/HistogramProtocolUtil.h b/plugins/mindstudio-insight-plugins/Histogram/server/src/utils/HistogramProtocolUtil.h new file mode 100644 index 00000000000..6767337ace6 --- /dev/null +++ b/plugins/mindstudio-insight-plugins/Histogram/server/src/utils/HistogramProtocolUtil.h @@ -0,0 +1,154 @@ +/* +* Copyright (c), Huawei Technologies Co., Ltd. 2024-2024.All rights reserved. +*/ +#ifndef BOARD_PLUGINS_HISTOGRAMVISUALLY_SRC_PROTOCOLUTIL_PROTOCOLUTIL_H_ +#define BOARD_PLUGINS_HISTOGRAMVISUALLY_SRC_PROTOCOLUTIL_PROTOCOLUTIL_H_ + +#include "document.h" +#include "writer.h" + +#ifdef _WIN32 +#include +namespace fs = std::filesystem; +#else +#include +namespace fs = std::experimental::filesystem; +#endif + +#include +#include +#include +#include +#include + +namespace Insight::Histogram::Protocol { +using json = rapidjson::Value; +using document_t = rapidjson::Document; + +template +static inline std::optional TryParseJson(std::string_view jsonStr, std::string &error) { + document_t doc; + doc.Parse(jsonStr.data(), jsonStr.length()); + if (doc.HasParseError()) { + constexpr size_t printErrorSize = 10; + auto offset = doc.GetErrorOffset(); + auto start = offset >= printErrorSize ? offset - printErrorSize : 0; + error = "Error code:" + std::to_string(doc.GetParseError()) + ", str:" + + std::string(jsonStr.substr(start, offset - start + printErrorSize)); + return std::nullopt; + } + return std::move(doc); +} + +static inline document_t ParseJsonToStr(std::string_view jsonStr) { + document_t doc; + doc.Parse(jsonStr.data(), jsonStr.length()); + return doc; +} + +static inline std::string DumpJsonToStr(const json &document) { + rapidjson::StringBuffer buffer; + rapidjson::Writer writer(buffer); + document.Accept(writer); + return {buffer.GetString(), buffer.GetSize()}; +} + +template +static inline void AddJsonMember(json &dst, + std::string_view key, + T &&value, + rapidjson::MemoryPoolAllocator<> &allocator) { + dst.AddMember(rapidjson::StringRef(key.data(), key.length()), std::forward(value), allocator); +} + +static inline void AddJsonMember(json &dst, + std::string_view key, + const std::string &value, + rapidjson::MemoryPoolAllocator<> &allocator) { + dst.AddMember(rapidjson::StringRef(key.data(), key.length()), + rapidjson::Value().SetString(value.data(), value.size(), allocator), + allocator); +} + +static inline void AddJsonMember(json &dst, + std::string_view key, + std::string &value, + rapidjson::MemoryPoolAllocator<> &allocator) { + dst.AddMember(rapidjson::StringRef(key.data(), key.length()), + rapidjson::Value().SetString(value.data(), value.size(), allocator), + allocator); +} + +static inline void AddJsonMember(json &dst, + std::string_view key, + std::string &&value, + rapidjson::MemoryPoolAllocator<> &allocator) { + dst.AddMember(rapidjson::StringRef(key.data(), key.length()), + rapidjson::Value().SetString(value.data(), value.size(), allocator), + allocator); +} + +template +static inline void AddJsonMember(json &dst, + std::string_view key, + std::vector &value, + rapidjson::MemoryPoolAllocator<> &allocator) { + json temp(rapidjson::kArrayType); + for (const T &item: value) { + if constexpr (std::is_same_v) { + temp.PushBack(json().SetString(item.data(), item.size(), allocator), allocator); + } else { + temp.PushBack(std::forward(item), allocator); + } + } + AddJsonMember(dst, key, temp, allocator); +} + +template +static inline void AddJsonMember(json &dst, + std::string_view key, + std::set &value, + rapidjson::MemoryPoolAllocator<> &allocator) { + json temp(rapidjson::kArrayType); + for (const T &item: value) { + if constexpr (std::is_same_v) { + temp.PushBack(json().SetString(item.data(), item.size(), allocator), allocator); + } else { + temp.PushBack(std::forward(item), allocator); + } + } + AddJsonMember(dst, key, temp, allocator); +} + +static inline void SetResponseError(int errCode, const std::string &errMsg, std::string &resultStr) { + document_t result = ParseJsonToStr(resultStr); + auto &allocator = result.GetAllocator(); + result["errCode"].SetInt(errCode); + result["msg"].SetString(errMsg.c_str(), errMsg.size(), allocator); + result["result"].SetBool(false); + resultStr = DumpJsonToStr(result); +} + +static inline std::string GetBasicResponse() { + return R"({"body":{}, "msg":"", "errCode":0, "result":true})"; +} + +static inline std::string GetReadableFileName(std::string_view path) { + if (path.empty()) { + return ""; + } + auto curPath = fs::path(path); + auto fileName = curPath.filename(); + if (!curPath.has_parent_path()) { + return std::string(path); + } + auto parentPath = curPath.parent_path(); + if (!parentPath.has_filename()) { + return fileName.string(); + } + auto parentDir = parentPath.filename(); + auto res = parentDir / fileName; + return res.string(); +} +} +#endif //BOARD_PLUGINS_HISTOGRAMVISUALLY_SRC_PROTOCOLUTIL_PROTOCOLUTIL_H_ -- Gitee