Search in sources :

Example 1 with TokenStatistic

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;
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) TokenStatistic(org.starcoin.bean.TokenStatistic) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) ParsedSum(org.elasticsearch.search.aggregations.metrics.ParsedSum) Result(org.starcoin.api.Result)

Example 2 with TokenStatistic

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;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) TokenStatistic(org.starcoin.bean.TokenStatistic) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) IOException(java.io.IOException) TimeValue(org.elasticsearch.common.unit.TimeValue) Result(org.starcoin.api.Result) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 3 with TokenStatistic

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;
}
Also used : TokenStatistic(org.starcoin.bean.TokenStatistic)

Example 4 with TokenStatistic

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;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) TokenStatistic(org.starcoin.bean.TokenStatistic) BigInteger(java.math.BigInteger) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) TokenInfo(org.starcoin.bean.TokenInfo) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) TokenHolderInfo(org.starcoin.bean.TokenHolderInfo)

Example 5 with TokenStatistic

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;
}
Also used : TokenStatistic(org.starcoin.bean.TokenStatistic) TokenInfo(org.starcoin.bean.TokenInfo)

Aggregations

TokenStatistic (org.starcoin.bean.TokenStatistic)7 SearchRequest (org.elasticsearch.action.search.SearchRequest)3 SearchResponse (org.elasticsearch.action.search.SearchResponse)3 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)3 Result (org.starcoin.api.Result)3 TokenInfo (org.starcoin.bean.TokenInfo)3 IOException (java.io.IOException)2 TimeValue (org.elasticsearch.common.unit.TimeValue)2 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)2 Aggregation (org.elasticsearch.search.aggregations.Aggregation)2 BigInteger (java.math.BigInteger)1 TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)1 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)1 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)1 ParsedSum (org.elasticsearch.search.aggregations.metrics.ParsedSum)1 TokenHolderInfo (org.starcoin.bean.TokenHolderInfo)1