Memoise BurningManAccountingService.getAverageBsqPriceByMonth()

Add an 'averagePricesValid' boolean field to avoid needless refilling of
the cached BSQ prices map when calling 'getAverageBsqPriceByMonth()'.
(Also skip a redundant filling of the map will non-historical data upon
startup of the service.) Since the prices are calculated from the
(observable) set of all trade statistics, add a listener to the set to
invalidate the cache whenever it changes.

This significantly speeds up the burning man view, since the getter is
called several times when activating it.
This commit is contained in:
Steven Barclay 2023-05-10 13:09:05 +01:00
parent ade339349c
commit b8505db48d
No known key found for this signature in database
GPG key ID: 9FED6BF1176D500B

View file

@ -32,6 +32,7 @@ import bisq.core.dao.state.DaoStateListener;
import bisq.core.dao.state.DaoStateService;
import bisq.core.dao.state.model.blockchain.Block;
import bisq.core.monetary.Price;
import bisq.core.trade.statistics.TradeStatistics3;
import bisq.core.trade.statistics.TradeStatisticsManager;
import bisq.core.user.Preferences;
import bisq.core.util.AveragePriceUtil;
@ -49,6 +50,8 @@ import javax.inject.Singleton;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.collections.SetChangeListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
@ -88,6 +91,7 @@ public class BurningManAccountingService implements DaoSetupService, DaoStateLis
private final Preferences preferences;
private final Map<Date, Price> averageBsqPriceByMonth = new HashMap<>(getHistoricalAverageBsqPriceByMonth());
private boolean averagePricesValid;
@Getter
private final Map<String, BalanceModel> balanceModelByBurningManName = new HashMap<>();
@Getter
@ -117,13 +121,13 @@ public class BurningManAccountingService implements DaoSetupService, DaoStateLis
@Override
public void addListeners() {
tradeStatisticsManager.getObservableTradeStatisticsSet().addListener(
(SetChangeListener<TradeStatistics3>) observable -> averagePricesValid = false);
}
@Override
public void start() {
UserThread.execute(() -> isProcessing.set(true));
// Create the map from now back to the last entry of the historical data (April 2019-Nov. 2022).
averageBsqPriceByMonth.putAll(getAverageBsqPriceByMonth(new Date(), 2022, 10));
updateBalanceModelByAddress();
CompletableFuture.runAsync(() -> {
@ -181,8 +185,11 @@ public class BurningManAccountingService implements DaoSetupService, DaoStateLis
}
public Map<Date, Price> getAverageBsqPriceByMonth() {
getAverageBsqPriceByMonth(new Date(), HIST_BSQ_PRICE_LAST_DATE_YEAR, HIST_BSQ_PRICE_LAST_DATE_MONTH)
.forEach((key, value) -> averageBsqPriceByMonth.put(new Date(key.getTime()), Price.valueOf("BSQ", value.getValue())));
if (!averagePricesValid) {
// Fill the map from now back to the last entry of the historical data (April 2019-Nov. 2022).
averageBsqPriceByMonth.putAll(getAverageBsqPriceByMonth(new Date(), HIST_BSQ_PRICE_LAST_DATE_YEAR, HIST_BSQ_PRICE_LAST_DATE_MONTH));
averagePricesValid = true;
}
return averageBsqPriceByMonth;
}
@ -311,6 +318,7 @@ public class BurningManAccountingService implements DaoSetupService, DaoStateLis
}));
}
@SuppressWarnings("SameParameterValue")
private Map<Date, Price> getAverageBsqPriceByMonth(Date from, int backToYear, int backToMonth) {
Map<Date, Price> averageBsqPriceByMonth = new HashMap<>();
Calendar calendar = new GregorianCalendar();