Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
feat: replacing AddressTxCountRepository.java and StakeAddressTxCount…
Browse files Browse the repository at this point in the history
…Repository.java with ExplorerAggregationService
  • Loading branch information
Kammerlo committed Nov 4, 2024
1 parent 4cd12b2 commit fee89b7
Show file tree
Hide file tree
Showing 15 changed files with 521 additions and 110 deletions.
23 changes: 21 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -473,15 +473,34 @@
<version>${version.openapi-generator-maven-plugin}</version>
<executions>
<execution>
<id>cf-product-tracing</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/openapi/cf-product-tracing.json
</inputSpec>
<generatorName>spring</generatorName>
<modelPackage>com.example.api.model</modelPackage>
<apiPackage>com.example.api.client</apiPackage>
<modelPackage>org.cardanofoundation.cfproducttracingaggregator</modelPackage>
<apiPackage>org.cardanofoundation.cfproducttracingaggregator</apiPackage>
<generateSupportingFiles>false</generateSupportingFiles>
<generateApis>false</generateApis>
<configOptions>
<useJakartaEe>true</useJakartaEe>
</configOptions>
</configuration>
</execution>
<execution>
<id>cf-explorer-aggregation</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/openapi/cf-explorer-aggregation.json
</inputSpec>
<generatorName>spring</generatorName>
<modelPackage>org.cardanofoundation.cfexploreraggregator</modelPackage>
<apiPackage>org.cardanofoundation.cfexploreraggregator</apiPackage>
<generateSupportingFiles>false</generateSupportingFiles>
<generateApis>false</generateApis>
<configOptions>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.cardanofoundation.explorer.api.mapper;

import com.example.api.model.ProductAggregationRecord;
import org.cardanofoundation.cfproducttracingaggregator.ProductAggregationRecord;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

Expand Down

This file was deleted.

This file was deleted.

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

import org.cardanofoundation.cfexploreraggregator.AddressTxCountRecord;
import org.springframework.data.domain.Pageable;

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

public interface ExplorerAggregatorService {

Optional<AddressTxCountRecord> getTxCountForAddress(String address);

List<AddressTxCountRecord> getAllTxCount(Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;

import org.cardanofoundation.cfexploreraggregator.AddressTxCountRecord;
import org.cardanofoundation.explorer.api.service.ExplorerAggregatorService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand All @@ -35,7 +37,6 @@
import org.cardanofoundation.explorer.api.repository.ledgersync.ScriptRepository;
import org.cardanofoundation.explorer.api.repository.ledgersyncagg.AddressRepository;
import org.cardanofoundation.explorer.api.repository.ledgersyncagg.AddressTxAmountRepository;
import org.cardanofoundation.explorer.api.repository.ledgersyncagg.AddressTxCountRepository;
import org.cardanofoundation.explorer.api.repository.ledgersyncagg.AggregateAddressTxBalanceRepository;
import org.cardanofoundation.explorer.api.repository.ledgersyncagg.LatestTokenBalanceRepository;
import org.cardanofoundation.explorer.api.service.AddressService;
Expand All @@ -44,7 +45,6 @@
import org.cardanofoundation.explorer.api.util.DateUtils;
import org.cardanofoundation.explorer.common.entity.enumeration.ScriptType;
import org.cardanofoundation.explorer.common.entity.ledgersyncsagg.Address;
import org.cardanofoundation.explorer.common.entity.ledgersyncsagg.AddressTxCount;
import org.cardanofoundation.explorer.common.entity.ledgersyncsagg.AggregateAddressTxBalance;
import org.cardanofoundation.explorer.common.entity.ledgersyncsagg.LatestTokenBalance;
import org.cardanofoundation.explorer.common.exception.BusinessException;
Expand All @@ -59,7 +59,7 @@ public class AddressServiceImpl implements AddressService {
private final TokenMapper tokenMapper;
private final ScriptRepository scriptRepository;
private final AggregateAddressTxBalanceRepository aggregateAddressTxBalanceRepository;
private final AddressTxCountRepository addressTxCountRepository;
private final ExplorerAggregatorService explorerAggregatorService;
private final AddressTxAmountRepository addressTxAmountRepository;
private final MultiAssetRepository multiAssetRepository;
private final CardanoConverters cardanoConverters;
Expand Down Expand Up @@ -133,13 +133,10 @@ public AddressChartBalanceResponse getAddressAnalytics(String address, AnalyticT
.findFirstByAddress(address)
.orElseThrow(() -> new BusinessException(BusinessCode.ADDRESS_NOT_FOUND));
AddressChartBalanceResponse response = new AddressChartBalanceResponse();
Optional<AddressTxCountRecord> txCountForAddress = explorerAggregatorService.getTxCountForAddress(address);

AddressTxCount addressTxCount =
addressTxCountRepository
.findById(address)
.orElse(AddressTxCount.builder().address(address).txCount(0L).build());

if (Long.valueOf(0).equals(addressTxCount.getTxCount())) {
if (txCountForAddress.isPresent()
&& txCountForAddress.get().getTxCount().equals(0L)) {
return AddressChartBalanceResponse.builder()
.highestBalance(BigInteger.ZERO)
.lowestBalance(BigInteger.ZERO)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;

import org.cardanofoundation.cfproducttracingaggregator.ProductAggregationRecord;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatusCode;
Expand All @@ -26,7 +27,6 @@
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;

import com.example.api.model.ProductAggregationRecord;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.cardanofoundation.explorer.api.service.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.cardanofoundation.cfexploreraggregator.AddressTxCountRecord;
import org.cardanofoundation.explorer.api.service.ExplorerAggregatorService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

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

@Service
@RequiredArgsConstructor
@Log4j2
public class ExplorerAggregatorServiceImpl implements ExplorerAggregatorService {

@Value("${application.api.explorer-aggregator.base-url}")
private String explorerAggregatorBaseUrl;


@Override
public Optional<AddressTxCountRecord> getTxCountForAddress(String address) {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<AddressTxCountRecord> forEntity = restTemplate.getForEntity(explorerAggregatorBaseUrl + "/addresstxcount/" + address, AddressTxCountRecord.class);
if (forEntity.getStatusCode().is2xxSuccessful()) {
return Optional.ofNullable(forEntity.getBody());
}
return Optional.empty();
}

@Override
public List<AddressTxCountRecord> getAllTxCount(Pageable pageable) {
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import lombok.RequiredArgsConstructor;

import org.cardanofoundation.cfexploreraggregator.AddressTxCountRecord;
import org.cardanofoundation.explorer.api.service.ExplorerAggregatorService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
Expand All @@ -19,21 +21,17 @@
import org.cardanofoundation.explorer.api.model.response.micar.AddressCarbonEmissionResponse;
import org.cardanofoundation.explorer.api.repository.ledgersync.StakeAddressRepository;
import org.cardanofoundation.explorer.api.repository.ledgersyncagg.AddressRepository;
import org.cardanofoundation.explorer.api.repository.ledgersyncagg.AddressTxCountRepository;
import org.cardanofoundation.explorer.api.repository.ledgersyncagg.StakeAddressTxCountRepository;
import org.cardanofoundation.explorer.api.service.MiCARService;
import org.cardanofoundation.explorer.api.util.AddressUtils;
import org.cardanofoundation.explorer.common.entity.ledgersync.StakeAddress;
import org.cardanofoundation.explorer.common.entity.ledgersyncsagg.Address;
import org.cardanofoundation.explorer.common.entity.ledgersyncsagg.AddressTxCount;
import org.cardanofoundation.explorer.common.entity.ledgersyncsagg.StakeAddressTxCount;
import org.cardanofoundation.explorer.common.exception.BusinessException;

@Service
@RequiredArgsConstructor
public class MiCARServiceImpl implements MiCARService {
private final StakeAddressTxCountRepository stakeAddressTxCountRepository;
private final AddressTxCountRepository addressTxCountRepository;

private final ExplorerAggregatorService explorerAggregatorService;
private final StakeAddressRepository stakeAddressRepository;
private final WebClient webClient;

Expand All @@ -58,11 +56,12 @@ public AddressCarbonEmissionResponse getCarbonEmissionsByAddressAndPool(String a
if (stakeAddress.isEmpty()) {
return AddressCarbonEmissionResponse.builder().build();
}
Optional<StakeAddressTxCount> stakeAddressTxCount =
stakeAddressTxCountRepository.findByStakeAddress(stakeAddress.get().getView());
AddressTxCountRecord addressTxCountRecord = explorerAggregatorService.getTxCountForAddress(stakeAddress.get().getView())
.orElseGet(AddressTxCountRecord::new);

return AddressCarbonEmissionResponse.builder()
.stakeAddress(address)
.txCount(stakeAddressTxCount.orElseGet(StakeAddressTxCount::new).getTxCount())
.txCount(addressTxCountRecord.getTxCount())
.carbonEmissionPerTx(CommonConstant.MiCAR.CO2_EMISSION_PER_TX)
.build();
} else {
Expand All @@ -72,10 +71,10 @@ public AddressCarbonEmissionResponse getCarbonEmissionsByAddressAndPool(String a
if (addr.isEmpty()) {
return AddressCarbonEmissionResponse.builder().build();
}
Optional<AddressTxCount> addressTxCount = addressTxCountRepository.findByAddress(address);
Optional<AddressTxCountRecord> txCountForAddress = explorerAggregatorService.getTxCountForAddress(address);
return AddressCarbonEmissionResponse.builder()
.address(address)
.txCount(addressTxCount.orElseGet(AddressTxCount::new).getTxCount())
.txCount(txCountForAddress.orElseGet(AddressTxCountRecord::new).getTxCount())
.carbonEmissionPerTx(CommonConstant.MiCAR.CO2_EMISSION_PER_TX)
.build();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;

import org.cardanofoundation.cfexploreraggregator.AddressTxCountRecord;
import org.cardanofoundation.explorer.api.service.ExplorerAggregatorService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
Expand Down Expand Up @@ -99,16 +101,12 @@
import org.cardanofoundation.explorer.api.repository.ledgersync.WithdrawalRepository;
import org.cardanofoundation.explorer.api.repository.ledgersyncagg.AddressRepository;
import org.cardanofoundation.explorer.api.repository.ledgersyncagg.AddressTxAmountRepository;
import org.cardanofoundation.explorer.api.repository.ledgersyncagg.AddressTxCountRepository;
import org.cardanofoundation.explorer.api.repository.ledgersyncagg.StakeAddressTxCountRepository;
import org.cardanofoundation.explorer.api.service.BolnisiMetadataService;
import org.cardanofoundation.explorer.api.service.ProtocolParamService;
import org.cardanofoundation.explorer.api.service.TxService;
import org.cardanofoundation.explorer.api.util.*;
import org.cardanofoundation.explorer.common.entity.ledgersync.*;
import org.cardanofoundation.explorer.common.entity.ledgersyncsagg.AddressTxAmount;
import org.cardanofoundation.explorer.common.entity.ledgersyncsagg.AddressTxCount;
import org.cardanofoundation.explorer.common.entity.ledgersyncsagg.StakeAddressTxCount;
import org.cardanofoundation.explorer.common.exception.BusinessException;

@Service
Expand All @@ -135,8 +133,7 @@ public class TxServiceImpl implements TxService {
private final MaTxMintMapper maTxMintMapper;
private final AddressTxAmountRepository addressTxAmountRepository;
private final MultiAssetRepository multiAssetRepository;
private final AddressTxCountRepository addressTxCountRepository;
private final StakeAddressTxCountRepository stakeAddressTxCountRepository;
private final ExplorerAggregatorService explorerAggregatorService;
private final AssetMetadataRepository assetMetadataRepository;
private final AssetMetadataMapper assetMetadataMapper;
private final StakeRegistrationRepository stakeRegistrationRepository;
Expand Down Expand Up @@ -285,8 +282,7 @@ public BaseFilterResponse<TxFilterResponse> getTransactionsByAddress(
.findFirstByAddress(address)
.orElseThrow(() -> new BusinessException(BusinessCode.ADDRESS_NOT_FOUND));

AddressTxCount addressTxCount =
addressTxCountRepository.findById(address).orElse(new AddressTxCount(address, 0L));
AddressTxCountRecord addressTxCountRecord = explorerAggregatorService.getTxCountForAddress(address).orElse(new AddressTxCountRecord());

List<TxProjection> txProjections =
addressTxAmountRepository.findAllTxByAddress(address, pageable);
Expand All @@ -298,7 +294,7 @@ public BaseFilterResponse<TxFilterResponse> getTransactionsByAddress(
new PageImpl<>(
mapTxDataFromAddressTxAmount(txProjections, addressTxAmounts),
pageable,
addressTxCount.getTxCount());
addressTxCountRecord.getTxCount());

return new BaseFilterResponse<>(txFilterResponsePage);
}
Expand All @@ -319,7 +315,7 @@ private List<TxFilterResponse> mapTxDataFromAddressTxAmount(

Map<Long, Block> blockMap =
blockRepository
.findAllByIdIn(txMap.values().stream().map(Tx::getBlockId).collect(Collectors.toList()))
.findAllByIdIn(txMap.values().stream().map(Tx::getBlockId).toList())
.stream()
.collect(Collectors.toMap(Block::getId, Function.identity()));

Expand Down Expand Up @@ -444,11 +440,7 @@ public BaseFilterResponse<TxFilterResponse> getTransactionsByStake(
.findByView(stakeKey)
.orElseThrow(() -> new BusinessException(BusinessCode.STAKE_ADDRESS_NOT_FOUND));

StakeAddressTxCount addressTxCount =
stakeAddressTxCountRepository
.findById(stakeKey)
.orElse(new StakeAddressTxCount(stakeKey, 0L));

AddressTxCountRecord addressTxCountRecord = explorerAggregatorService.getTxCountForAddress(stakeKey).orElse(new AddressTxCountRecord());
List<TxProjection> txProjections =
addressTxAmountRepository.findAllTxByStakeAddress(stakeKey, pageable);
List<String> txHashes = txProjections.stream().map(TxProjection::getTxHash).toList();
Expand All @@ -459,7 +451,7 @@ public BaseFilterResponse<TxFilterResponse> getTransactionsByStake(
new PageImpl<>(
mapTxDataFromAddressTxAmount(txProjections, addressTxAmounts),
pageable,
addressTxCount.getTxCount());
addressTxCountRecord.getTxCount());

return new BaseFilterResponse<>(txFilterResponsePage);
}
Expand Down Expand Up @@ -854,14 +846,6 @@ private List<ContractResponse> getContractResponses(Tx tx, TxResponse txResponse
return contractResponses;
}

private void setVoteContractResponse(ContractResponse contractResponse) {
// TODO
}

private void setProposeContractResponse(ContractResponse contractResponse) {
// TODO
}

/**
* Set cert contract response:
*
Expand Down Expand Up @@ -1141,7 +1125,7 @@ private void getStakeCertificates(Tx tx, TxResponse txResponse) {
item ->
new TxStakeCertificate(
item.getAddr().getView(), CertificateType.STAKE_DEREGISTRATION))
.collect(Collectors.toList()));
.toList());
if (!CollectionUtils.isEmpty(stakeCertificates)) {
txResponse.setStakeCertificates(stakeCertificates);
}
Expand Down Expand Up @@ -1224,7 +1208,7 @@ private void getPoolCertificates(Tx tx, TxResponse txResponse) {
.epoch(item.getRetiringEpoch())
.type(CertificateType.POOL_DEREGISTRATION)
.build())
.collect(Collectors.toList()));
.toList());
if (!CollectionUtils.isEmpty(poolCertificates)) {
txResponse.setPoolCertificates(poolCertificates);
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/config/application-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ application:
fallback: ${API_BOLNISI_PUBLIC_KEY_FALLBACK_URL:https://api.pro.cf-bolnisi-mainnet.eu-west-1.bnwa.metadata.dev.cf-deployments.org/api/v1/pubkeys/{wineryId}/v/0}
conformity-cert: ${API_BOLNISI_PUBLIC_KEY_CONFORMITY_CERT_URL:https://cardano.mepa.gov.ge/api/v1/publickeys/nwa/v/0}
api.product-aggregator:
latest-url: ${API_PRODUCT_AGGREGATOR_LATEST_URL:http://localhost:8081/productaggregation/latest}
latest-url: ${API_PRODUCT_AGGREGATOR_LATEST_URL:http://localhost:8081/api/v1/product-tracing-aggregator/productaggregation/latest}
api.explorer-aggregator:
base-url: ${API_PRODUCT_AGGREGATOR_BASE_URL:http://localhost:8081/api/v1/explorer-aggregator}

springdoc:
api-docs.path: /apidocs
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/config/application-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ application:
fallback: ${API_BOLNISI_PUBLIC_KEY_FALLBACK_URL:https://api.pro.cf-bolnisi-mainnet.eu-west-1.bnwa.metadata.dev.cf-deployments.org/api/v1/pubkeys/{wineryId}/v/0}
conformity-cert: ${API_BOLNISI_PUBLIC_KEY_CONFORMITY_CERT_URL:https://cardano.mepa.gov.ge/api/v1/publickeys/nwa/v/0}
api.product-aggregator:
latest-url: ${API_PRODUCT_AGGREGATOR_LATEST_URL:http://localhost:8081/productaggregation/latest}
latest-url: ${API_PRODUCT_AGGREGATOR_LATEST_URL:http://localhost:8081/api/v1/product-tracing-aggregator/productaggregation/latest}
api.explorer-aggregator:
base-url: ${API_PRODUCT_AGGREGATOR_BASE_URL:http://localhost:8081/api/v1/explorer-aggregator}

springdoc:
api-docs.path: /apidocs
Expand Down
Loading

0 comments on commit fee89b7

Please sign in to comment.