Skip to content

Commit

Permalink
Use DurationToString in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanel committed Jan 17, 2024
1 parent e7c5fcf commit c72d9f3
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 116 deletions.
10 changes: 4 additions & 6 deletions src/api-objects/src/tradeoptions.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include "tradeoptions.hpp"

#include <chrono>
#include <string_view>

#include "cct_string.hpp"
#include "durationstring.hpp"
#include "priceoptions.hpp"
#include "stringhelpers.hpp"
#include "timedef.hpp"
#include "tradedefinitions.hpp"
#include "unreachable.hpp"
Expand Down Expand Up @@ -79,10 +78,9 @@ string TradeOptions::str(bool placeRealOrderInSimulationMode) const {
ret.append(", ");
ret.append(tradeSyncPolicyStr());
ret.append(" mode, timeout of ");
AppendString(ret, std::chrono::duration_cast<TimeInS>(_maxTradeTime).count());
ret.append("s, ").append(timeoutActionStr()).append(" at timeout, min time between two price updates of ");
AppendString(ret, std::chrono::duration_cast<TimeInS>(_minTimeBetweenPriceUpdates).count());
ret.push_back('s');
ret.append(DurationToString(_maxTradeTime));
ret.append(", ").append(timeoutActionStr()).append(" at timeout, min time between two price updates of ");
ret.append(DurationToString(_minTimeBetweenPriceUpdates));
return ret;
}
} // namespace cct
5 changes: 3 additions & 2 deletions src/api/common/src/exchangeprivateapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "currencycode.hpp"
#include "deposit.hpp"
#include "depositsconstraints.hpp"
#include "durationstring.hpp"
#include "exchangebase.hpp"
#include "exchangeinfo.hpp"
#include "exchangename.hpp"
Expand Down Expand Up @@ -253,9 +254,9 @@ DeliveredWithdrawInfo ExchangePrivate::withdraw(MonetaryAmount grossAmount, Exch
InitiatedWithdrawInfo initiatedWithdrawInfo =
launchWithdraw(grossAmount, targetExchange.queryDepositWallet(currencyCode));
Duration withdrawRefreshTime = withdrawOptions.withdrawRefreshTime();
log::info("Withdraw {} of {} to {} initiated from {} to {}, with a periodic refresh time of {} s",
log::info("Withdraw {} of {} to {} initiated from {} to {}, with a periodic refresh time of {}",
initiatedWithdrawInfo.withdrawId(), grossAmount, initiatedWithdrawInfo.receivingWallet(), exchangeName(),
targetExchange.exchangeName(), std::chrono::duration_cast<TimeInS>(withdrawRefreshTime).count());
targetExchange.exchangeName(), DurationToString(withdrawRefreshTime));
SentWithdrawInfo sentWithdrawInfo(currencyCode);
MonetaryAmount netDeliveredAmount;

Expand Down
11 changes: 6 additions & 5 deletions src/api/exchanges/src/binanceprivateapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "currencyexchangeflatset.hpp"
#include "deposit.hpp"
#include "depositsconstraints.hpp"
#include "durationstring.hpp"
#include "exchangename.hpp"
#include "exchangeprivateapi.hpp"
#include "exchangeprivateapitypes.hpp"
Expand Down Expand Up @@ -120,8 +121,8 @@ bool CheckErrorDoRetry(int statusCode, const json& ret, QueryDelayDir& queryDela
sleepingTime = kInitialDurationQueryDelay;
}
queryDelay -= sleepingTime;
log::warn("Our local time is ahead of Binance server's time. Query delay modified to {} ms",
std::chrono::duration_cast<TimeInMs>(queryDelay).count());
log::warn("Our local time is ahead of Binance server's time. Query delay modified to {}",
DurationToString(queryDelay));
// Ensure Nonce is increasing while modifying the query delay
std::this_thread::sleep_for(sleepingTime);
return true;
Expand All @@ -136,8 +137,8 @@ bool CheckErrorDoRetry(int statusCode, const json& ret, QueryDelayDir& queryDela
sleepingTime = kInitialDurationQueryDelay;
}
queryDelay += sleepingTime;
log::warn("Our local time is behind of Binance server's time. Query delay modified to {} ms",
std::chrono::duration_cast<TimeInMs>(queryDelay).count());
log::warn("Our local time is behind of Binance server's time. Query delay modified to {}",
DurationToString(queryDelay));
return true;
}
}
Expand Down Expand Up @@ -172,7 +173,7 @@ json PrivateQuery(CurlHandle& curlHandle, const APIKey& apiKey, HttpRequestType
json ret;
for (int retryPos = 0; retryPos < kNbOrderRequestsRetries; ++retryPos) {
if (retryPos != 0) {
log::trace("Wait {} ms...", std::chrono::duration_cast<TimeInMs>(sleepingTime).count());
log::trace("Wait {}...", DurationToString(sleepingTime));
std::this_thread::sleep_for(sleepingTime);
sleepingTime = (3 * sleepingTime) / 2;
}
Expand Down
5 changes: 3 additions & 2 deletions src/api/exchanges/src/bithumbprivateapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "currencycode.hpp"
#include "deposit.hpp"
#include "depositsconstraints.hpp"
#include "durationstring.hpp"
#include "exchangeinfo.hpp"
#include "exchangename.hpp"
#include "exchangeprivateapi.hpp"
Expand Down Expand Up @@ -975,8 +976,8 @@ InitiatedWithdrawInfo BithumbPrivate::launchWithdraw(MonetaryAmount grossAmount,
static constexpr int kNbRetriesCatchWindow = 15;
for (int retryPos = 0; retryPos < kNbRetriesCatchWindow && newWithdrawTrx.empty(); ++retryPos) {
if (retryPos != 0) {
log::warn("Cannot retrieve just launched withdraw, retry {}/{} in {} s...", retryPos, kNbRetriesCatchWindow,
std::chrono::duration_cast<TimeInS>(sleepingTime).count());
log::warn("Cannot retrieve just launched withdraw, retry {}/{} in {}...", retryPos, kNbRetriesCatchWindow,
DurationToString(sleepingTime));
std::this_thread::sleep_for(sleepingTime);
sleepingTime = (3 * sleepingTime) / 2;
}
Expand Down
3 changes: 2 additions & 1 deletion src/api/exchanges/src/krakenprivateapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "currencyexchangeflatset.hpp"
#include "deposit.hpp"
#include "depositsconstraints.hpp"
#include "durationstring.hpp"
#include "exchangeinfo.hpp"
#include "exchangename.hpp"
#include "exchangeprivateapi.hpp"
Expand Down Expand Up @@ -99,7 +100,7 @@ std::pair<json, KrakenErrorEnum> PrivateQuery(CurlHandle& curlHandle, const APIK
errorIt = response.find(kErrorKey)) {
log::error("Kraken private API rate limit exceeded");
sleepingTime *= 2;
log::debug("Wait {} ms", std::chrono::duration_cast<std::chrono::milliseconds>(sleepingTime).count());
log::debug("Wait {}", DurationToString(sleepingTime));
std::this_thread::sleep_for(sleepingTime);

// We need to update the nonce
Expand Down
5 changes: 2 additions & 3 deletions src/api/exchanges/src/upbitprivateapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <algorithm>
#include <cassert>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <optional>
Expand Down Expand Up @@ -33,6 +32,7 @@
#include "currencyexchangeflatset.hpp"
#include "deposit.hpp"
#include "depositsconstraints.hpp"
#include "durationstring.hpp"
#include "exchangeinfo.hpp"
#include "exchangename.hpp"
#include "exchangeprivateapi.hpp"
Expand Down Expand Up @@ -219,8 +219,7 @@ Wallet UpbitPrivate::DepositWalletFunc::operator()(CurrencyCode currencyCode) {
int nbRetries = 0;
do {
if (nbRetries > 0) {
log::info("Waiting {} s for address to be generated...",
std::chrono::duration_cast<TimeInS>(sleepingTime).count());
log::info("Waiting {} for address to be generated...", DurationToString(sleepingTime));
}
std::this_thread::sleep_for(sleepingTime);
result = PrivateQuery(_curlHandle, _apiKey, HttpRequestType::kGet, "/v1/deposits/coin_address", postData);
Expand Down
8 changes: 4 additions & 4 deletions src/engine/include/coincenteroptionsdef.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class CoincenterCmdLineOptionsDefinitions {
static constexpr int kDefaultMonitoringPort = 9091; // Prometheus default push port
static constexpr Duration kDefaultRepeatTime = TimeInS(1);

static constexpr int64_t kDefaultTradeTimeout =
static constexpr int64_t kDefaultTradeTimeoutSeconds =
std::chrono::duration_cast<TimeInS>(TradeOptions().maxTradeTime()).count();
static constexpr int64_t kMinUpdatePriceTime =
static constexpr int64_t kMinUpdatePriceTimeSeconds =
std::chrono::duration_cast<TimeInS>(TradeOptions().minTimeBetweenPriceUpdates()).count();
static constexpr int64_t kDefaultRepeatDurationSeconds =
std::chrono::duration_cast<TimeInS>(kDefaultRepeatTime).count();
Expand Down Expand Up @@ -76,15 +76,15 @@ class CoincenterCmdLineOptionsDefinitions {
static constexpr std::string_view kTradeTimeout1 = "Adjust trade timeout (default: ";
static constexpr std::string_view kTradeTimeout2 = "s). Remaining orders will be cancelled after the timeout.";
static constexpr std::string_view kTradeTimeout =
JoinStringView_v<kTradeTimeout1, IntToStringView_v<kDefaultTradeTimeout>, kTradeTimeout2>;
JoinStringView_v<kTradeTimeout1, IntToStringView_v<kDefaultTradeTimeoutSeconds>, kTradeTimeout2>;

static constexpr std::string_view kTradeUpdatePrice1 =
"Set the min time allowed between two limit price updates (default: ";
static constexpr std::string_view kTradeUpdatePrice2 =
"s). Avoids cancelling / placing new orders too often with high volumes which can be counter productive "
"sometimes.";
static constexpr std::string_view kTradeUpdatePrice =
JoinStringView_v<kTradeUpdatePrice1, IntToStringView_v<kMinUpdatePriceTime>, kTradeUpdatePrice2>;
JoinStringView_v<kTradeUpdatePrice1, IntToStringView_v<kMinUpdatePriceTimeSeconds>, kTradeUpdatePrice2>;

static constexpr std::string_view kSimulationMode1 = "Activates simulation mode only (default: ";
static constexpr std::string_view kSimulationMode2 = TradeOptions().isSimulation() ? "true" : "false";
Expand Down
4 changes: 1 addition & 3 deletions src/engine/src/coincentercommandfactory.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "coincentercommandfactory.hpp"

#include <chrono>
#include <string_view>
#include <utility>

Expand Down Expand Up @@ -34,8 +33,7 @@ CoincenterCommand CoincenterCommandFactory::createOrderCommand(CoincenterCommand
}
CoincenterCommand ret(type);
ret.setOrdersConstraints(
OrdersConstraints(market.base(), market.quote(), std::chrono::duration_cast<Duration>(_cmdLineOptions.minAge),
std::chrono::duration_cast<Duration>(_cmdLineOptions.maxAge),
OrdersConstraints(market.base(), market.quote(), _cmdLineOptions.minAge, _cmdLineOptions.maxAge,
OrdersConstraints::OrderIdSet(StringOptionParser(_cmdLineOptions.ids).getCSVValues())))
.setExchangeNames(optionParser.parseExchanges());
return ret;
Expand Down
17 changes: 6 additions & 11 deletions src/engine/src/coincentercommands.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "coincentercommands.hpp"

#include <chrono>
#include <span>
#include <string_view>
#include <utility>
Expand Down Expand Up @@ -116,22 +115,18 @@ void CoincenterCommands::addOption(const CoincenterCmdLineOptions &cmdLineOption
if (cmdLineOptions.recentDepositsInfo) {
optionParser = StringOptionParser(*cmdLineOptions.recentDepositsInfo);
_commands.emplace_back(CoincenterCommandType::kRecentDeposits)
.setDepositsConstraints(
DepositsConstraints(optionParser.parseCurrency(StringOptionParser::FieldIs::kOptional),
std::chrono::duration_cast<Duration>(cmdLineOptions.minAge),
std::chrono::duration_cast<Duration>(cmdLineOptions.maxAge),
DepositsConstraints::IdSet(StringOptionParser(cmdLineOptions.ids).getCSVValues())))
.setDepositsConstraints(DepositsConstraints(
optionParser.parseCurrency(StringOptionParser::FieldIs::kOptional), cmdLineOptions.minAge,
cmdLineOptions.maxAge, DepositsConstraints::IdSet(StringOptionParser(cmdLineOptions.ids).getCSVValues())))
.setExchangeNames(optionParser.parseExchanges());
}

if (cmdLineOptions.recentWithdrawsInfo) {
optionParser = StringOptionParser(*cmdLineOptions.recentWithdrawsInfo);
_commands.emplace_back(CoincenterCommandType::kRecentWithdraws)
.setWithdrawsConstraints(
WithdrawsConstraints(optionParser.parseCurrency(StringOptionParser::FieldIs::kOptional),
std::chrono::duration_cast<Duration>(cmdLineOptions.minAge),
std::chrono::duration_cast<Duration>(cmdLineOptions.maxAge),
WithdrawsConstraints::IdSet(StringOptionParser(cmdLineOptions.ids).getCSVValues())))
.setWithdrawsConstraints(WithdrawsConstraints(
optionParser.parseCurrency(StringOptionParser::FieldIs::kOptional), cmdLineOptions.minAge,
cmdLineOptions.maxAge, WithdrawsConstraints::IdSet(StringOptionParser(cmdLineOptions.ids).getCSVValues())))
.setExchangeNames(optionParser.parseExchanges());
}

Expand Down
8 changes: 4 additions & 4 deletions src/http-request/include/besturlpicker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ class BestURLPicker {
: _pBaseUrls(baseUrls.data()), _responseTimeStatsPerBaseUrl(baseUrls.size()) {}

struct ResponseTimeStats {
uint16_t nbRequestsDone; // when reaching max, all stats are reset to give equal chances to all base URLs
uint16_t avgResponseTime; // approximation of moving average
uint16_t avgDeviation; // approximation of moving standard deviation
uint16_t nbRequestsDone; // when reaching max, all stats are reset to give equal chances to all base URLs
uint16_t avgResponseTimeInMs; // approximation of moving average
uint16_t avgDeviationInMs; // approximation of moving standard deviation

bool operator==(const ResponseTimeStats &) const noexcept = default;

uint32_t score() const { return static_cast<uint32_t>(avgResponseTime) + avgDeviation; }
uint32_t score() const { return static_cast<uint32_t>(avgResponseTimeInMs) + avgDeviationInMs; }
};

using ResponseTimeStatsPerBaseUrl = FixedCapacityVector<ResponseTimeStats, kNbMaxBaseUrl>;
Expand Down
5 changes: 3 additions & 2 deletions src/http-request/include/invariant-request-retry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "cct_type_traits.hpp"
#include "curlhandle.hpp"
#include "curloptions.hpp"
#include "durationstring.hpp"
#include "timedef.hpp"
#include "unreachable.hpp"

Expand Down Expand Up @@ -50,8 +51,8 @@ class InvariantRequestRetry {

do {
if (nbRetries != 0) {
log::warn("Got query error: '{}', retry {}/{} after {} ms", ret.dump(), nbRetries,
_queryRetryPolicy.nbMaxRetries, std::chrono::duration_cast<TimeInMs>(sleepingTime).count());
log::warn("Got query error: '{}', retry {}/{} after {}", ret.dump(), nbRetries, _queryRetryPolicy.nbMaxRetries,
DurationToString(sleepingTime));
std::this_thread::sleep_for(sleepingTime);
sleepingTime *= _queryRetryPolicy.exponentialBackoff;
}
Expand Down
24 changes: 12 additions & 12 deletions src/http-request/src/besturlpicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,38 +67,38 @@ void BestURLPicker::storeResponseTimePerBaseURL(int8_t baseUrlPos, uint32_t resp
NbRequestType nbRequestsToConsider = std::min(stats.nbRequestsDone, kMaxLastNbRequestsToConsider);

// Update moving average
uint64_t sumResponseTime = static_cast<uint64_t>(stats.avgResponseTime) * (nbRequestsToConsider - 1);
uint64_t sumResponseTime = static_cast<uint64_t>(stats.avgResponseTimeInMs) * (nbRequestsToConsider - 1);
sumResponseTime += responseTimeInMs;
uint64_t newAverageResponseTime = sumResponseTime / nbRequestsToConsider;
using RTType = decltype(stats.avgResponseTime);
using RTType = decltype(stats.avgResponseTimeInMs);
if (newAverageResponseTime > static_cast<uint64_t>(std::numeric_limits<RTType>::max())) {
log::warn("Cannot update accurately the new average response time {} because of overflow", newAverageResponseTime);
stats.avgResponseTime = std::numeric_limits<RTType>::max();
stats.avgResponseTimeInMs = std::numeric_limits<RTType>::max();
} else {
stats.avgResponseTime = static_cast<RTType>(newAverageResponseTime);
stats.avgResponseTimeInMs = static_cast<RTType>(newAverageResponseTime);
}

// Update moving deviation
uint64_t sumDeviation = static_cast<uint64_t>(ipow(stats.avgDeviation, 2)) * (nbRequestsToConsider - 1);
uint64_t sumDeviation = static_cast<uint64_t>(ipow(stats.avgDeviationInMs, 2)) * (nbRequestsToConsider - 1);
sumDeviation += static_cast<uint64_t>(
ipow(static_cast<int64_t>(stats.avgResponseTime) - static_cast<int64_t>(responseTimeInMs), 2));
uint64_t newDeviationResponseTime = static_cast<uint64_t>(std::sqrt(sumDeviation / nbRequestsToConsider));
using DevType = decltype(stats.avgDeviation);
ipow(static_cast<int64_t>(stats.avgResponseTimeInMs) - static_cast<int64_t>(responseTimeInMs), 2));
auto newDeviationResponseTime = static_cast<uint64_t>(std::sqrt(sumDeviation / nbRequestsToConsider));
using DevType = decltype(stats.avgDeviationInMs);
if (newDeviationResponseTime > static_cast<uint64_t>(std::numeric_limits<DevType>::max())) {
log::warn("Cannot update accurately the new deviation response time {} because of overflow",
newDeviationResponseTime);
stats.avgDeviation = std::numeric_limits<DevType>::max();
stats.avgDeviationInMs = std::numeric_limits<DevType>::max();
} else {
stats.avgDeviation = static_cast<DevType>(newDeviationResponseTime);
stats.avgDeviationInMs = static_cast<DevType>(newDeviationResponseTime);
}

log::debug("Response time stats for '{}': Avg: {} ms, Dev: {} ms, Nb: {} (last: {} ms)", _pBaseUrls[baseUrlPos],
stats.avgResponseTime, stats.avgDeviation, stats.nbRequestsDone, responseTimeInMs);
stats.avgResponseTimeInMs, stats.avgDeviationInMs, stats.nbRequestsDone, responseTimeInMs);
}

int BestURLPicker::nbRequestsDone() const {
return std::accumulate(_responseTimeStatsPerBaseUrl.begin(), _responseTimeStatsPerBaseUrl.end(), 0,
[](int sum, ResponseTimeStats stats) { return sum + stats.nbRequestsDone; });
}

} // namespace cct
} // namespace cct
12 changes: 6 additions & 6 deletions src/http-request/src/curlhandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "curlmetrics.hpp"
#include "curloptions.hpp"
#include "curlpostdata.hpp"
#include "durationstring.hpp"
#include "flatkeyvaluestring.hpp"
#include "httprequesttype.hpp"
#include "metric.hpp"
Expand Down Expand Up @@ -115,9 +116,8 @@ CurlHandle::CurlHandle(const BestURLPicker &bestURLPicker, AbstractMetricGateway
CurlSetLogIfError(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA);
#endif

log::debug("Initialize CurlHandle for {} with {} ms as minimum duration between queries",
bestURLPicker.getNextBaseURL(),
std::chrono::duration_cast<TimeInMs>(_minDurationBetweenQueries).count());
log::debug("Initialize CurlHandle for {} with {} as minimum duration between queries",
bestURLPicker.getNextBaseURL(), DurationToString(_minDurationBetweenQueries));

if (settings::IsProxyRequested(runMode)) {
if (IsProxyAvailable()) {
Expand Down Expand Up @@ -224,7 +224,7 @@ std::string_view CurlHandle::query(std::string_view endpoint, const CurlOptions
if (nowTime < _lastQueryTime + _minDurationBetweenQueries) {
// We should sleep a bit before performing query
const Duration sleepingTime = _minDurationBetweenQueries - (nowTime - _lastQueryTime);
log::debug("Wait {} ms before performing query", std::chrono::duration_cast<TimeInMs>(sleepingTime).count());
log::debug("Wait {} before performing query", DurationToString(sleepingTime));
std::this_thread::sleep_for(sleepingTime);
_lastQueryTime = nowTime + sleepingTime;
} else {
Expand All @@ -247,8 +247,8 @@ std::string_view CurlHandle::query(std::string_view endpoint, const CurlOptions
_pMetricGateway->add(MetricType::kCounter, MetricOperation::kIncrement,
CurlMetrics::kNbRequestErrorKeys.find(opts.requestType())->second);
}
log::error("Got curl error ({}), retry {}/{} after {} ms", static_cast<int>(res), retryPos, kNbMaxRetries,
std::chrono::duration_cast<TimeInMs>(sleepingTime).count());
log::error("Got curl error ({}), retry {}/{} after {}", static_cast<int>(res), retryPos, kNbMaxRetries,
DurationToString(sleepingTime));
std::this_thread::sleep_for(sleepingTime);
sleepingTime *= 2;
}
Expand Down
4 changes: 2 additions & 2 deletions src/monitoring/src/prometheusmetricgateway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "abstractmetricgateway.hpp"
#include "cct_exception.hpp"
#include "cct_log.hpp"
#include "durationstring.hpp"
#include "gethostname.hpp"
#include "metric.hpp"
#include "monitoringinfo.hpp"
Expand Down Expand Up @@ -216,8 +217,7 @@ void PrometheusMetricGateway::flush() {
auto nowTime = Clock::now();
int returnCode = _gateway.Push();
if (returnCode == kHTTPSuccessReturnCode) {
log::info("Flushed data to Prometheus in {} ms",
std::chrono::duration_cast<TimeInMs>(Clock::now() - nowTime).count());
log::info("Flushed data to Prometheus in {}", DurationToString(Clock::now() - nowTime));
} else {
log::error("Unable to push metrics to Prometheus instance - Bad return code {}", returnCode);
}
Expand Down
Loading

0 comments on commit c72d9f3

Please sign in to comment.