Skip to content

Commit

Permalink
Make getUpdateChartResult ansync
Browse files Browse the repository at this point in the history
  • Loading branch information
chimp1984 committed Nov 4, 2021
1 parent fb0f91a commit 74ce66c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,43 +99,45 @@ static CompletableFuture<List<TradeStatistics3>> getTradeStatisticsForCurrency(S
});
}

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);

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

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>> 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);
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);

// 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>> 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());

return new UpdateChartResult(itemsPerInterval, priceItems, volumeItems, volumeInUsdItems);
});
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ protected void activate() {
}
//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
Expand Down Expand Up @@ -214,10 +214,27 @@ protected void activate() {
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
Expand Down Expand Up @@ -321,17 +338,6 @@ private void syncPriceFeedCurrency() {
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()
Expand Down

0 comments on commit 74ce66c

Please sign in to comment.