Skip to content

Commit

Permalink
feat: replaced unique account and poolstatus with explorer aggregator
Browse files Browse the repository at this point in the history
  • Loading branch information
Kammerlo committed Nov 7, 2024
1 parent 9f499ce commit e924c82
Show file tree
Hide file tree
Showing 26 changed files with 318 additions and 334 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ public static final class MiCAR {

public static final String REDIS_TOTAL_ACTIVATE_STAKE = "TOTAL_ACTIVATE_STAKE_";

public static final String REDIS_POOL_ACTIVATE = "POOL_ACTIVATE_";

public static final String REDIS_POOL_INACTIVATE = "POOL_INACTIVATE_";

public static final String REDIS_TOTAL_DELEGATOR = "TOTAL_DELEGATOR_";

public static final String PREFIXED_STAKE_KEY = "stake";
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;

@Configuration
public class S3Configuration {
Expand All @@ -23,19 +19,21 @@ public AmazonS3 amazonS3Clone(
@Value("${cloud.aws.credentials.secret-key}") String secretKey,
@Value("${cloud.aws.region.static}") String region,
@Value("${cloud.aws.s3.bucket.name}") String bucketName) {
final AmazonS3 amazonS3 =
AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration(serviceEndpoint, region))
.withPathStyleAccessEnabled(pathStyleAccessEnabled)
.withCredentials(
new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
.build();

if (!amazonS3.doesBucketExistV2(bucketName)) {
amazonS3.createBucket(bucketName);
}
return amazonS3;
// final AmazonS3 amazonS3 =
// AmazonS3ClientBuilder.standard()
// .withEndpointConfiguration(
// new AwsClientBuilder.EndpointConfiguration(serviceEndpoint, region))
// .withPathStyleAccessEnabled(pathStyleAccessEnabled)
// .withCredentials(
// new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey,
// secretKey)))
// .build();
//
// if (!amazonS3.doesBucketExistV2(bucketName)) {
// amazonS3.createBucket(bucketName);
// }
// return amazonS3;
return null;
}

@Bean
Expand All @@ -45,16 +43,6 @@ public AmazonS3 amazonS3(
@Value("${cloud.aws.credentials.secret-key}") String secretKey,
@Value("${cloud.aws.region.static}") String region,
@Value("${cloud.aws.s3.bucket.name}") String bucketName) {
final AmazonS3 amazonS3 =
AmazonS3ClientBuilder.standard()
.withRegion(region)
.withCredentials(
new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
.build();

if (!amazonS3.doesBucketExistV2(bucketName)) {
amazonS3.createBucket(bucketName);
}
return amazonS3;
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import lombok.extern.log4j.Log4j2;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
Expand Down Expand Up @@ -43,22 +42,16 @@ public class AuthInterceptor implements HandlerInterceptor {

private final RsaConfig rsaConfig;

private final RedisTemplate<String, Object> redisTemplate;

private final AuthService authService;

private List<AntPathRequestMatcher> matchers;

private Map<String, Request> authorEndpoint;

public AuthInterceptor(
RoleFilterMapper roleFilterMapper,
RsaConfig rsaConfig,
RedisTemplate<String, Object> redisTemplate,
AuthService authService) {
RoleFilterMapper roleFilterMapper, RsaConfig rsaConfig, AuthService authService) {
this.roleConf = roleFilterMapper;
this.rsaConfig = rsaConfig;
this.redisTemplate = redisTemplate;
this.authService = authService;
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package org.cardanofoundation.explorer.api.service;

import java.util.List;
import java.util.Optional;

import org.springframework.data.domain.Pageable;

import org.cardanofoundation.cf_explorer_aggregator.AddressTxCountRecord;
import org.cardanofoundation.cf_explorer_aggregator.PoolAggregationRecord;
import org.cardanofoundation.cf_explorer_aggregator.PoolStatusRecord;
import org.cardanofoundation.cf_explorer_aggregator.UniqueAccountRecord;

public interface ExplorerAggregatorService {

Optional<AddressTxCountRecord> getTxCountForAddress(String address);

Optional<UniqueAccountRecord> getUniqueAccountForEpoch(int epoch);

Optional<PoolStatusRecord> getPoolStatusForPoolId(String poolId);

PoolAggregationRecord getLatestPoolAggregation();

Optional<PoolAggregationRecord> getPoolAggregationByEpoch(int epoch);
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ public AddressChartBalanceResponse getAddressAnalytics(String address, AnalyticT
.findFirstByAddress(address)
.orElseThrow(() -> new BusinessException(BusinessCode.ADDRESS_NOT_FOUND));
AddressChartBalanceResponse response = new AddressChartBalanceResponse();
Long txCount = explorerAggregatorService.getTxCountForAddress(address).map(AddressTxCountRecord::getTxCount).orElse(0L);
Long txCount =
explorerAggregatorService
.getTxCountForAddress(address)
.map(AddressTxCountRecord::getTxCount)
.orElse(0L);

if (Long.valueOf(0L).equals(txCount)) {
return AddressChartBalanceResponse.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.cardanofoundation.cf_explorer_aggregator.PoolAggregationRecord;
import org.cardanofoundation.explorer.api.common.constant.CommonConstant;
import org.cardanofoundation.explorer.api.exception.BusinessCode;
import org.cardanofoundation.explorer.api.exception.NoContentException;
Expand Down Expand Up @@ -83,6 +84,7 @@
import org.cardanofoundation.explorer.api.repository.ledgersyncagg.StakeAddressBalanceRepository;
import org.cardanofoundation.explorer.api.service.DelegationService;
import org.cardanofoundation.explorer.api.service.EpochService;
import org.cardanofoundation.explorer.api.service.ExplorerAggregatorService;
import org.cardanofoundation.explorer.api.service.FetchRewardDataService;
import org.cardanofoundation.explorer.api.service.PoolCertificateService;
import org.cardanofoundation.explorer.api.util.DataUtil;
Expand Down Expand Up @@ -121,6 +123,8 @@ public class DelegationServiceImpl implements DelegationService {

private final FetchRewardDataService fetchRewardDataService;

private final ExplorerAggregatorService explorerAggregatorService;

private final StakeAddressRepository stakeAddressRepository;

private final WithdrawalRepository withdrawalRepository;
Expand Down Expand Up @@ -193,10 +197,10 @@ public BaseFilterResponse<DelegationResponse> getDelegations(Pageable pageable)
public DelegationHeaderResponse getDataForDelegationHeader() {
EpochSummary epoch = epochService.getCurrentEpochSummary();
Integer epochNo = epoch.getNo();
Object poolActiveObj =
redisTemplate.opsForValue().get(CommonConstant.REDIS_POOL_ACTIVATE + network);
Object poolInActiveObj =
redisTemplate.opsForValue().get(CommonConstant.REDIS_POOL_INACTIVATE + network);

PoolAggregationRecord latestPoolAggregation =
explorerAggregatorService.getLatestPoolAggregation();

LocalDateTime endTime = epoch.getEndTime();
Integer slot = epoch.getSlot();
long countDownTime =
Expand Down Expand Up @@ -225,9 +229,8 @@ public DelegationHeaderResponse getDataForDelegationHeader() {
.epochSlotNo(slot)
.liveStake(liveStake)
.delegators(delegators)
.activePools(Objects.nonNull(poolActiveObj) ? (Integer) poolActiveObj : CommonConstant.ZERO)
.retiredPools(
Objects.nonNull(poolInActiveObj) ? (Integer) poolInActiveObj : CommonConstant.ZERO)
.activePools(latestPoolAggregation.getActivePools())
.retiredPools(latestPoolAggregation.getRetiredPools())
.countDownEndTime(countDownTime > CommonConstant.ZERO ? countDownTime : CommonConstant.ZERO)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import org.cardanofoundation.cf_explorer_aggregator.UniqueAccountRecord;
import org.cardanofoundation.explorer.api.common.enumeration.EpochStatus;
import org.cardanofoundation.explorer.api.exception.BusinessCode;
import org.cardanofoundation.explorer.api.exception.NoContentException;
import org.cardanofoundation.explorer.api.mapper.EpochMapper;
import org.cardanofoundation.explorer.api.model.response.BaseFilterResponse;
import org.cardanofoundation.explorer.api.model.response.EpochResponse;
import org.cardanofoundation.explorer.api.model.response.dashboard.EpochSummary;
import org.cardanofoundation.explorer.api.repository.ledgersync.AdaPotsRepository;
import org.cardanofoundation.explorer.api.repository.ledgersync.BlockRepository;
import org.cardanofoundation.explorer.api.repository.ledgersync.EpochRepository;
import org.cardanofoundation.explorer.api.service.EpochService;
import org.cardanofoundation.explorer.api.service.ExplorerAggregatorService;
import org.cardanofoundation.explorer.api.service.FetchRewardDataService;
import org.cardanofoundation.explorer.api.util.StreamUtil;
import org.cardanofoundation.explorer.common.entity.ledgersync.Block;
Expand All @@ -37,13 +37,12 @@
@RequiredArgsConstructor
public class EpochServiceImpl implements EpochService {

private final ExplorerAggregatorService explorerAggregatorService;

private final EpochRepository epochRepository;
private final BlockRepository blockRepository;
private final EpochMapper epochMapper;
private final RedisTemplate<String, Object> redisTemplate;
private final FetchRewardDataService fetchRewardDataService;
private final AdaPotsRepository adaPotsRepository;
private static final String UNIQUE_ACCOUNTS_KEY = "UNIQUE_ACCOUNTS";
private static final String UNDERSCORE = "_";

@Value("${application.network}")
Expand Down Expand Up @@ -92,10 +91,12 @@ public EpochResponse getEpochDetail(String no) {
modifyStartTimeAndEndTimeOfEpoch(firstEpochStartTime, response.getStartTime());
response.setStartTime(startTime);
response.setEndTime(startTime.plusDays(epochDays));
String uniqueAccountRedisKey =
String.join(UNDERSCORE, getRedisKey(UNIQUE_ACCOUNTS_KEY), epoch.getNo().toString());
Integer account = redisTemplate.opsForHash().size(uniqueAccountRedisKey).intValue();
response.setAccount(account);

explorerAggregatorService
.getUniqueAccountForEpoch(epochNo)
.ifPresent(
uniqueAccountRecord -> response.setAccount(uniqueAccountRecord.getUniqueAccounts()));

return response;
} catch (NumberFormatException e) {
throw new BusinessException(BusinessCode.EPOCH_NOT_FOUND);
Expand Down Expand Up @@ -154,10 +155,11 @@ public BaseFilterResponse<EpochResponse> getAllEpoch(Pageable pageable) {
modifyStartTimeAndEndTimeOfEpoch(firstEpochStartTime, epoch.getStartTime());
epoch.setStartTime(startTime);
epoch.setEndTime(startTime.plusDays(epochDays));
String uniqueAccountRedisKey =
String.join(
UNDERSCORE, getRedisKey(UNIQUE_ACCOUNTS_KEY), epoch.getNo().toString());
epoch.setAccount(redisTemplate.opsForHash().size(uniqueAccountRedisKey).intValue());
explorerAggregatorService
.getUniqueAccountForEpoch(epoch.getNo())
.ifPresent(
uniqueAccountRecord ->
epoch.setAccount(uniqueAccountRecord.getUniqueAccounts()));
});
return new BaseFilterResponse<>(pageResponse);
}
Expand Down Expand Up @@ -244,12 +246,12 @@ public EpochSummary getCurrentEpochSummary() {
var slot =
currentLocalDateTime.toEpochSecond(ZoneOffset.UTC)
- epochStartTime.toEpochSecond(ZoneOffset.UTC);
String uniqueAccountRedisKey =
String.join(
UNDERSCORE,
getRedisKey(UNIQUE_ACCOUNTS_KEY),
epochSummaryProjection.getNo().toString());
var account = redisTemplate.opsForHash().size(uniqueAccountRedisKey).intValue();
int account =
explorerAggregatorService
.getUniqueAccountForEpoch(epochSummaryProjection.getNo())
.map(UniqueAccountRecord::getUniqueAccounts)
.orElse(0);

if (Boolean.FALSE.equals(
fetchRewardDataService.checkAdaPots(epochSummaryProjection.getNo()))) {
fetchRewardDataService.fetchAdaPots(List.of(epochSummaryProjection.getNo()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package org.cardanofoundation.explorer.api.service.impl;

import java.util.List;
import java.util.Optional;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;

import org.cardanofoundation.cf_explorer_aggregator.AddressTxCountRecord;
import org.cardanofoundation.cf_explorer_aggregator.PoolAggregationRecord;
import org.cardanofoundation.cf_explorer_aggregator.PoolStatusRecord;
import org.cardanofoundation.cf_explorer_aggregator.UniqueAccountRecord;
import org.cardanofoundation.explorer.api.service.ExplorerAggregatorService;

@Service
Expand Down Expand Up @@ -41,4 +42,60 @@ public Optional<AddressTxCountRecord> getTxCountForAddress(String address) {
.block();
}

@Override
public Optional<UniqueAccountRecord> getUniqueAccountForEpoch(int epoch) {
String url = explorerAggregatorBaseUrl + "/uniqueaccount/" + epoch;
return webClient
.get()
.uri(url)
.retrieve()
.bodyToMono(UniqueAccountRecord.class)
.map(
uniqueAccountRecord -> {
log.info("UniqueAccountRecord: {}", uniqueAccountRecord);
return Optional.of(uniqueAccountRecord);
})
.onErrorReturn(Optional.empty())
.block();
}

@Override
public Optional<PoolStatusRecord> getPoolStatusForPoolId(String poolId) {
String url = explorerAggregatorBaseUrl + "/poolstatus/" + poolId;
return webClient
.get()
.uri(url)
.retrieve()
.bodyToMono(PoolStatusRecord.class)
.map(
poolStatusRecord -> {
log.info("PoolStatusRecord: {}", poolStatusRecord);
return Optional.of(poolStatusRecord);
})
.onErrorReturn(Optional.empty())
.block();
}

@Override
public PoolAggregationRecord getLatestPoolAggregation() {
String url = explorerAggregatorBaseUrl + "/poolstatus/latest";
return webClient.get().uri(url).retrieve().bodyToMono(PoolAggregationRecord.class).block();
}

@Override
public Optional<PoolAggregationRecord> getPoolAggregationByEpoch(int epoch) {
String url = explorerAggregatorBaseUrl + "/poolstatus/epoch/" + epoch;
return webClient
.get()
.uri(url)
.retrieve()
.bodyToMono(PoolAggregationRecord.class)
.map(
poolAggregationRecord -> {
log.info("PoolAggregationRecord: {}", poolAggregationRecord);
return Optional.of(poolAggregationRecord);
})
.onErrorReturn(Optional.empty())
.block();
}
}
Loading

0 comments on commit e924c82

Please sign in to comment.