diff --git a/AyonBuild.py b/AyonBuild.py index f98b73e..becf107 100644 --- a/AyonBuild.py +++ b/AyonBuild.py @@ -19,6 +19,15 @@ AyonCppApiPrj.setup_prj() +SetDefaultVars = Project.Stage("SetDefaultVars") +SetDefaultVars.add_funcs( + Project.Func("", AyonCppApiPrj.setVar, "AYON_CPP_API_ENALBE_GBENCH", "OFF"), + Project.Func("", AyonCppApiPrj.setVar, "AYON_CPP_API_ENALBE_GTEST", "OFF"), + Project.Func("", AyonCppApiPrj.setVar, "JTRACE", "0"), + Project.Func("", AyonCppApiPrj.setVar, "ReleaseType", "Release"), +) +AyonCppApiPrj.add_stage(SetDefaultVars) + SetTestVars = Project.Stage("SetTestVars") SetTestVars.add_funcs( Project.Func("", AyonCppApiPrj.setVar, "AYON_CPP_API_ENALBE_GBENCH", "ON"), diff --git a/ext/ayon-cpp-dev-tools b/ext/ayon-cpp-dev-tools index 9831c56..0b5bf00 160000 --- a/ext/ayon-cpp-dev-tools +++ b/ext/ayon-cpp-dev-tools @@ -1 +1 @@ -Subproject commit 9831c56a3d8e61385dcc706d003086bd6e43f4d8 +Subproject commit 0b5bf00b911a8a57560c08ab7c79089d6e846baa diff --git a/src/AyonCppApi/AyonCppApi.cpp b/src/AyonCppApi/AyonCppApi.cpp index 2832128..af65a1e 100644 --- a/src/AyonCppApi/AyonCppApi.cpp +++ b/src/AyonCppApi/AyonCppApi.cpp @@ -97,7 +97,7 @@ AyonApi::AyonApi(const std::optional &logFilePos, const std::string &ayonProjectName, const std::string &siteId, std::optional concurrency) - : m_num_threads(concurrency.value_or(std::max(int(std::thread::hardware_concurrency() / 2), 1))), + : m_numThreads(concurrency.value_or(std::max(int(std::thread::hardware_concurrency() / 2), 1))), m_authKey(authKey), m_serverUrl(serverUrl), m_ayonProjectName(ayonProjectName), @@ -135,7 +135,7 @@ AyonApi::AyonApi(const std::optional &logFilePos, } } - // ----------- Init m_Logger (Singleton Logic) + // ----------- Init m_log (Singleton Logic) std::cout << "[AyonApi] Retrieving AyonLogger Singleton..." << std::endl; AyonLogger& loggerRef = AyonLogger::getInstance(); @@ -144,73 +144,74 @@ AyonApi::AyonApi(const std::optional &logFilePos, loggerRef.initFileLogger(logPath.string()); } - m_Log = std::shared_ptr(&loggerRef, [](AyonLogger*){}); + m_log = std::shared_ptr(&loggerRef, [](AyonLogger*){}); - m_Log->registerLoggingKey("AyonApi"); + m_log->registerLoggingKey("AyonApi"); - m_Log->LogLevelInfo(); - m_Log->info(m_Log->key("AyonApi"), "Init AyonServer httplib::Client"); + m_log->LogLevelInfo(); + m_log->info(m_log->key("AyonApi"), "Init AyonServer httplib::Client"); - m_AyonServer = std::make_unique(m_serverUrl); - m_Log->info(m_Log->key("AyonApi"), "After creating httplib::Client - {}", m_serverUrl); + m_ayonServer = std::make_unique(m_serverUrl); + m_log->info(m_log->key("AyonApi"), "After creating httplib::Client - {}", m_serverUrl); if (isSSL()) { std::string ayonSSLPath = std::getenv("AYON_SSL_CERT_PATH") ? std::getenv("AYON_SSL_CERT_PATH") : ""; if (!ayonSSLPath.empty()) { - m_Log->info(m_Log->key("AyonApi"), "Using AYON_SSL_CERT_PATH: {}", ayonSSLPath); - m_AyonServer->set_ca_cert_path(ayonSSLPath.c_str()); + m_log->info(m_log->key("AyonApi"), "Using AYON_SSL_CERT_PATH: {}", ayonSSLPath); + m_ayonServer->set_ca_cert_path(ayonSSLPath.c_str()); } else { - m_Log->warn(m_Log->key("AyonApi"), "No AYON_SSL_CERT_PATH set, trying to get OpenSSL dir"); + m_log->warn(m_log->key("AyonApi"), "No AYON_SSL_CERT_PATH set, trying to get OpenSSL dir"); try { setSSL(); } catch (const std::exception &e) { - m_Log->error("Failed to get OpenSSL directory: {}", e.what()); - m_AyonServer->set_ca_cert_path(nullptr); + m_log->error("Failed to get OpenSSL directory: {}", e.what()); + m_ayonServer->set_ca_cert_path(nullptr); } } - m_AyonServer->enable_server_certificate_verification(true); + m_ayonServer->enable_server_certificate_verification(true); } - if (!m_AyonServer) { - m_Log->error("m_AyonServer is null. serverUrl='{}'", m_serverUrl); + if (!m_ayonServer) { + m_log->error("m_ayonServer is null. serverUrl='{}'", m_serverUrl); throw std::runtime_error("AyonApi: HTTP client not initialized"); } httplib::Result res; try { - res = m_AyonServer->Get("/api/info"); + res = m_ayonServer->Get("/api/info"); } catch (const std::exception& e) { - m_Log->error("Exception during GET /api/info: {}", e.what()); + m_log->error("Exception during GET /api/info: {}", e.what()); throw; } if (!res) { - m_Log->error("Failed to connect to the Ayon server."); + m_log->error("Failed to connect to the Ayon server."); } else { - m_Log->info(m_Log->key("AyonApi"), "Ayon server info: {}", res->body); - m_Log->info(m_Log->key("AyonApi"), "Status code: {}", res->status); + m_log->info(m_log->key("AyonApi"), "Ayon server info: {}", res->body); + m_log->info(m_log->key("AyonApi"), "Status code: {}", res->status); m_headers = { {"X-Api-Key", m_authKey}, + {"X-ayon-site-id", m_siteId} }; - auto resMe = m_AyonServer->Get("/api/users/me", m_headers); + auto resMe = m_ayonServer->Get("/api/users/me", m_headers); if (resMe && resMe->status != 200) { m_headers = {}; - m_AyonServer->set_bearer_token_auth(m_authKey); + m_ayonServer->set_bearer_token_auth(m_authKey); } } - m_Log->info(m_Log->key("AyonApi"), "Constructor Getting Site Roots"); + m_log->info(m_log->key("AyonApi"), "Constructor Getting Site Roots"); getSiteRoots(); } AyonApi::~AyonApi() { - m_Log->info(m_Log->key("AyonApi"), "AyonApi::~AyonApi()"); + m_log->info(m_log->key("AyonApi"), "AyonApi::~AyonApi()"); }; -std::unordered_map* +const std::unordered_map& AyonApi::getSiteRoots() { - m_Log->info(m_Log->key("AyonApi"), "AyonApi::getSiteRoots()"); + m_log->info(m_log->key("AyonApi"), "AyonApi::getSiteRoots()"); if (m_siteRoots.size() < 1) { std::string platform; #ifdef _WIN32 @@ -222,48 +223,48 @@ AyonApi::getSiteRoots() { std::make_shared(m_headers), 200); if (response.empty()) { - m_Log->error("AyonApi::getSiteRoots response is empty"); - return &m_siteRoots; + m_log->error("AyonApi::getSiteRoots response is empty"); + return m_siteRoots; } else { m_siteRoots = response; } } - if (m_Log->isKeyActive(m_Log->key("AyonApi"))) { - m_Log->info(m_Log->key("AyonApi"), "found site Roots: "); + if (m_log->isKeyActive(m_log->key("AyonApi"))) { + m_log->info(m_log->key("AyonApi"), "found site Roots: "); for (auto &e: m_siteRoots) { - m_Log->info("{}, {}", e.first, e.second); + m_log->info("{}, {}", e.first, e.second); } } - return &m_siteRoots; + return m_siteRoots; }; std::string AyonApi::rootReplace(const std::string &rootLessPath) { - m_Log->info(m_Log->key("AyonApi"), "AyonApi::rootReplace({})", rootLessPath); + m_log->info(m_log->key("AyonApi"), "AyonApi::rootReplace({})", rootLessPath); std::string rootedPath; - std::smatch matchea; + std::smatch matchesA; std::regex rootFindPattern("\\{root\\[.*?\\]\\}"); - if (std::regex_search(rootLessPath, matchea, rootFindPattern)) { - std::string siteRootOverwriteName = matchea.str(0); - - std::smatch matcheb; - std::regex rootBraketPattern("\\[(.*?)\\]"); - if (std::regex_search(rootLessPath, matcheb, rootBraketPattern)) { - std::string breakedString = matcheb.str(0); - breakedString = breakedString.substr(1, breakedString.length() - 2); + if (std::regex_search(rootLessPath, matchesA, rootFindPattern)) { + std::string siteRootOverwriteName = matchesA.str(0); + + std::smatch matchesB; + std::regex rootBracketPattern("\\[(.*?)\\]"); + if (std::regex_search(rootLessPath, matchesB, rootBracketPattern)) { + std::string bracketedString = matchesB.str(0); + bracketedString = bracketedString.substr(1, bracketedString.length() - 2); try { - std::string replacement = m_siteRoots.at(breakedString); + std::string replacement = m_siteRoots.at(bracketedString); rootedPath = std::regex_replace(rootLessPath, rootFindPattern, replacement); - m_Log->info(m_Log->key("AyonApi"), "AyonApi::rootReplace({}) rooted", rootedPath); + m_log->info(m_log->key("AyonApi"), "AyonApi::rootReplace({}) rooted", rootedPath); return rootedPath; } catch (std::out_of_range &e) { - m_Log->warn("AyonApi::rootedPath error acured {}, list off available root replace str: ", e.what()); + m_log->warn("AyonApi::rootedPath error occurred {}, list of available root replace str: ", e.what()); for (auto &g: m_siteRoots) { - m_Log->warn("Key: {}, replacement: {}", g.first, g.second); + m_log->warn("Key: {}, replacement: {}", g.first, g.second); } return rootLessPath; } @@ -276,47 +277,47 @@ AyonApi::rootReplace(const std::string &rootLessPath) { nlohmann::json AyonApi::GET(const std::shared_ptr endPoint, const std::shared_ptr headers, - uint8_t sucsessStatus) { + uint8_t successStatus) { PerfTimer("AyonApi::GET"); - m_Log->info(m_Log->key("AyonApi"), "AyonApi::GET({})", *endPoint); + m_log->info(m_log->key("AyonApi"), "AyonApi::GET({})", *endPoint); httplib::Result response; - int responeStatus; - uint8_t retryes = 0; - while (retryes <= m_maxCallRetrys) { + int responseStatus; + uint8_t retries = 0; + while (retries <= m_maxCallRetries) { try { - response = m_AyonServer->Get(*endPoint, *headers); + response = m_ayonServer->Get(*endPoint, *headers); - if (response == nullptr) { - m_Log->warn("AyonApi::GET response is null: {}", httplib::to_string(response.error())); + if (!response) { + m_log->warn("AyonApi::GET response is null: {}", httplib::to_string(response.error())); return nlohmann::json(); } - responeStatus = response->status; - retryes++; + responseStatus = response->status; + retries++; - if (responeStatus == sucsessStatus) { + if (responseStatus == successStatus) { return nlohmann::json::parse(response->body); - } - else { - m_Log->info("AyonApi::serialCorePost wrong status code: {} expected: {}", responeStatus, sucsessStatus); - if (responeStatus == 401) { - m_Log->warn("not logged in 401 "); + } else { + m_log->info("AyonApi::GET wrong status code: {} expected: {}", responseStatus, successStatus); + if (responseStatus == 401) { + m_log->warn("not logged in 401 "); return nlohmann::json(); } - if (responeStatus == 500) { - m_Log->warn("internal server error "); + if (responseStatus == 500) { + m_log->warn("internal server error "); return nlohmann::json(); } std::this_thread::sleep_for(std::chrono::milliseconds( - responeStatus == m_ServerBusyCode ? m_RequestDelayWhenServerBusy : m_retryWaight)); + responseStatus == m_serverBusyCode ? m_requestDelayWhenServerBusy : m_retryWait)); } - } // TODO error reason not printed + + } catch (const httplib::Error &e) { - m_Log->warn("Request Failed because: {}", httplib::to_string(e)); + m_log->warn("Request Failed because: {}", httplib::to_string(e)); break; } - m_Log->warn("The connection failed Rety now."); + m_log->warn("The connection failed. Retry now."); } return nlohmann::json(); }; @@ -325,91 +326,89 @@ nlohmann::json AyonApi::SPOST(const std::shared_ptr endPoint, const std::shared_ptr headers, nlohmann::json jsonPayload, - const std::shared_ptr sucsessStatus) { + const std::shared_ptr successStatus) { PerfTimer("AyonApi::SPOST"); - m_Log->info(m_Log->key("AyonApi"), "AyonApi::SPOST endPoint: {}, jsonPayload: {}, sucsessStatus: {}", *endPoint, - jsonPayload.dump(), *sucsessStatus); + m_log->info(m_log->key("AyonApi"), "AyonApi::SPOST endPoint: {}, jsonPayload: {}, successStatus: {}", *endPoint, + jsonPayload.dump(), *successStatus); - nlohmann::json jsonRespne; + nlohmann::json jsonResponse; if (jsonPayload.empty()) { - m_Log->info("JSON payload is empty. No request created"); - return jsonRespne; + m_log->info("JSON payload is empty. No request created"); + return jsonResponse; } - if (endPoint == nullptr || headers == nullptr || sucsessStatus == nullptr) { - m_Log->error("One or more of the provided pointers are null: endPoint, headers, sucsessStatus."); + if (endPoint == nullptr || headers == nullptr || successStatus == nullptr) { + m_log->error("One or more of the provided pointers are null: endPoint, headers, successStatus."); - return jsonRespne; + return jsonResponse; } - m_AyonServerMutex.lock(); + m_ayonServerMutex.lock(); std::string payload = jsonPayload.dump(); - std::string rawResponse = serialCorePost(*endPoint, *headers, payload, *sucsessStatus); + std::string rawResponse = serialCorePost(*endPoint, *headers, payload, *successStatus); if (!rawResponse.empty()) { - jsonRespne = nlohmann::json::parse(rawResponse)[0]; // TODO figure out why this is isnt the same as CPOST and - // find a better way to make shure its not a array + jsonResponse = nlohmann::json::parse(rawResponse)[0]; // TODO figure out why this is isnt the same as CPOST and + // find a better way to make sure its not an array } else { - m_Log->warn("SPOST cant phrase JSON // response empty"); + m_log->warn("SPOST can't parse JSON // response empty"); } - m_AyonServerMutex.unlock(); - return jsonRespne; + m_ayonServerMutex.unlock(); + return jsonResponse; }; nlohmann::json AyonApi::CPOST(const std::shared_ptr endPoint, const std::shared_ptr headers, nlohmann::json jsonPayload, - const std::shared_ptr sucsessStatus) { + const std::shared_ptr successStatus) { PerfTimer("AyonApi::CPOST"); - m_Log->info(m_Log->key("AyonApi"), "AyonApi::CPOST endPoint: {}, jsonPayload: {}, sucsessStatus: {}", *endPoint, - jsonPayload.dump(), *sucsessStatus); - nlohmann::json jsonRespne; + m_log->info(m_log->key("AyonApi"), "AyonApi::CPOST endPoint: {}, jsonPayload: {}, successStatus: {}", *endPoint, + jsonPayload.dump(), *successStatus); + nlohmann::json jsonResponse; if (jsonPayload.empty()) { - m_Log->info("JSON payload is empty. No request created"); - return jsonRespne; + m_log->info("JSON payload is empty. No request created"); + return jsonResponse; } - if (endPoint == nullptr || headers == nullptr || sucsessStatus == nullptr) { - m_Log->error("One or more of the provided pointers are null: endPoint, headers, sucsessStatus"); + if (endPoint == nullptr || headers == nullptr || successStatus == nullptr) { + m_log->error("One or more of the provided pointers are null: endPoint, headers, successStatus"); - return jsonRespne; + return jsonResponse; } std::string payload = jsonPayload.dump(); - std::string rawResponse = GenerativeCorePost(*endPoint, *headers, payload, *sucsessStatus); + std::string rawResponse = generativeCorePost(*endPoint, *headers, payload, *successStatus); if (!rawResponse.empty()) { - jsonRespne = nlohmann::json::parse(rawResponse); + jsonResponse = nlohmann::json::parse(rawResponse); } else { - m_Log->warn("CPOST cant phrase JSON // response empty"); + m_log->warn("CPOST can't parse JSON // response empty"); } - return jsonRespne; + return jsonResponse; }; -// TODO change the pointer work in here because the pointers consume more data that coping would + +// TODO change the pointer work in here because the pointers consume more data that copying would std::pair AyonApi::resolvePath(const std::string &uriPath) { PerfTimer("AyonApi::resolvePath"); - m_Log->info(m_Log->key("AyonApi"), "AyonApi::resolvePath({})", uriPath); + m_log->info(m_log->key("AyonApi"), "AyonApi::resolvePath({})", uriPath); if (uriPath.empty()) { - m_Log->info("Path was empty: {}", uriPath.c_str()); + m_log->info("Path was empty: {}", uriPath.c_str()); return {}; } std::pair resolvedAsset; nlohmann::json jsonPayload = {{"resolveRoots", false}, {"uris", nlohmann::json::array({uriPath})}}; - httplib::Headers headers = m_headers; - headers.insert({"X-ayon-site-id", m_siteId}); - - uint8_t sucsessStatus = 200; + uint8_t successStatus = 200; nlohmann::json response = SPOST(std::make_shared(m_uriResolverEndpoint + m_uriResolverEndpointPathOnlyVar), - std::make_shared(headers), jsonPayload, std::make_shared(sucsessStatus)); + std::make_shared(m_headers), jsonPayload, std::make_shared(successStatus)); resolvedAsset = getAssetIdent(response); return resolvedAsset; @@ -418,25 +417,25 @@ AyonApi::resolvePath(const std::string &uriPath) { std::unordered_map AyonApi::batchResolvePath(std::vector &uriPaths) { PerfTimer("AyonApi::batchResolvePath"); - m_Log->info(m_Log->key("AyonApi"), "AyonApi::batchResolvePath({})", + m_log->info(m_log->key("AyonApi"), "AyonApi::batchResolvePath({})", std::accumulate( uriPaths.begin(), uriPaths.end(), std::string(), [](const std::string &a, const std::string &b) { return a + (a.length() > 0 ? " " : "") + b; })); if (uriPaths.size() < 1) { - m_Log->warn("AyonApi::batchResolvePath Got empty vector stopped resolution"); + m_log->warn("AyonApi::batchResolvePath Got empty vector stopped resolution"); return {}; } if (m_batchResolveOptimizeVector) { { - PerfTimer("AyonApi::batchResolvePath::sanatizeVector"); + PerfTimer("AyonApi::batchResolvePath::sanitizeVector"); std::set s; size_t size = uriPaths.size(); for (size_t i = 0; i < size; ++i) s.insert(uriPaths[i]); uriPaths.assign(s.begin(), s.end()); - m_Log->info("Make sure that the vector has no duplicates. vecSize before: {} after: {}", size, + m_log->info("Make sure that the vector has no duplicates. vecSize before: {} after: {}", size, uriPaths.size()); } } @@ -447,7 +446,7 @@ AyonApi::batchResolvePath(std::vector &uriPaths) { = std::make_shared(m_headers); std::shared_ptr batchResolveEndpoint; - if (m_pathOnlyReselution) { + if (m_pathOnlyResolution) { batchResolveEndpoint = std::make_shared(std::string_view(m_uriResolverEndpoint + m_uriResolverEndpointPathOnlyVar)); } @@ -462,29 +461,28 @@ AyonApi::batchResolvePath(std::vector &uriPaths) { int groupSize; int groupAmount; - // set defaults for the grouping incase the vector is to small + // set defaults for the grouping in case the vector is too small groupSize = uriPathsVecSize; groupAmount = 1; grpReason = "The vector is too small."; - // check what scaling the groups schould have + // check what scaling the groups should have if (uriPathsVecSize > m_minVecSizeForGroupSplitAsyncRequests) { - // vector size is large eonught to build groups - // double result = static_cast(uriPathsVecSize) / num_threads; - groupSize = std::ceil(static_cast(uriPathsVecSize) / m_num_threads); - if (groupSize > m_minGrpSizeForAsyncRequests) { - // the group size is lagre enought to build groups from them + // vector size is large enough to build groups + groupSize = std::ceil(static_cast(uriPathsVecSize) / m_numThreads); + if (groupSize > m_minGroupSizeForAsyncRequests) { + // the group size is large enough to build groups from them if (groupSize < m_maxGroupSizeForAsyncRequests) { - // now its bigger than 5 and smaller than 500 + // now it's bigger than 5 and smaller than 500 // now we can just generate a group per thread and set the group amount - groupSize = std::ceil(static_cast(uriPathsVecSize) / m_num_threads); + groupSize = std::ceil(static_cast(uriPathsVecSize) / m_numThreads); groupAmount = std::floor(static_cast(uriPathsVecSize) / groupSize); // TODO explicit rounding .x group amount grpReason = "5> <500 build group amount by size"; } else { - // the groups are to beig + // the groups are too big // we have to generate more groups than we have threads groupSize = m_regroupSizeForAsyncRequests; groupAmount = std::floor(static_cast(uriPathsVecSize) / m_regroupSizeForAsyncRequests); @@ -492,15 +490,15 @@ AyonApi::batchResolvePath(std::vector &uriPaths) { } } else { - // the groups are to small so we build groups by size + // the groups are too small so we build groups by size groupSize = std::min((int)m_regroupSizeForAsyncRequests, uriPathsVecSize); groupAmount = std::floor(static_cast(uriPathsVecSize) / groupSize); grpReason = "If groups are too small, we will build them by size."; } } - m_Log->info( - "AyonApi::batchResolvePath Build groups with grpSize: {} grpAmount: {} grouingReason: {} vectorSize: {}", + m_log->info( + "AyonApi::batchResolvePath Build groups with grpSize: {} grpAmount: {} groupingReason: {} vectorSize: {}", groupSize, groupAmount, grpReason, uriPathsVecSize); int groupStartPos = 0; @@ -510,10 +508,10 @@ AyonApi::batchResolvePath(std::vector &uriPaths) { std::string perTimerLoopName = "AyonApi::batchResolvePath Thread Loop: " + std::to_string(thread); PerfTimer(perTimerLoopName.c_str()); - // check if we are to close to the end and extend the group to catch all the data and end the loop + // check if we are too close to the end and extend the group to catch all the data and end the loop if (uriPathsVecSize - groupEndPos < groupSize + (groupSize / 2)) { - m_Log->info("the group with the threadId: {} It is too close to the end. This group will be extended. ", + m_log->info("the group with the threadId: {} It is too close to the end. This group will be extended. ", thread); groupEndPos = uriPathsVecSize - 1; thread = groupAmount; @@ -541,28 +539,27 @@ AyonApi::batchResolvePath(std::vector &uriPaths) { return assetIdentGrp; }; + // TODO make it so that hero version is chosen if available std::pair -AyonApi::getAssetIdent(const nlohmann::json &uriResolverRespone) { +AyonApi::getAssetIdent(const nlohmann::json &uriResolverResponse) { PerfTimer("AyonApi::getAssetIdent"); - m_Log->info(m_Log->key("AyonApi"), "AyonApi::getAssetIdent({})", uriResolverRespone.dump()); + m_log->info(m_log->key("AyonApi"), "AyonApi::getAssetIdent({})", uriResolverResponse.dump()); std::pair AssetIdent; - if (uriResolverRespone.empty()) { + if (uriResolverResponse.empty()) { return AssetIdent; } try { - AssetIdent.first = uriResolverRespone.at("uri"); - if (uriResolverRespone.at("entities").size() > 1) { - m_Log->warn("Uri reselution returned more than one path (%s)", uriResolverRespone.at("entities").dump()); + AssetIdent.first = uriResolverResponse.at("uri"); + if (uriResolverResponse.at("entities").size() > 1) { + m_log->warn("Uri resolution returned more than one path: {}", uriResolverResponse.at("entities").dump()); } AssetIdent.second = rootReplace( - uriResolverRespone.at("entities").at(uriResolverRespone.at("entities").size() - 1).at("filePath")); + uriResolverResponse.at("entities").at(uriResolverResponse.at("entities").size() - 1).at("filePath")); } catch (const nlohmann::json::exception &e) { - std::cout << "AyonApi::getAssetIdent JSON exception: " << e.what() << std::endl; - std::cout << "uriResolverRespone: " << uriResolverRespone.dump() << std::endl; - m_Log->warn("asset identification cant be generated {}", uriResolverRespone.dump()); + m_log->warn("asset identification cant be generated {}", uriResolverResponse.dump()); } return AssetIdent; }; @@ -570,14 +567,14 @@ AyonApi::getAssetIdent(const nlohmann::json &uriResolverRespone) { std::string AyonApi::getKey() { PerfTimer("AyonApi::getKey"); - m_Log->info(m_Log->key("AyonApi"), "AyonApi::getKey"); + m_log->info(m_log->key("AyonApi"), "AyonApi::getKey"); return m_authKey; }; std::string AyonApi::getUrl() { PerfTimer("AyonApi::getUrl"); - m_Log->info(m_Log->key("AyonApi"), "AyonApi::getUrl"); + m_log->info(m_log->key("AyonApi"), "AyonApi::getUrl"); return m_serverUrl; } @@ -586,136 +583,136 @@ std::string AyonApi::serialCorePost(const std::string &endPoint, httplib::Headers headers, std::string &Payload, - const int &sucsessStatus) { + const int &successStatus) { PerfTimer("AyonApi::serialCorePost"); - m_Log->info(m_Log->key("AyonApi"), "AyonApi::serialCorePost() endPoint: {}, Payload: {}, sucsessStatus: {}", - endPoint, Payload, sucsessStatus); + m_log->info(m_log->key("AyonApi"), "AyonApi::serialCorePost() endPoint: {}, Payload: {}, successStatus: {}", + endPoint, Payload, successStatus); httplib::Result response; - int responeStatus; - uint8_t retryes = 0; - while (retryes <= m_maxCallRetrys) { + int responseStatus; + uint8_t retries = 0; + while (retries <= m_maxCallRetries) { try { - response = m_AyonServer->Post(endPoint, headers, Payload, "application/json"); - responeStatus = response->status; - retryes++; + response = m_ayonServer->Post(endPoint, headers, Payload, "application/json"); + responseStatus = response->status; + retries++; - if (responeStatus == sucsessStatus) { + if (responseStatus == successStatus) { return response->body; } else { - m_Log->info("AyonApi::serialCorePost wrong status code: {} expected: {}", responeStatus, sucsessStatus); - if (responeStatus == 401) { - m_Log->warn("not logged in 401 "); + m_log->info("AyonApi::serialCorePost wrong status code: {} expected: {}", responseStatus, successStatus); + if (responseStatus == 401) { + m_log->warn("not logged in 401 "); return ""; } - if (responeStatus == 500) { - m_Log->warn("internal server error "); + if (responseStatus == 500) { + m_log->warn("internal server error "); return ""; } std::this_thread::sleep_for(std::chrono::milliseconds( - responeStatus == m_ServerBusyCode ? m_RequestDelayWhenServerBusy : m_retryWaight)); + responseStatus == m_serverBusyCode ? m_requestDelayWhenServerBusy : m_retryWait)); } - } // TODO error reason not printed + } catch (const httplib::Error &e) { - m_Log->warn("Request Failed because: {}"); + m_log->warn("Request Failed because: {}", httplib::to_string(e)); break; } - m_Log->warn("The connection failed Rety now."); + m_log->warn("The connection failed. Retry now."); } return ""; }; std::string -AyonApi::GenerativeCorePost(const std::string &endPoint, +AyonApi::generativeCorePost(const std::string &endPoint, httplib::Headers headers, std::string &Payload, - const int &sucsessStatus) { - PerfTimer("AyonApi::GenerativeCorePost"); - m_Log->info(m_Log->key("AyonApi"), "AyonApi::GenerativeCorePost() endPoint: {}, Payload: {}, sucsessStatus: {}", - endPoint, Payload, sucsessStatus); + const int &successStatus) { + PerfTimer("AyonApi::generativeCorePost"); + m_log->info(m_log->key("AyonApi"), "AyonApi::generativeCorePost() endPoint: {}, Payload: {}, successStatus: {}", + endPoint, Payload, successStatus); httplib::Client AyonServerClient(m_serverUrl); AyonServerClient.set_bearer_token_auth(m_authKey); - AyonServerClient.set_connection_timeout(m_connectionTimeOutMax); - AyonServerClient.set_read_timeout(m_readTimeOutMax); + AyonServerClient.set_connection_timeout(m_connectionTimeoutMax); + AyonServerClient.set_read_timeout(m_readTimeoutMax); httplib::Result response; - int responeStatus; - uint8_t retryes = 0; - bool ffoLocking = false; - uint16_t loopIertaion = 0; - while (retryes <= m_maxCallRetrys || m_GenerativeCorePostMaxLoopIterations > loopIertaion) { - loopIertaion++; - m_Log->info("AyonApi::GenerativeCorePost while loop thread {} iteration {}", - std::hash{}(std::this_thread::get_id()), loopIertaion); - - if (ffoLocking) { - m_ConcurentRequestAfterffoMutex.lock(); - m_Log->info("AyonApi::GenerativeCorePost ffoLocking enabled"); - if (m_maxConcurentRequestAfterffo >= 1) { - m_maxConcurentRequestAfterffo--; - - m_Log->info("AyonApi::GenerativeCorePost thread pool open available: {}", - m_maxConcurentRequestAfterffo); - - m_ConcurentRequestAfterffoMutex.unlock(); + int responseStatus; + uint8_t retries = 0; + bool ffoLock = false; + uint16_t loopIteration = 0; + while (retries <= m_maxCallRetries || m_generativeCorePostMaxLoopIterations > loopIteration) { + loopIteration++; + m_log->info("AyonApi::generativeCorePost while loop thread {} iteration {}", + std::hash{}(std::this_thread::get_id()), loopIteration); + + if (ffoLock) { + m_concurrentRequestAfter503Mutex.lock(); + m_log->info("AyonApi::generativeCorePost ffoLock enabled"); + if (m_maxConcurrentRequestsAfter503 >= 1) { + m_maxConcurrentRequestsAfter503--; + + m_log->info("AyonApi::generativeCorePost thread pool open available: {}", + m_maxConcurrentRequestsAfter503); + + m_concurrentRequestAfter503Mutex.unlock(); } else { - m_Log->info("AyonApi::GenerativeCorePost Thread pool closed"); + m_log->info("AyonApi::generativeCorePost Thread pool closed"); - m_ConcurentRequestAfterffoMutex.unlock(); + m_concurrentRequestAfter503Mutex.unlock(); std::this_thread::sleep_for(std::chrono::milliseconds(800)); continue; } } - m_Log->info("AyonApi::GenerativeCorePost sending request"); + m_log->info("AyonApi::generativeCorePost sending request"); try { response = AyonServerClient.Post(endPoint, headers, Payload, "application/json"); - responeStatus = response->status; - retryes++; - if (ffoLocking) { - m_ConcurentRequestAfterffoMutex.lock(); - m_maxConcurentRequestAfterffo++; - m_ConcurentRequestAfterffoMutex.unlock(); + responseStatus = response->status; + retries++; + if (ffoLock) { + m_concurrentRequestAfter503Mutex.lock(); + m_maxConcurrentRequestsAfter503++; + m_concurrentRequestAfter503Mutex.unlock(); } - if (responeStatus == sucsessStatus) { - m_Log->info("AyonApi::GenerativeCorePost The request worked, unlocking and returning. "); + if (responseStatus == successStatus) { + m_log->info("AyonApi::generativeCorePost The request worked, unlocking and returning. "); return response->body; } else { - if (responeStatus == m_ServerBusyCode) { - m_Log->warn("AyonApi::GenerativeCorePost The server responded with 503"); + if (responseStatus == m_serverBusyCode) { + m_log->warn("AyonApi::generativeCorePost The server responded with 503"); - retryes = 0; - ffoLocking = true; + retries = 0; + ffoLock = true; continue; } - if (responeStatus == 401) { - m_Log->warn("not logged in 401 "); + if (responseStatus == 401) { + m_log->warn("not logged in 401 "); return ""; } - if (responeStatus == 500) { - m_Log->warn("internal server error "); + if (responseStatus == 500) { + m_log->warn("internal server error "); return ""; } - m_Log->info("AyonApi::GenerativeCorePost wrong status code: {} expected: {} retrying", responeStatus, - sucsessStatus); - std::this_thread::sleep_for(std::chrono::milliseconds(m_retryWaight)); + m_log->info("AyonApi::generativeCorePost wrong status code: {} expected: {} retrying", responseStatus, + successStatus); + std::this_thread::sleep_for(std::chrono::milliseconds(m_retryWait)); continue; } - } // TODO error reason not printed + } catch (const httplib::Error &e) { - m_Log->warn("AyonApi::GenerativeCorePost Request Failed because: {}"); + m_log->warn("AyonApi::generativeCorePost Request Failed because: {}", httplib::to_string(e)); break; } } - m_Log->warn( - "AyonApi::GenerativeCorePost Too many resolve retries without the correct response code for: {}, on: {}", + m_log->warn( + "AyonApi::generativeCorePost Too many resolve retries without the correct response code for: {}, on: {}", Payload, endPoint); return ""; }; @@ -723,13 +720,16 @@ AyonApi::GenerativeCorePost(const std::string &endPoint, std::string AyonApi::convertUriVecToString(const std::vector &uriVec) { PerfTimer("AyonApi::convertUriVecToString"); - m_Log->info(m_Log->key("AyonApi"), "AyonApi::convertUriVecToString({})", + m_log->info(m_log->key("AyonApi"), "AyonApi::convertUriVecToString({})", std::accumulate(uriVec.begin(), uriVec.end(), std::string())); - std::string payload = R"({{"resolveRoots": true,"uris": [)"; + std::string payload = R"({"resolveRoots": true,"uris": [)"; - for (int i = 0; i <= int(uriVec.size()); i++) { - payload += uriVec[i]; + for (size_t i = 0; i < uriVec.size(); i++) { + payload += "\"" + uriVec[i] + "\""; + if (i < uriVec.size() - 1) { + payload += ","; + } } payload += "]}"; @@ -739,8 +739,8 @@ AyonApi::convertUriVecToString(const std::vector &uriVec) { std::shared_ptr AyonApi::logPointer() { - m_Log->info(m_Log->key("AyonApi"), "AyonApi::logPointer()"); - return m_Log; + m_log->info(m_log->key("AyonApi"), "AyonApi::logPointer()"); + return m_log; }; bool @@ -754,8 +754,8 @@ AyonApi::setSSL() { const char* envCertFile = getenv("SSL_CERT_FILE"); if (envCertFile) { if (std::filesystem::exists(envCertFile)) { - m_Log->info("Using cert based on env variable (SSL_CERT_FILE): {}", envCertFile); - m_AyonServer->set_ca_cert_path(envCertFile); + m_log->info("Using cert based on env variable (SSL_CERT_FILE): {}", envCertFile); + m_ayonServer->set_ca_cert_path(envCertFile); return; } } @@ -765,8 +765,8 @@ AyonApi::setSSL() { std::string certFileCLI = opensslDirCLI.string(); if (std::filesystem::exists(certFileCLI)) { - m_Log->info("Using cert based on CLI var: {}", certFileCLI); - m_AyonServer->set_ca_cert_path(certFileCLI.c_str()); + m_log->info("Using cert based on CLI var: {}", certFileCLI); + m_ayonServer->set_ca_cert_path(certFileCLI.c_str()); return; } @@ -775,12 +775,12 @@ AyonApi::setSSL() { std::string certFileSSLEAY = opensslDirSSLEAY.string(); if (std::filesystem::exists(certFileSSLEAY)) { - m_Log->info("Using cert based on SSLEAY_DIR: {}", certFileSSLEAY); - m_AyonServer->set_ca_cert_path(certFileSSLEAY.c_str()); + m_log->info("Using cert based on SSLEAY_DIR: {}", certFileSSLEAY); + m_ayonServer->set_ca_cert_path(certFileSSLEAY.c_str()); return; } - m_Log->info("Failed to determine the OpenSSL directory or load system CAs. Falling back to bundled certificate path."); + m_log->info("Failed to determine the OpenSSL directory or load system CAs. Falling back to bundled certificate path."); std::filesystem::path soPath; Dl_info dl_info; @@ -799,14 +799,14 @@ AyonApi::setSSL() { std::string certPath = bundledPath.string(); if (std::filesystem::exists(certPath)) { - m_Log->info("Using bundled certificate (via SO path): {}", certPath); - m_AyonServer->set_ca_cert_path(certPath.c_str()); + m_log->info("Using bundled certificate (via SO path): {}", certPath); + m_ayonServer->set_ca_cert_path(certPath.c_str()); return; } - m_Log->error("Bundled cacert.pem file not found at expected runtime path: {}", certPath); + m_log->error("Bundled cacert.pem file not found at expected runtime path: {}", certPath); } else { - m_Log->error("Failed to determine the path of the loaded shared library (dladdr failed)."); + m_log->error("Failed to determine the path of the loaded shared library (dladdr failed)."); } throw std::runtime_error("Failed to set SSL certificate path. No valid certificate found."); diff --git a/src/AyonCppApi/AyonCppApi.h b/src/AyonCppApi/AyonCppApi.h index 711054f..f30c56b 100644 --- a/src/AyonCppApi/AyonCppApi.h +++ b/src/AyonCppApi/AyonCppApi.h @@ -10,7 +10,7 @@ #include #include #include "lib/ynput/lib/logging/AyonLogger.hpp" -#include "appDataFoulder.h" +#include "appDataFolder.h" #include "httplib.h" #include "nlohmann/json_fwd.hpp" @@ -21,14 +21,13 @@ /** * @class AyonApi - * @brief Central Ayon api class \n + * @brief Central Ayon api class * Class for exposing Ayon server functions to C++ users. Uses httplib internally for communication with the server - * */ class AyonApi { public: /** - * @brief constructor + * @brief Constructor */ AyonApi(const std::optional &logFilePos, const std::string &authKey, @@ -37,205 +36,193 @@ class AyonApi { const std::string &siteId, std::optional concurrency = std::nullopt); /** - * @brief destructor + * @brief Destructor */ ~AyonApi(); /** - * @brief returns the stored apikey. Retrieved from the appropriate env variable. (the variable is loaded from - * loadEnvVars()) + * @brief Returns the stored API key. Passed in the constructor. */ std::string getKey(); /** - * @brief returns the stored AYON server url. Retrieved from the appropriate env variable. (the variable is - * loaded from loadEnvVars()) + * @brief Returns the stored AYON server URL. Passed in the constructor. */ std::string getUrl(); /** - * @brief runns a get command and returns the response body as std::string + * @brief Runs a GET command * - * @param endPoint reachable http / https endpoint - * @param headers http headers - * @param sucsessStatus define what http response code should be considered a success. + * @param endPoint Reachable HTTP/HTTPS endpoint + * @param headers HTTP headers + * @param successStatus Defines what HTTP response code should be considered a success. + * @return The response body as nlohmann::json */ nlohmann::json GET(const std::shared_ptr endPoint, const std::shared_ptr headers, - uint8_t sucsessStatus); + uint8_t successStatus); /** - * @brief post Request via a shared httplib client ( serial ) + * @brief POST request via a shared httplib client (serial) * - * @param endPoint the AYON enpoint to hit - * @param headers the http header that you want to send - * @param jsonPayload the payload in json format - * @param sucsessStatus defines what status code is considered a success and brakes the retry loop. + * @param endPoint The AYON endpoint to hit + * @param headers The HTTP headers to send + * @param jsonPayload The payload in JSON format + * @param successStatus Defines what status code is considered a success and breaks the retry loop + * @return The response body as nlohmann::json */ nlohmann::json SPOST(const std::shared_ptr endPoint, const std::shared_ptr headers, nlohmann::json jsonPayload, - const std::shared_ptr sucsessStatus); + const std::shared_ptr successStatus); /** - * @brief http post request utilizing the creation of a new httplib client ( Generative Async ) + * @brief HTTP POST request utilizing the creation of a new httplib client (Generative Async) * - * @param endPoint the AYON enpoint to hit - * @param headers the http header that you want to send - * @param jsonPayload the payload in json format - * @param sucsessStatus defines what status code is considered a success and brakes the retry loop. + * @param endPoint The AYON endpoint to hit + * @param headers The HTTP headers to send + * @param jsonPayload The payload in JSON format + * @param successStatus Defines what status code is considered a success and breaks the retry loop. + * @return The response body as nlohmann::json */ nlohmann::json CPOST(const std::shared_ptr endPoint, const std::shared_ptr headers, nlohmann::json jsonPayload, - const std::shared_ptr sucsessStatus); + const std::shared_ptr successStatus); /** - * @brief uses the uri resolve endpoint on the AYON server in order to resolve an uri path towards the local - * path \n gets the siteId from an variable stored in the class + * @brief Uses the URI resolve endpoint on the AYON server to resolve a URI path to the local path. + * Gets the siteId from a variable stored in the class. * - * @param uriPath + * @param uriPath The URI path to resolve. + * @return A pair containing the asset identifier (ayon:// path) and the machine local file location. */ std::pair resolvePath(const std::string &uriPath); + /** - * @brief resolves a vector off paths against the AYON server in an async way uses auto generated batch requests + * @brief Resolves a vector of paths against the AYON server asynchronously using auto-generated batch requests. * - * @param uriPaths + * @param uriPaths The vector of URI paths to resolve. + * @return An unordered map containing the resolved paths. */ std::unordered_map batchResolvePath(std::vector &uriPaths); /** - * @brief this function takes a ayon path uri response(resolved ayon://path) and returns a pair of - * assetIdentifier(ayon:// path) and the machine local file location + * @brief Takes an AYON path URI response (resolved ayon://path) and returns a pair of + * asset identifier (ayon:// path) and the machine local file location. * - * @param uriResolverRespone json representation off the resolves the ayon/api/resolve endpoint returns + * @param uriResolverResponse JSON representation of the response from the AYON API resolve endpoint. + * @return A pair containing the asset identifier and the machine local file location. */ - std::pair getAssetIdent(const nlohmann::json &uriResolverRespone); + std::pair getAssetIdent(const nlohmann::json &uriResolverResponse); /** - * @brief get function for shared AyonLogger pointer used by this class instance + * @brief Get function for shared AyonLogger pointer used by this class instance */ std::shared_ptr logPointer(); /** - * @brief gets the site root overwrites for the current project. Current project is defined via an env variable + * @brief Gets the site root overwrites for the current project. Current project is defined via an env variable * for now */ - std::unordered_map* - getSiteRoots(); // TODO think about if this should only support current project or multiple projects + const std::unordered_map& getSiteRoots(); // TODO think about if this should only support current project or multiple projects /** - * @brief replaces {root[var]} for ayon:// paths + * @brief Replaces {root[var]} for ayon:// paths. * - * @param rootLessPath endpoint response for ayon://path with {root[var]} available if no root can be found the - * path will be returned as is + * @param rootLessPath Endpoint response for ayon://path with {root[var]}. + * If no root can be found, the path will be returned as is. */ std::string rootReplace(const std::string &rootLessPath); private: /** - * @brief calls the server in an serial way by sharing the AyonServer pointer + * @brief Calls the server in a serial way by sharing the AyonServer pointer. * - * @param endPoint endpoint that ayon resolve is loaded on - * @param headers http headers - * @param Payload json payload to be resolved by endpoint - * @param sucsessStatus defines what is considered a success response to break the retry loop + * @param endPoint Endpoint that AYON resolve is loaded on. + * @param headers HTTP headers. + * @param payload JSON payload to be resolved by endpoint. + * @param successStatus Defines what is considered a success response to break the retry loop. */ std::string serialCorePost(const std::string &endPoint, httplib::Headers headers, - std::string &Payload, - const int &sucsessStatus); + std::string &payload, + const int &successStatus); /** - * @brief calls the server while creating a new client instance to stay async + * @brief Calls the server while creating a new client instance to stay async. * - * @param endPoint endpoint that ayon resolve is loaded on - * @param headers http headers - * @param Payload json payload to be resolved by endpoint - * @param sucsessStatus defines what is considered a success response to break the retry loop + * @param endPoint Endpoint that AYON resolve is loaded on. + * @param headers HTTP headers. + * @param payload JSON payload to be resolved by endpoint. + * @param successStatus Defines what is considered a success response to break the retry loop. */ - std::string GenerativeCorePost(const std::string &endPoint, + std::string generativeCorePost(const std::string &endPoint, httplib::Headers headers, - std::string &Payload, - const int &sucsessStatus); + std::string &payload, + const int &successStatus); /** - * @brief converts a vector off uris into an string to serve into CorePost funcs + * @brief Converts a vector of URIs into a string to serve into CorePost functions. * - * @param uriVec vector off str uris + * @param uriVec Vector of string URIs. */ std::string convertUriVecToString(const std::vector &uriVec); /** - * @brief checks if the m_AyonServer is running on ssl based on m_serverUrl - * dumb implementation but it should work - function from httplib is not working + * @brief Checks if the m_ayonServer is running on SSL based on m_serverUrl + * Simple implementation - httplib's built-in check is not working * * @return true if m_serverUrl starts with https:// */ bool isSSL() const; /** - * @brief sets the ssl cert path for the m_AyonServer httplib client + * @brief Sets the SSL cert path for the m_ayonServer httplib client */ void setSSL(); - std::unique_ptr m_AyonServer; - - std::unordered_map m_siteRoots; + // Core Dependencies + std::unique_ptr m_ayonServer; + std::shared_ptr m_log; - // ----- Env Varibles + // Configuration from Constructor const std::string m_authKey; const std::string m_serverUrl; std::string m_ayonProjectName; - - // ---- Server Vars std::string m_siteId; - std::string m_userName; - - // --- HTTP Headers + const int m_numThreads; + + // Runtime State + std::unordered_map m_siteRoots; httplib::Headers m_headers; - - // --- Runtime Dep Vars - - // Async Grp Generation Varibles - uint8_t m_minGrpSizeForAsyncRequests = 10; - uint16_t m_regroupSizeForAsyncRequests = 200; - uint16_t m_maxGroupSizeForAsyncRequests = 300; - uint16_t m_minVecSizeForGroupSplitAsyncRequests = 50; - uint8_t m_maxCallRetrys = 8; - uint16_t m_retryWaight = 800; - - /** - * @brief maximum number off threads that the cpu can handle at the same time. Will be set via constructor - */ - const int m_num_threads; // set by constructor - std::shared_ptr m_Log; + std::string m_userName; + bool m_serverBusy = false; + + // URI Resolution Configuration std::string m_uriResolverEndpoint = "/api/resolve"; std::string m_uriResolverEndpointPathOnlyVar = "?pathOnly=true"; - bool m_pathOnlyReselution = true; - - std::mutex m_ConcurentRequestAfterffoMutex; - uint8_t m_maxConcurentRequestAfterffo = 8; - - uint16_t m_GenerativeCorePostMaxLoopIterations = 200; - - uint16_t m_connectionTimeOutMax = 200; - uint8_t m_readTimeOutMax = 160; - + bool m_pathOnlyResolution = true; + /** - * @brief decides if the cpp api removes duplicates from batch request vector default is true + * @brief Decides if the cpp API removes duplicates from batch request vector. Default is true */ bool m_batchResolveOptimizeVector = true; - - uint16_t m_ServerBusyCode = 503; - uint16_t m_RequestDelayWhenServerBusy = 10000; - - /** - * @brief this bool will be set to true if a 503 is encountered - */ - bool m_serverBusy = false; - - /** - * @brief needed for serial resolve operations. to lock acces to AyonServer shared pointer - */ - std::mutex m_AyonServerMutex; + uint8_t m_minGroupSizeForAsyncRequests = 10; + uint16_t m_regroupSizeForAsyncRequests = 200; + uint16_t m_maxGroupSizeForAsyncRequests = 300; + uint16_t m_minVecSizeForGroupSplitAsyncRequests = 50; + + // Retry and Timeout Configuration + uint8_t m_maxCallRetries = 8; + uint16_t m_retryWait = 800; + uint16_t m_serverBusyCode = 503; + uint16_t m_requestDelayWhenServerBusy = 10000; + uint16_t m_connectionTimeoutMax = 200; + uint8_t m_readTimeoutMax = 160; + uint16_t m_generativeCorePostMaxLoopIterations = 200; + + // Thread Synchronization + std::mutex m_ayonServerMutex; + std::mutex m_concurrentRequestAfter503Mutex; + uint8_t m_maxConcurrentRequestsAfter503 = 8; }; #endif // !AYONCPPAPI_H diff --git a/src/AyonCppApi/CMakeLists.txt b/src/AyonCppApi/CMakeLists.txt index 2e4d2ab..b9cc983 100755 --- a/src/AyonCppApi/CMakeLists.txt +++ b/src/AyonCppApi/CMakeLists.txt @@ -66,6 +66,6 @@ install ( ) install ( - FILES AyonCppApi.h appDataFoulder.h devMacros.h Instrumentor.h + FILES AyonCppApi.h appDataFolder.h devMacros.h Instrumentor.h DESTINATION include ) diff --git a/src/AyonCppApi/Instrumentor.h b/src/AyonCppApi/Instrumentor.h index c826d69..3b78613 100644 --- a/src/AyonCppApi/Instrumentor.h +++ b/src/AyonCppApi/Instrumentor.h @@ -10,13 +10,13 @@ #include struct ProfileResult { - std::string Name; - long long Start, End; - uint32_t ThreadID; + std::string Name; + long long Start, End; + uint32_t ThreadID; }; struct InstrumentationSession { - std::string Name; + std::string Name; }; class Instrumentor { @@ -30,16 +30,14 @@ class Instrumentor { Instrumentor(): m_CurrentSession(nullptr), m_ProfileCount(0) { } - void - BeginSession(const std::string &name, const std::string &filepath = "results.json") { + void BeginSession(const std::string &name, const std::string &filepath = "results.json") { std::lock_guard lock(m_Mutex); // Lock mutex for critical section m_OutputStream.open(filepath); WriteHeader(); m_CurrentSession = new InstrumentationSession{name}; } - void - EndSession() { + void EndSession() { WriteFooter(); m_OutputStream.close(); delete m_CurrentSession; @@ -47,14 +45,12 @@ class Instrumentor { m_ProfileCount = 0; } - void - WriteProfileAsync(const ProfileResult &result) { - std::future async = std::async(std::launch::async, [this, result]() { WriteProfile(result); }); - async.get(); + void WriteProfileAsync(const ProfileResult &result) { + std::future asyncResult = std::async(std::launch::async, [this, result]() { WriteProfile(result); }); + asyncResult.get(); } - void - WriteProfile(const ProfileResult &result) { + void WriteProfile(const ProfileResult &result) { std::lock_guard lock(m_Mutex); // Lock mutex for critical section if (m_ProfileCount++ > 0) m_OutputStream << ","; @@ -75,20 +71,17 @@ class Instrumentor { m_OutputStream.flush(); } - void - WriteHeader() { + void WriteHeader() { m_OutputStream << "{\"otherData\": {},\"traceEvents\":["; m_OutputStream.flush(); } - void - WriteFooter() { + void WriteFooter() { m_OutputStream << "]}"; m_OutputStream.flush(); } - static Instrumentor & - Get() { + static Instrumentor & Get() { static Instrumentor instance; return instance; } @@ -105,13 +98,12 @@ class InstrumentationTimer { Stop(); } - void - Stop() { + void Stop() { auto endTimepoint = std::chrono::high_resolution_clock::now(); - long long start + long long start = std::chrono::time_point_cast(m_StartTimepoint).time_since_epoch().count(); - long long end + long long end = std::chrono::time_point_cast(endTimepoint).time_since_epoch().count(); uint32_t threadID = std::hash{}(std::this_thread::get_id()); diff --git a/src/AyonCppApi/appDataFoulder.h b/src/AyonCppApi/appDataFolder.h similarity index 100% rename from src/AyonCppApi/appDataFoulder.h rename to src/AyonCppApi/appDataFolder.h diff --git a/src/AyonCppApi/devMacros.h b/src/AyonCppApi/devMacros.h index 5492c02..f909ffc 100644 --- a/src/AyonCppApi/devMacros.h +++ b/src/AyonCppApi/devMacros.h @@ -1,7 +1,7 @@ #ifndef DEV_MACRO_H #define DEV_MACRO_H -#if JTRACE == 1 +#if defined(JTRACE) && JTRACE == 1 #include "Instrumentor.h" #define PerfTimer(x) InstrumentationTimer timer(x); #else diff --git a/src/AyonCppApi/perfPrinter.h b/src/AyonCppApi/perfPrinter.h index 21f9cb6..73a6b8b 100644 --- a/src/AyonCppApi/perfPrinter.h +++ b/src/AyonCppApi/perfPrinter.h @@ -8,7 +8,7 @@ struct perfStats { std::chrono::time_point endTime; std::string StatName; std::string HeaderText; - std::string FoderText; + std::string FooterText; std::string Note; std::vector extraInfo; bool Stop = false; @@ -26,8 +26,7 @@ class perfPrinter { this->printStats(); }; - void - printStats() { + void printStats() { std::cout << currentRunStats.StatName << "\n" << currentRunStats.HeaderText << "\n" << currentRunStats.Note << "\n"; @@ -38,11 +37,11 @@ class perfPrinter { .time_since_epoch() .count(); - long long castEndTime + long long castedEndTime = std::chrono::time_point_cast(std::chrono::high_resolution_clock::now()) .time_since_epoch() .count(); - std::cout << "Execution Time: " << castedStartTime - castEndTime << "\n"; + std::cout << "Execution Time: " << castedEndTime - castedStartTime << "\n"; std::cout << "ThreadId: " << std::hash{}(std::this_thread::get_id()) << "\n"; @@ -50,6 +49,6 @@ class perfPrinter { for (const std::string &infoEntry: currentRunStats.extraInfo) { std::cout << infoEntry << "\n"; } - std::cout << currentRunStats.FoderText << std::endl; + std::cout << currentRunStats.FooterText << std::endl; }; }; diff --git a/test/AyonCppApiTestsMain.cpp b/test/AyonCppApiTestsMain.cpp index 755084a..a286ee5 100644 --- a/test/AyonCppApiTestsMain.cpp +++ b/test/AyonCppApiTestsMain.cpp @@ -42,12 +42,12 @@ AyonCppApiTest::load_EnvVariables(std::string &envFilePath, bool AyonCppApiTest::test_SimpleResolve(nlohmann::json &JsonFile, const bool &RunOnlyOnce, const bool &Print, AyonApi &Api) { - nlohmann::json JsonFileStage = JsonFile["Resolve"]; + nlohmann::json jsonFileStage = JsonFile["Resolve"]; - for (auto it = JsonFileStage.begin(); it != JsonFileStage.end(); it++) { - std::pair test = Api.resolvePath(it.key()); + for (const auto& item : jsonFileStage.items()) { + std::pair test = Api.resolvePath(item.key()); - if (test.second != JsonFileStage[it.key()]["RootResolved"]) { + if (test.second != item.value()["RootResolved"]) { return false; } if (Print) { @@ -62,21 +62,21 @@ AyonCppApiTest::test_SimpleResolve(nlohmann::json &JsonFile, const bool &RunOnly bool AyonCppApiTest::test_BatchResolve(nlohmann::json &JsonFile, const bool &Print, AyonApi &Api) { - nlohmann::json JsonFileStage = JsonFile["Resolve"]; + nlohmann::json jsonFileStage = JsonFile["Resolve"]; std::vector uriListSource; - for (auto it = JsonFileStage.begin(); it != JsonFileStage.end(); it++) { - uriListSource.push_back(it.key()); + for (const auto& item : jsonFileStage.items()) { + uriListSource.push_back(item.key()); } std::unordered_map test = Api.batchResolvePath(uriListSource); - for (std::pair element: test) { + for (const auto& element : test) { if (Print) { std::cout << "BatchTest Run Result: " << element.first << " / " << element.second; } - if (JsonFileStage.find(element.first) != JsonFileStage.end()) { - if (element.second != JsonFileStage[element.first]["RootResolved"]) { + if (jsonFileStage.find(element.first) != jsonFileStage.end()) { + if (element.second != jsonFileStage[element.first]["RootResolved"]) { return false; } } diff --git a/test/GTestMain.cpp b/test/GTestMain.cpp index 1187bff..291ed26 100644 --- a/test/GTestMain.cpp +++ b/test/GTestMain.cpp @@ -8,21 +8,14 @@ nlohmann::json JsonFile; -AyonApi getApiInstance() { - std::string AYON_API_KEY; - std::string AYON_SERVER_URL; - std::string AYON_SITE_ID; - std::string AYON_PROJECT_NAME; - - #ifdef _WIN32 - std::string envFilePath("test\\.env_http"); - #else - std::string envFilePath("test/.env_http"); - #endif - if (!AyonCppApiTest::load_EnvVariables(envFilePath, AYON_API_KEY, AYON_SERVER_URL, AYON_SITE_ID, AYON_PROJECT_NAME)) { - std::cerr << "Failed to load environment variables!" << std::endl; - throw std::runtime_error("Failed to load environment variables!"); - } +AyonApi +getApiInstance() { + std::string AYON_API_KEY("SuperSaveTestKey"); + std::string AYON_SERVER_URL("http://localhost:8003"); + std::string AYON_SITE_ID("TestId"); + std::string AYON_PROJECT_NAME("TestPrjName"); + std::string AYONLOGGERLOGLVL("CRITICAL"); + std::string AYONLOGGERFILELOGGING("OFF"); return AyonApi("./test_logs", AYON_API_KEY, AYON_SERVER_URL, AYON_PROJECT_NAME, AYON_SITE_ID); } @@ -34,7 +27,6 @@ TEST(AyonCppApi, AyonCppApiCreation) { TEST(AyonCppApi, AyonCppApiSerialResolveRootReplace) { Instrumentor::Get().BeginSession("Profile", "bin/profSerial.json"); AyonApi Api = getApiInstance(); - nlohmann::json JsonFileStage = JsonFile["Resolve"]; bool RunOnlyOneResolveIteration = false; bool printResult = false; @@ -48,7 +40,6 @@ TEST(AyonCppApi, AyonCppApiSerialResolveRootReplace) { TEST(AyonCppApi, AyonCppApiBatchResolveRootReplace) { Instrumentor::Get().BeginSession("Profile", "bin/profBatch.json"); AyonApi Api = getApiInstance(); - nlohmann::json JsonFileStage = JsonFile["Resolve"]; bool RunOnlyOneResolveIteration = false; bool printResult = false; @@ -57,33 +48,11 @@ TEST(AyonCppApi, AyonCppApiBatchResolveRootReplace) { } Instrumentor::Get().EndSession(); + std::cout << std::endl; } -AyonApi getApiInstanceSSL() { - std::string AYON_API_KEY; - std::string AYON_SERVER_URL; - std::string AYON_SITE_ID; - std::string AYON_PROJECT_NAME; - - #ifdef _WIN32 - std::string envFilePath("test\\.env_https"); - #else - std::string envFilePath("test/.env_https"); - #endif - if (!AyonCppApiTest::load_EnvVariables(envFilePath, AYON_API_KEY, AYON_SERVER_URL, AYON_SITE_ID, AYON_PROJECT_NAME)) { - std::cerr << "Failed to load environment variables!" << std::endl; - throw std::runtime_error("Failed to load environment variables!"); - } - - return AyonApi("./test_logs", AYON_API_KEY, AYON_SERVER_URL, AYON_PROJECT_NAME, AYON_SITE_ID); -} - -TEST(AyonCppApi, AyonCppApiCreationSSL) { - AyonApi Test = getApiInstanceSSL(); -} - -int main(int argc, char** argv) { - std::cout << "Running tests..." << std::endl; +int +main(int argc, char** argv) { std::ifstream file("test/testData.json"); if (!file.is_open()) { std::cerr << "Failed to open file!" << std::endl;