Merge branch 'mrosseel-priceNodeEnrichJson' into Development

This commit is contained in:
Manfred Karrer 2017-12-14 00:58:38 +01:00
commit 071b39eea2
No known key found for this signature in database
GPG key ID: 401250966A6B2C46
5 changed files with 30 additions and 21 deletions

View file

@ -25,10 +25,5 @@ public class PriceData {
private final String currencyCode;
private final double price;
private final long timestampSec;
public PriceData(String currencyCode, double price, long timestampSec) {
this.currencyCode = currencyCode;
this.price = price;
this.timestampSec = timestampSec;
}
private final String provider;
}

View file

@ -21,22 +21,25 @@ import io.bisq.common.util.Utilities;
import io.bisq.provider.price.providers.BtcAverageProvider;
import io.bisq.provider.price.providers.CoinmarketcapProvider;
import io.bisq.provider.price.providers.PoloniexProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
@Slf4j
public class PriceRequestService {
private static final Logger log = LoggerFactory.getLogger(PriceRequestService.class);
public static final String POLO_PROVIDER = "POLO";
public static final String COINMKTC_PROVIDER = "CMC";
public static final String BTCAVERAGE_LOCAL_PROVIDER = "BTCA_L";
public static final String BTCAVERAGE_GLOBAL_PROVIDER = "BTCA_G";
// We adjust request time to fit into BitcoinAverage developer plan (45k request per month).
// We get 42514 (29760+12754) request with below numbers.
@ -63,6 +66,10 @@ public class PriceRequestService {
private long btcAverageTs;
private long poloniexTs;
private long coinmarketcapTs;
private long btcAverageLCount;
private long btcAverageGCount;
private long poloniexCount;
private long coinmarketcapCount;
private String json;
@ -149,6 +156,7 @@ public class PriceRequestService {
.filter(e -> poloniexMap == null || !poloniexMap.containsKey(e.getKey()))
.forEach(e -> allPricesMap.put(e.getKey(), e.getValue()));
coinmarketcapTs = Instant.now().getEpochSecond();
coinmarketcapCount = map.size();
if (map.get("LTC") != null)
log.info("Coinmarketcap LTC (last): " + map.get("LTC").getPrice());
@ -164,6 +172,7 @@ public class PriceRequestService {
removeOutdatedPrices(allPricesMap);
allPricesMap.putAll(poloniexMap);
poloniexTs = Instant.now().getEpochSecond();
poloniexCount = poloniexMap.size();
if (poloniexMap.get("LTC") != null)
log.info("Poloniex LTC (last): " + poloniexMap.get("LTC").getPrice());
@ -182,6 +191,7 @@ public class PriceRequestService {
removeOutdatedPrices(allPricesMap);
allPricesMap.putAll(btcAverageLocalMap);
btcAverageTs = Instant.now().getEpochSecond();
btcAverageLCount = btcAverageLocalMap.size();
writeToJson();
}
@ -201,14 +211,19 @@ public class PriceRequestService {
.filter(e -> btcAverageLocalMap == null || !btcAverageLocalMap.containsKey(e.getKey()))
.forEach(e -> allPricesMap.put(e.getKey(), e.getValue()));
btcAverageTs = Instant.now().getEpochSecond();
btcAverageGCount = map.size();
writeToJson();
}
private void writeToJson() {
Map<String, Object> map = new HashMap<>();
Map<String, Object> map = new LinkedHashMap<>();
map.put("btcAverageTs", btcAverageTs);
map.put("poloniexTs", poloniexTs);
map.put("coinmarketcapTs", coinmarketcapTs);
map.put("btcAverageLCount", btcAverageLCount);
map.put("btcAverageGCount", btcAverageGCount);
map.put("poloniexCount", poloniexCount);
map.put("coinmarketcapCount", coinmarketcapCount);
map.put("data", allPricesMap.values().toArray());
json = Utilities.objectToJson(map);
}

View file

@ -21,6 +21,7 @@ import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import io.bisq.network.http.HttpClient;
import io.bisq.provider.price.PriceData;
import io.bisq.provider.price.PriceRequestService;
import org.bouncycastle.util.encoders.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -56,14 +57,14 @@ public class BtcAverageProvider {
}
public Map<String, PriceData> getLocal() throws NoSuchAlgorithmException, InvalidKeyException, IOException {
return getMap(httpClient.requestWithGETNoProxy("indices/local/ticker/all?crypto=BTC", "X-signature", getHeader()));
return getMap(httpClient.requestWithGETNoProxy("indices/local/ticker/all?crypto=BTC", "X-signature", getHeader()), PriceRequestService.BTCAVERAGE_LOCAL_PROVIDER);
}
public Map<String, PriceData> getGlobal() throws NoSuchAlgorithmException, InvalidKeyException, IOException {
return getMap(httpClient.requestWithGETNoProxy("indices/global/ticker/all?crypto=BTC", "X-signature", getHeader()));
return getMap(httpClient.requestWithGETNoProxy("indices/global/ticker/all?crypto=BTC", "X-signature", getHeader()), PriceRequestService.BTCAVERAGE_GLOBAL_PROVIDER);
}
private Map<String, PriceData> getMap(String json) {
private Map<String, PriceData> getMap(String json, String provider) {
Map<String, PriceData> marketPriceMap = new HashMap<>();
LinkedTreeMap<String, Object> treeMap = new Gson().<LinkedTreeMap<String, Object>>fromJson(json, LinkedTreeMap.class);
long ts = Instant.now().getEpochSecond();
@ -88,9 +89,7 @@ public class BtcAverageProvider {
log.warn("Unexpected data type: lastAsObject=" + lastAsObject);
marketPriceMap.put(currencyCode,
new PriceData(currencyCode,
last,
ts));
new PriceData(currencyCode, last, ts, provider));
} catch (Throwable exception) {
log.error("Error converting btcaverage data: " + currencyCode, exception);
}

View file

@ -6,6 +6,7 @@ import io.bisq.common.locale.CurrencyUtil;
import io.bisq.common.locale.TradeCurrency;
import io.bisq.network.http.HttpClient;
import io.bisq.provider.price.PriceData;
import io.bisq.provider.price.PriceRequestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -39,9 +40,7 @@ public class CoinmarketcapProvider {
String code = (String) treeMap.get("symbol");
if (supportedAltcoins.contains(code)) {
double price_btc = parseDouble((String) treeMap.get("price_btc"));
marketPriceMap.put(code, new PriceData(code,
price_btc,
ts));
marketPriceMap.put(code, new PriceData(code, price_btc, ts, PriceRequestService.COINMKTC_PROVIDER));
}
});
return marketPriceMap;

View file

@ -6,6 +6,7 @@ import io.bisq.common.locale.CurrencyUtil;
import io.bisq.common.locale.TradeCurrency;
import io.bisq.network.http.HttpClient;
import io.bisq.provider.price.PriceData;
import io.bisq.provider.price.PriceRequestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -53,7 +54,7 @@ public class PoloniexProvider {
marketPriceMap.put(altcoinCurrency,
new PriceData(altcoinCurrency,
parseDouble((String) data.get("last")),
ts)
ts, PriceRequestService.POLO_PROVIDER)
);
}
}