Skip to content

Commit

Permalink
feat: create job to count address transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
Sotatek-ThinhVu committed May 6, 2024
1 parent 4c4cf3b commit 4fa6509
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.cardanofoundation.job.repository.ledgersync;

import jakarta.transaction.Transactional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import org.cardanofoundation.explorer.common.entity.ledgersync.AddressTxCount;

public interface AddressTxCountRepository extends JpaRepository<AddressTxCount, Long> {
@Modifying(clearAutomatically = true)
@Transactional
@Query(value = "REFRESH MATERIALIZED VIEW address_tx_count", nativeQuery = true)
void refreshMaterializedView();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import org.cardanofoundation.job.repository.ledgersync.AddressTxCountRepository;
import org.cardanofoundation.job.repository.ledgersync.LatestTokenBalanceRepository;
import org.cardanofoundation.job.repository.ledgersync.aggregate.AggregateAddressTokenRepository;
import org.cardanofoundation.job.repository.ledgersync.aggregate.AggregateAddressTxBalanceRepository;
Expand All @@ -18,6 +19,7 @@ public class AggregateAnalyticSchedule {
private final AggregateAddressTokenRepository aggregateAddressTokenRepository;
private final AggregateAddressTxBalanceRepository aggregateAddressTxBalanceRepository;
private final LatestTokenBalanceRepository latestTokenBalanceRepository;
private final AddressTxCountRepository addressTxCountRepository;

@Scheduled(
cron = "0 20 0 * * *",
Expand Down Expand Up @@ -55,4 +57,13 @@ public void refreshLatestTokenBalance() {
"End Job refreshLatestTokenBalance, Time taken {}ms",
System.currentTimeMillis() - currentTime);
}

@Scheduled(fixedRateString = "${jobs.agg-address.fixed-delay}")
public void updateTxCountTable() {
log.info("Start job to update tx count for address");
long startTime = System.currentTimeMillis();
addressTxCountRepository.refreshMaterializedView();
long executionTime = System.currentTimeMillis() - startTime;
log.info("Update tx count for address successfully, takes: [{} ms]", executionTime);
}
}
2 changes: 2 additions & 0 deletions src/main/resources/config/application-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ jobs:
tx-chart:
enabled: ${TX_CHART_JOB_ENABLED:true}
fixed-delay: ${TX_CHART_FIXED_DELAY:300000}
agg-address:
fixed-delay: ${AGG_ADDRESS_FIXED_DELAY:300000}
install-batch: 100
limit-content: ${LIMIT_CONTENT_PER_SHEET:1000000}

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/config/application-local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ jobs:
tx-chart:
enabled: ${TX_CHART_JOB_ENABLED:true}
fixed-delay: ${TX_CHART_FIXED_DELAY:300000}
agg-address:
fixed-delay: ${AGG_ADDRESS_FIXED_DELAY:300000}
install-batch: 100
limit-content: ${LIMIT_CONTENT_PER_SHEET:1000000}

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/config/application-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ jobs:
tx-chart:
enabled: ${TX_CHART_JOB_ENABLED:true}
fixed-delay: ${TX_CHART_FIXED_DELAY:300000}
agg-address:
fixed-delay: ${AGG_ADDRESS_FIXED_DELAY:300000}
install-batch: 100
limit-content: ${LIMIT_CONTENT_PER_SHEET:1000000}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
DROP MATERIALIZED VIEW IF EXISTS address_tx_count;

CREATE MATERIALIZED VIEW address_tx_count AS
SELECT add.address AS address,
count(distinct ata.tx_hash) AS tx_count
FROM address add
left join address_tx_amount ata on ata.address = add.address
GROUP BY add.address;

0 comments on commit 4fa6509

Please sign in to comment.