Make getUpdateChartResult ansync

This commit is contained in:
chimp1984 2021-11-01 23:36:39 +01:00
parent 041b07e2e1
commit ce8a91fdb3
No known key found for this signature in database
GPG Key ID: 9801B4EC591F90E3
2 changed files with 54 additions and 46 deletions

View File

@ -99,43 +99,45 @@ public class ChartCalculations {
});
}
static UpdateChartResult getUpdateChartResult(List<TradeStatistics3> tradeStatisticsByCurrency,
TradesChartsViewModel.TickUnit tickUnit,
Map<TradesChartsViewModel.TickUnit, Map<Long, Long>> usdAveragePriceMapsPerTickUnit,
String currencyCode) {
// Generate date range and create sets for all ticks
Map<Long, Pair<Date, Set<TradeStatistics3>>> itemsPerInterval = getItemsPerInterval(tradeStatisticsByCurrency, tickUnit);
static CompletableFuture<UpdateChartResult> getUpdateChartResult(List<TradeStatistics3> tradeStatisticsByCurrency,
TradesChartsViewModel.TickUnit tickUnit,
Map<TradesChartsViewModel.TickUnit, Map<Long, Long>> usdAveragePriceMapsPerTickUnit,
String currencyCode) {
return CompletableFuture.supplyAsync(() -> {
// Generate date range and create sets for all ticks
Map<Long, Pair<Date, Set<TradeStatistics3>>> itemsPerInterval = getItemsPerInterval(tradeStatisticsByCurrency, tickUnit);
Map<Long, Long> usdAveragePriceMap = usdAveragePriceMapsPerTickUnit.get(tickUnit);
AtomicLong averageUsdPrice = new AtomicLong(0);
Map<Long, Long> usdAveragePriceMap = usdAveragePriceMapsPerTickUnit.get(tickUnit);
AtomicLong averageUsdPrice = new AtomicLong(0);
// create CandleData for defined time interval
List<CandleData> 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<CandleData> 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<XYChart.Data<Number, Number>> priceItems = candleDataList.stream()
.map(e -> new XYChart.Data<Number, Number>(e.tick, e.open, e))
.collect(Collectors.toList());
List<XYChart.Data<Number, Number>> priceItems = candleDataList.stream()
.map(e -> new XYChart.Data<Number, Number>(e.tick, e.open, e))
.collect(Collectors.toList());
List<XYChart.Data<Number, Number>> volumeItems = candleDataList.stream()
.map(candleData -> new XYChart.Data<Number, Number>(candleData.tick, candleData.accumulatedAmount, candleData))
.collect(Collectors.toList());
List<XYChart.Data<Number, Number>> volumeItems = candleDataList.stream()
.map(candleData -> new XYChart.Data<Number, Number>(candleData.tick, candleData.accumulatedAmount, candleData))
.collect(Collectors.toList());
List<XYChart.Data<Number, Number>> volumeInUsdItems = candleDataList.stream()
.map(candleData -> new XYChart.Data<Number, Number>(candleData.tick, candleData.volumeInUsd, candleData))
.collect(Collectors.toList());
List<XYChart.Data<Number, Number>> volumeInUsdItems = candleDataList.stream()
.map(candleData -> new XYChart.Data<Number, Number>(candleData.tick, candleData.volumeInUsd, candleData))
.collect(Collectors.toList());
return new UpdateChartResult(itemsPerInterval, priceItems, volumeItems, volumeInUsdItems);
return new UpdateChartResult(itemsPerInterval, priceItems, volumeItems, volumeInUsdItems);
});
}
@Getter

View File

@ -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()