Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent UI freeze in BSQ dashboard view. #6131

Merged
merged 2 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 15 additions & 33 deletions core/src/main/java/bisq/core/provider/price/PriceFeedService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import bisq.core.monetary.Price;
import bisq.core.provider.PriceHttpClient;
import bisq.core.provider.ProvidersRepository;
import bisq.core.trade.statistics.TradeStatistics3;
import bisq.core.user.Preferences;

import bisq.network.http.HttpClient;
Expand All @@ -49,15 +48,11 @@

import java.time.Instant;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import java.util.function.Consumer;

import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -257,12 +252,8 @@ public MarketPrice getMarketPrice(String currencyCode) {
return cache.getOrDefault(currencyCode, null);
}

private void setBisqMarketPrice(String currencyCode, Price price) {
if (!cache.containsKey(currencyCode) || !cache.get(currencyCode).isExternallyProvidedPrice()) {
cache.put(currencyCode, new MarketPrice(currencyCode,
MathUtils.scaleDownByPowerOf10(price.getValue(), CurrencyUtil.isCryptoCurrency(currencyCode) ? 8 : 4),
0,
false));
public void setBisqMarketPrice(String currencyCode, Price price) {
if (applyPriceToCache(currencyCode, price)) {
updateCounter.set(updateCounter.get() + 1);
}
}
Expand Down Expand Up @@ -301,28 +292,20 @@ public Date getLastRequestTimeStamp() {
return new Date(epochInMillisAtLastRequest);
}

public void applyLatestBisqMarketPrice(Set<TradeStatistics3> tradeStatisticsSet) {
// takes about 10 ms for 5000 items
Map<String, List<TradeStatistics3>> mapByCurrencyCode = new HashMap<>();
tradeStatisticsSet.forEach(e -> {
List<TradeStatistics3> list;
String currencyCode = e.getCurrency();
if (mapByCurrencyCode.containsKey(currencyCode)) {
list = mapByCurrencyCode.get(currencyCode);
} else {
list = new ArrayList<>();
mapByCurrencyCode.put(currencyCode, list);
}
list.add(e);
});
public void applyInitialBisqMarketPrice(Map<String, Price> priceByCurrencyCode) {
priceByCurrencyCode.forEach(this::applyPriceToCache);
updateCounter.set(updateCounter.get() + 1);
}

mapByCurrencyCode.values().stream()
.filter(list -> !list.isEmpty())
.forEach(list -> {
list.sort(Comparator.comparing(TradeStatistics3::getDate));
TradeStatistics3 tradeStatistics = list.get(list.size() - 1);
setBisqMarketPrice(tradeStatistics.getCurrency(), tradeStatistics.getTradePrice());
});
private boolean applyPriceToCache(String currencyCode, Price price) {
if (!cache.containsKey(currencyCode) || !cache.get(currencyCode).isExternallyProvidedPrice()) {
cache.put(currencyCode, new MarketPrice(currencyCode,
MathUtils.scaleDownByPowerOf10(price.getValue(), CurrencyUtil.isCryptoCurrency(currencyCode) ? 8 : 4),
0,
false));
return true;
}
return false;
}

public Optional<Price> getBsqPrice() {
Expand Down Expand Up @@ -386,7 +369,6 @@ private boolean applyPriceToConsumer() {
if (faultHandler != null)
faultHandler.handleFault(errorMessage, new PriceRequestException(errorMessage));
}

updateCounter.set(updateCounter.get() + 1);

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import bisq.core.locale.CurrencyTuple;
import bisq.core.locale.CurrencyUtil;
import bisq.core.locale.Res;
import bisq.core.monetary.Price;
import bisq.core.provider.price.PriceFeedService;
import bisq.core.trade.model.TradeModel;
import bisq.core.trade.model.bisq_v1.BuyerTrade;
Expand All @@ -46,7 +47,10 @@
import java.io.File;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -93,14 +97,15 @@ public void shutDown() {
}

public void onAllServicesInitialized() {
// tradestatistics listener for updating last traded price
p2PService.getP2PDataStorage().addAppendOnlyDataStoreListener(payload -> {
if (payload instanceof TradeStatistics3) {
TradeStatistics3 tradeStatistics = (TradeStatistics3) payload;
if (!tradeStatistics.isValid()) {
return;
}
observableTradeStatisticsSet.add(tradeStatistics);
priceFeedService.applyLatestBisqMarketPrice(observableTradeStatisticsSet);
priceFeedService.setBisqMarketPrice(tradeStatistics.getCurrency(), tradeStatistics.getTradePrice());
maybeDumpStatistics();
}
});
Expand All @@ -111,7 +116,30 @@ public void onAllServicesInitialized() {
.filter(TradeStatistics3::isValid)
.collect(Collectors.toSet());
observableTradeStatisticsSet.addAll(set);
priceFeedService.applyLatestBisqMarketPrice(observableTradeStatisticsSet);

// collate prices by ccy -- takes about 10 ms for 5000 items
Map<String, List<TradeStatistics3>> allPriceByCurrencyCode = new HashMap<>();
observableTradeStatisticsSet.forEach(e -> {
List<TradeStatistics3> list;
String currencyCode = e.getCurrency();
if (allPriceByCurrencyCode.containsKey(currencyCode)) {
list = allPriceByCurrencyCode.get(currencyCode);
} else {
list = new ArrayList<>();
allPriceByCurrencyCode.put(currencyCode, list);
}
list.add(e);
});
// get the most recent price for each ccy and notify priceFeedService
Map<String, Price> newestPriceByCurrencyCode = new HashMap<>();
allPriceByCurrencyCode.values().stream()
.filter(list -> !list.isEmpty())
.forEach(list -> {
list.sort(Comparator.comparing(TradeStatistics3::getDate));
TradeStatistics3 tradeStatistics = list.get(list.size() - 1);
newestPriceByCurrencyCode.put(tradeStatistics.getCurrency(), tradeStatistics.getTradePrice());
});
priceFeedService.applyInitialBisqMarketPrice(newestPriceByCurrencyCode);
maybeDumpStatistics();
}

Expand Down