From a8b0580d515946e5b447c8dc7c600ff1f3e5694d Mon Sep 17 00:00:00 2001 From: Stephane Janel Date: Sat, 28 Sep 2024 12:30:46 +0200 Subject: [PATCH] Return market data instead of void --- src/engine/include/coincenter.hpp | 4 ++-- src/engine/src/coincenter.cpp | 8 +++++--- src/engine/src/exchangesorchestrator.cpp | 5 +---- src/engine/test/coincenteroptions_test.cpp | 5 +++-- src/objects/include/file.hpp | 5 +++++ src/objects/src/file.cpp | 4 +++- src/tech/include/cct_const.hpp | 3 +++ 7 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/engine/include/coincenter.hpp b/src/engine/include/coincenter.hpp index 605d269d..00a1bd1f 100644 --- a/src/engine/include/coincenter.hpp +++ b/src/engine/include/coincenter.hpp @@ -53,9 +53,9 @@ class Coincenter { CurrencyCode equiCurrencyCode, std::optional depth = std::nullopt); - /// Query market data without returning it. + /// Query market data (order books and last trades). /// This method is especially useful for serialization and metric exports. - void queryMarketDataPerExchange(std::span marketPerPublicExchange); + MarketDataPerExchange queryMarketDataPerExchange(std::span marketPerPublicExchangePos); /// Retrieve the last 24h traded volume for exchanges supporting given market. MonetaryAmountPerExchange getLast24hTradedVolumePerExchange(Market mk, ExchangeNameSpan exchangeNames); diff --git a/src/engine/src/coincenter.cpp b/src/engine/src/coincenter.cpp index 03747e99..6e7eecea 100644 --- a/src/engine/src/coincenter.cpp +++ b/src/engine/src/coincenter.cpp @@ -72,11 +72,11 @@ MarketOrderBookConversionRates Coincenter::getMarketOrderBooks(Market mk, Exchan return ret; } -void Coincenter::queryMarketDataPerExchange(std::span marketPerPublicExchange) { +MarketDataPerExchange Coincenter::queryMarketDataPerExchange(std::span marketPerPublicExchangePos) { ExchangeNames exchangeNames; int exchangePos{}; - for (Market market : marketPerPublicExchange) { + for (Market market : marketPerPublicExchangePos) { if (market.isDefined()) { exchangeNames.emplace_back(kSupportedExchanges[exchangePos]); } @@ -84,7 +84,7 @@ void Coincenter::queryMarketDataPerExchange(std::span marketPerPub } const auto marketDataPerExchange = - _exchangesOrchestrator.getMarketDataPerExchange(marketPerPublicExchange, exchangeNames); + _exchangesOrchestrator.getMarketDataPerExchange(marketPerPublicExchangePos, exchangeNames); // Transform data structures to export metrics input format MarketOrderBookConversionRates marketOrderBookConversionRates(marketDataPerExchange.size()); @@ -104,6 +104,8 @@ void Coincenter::queryMarketDataPerExchange(std::span marketPerPub _metricsExporter.exportOrderbookMetrics(marketOrderBookConversionRates); _metricsExporter.exportLastTradesMetrics(lastTradesPerExchange); + + return marketDataPerExchange; } BalancePerExchange Coincenter::getBalance(std::span privateExchangeNames, diff --git a/src/engine/src/exchangesorchestrator.cpp b/src/engine/src/exchangesorchestrator.cpp index 4afb6dd9..f767090c 100644 --- a/src/engine/src/exchangesorchestrator.cpp +++ b/src/engine/src/exchangesorchestrator.cpp @@ -968,9 +968,6 @@ MarketDataPerExchange ExchangesOrchestrator::getMarketDataPerExchange(std::span< MarketDataPerExchange ret(selectedExchanges.size()); _threadPool.parallelTransform(selectedExchanges, ret.begin(), [&marketPerPublicExchange](Exchange *exchange) { - if (!exchange->exchangeConfig().withMarketDataSerialization()) { - log::warn("Calling market-data on {} with data serialization disabled", exchange->name()); - } // Call order book and last trades sequentially for this exchange Market market = marketPerPublicExchange[exchange->publicExchangePos()]; @@ -1071,4 +1068,4 @@ MarketTradingGlobalResultPerExchange ExchangesOrchestrator::getMarketTraderResul return marketTradingGlobalResultPerExchange; } -} // namespace cct +} // namespace cct \ No newline at end of file diff --git a/src/engine/test/coincenteroptions_test.cpp b/src/engine/test/coincenteroptions_test.cpp index 360e4e2f..f7d1da6f 100644 --- a/src/engine/test/coincenteroptions_test.cpp +++ b/src/engine/test/coincenteroptions_test.cpp @@ -49,8 +49,9 @@ TEST_F(CoincenterCmdLineOptionsTest, DefaultConstructorShouldValueInitializeAll) EXPECT_EQ(opts, *pRhs); - static_assert(std::is_trivially_destructible_v, - "then destructor should be called (but ideally this type should stay trivially destructible)"); + if constexpr (!std::is_trivially_destructible_v) { + std::destroy_at(pRhs); + } } TEST_F(CoincenterCmdLineOptionsTest, MergeGlobal) { diff --git a/src/objects/include/file.hpp b/src/objects/include/file.hpp index 85cd88a0..2bda4c92 100644 --- a/src/objects/include/file.hpp +++ b/src/objects/include/file.hpp @@ -15,6 +15,11 @@ class File : public Reader, public Writer { enum class Type : int8_t { kCache, kSecret, kStatic, kLog }; enum class IfError : int8_t { kThrow, kNoThrow }; + /// Creates a File directly from a file path. + File(std::string_view filePath, IfError ifError); + + /// Creates a File from the coincenter data directory, with the type of the file and its name in the main data + /// directory. File(std::string_view dataDir, Type type, std::string_view name, IfError ifError); [[nodiscard]] string readAll() const override; diff --git a/src/objects/src/file.cpp b/src/objects/src/file.cpp index c35898d1..5ba541cc 100644 --- a/src/objects/src/file.cpp +++ b/src/objects/src/file.cpp @@ -37,6 +37,8 @@ string FullFileName(std::string_view dataDir, std::string_view fileName, File::T } } // namespace +File::File(std::string_view filePath, IfError ifError) : _filePath(filePath), _ifError(ifError) {} + File::File(std::string_view dataDir, Type type, std::string_view name, IfError ifError) : _filePath(FullFileName(dataDir, name, type)), _ifError(ifError) {} @@ -49,7 +51,7 @@ string File::readAll() const { throw exception("Unable to open {} for reading", _filePath); } try { - data = string((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + data = string(std::istreambuf_iterator(file), std::istreambuf_iterator()); } catch (const std::exception& e) { if (_ifError == IfError::kThrow) { throw e; diff --git a/src/tech/include/cct_const.hpp b/src/tech/include/cct_const.hpp index c1ea85aa..62006368 100644 --- a/src/tech/include/cct_const.hpp +++ b/src/tech/include/cct_const.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -18,6 +19,8 @@ static constexpr std::string_view kDepositAddressesFileName = "depositaddresses. static constexpr std::string_view kSupportedExchanges[] = {"binance", "bithumb", "huobi", "kraken", "kucoin", "upbit"}; +static_assert(std::ranges::is_sorted(kSupportedExchanges)); + static constexpr int kNbSupportedExchanges = static_cast(std::size(kSupportedExchanges)); static constexpr int kTypicalNbPrivateAccounts = kNbSupportedExchanges;