diff --git a/desktop/src/main/java/bisq/desktop/main/market/trades/ChartCalculations.java b/desktop/src/main/java/bisq/desktop/main/market/trades/ChartCalculations.java index 25165b4d8c..9612fdad05 100644 --- a/desktop/src/main/java/bisq/desktop/main/market/trades/ChartCalculations.java +++ b/desktop/src/main/java/bisq/desktop/main/market/trades/ChartCalculations.java @@ -99,43 +99,45 @@ public class ChartCalculations { }); } - static UpdateChartResult getUpdateChartResult(List tradeStatisticsByCurrency, - TradesChartsViewModel.TickUnit tickUnit, - Map> usdAveragePriceMapsPerTickUnit, - String currencyCode) { - // Generate date range and create sets for all ticks - Map>> itemsPerInterval = getItemsPerInterval(tradeStatisticsByCurrency, tickUnit); + static CompletableFuture getUpdateChartResult(List tradeStatisticsByCurrency, + TradesChartsViewModel.TickUnit tickUnit, + Map> usdAveragePriceMapsPerTickUnit, + String currencyCode) { + return CompletableFuture.supplyAsync(() -> { + // Generate date range and create sets for all ticks + Map>> itemsPerInterval = getItemsPerInterval(tradeStatisticsByCurrency, tickUnit); - Map usdAveragePriceMap = usdAveragePriceMapsPerTickUnit.get(tickUnit); - AtomicLong averageUsdPrice = new AtomicLong(0); + Map usdAveragePriceMap = usdAveragePriceMapsPerTickUnit.get(tickUnit); + AtomicLong averageUsdPrice = new AtomicLong(0); - // create CandleData for defined time interval - List candleDataList = itemsPerInterval.entrySet().stream() - .filter(entry -> entry.getKey() >= 0 && !entry.getValue().getValue().isEmpty()) - .map(entry -> { - long tickStartDate = entry.getValue().getKey().getTime(); - // If we don't have a price we take the previous one - if (usdAveragePriceMap.containsKey(tickStartDate)) { - averageUsdPrice.set(usdAveragePriceMap.get(tickStartDate)); - } - return getCandleData(entry.getKey(), entry.getValue().getValue(), averageUsdPrice.get(), tickUnit, currencyCode, itemsPerInterval); - }) - .sorted(Comparator.comparingLong(o -> o.tick)) - .collect(Collectors.toList()); + // create CandleData for defined time interval + List candleDataList = itemsPerInterval.entrySet().stream() + .filter(entry -> entry.getKey() >= 0 && !entry.getValue().getValue().isEmpty()) + .map(entry -> { + long tickStartDate = entry.getValue().getKey().getTime(); + // If we don't have a price we take the previous one + if (usdAveragePriceMap.containsKey(tickStartDate)) { + averageUsdPrice.set(usdAveragePriceMap.get(tickStartDate)); + } + return getCandleData(entry.getKey(), entry.getValue().getValue(), averageUsdPrice.get(), tickUnit, currencyCode, itemsPerInterval); + }) + .sorted(Comparator.comparingLong(o -> o.tick)) + .collect(Collectors.toList()); - List> priceItems = candleDataList.stream() - .map(e -> new XYChart.Data(e.tick, e.open, e)) - .collect(Collectors.toList()); + List> priceItems = candleDataList.stream() + .map(e -> new XYChart.Data(e.tick, e.open, e)) + .collect(Collectors.toList()); - List> volumeItems = candleDataList.stream() - .map(candleData -> new XYChart.Data(candleData.tick, candleData.accumulatedAmount, candleData)) - .collect(Collectors.toList()); + List> volumeItems = candleDataList.stream() + .map(candleData -> new XYChart.Data(candleData.tick, candleData.accumulatedAmount, candleData)) + .collect(Collectors.toList()); - List> volumeInUsdItems = candleDataList.stream() - .map(candleData -> new XYChart.Data(candleData.tick, candleData.volumeInUsd, candleData)) - .collect(Collectors.toList()); + List> volumeInUsdItems = candleDataList.stream() + .map(candleData -> new XYChart.Data(candleData.tick, candleData.volumeInUsd, candleData)) + .collect(Collectors.toList()); - return new UpdateChartResult(itemsPerInterval, priceItems, volumeItems, volumeInUsdItems); + return new UpdateChartResult(itemsPerInterval, priceItems, volumeItems, volumeInUsdItems); + }); } @Getter diff --git a/desktop/src/main/java/bisq/desktop/main/market/trades/TradesChartsViewModel.java b/desktop/src/main/java/bisq/desktop/main/market/trades/TradesChartsViewModel.java index b7488cb064..2a18f3962d 100644 --- a/desktop/src/main/java/bisq/desktop/main/market/trades/TradesChartsViewModel.java +++ b/desktop/src/main/java/bisq/desktop/main/market/trades/TradesChartsViewModel.java @@ -167,7 +167,7 @@ class TradesChartsViewModel extends ActivatableViewModel { } //Once getUsdAveragePriceMapsPerTickUnit and getUsdAveragePriceMapsPerTickUnit are both completed we // call updateChartData2 - UserThread.execute(this::asyncUpdateChartData); + UserThread.execute(this::updateChartData); }); // We start getUsdAveragePriceMapsPerTickUnit and getUsdAveragePriceMapsPerTickUnit in parallel threads for @@ -214,10 +214,27 @@ class TradesChartsViewModel extends ActivatableViewModel { log.error("activate took {}", System.currentTimeMillis() - ts); } - private void asyncUpdateChartData() { + private void updateChartData() { long ts = System.currentTimeMillis(); - updateChartData(); - log.error("updateChartData took {}", System.currentTimeMillis() - ts); + ChartCalculations.getUpdateChartResult(tradeStatisticsByCurrency, tickUnit, usdAveragePriceMapsPerTickUnit, getCurrencyCode()) + .whenComplete((updateChartResult, throwable) -> { + if (deactivateCalled) { + return; + } + if (throwable != null) { + log.error(throwable.toString()); + return; + } + UserThread.execute(() -> { + itemsPerInterval.clear(); + itemsPerInterval.putAll(updateChartResult.getItemsPerInterval()); + + priceItems.setAll(updateChartResult.getPriceItems()); + volumeItems.setAll(updateChartResult.getVolumeItems()); + volumeInUsdItems.setAll(updateChartResult.getVolumeInUsdItems()); + log.error("updateChartData took {}", System.currentTimeMillis() - ts); + }); + }); } @Override @@ -321,17 +338,6 @@ class TradesChartsViewModel extends ActivatableViewModel { priceFeedService.setCurrencyCode(selectedTradeCurrencyProperty.get().getCode()); } - void updateChartData() { - ChartCalculations.UpdateChartResult updateChartResult = ChartCalculations.getUpdateChartResult(tradeStatisticsByCurrency, tickUnit, usdAveragePriceMapsPerTickUnit, getCurrencyCode()); - itemsPerInterval.clear(); - itemsPerInterval.putAll(updateChartResult.getItemsPerInterval()); - - priceItems.setAll(updateChartResult.getPriceItems()); - volumeItems.setAll(updateChartResult.getVolumeItems()); - volumeInUsdItems.setAll(updateChartResult.getVolumeInUsdItems()); - getCurrencyCode(); - } - //todo private void updateSelectedTradeStatistics(String currencyCode) { tradeStatisticsByCurrency.setAll(tradeStatisticsManager.getObservableTradeStatisticsSet().stream()