Search in sources :

Example 1 with PlainTextWrapper

use of io.bitsquare.storage.PlainTextWrapper in project bitsquare by bitsquare.

the class TradeStatisticsManager method dump.

private void dump() {
    if (dumpStatistics) {
        // We store the statistics as json so it is easy for further processing (e.g. for web based services)
        // TODO This is just a quick solution for storing to one file. 
        // 1 statistic entry has 500 bytes as json.
        // Need a more scalable solution later when we get more volume.
        // The flag will only be activated by dedicated nodes, so it should not be too critical for the moment, but needs to
        // get improved. Maybe a LevelDB like DB...? Could be impl. in a headless version only.
        List<TradeStatisticsForJson> list = tradeStatisticsSet.stream().map(TradeStatisticsForJson::new).collect(Collectors.toList());
        list.sort((o1, o2) -> (o1.tradeDate < o2.tradeDate ? 1 : (o1.tradeDate == o2.tradeDate ? 0 : -1)));
        TradeStatisticsForJson[] array = new TradeStatisticsForJson[list.size()];
        list.toArray(array);
        statisticsJsonStorage.queueUpForSave(new PlainTextWrapper(Utilities.objectToJson(array)), 5000);
    }
}
Also used : PlainTextWrapper(io.bitsquare.storage.PlainTextWrapper)

Example 2 with PlainTextWrapper

use of io.bitsquare.storage.PlainTextWrapper in project bitsquare by bitsquare.

the class TradeStatisticsManager method init.

private void init(P2PService p2PService) {
    if (dumpStatistics) {
        this.statisticsJsonStorage.initWithFileName("trade_statistics.json");
        this.fiatCurrencyListJsonStorage.initWithFileName("fiat_currency_list.json");
        ArrayList<CurrencyTuple> fiatCurrencyList = new ArrayList<>(CurrencyUtil.getAllSortedFiatCurrencies().stream().map(e -> new CurrencyTuple(e.getCode(), e.getName(), 8)).collect(Collectors.toList()));
        fiatCurrencyListJsonStorage.queueUpForSave(new PlainTextWrapper(Utilities.objectToJson(fiatCurrencyList)), 2000);
        this.cryptoCurrencyListJsonStorage.initWithFileName("crypto_currency_list.json");
        ArrayList<CurrencyTuple> cryptoCurrencyList = new ArrayList<>(CurrencyUtil.getAllSortedCryptoCurrencies().stream().map(e -> new CurrencyTuple(e.getCode(), e.getName(), 8)).collect(Collectors.toList()));
        cryptoCurrencyList.add(0, new CurrencyTuple("BTC", "Bitcoin", 8));
        cryptoCurrencyListJsonStorage.queueUpForSave(new PlainTextWrapper(Utilities.objectToJson(cryptoCurrencyList)), 2000);
    }
    HashSet<TradeStatistics> persisted = statisticsStorage.initAndGetPersistedWithFileName("TradeStatistics");
    if (persisted != null)
        persisted.stream().forEach(e -> add(e, false));
    p2PService.addHashSetChangedListener(new HashMapChangedListener() {

        @Override
        public void onAdded(ProtectedStorageEntry data) {
            final StoragePayload storagePayload = data.getStoragePayload();
            if (storagePayload instanceof TradeStatistics)
                add((TradeStatistics) storagePayload, true);
        }

        @Override
        public void onRemoved(ProtectedStorageEntry data) {
        // We don't remove items
        }
    });
    // At startup the P2PDataStorage inits earlier, otherwise we ge the listener called.
    p2PService.getP2PDataStorage().getMap().values().forEach(e -> {
        final StoragePayload storagePayload = e.getStoragePayload();
        if (storagePayload instanceof TradeStatistics)
            add((TradeStatistics) storagePayload, false);
    });
}
Also used : CurrencyTuple(io.bitsquare.locale.CurrencyTuple) ObservableSet(javafx.collections.ObservableSet) Utilities(io.bitsquare.common.util.Utilities) Logger(org.slf4j.Logger) P2PService(io.bitsquare.p2p.P2PService) Inject(com.google.inject.Inject) ProtectedStorageEntry(io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry) LoggerFactory(org.slf4j.LoggerFactory) FXCollections(javafx.collections.FXCollections) HashMapChangedListener(io.bitsquare.p2p.storage.HashMapChangedListener) StoragePayload(io.bitsquare.p2p.storage.payload.StoragePayload) Collectors(java.util.stream.Collectors) AppOptionKeys(io.bitsquare.app.AppOptionKeys) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) PlainTextWrapper(io.bitsquare.storage.PlainTextWrapper) Named(com.google.inject.name.Named) Storage(io.bitsquare.storage.Storage) CurrencyUtil(io.bitsquare.locale.CurrencyUtil) CurrencyTuple(io.bitsquare.locale.CurrencyTuple) HashMapChangedListener(io.bitsquare.p2p.storage.HashMapChangedListener) ArrayList(java.util.ArrayList) PlainTextWrapper(io.bitsquare.storage.PlainTextWrapper) StoragePayload(io.bitsquare.p2p.storage.payload.StoragePayload) ProtectedStorageEntry(io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry)

Example 3 with PlainTextWrapper

use of io.bitsquare.storage.PlainTextWrapper in project bitsquare by bitsquare.

the class OfferBookService method doDumpStatistics.

///////////////////////////////////////////////////////////////////////////////////////////
// Private
///////////////////////////////////////////////////////////////////////////////////////////
private void doDumpStatistics() {
    // We filter the case that it is a MarketBasedPrice but the price is not available
    // That should only be possible if the price feed provider is not available
    final List<OfferForJson> offerForJsonList = getOffers().stream().filter(offer -> !offer.getUseMarketBasedPrice() || priceFeedService.getMarketPrice(offer.getCurrencyCode()) != null).map(offer -> {
        try {
            return new OfferForJson(offer.getDirection(), offer.getCurrencyCode(), offer.getMinAmount(), offer.getAmount(), offer.getPrice(), offer.getDate(), offer.getId(), offer.getUseMarketBasedPrice(), offer.getMarketPriceMargin(), offer.getPaymentMethod(), offer.getOfferFeePaymentTxID());
        } catch (Throwable t) {
            return null;
        }
    }).filter(e -> e != null).collect(Collectors.toList());
    offersJsonStorage.queueUpForSave(new PlainTextWrapper(Utilities.objectToJson(offerForJsonList)), 5000);
}
Also used : PriceFeedService(io.bitsquare.btc.pricefeed.PriceFeedService) Utilities(io.bitsquare.common.util.Utilities) Logger(org.slf4j.Logger) P2PService(io.bitsquare.p2p.P2PService) ErrorMessageHandler(io.bitsquare.common.handlers.ErrorMessageHandler) UserThread(io.bitsquare.common.UserThread) ProtectedStorageEntry(io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry) LoggerFactory(org.slf4j.LoggerFactory) BootstrapListener(io.bitsquare.p2p.BootstrapListener) HashMapChangedListener(io.bitsquare.p2p.storage.HashMapChangedListener) Collectors(java.util.stream.Collectors) AppOptionKeys(io.bitsquare.app.AppOptionKeys) Inject(javax.inject.Inject) List(java.util.List) PlainTextWrapper(io.bitsquare.storage.PlainTextWrapper) Named(com.google.inject.name.Named) ResultHandler(io.bitsquare.common.handlers.ResultHandler) Storage(io.bitsquare.storage.Storage) LinkedList(java.util.LinkedList) Nullable(javax.annotation.Nullable) PlainTextWrapper(io.bitsquare.storage.PlainTextWrapper)

Aggregations

PlainTextWrapper (io.bitsquare.storage.PlainTextWrapper)3 Named (com.google.inject.name.Named)2 AppOptionKeys (io.bitsquare.app.AppOptionKeys)2 Utilities (io.bitsquare.common.util.Utilities)2 P2PService (io.bitsquare.p2p.P2PService)2 HashMapChangedListener (io.bitsquare.p2p.storage.HashMapChangedListener)2 ProtectedStorageEntry (io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry)2 Storage (io.bitsquare.storage.Storage)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Inject (com.google.inject.Inject)1 PriceFeedService (io.bitsquare.btc.pricefeed.PriceFeedService)1 UserThread (io.bitsquare.common.UserThread)1 ErrorMessageHandler (io.bitsquare.common.handlers.ErrorMessageHandler)1 ResultHandler (io.bitsquare.common.handlers.ResultHandler)1 CurrencyTuple (io.bitsquare.locale.CurrencyTuple)1 CurrencyUtil (io.bitsquare.locale.CurrencyUtil)1 BootstrapListener (io.bitsquare.p2p.BootstrapListener)1