use of org.starcoin.bean.TokenStatistic in project starcoin-search by starcoinorg.
the class TokenService method searchStatistic.
private Result<TokenStatistic> searchStatistic(SearchResponse searchResponse, StatisticType statisticType) {
List<Aggregation> aggregationList = searchResponse.getAggregations().asList();
if (aggregationList.isEmpty()) {
return Result.EmptyResult;
}
Result<TokenStatistic> result = new Result<>();
List<TokenStatistic> statistics = new ArrayList<>();
for (Aggregation agg : aggregationList) {
List<? extends Terms.Bucket> buckets = ((Terms) agg).getBuckets();
for (Terms.Bucket elasticBucket : buckets) {
TokenStatistic statistic = new TokenStatistic();
statistic.setTypeTag(elasticBucket.getKeyAsString());
if (statisticType == StatisticType.AddressHolder) {
statistic.setAddressHolder(elasticBucket.getDocCount());
} else if (statisticType == StatisticType.Volumes) {
Aggregation amountAgg = elasticBucket.getAggregations().get("amounts");
if (amountAgg instanceof ParsedSum) {
double value = ((ParsedSum) amountAgg).getValue();
statistic.setVolume(Double.valueOf(value).longValue());
}
}
statistics.add(statistic);
}
}
result.setContents(statistics);
return result;
}
use of org.starcoin.bean.TokenStatistic in project starcoin-search by starcoinorg.
the class TokenService method getTokenMarketCap.
public BigDecimal getTokenMarketCap(String network, String token) {
Result<TokenStatistic> result2 = new Result<>();
SearchRequest searchRequest = new SearchRequest(getIndex(network, Constant.MARKET_CAP_INDEX));
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
queryBuilder.must(QueryBuilders.termsQuery("type_tag.keyword", token));
searchSourceBuilder.query(queryBuilder);
searchSourceBuilder.from(0);
searchSourceBuilder.trackTotalHits(true);
searchRequest.source(searchSourceBuilder);
searchSourceBuilder.timeout(new TimeValue(20, TimeUnit.SECONDS));
try {
result2 = ServiceUtils.getSearchResult(client.search(searchRequest, RequestOptions.DEFAULT), TokenStatistic.class);
} catch (IOException e) {
logger.error("get token market cap error:", e);
}
// get holder
TokenStatistic tokenStatistic3 = new TokenStatistic();
searchRequest = new SearchRequest(getIndex(network, Constant.ADDRESS_INDEX));
searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.termQuery("type_tag.keyword", token));
searchSourceBuilder.aggregation(AggregationBuilders.count("address_holders").field("address.keyword"));
searchSourceBuilder.timeout(new TimeValue(10, TimeUnit.SECONDS));
searchRequest.source(searchSourceBuilder);
try {
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
tokenStatistic3.setAddressHolder(searchResponse.getHits().getTotalHits().value);
} catch (IOException e) {
logger.error("get token holder error:", e);
}
if (!result2.getContents().isEmpty()) {
TokenStatistic tokenStatistic2 = result2.getContents().get(0);
return BigDecimal.valueOf(tokenStatistic2.getMarketCap());
}
return BigDecimal.ZERO;
}
use of org.starcoin.bean.TokenStatistic in project starcoin-search by starcoinorg.
the class TokenService method tokenAggregateList.
public Result<TokenStatistic> tokenAggregateList(String network, int page, int count) {
// get token holder
Result<TokenStatistic> holders = tokenHolderList(network, page, count);
List<TokenStatistic> holderContents = holders.getContents();
if (holderContents.isEmpty()) {
return holders;
}
// get volume
Result<TokenStatistic> volumes = tokenVolumeList(network, page, count);
Map<String, Long> volumeMap = getVolumeMap(network, volumes);
// get market cap
Result<TokenStatistic> market = tokenMarketCap(network, page, ELASTICSEARCH_MAX_HITS);
Map<String, Double> marketMap = getMarketMap(market);
for (TokenStatistic tokenStatistic : holderContents) {
String typeTag = tokenStatistic.getTypeTag();
Long volume = volumeMap.get(typeTag);
if (volume != null) {
tokenStatistic.setVolume(volume);
}
Double marketCap = marketMap.get(typeTag);
if (marketCap != null) {
tokenStatistic.setMarketCap(marketCap);
}
}
return holders;
}
use of org.starcoin.bean.TokenStatistic in project starcoin-search by starcoinorg.
the class TokenService method getHoldersByToken.
public Result<TokenHolderInfo> getHoldersByToken(String network, int page, int count, String tokenType) throws IOException {
SearchRequest searchRequest = new SearchRequest(getIndex(network, Constant.ADDRESS_INDEX));
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.size(count);
// begin offset
int offset = 0;
if (page > 1) {
offset = (page - 1) * count;
if (offset >= ELASTICSEARCH_MAX_HITS) {
searchSourceBuilder.searchAfter(new Object[] { offset });
searchSourceBuilder.from(0);
} else {
searchSourceBuilder.from(offset);
}
}
searchSourceBuilder.from(offset);
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("type_tag.keyword", tokenType);
searchSourceBuilder.query(termQueryBuilder);
searchSourceBuilder.sort("amount", SortOrder.DESC);
searchRequest.source(searchSourceBuilder);
searchSourceBuilder.trackTotalHits(true);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
Result<TokenHolderInfo> result = ServiceUtils.getSearchResult(searchResponse, TokenHolderInfo.class);
Result<TokenStatistic> tokenStatisticResult = this.tokenMarketCap(network, tokenType);
if (tokenStatisticResult.getContents() != null && tokenStatisticResult.getContents().size() > 0) {
TokenStatistic tokenStatistic = tokenStatisticResult.getContents().get(0);
BigInteger totalSupply = BigInteger.valueOf((Double.valueOf(tokenStatistic.getMarketCap())).longValue());
TokenInfo tokenInfo = getTokenInfo(network, tokenStatistic.getTypeTag());
for (TokenHolderInfo info : result.getContents()) {
info.setSupply(totalSupply);
if (tokenInfo != null) {
info.setHoldAmount(info.getHoldAmount().divide(new BigInteger(String.valueOf(tokenInfo.getScalingFactor()))));
} else {
logger.warn("token info not exist: {}", tokenStatistic.getTypeTag());
}
}
}
return result;
}
use of org.starcoin.bean.TokenStatistic in project starcoin-search by starcoinorg.
the class TokenService method getVolumeMap.
private Map<String, Long> getVolumeMap(String network, Result<TokenStatistic> volumes) {
Map<String, Long> volumeMap = new HashMap<>();
List<TokenStatistic> volumeContents = volumes.getContents();
if (volumeContents.isEmpty()) {
return volumeMap;
}
String typeTag;
Map<String, TokenInfo> tokens = getTokenInfoMap(network);
for (TokenStatistic statistic : volumeContents) {
typeTag = statistic.getTypeTag();
TokenInfo tokenInfo = tokens.get(typeTag);
if (tokenInfo != null) {
volumeMap.put(typeTag, statistic.getVolume() / tokenInfo.getScalingFactor());
} else {
logger.warn("token info not cached: {}", typeTag);
volumeMap.put(statistic.getTypeTag(), statistic.getVolume());
}
}
return volumeMap;
}
Aggregations