Skip to content

Commit

Permalink
feat: create job to count token tx and add index for address tx count…
Browse files Browse the repository at this point in the history
… table
  • Loading branch information
Sotatek-ThinhVu committed May 7, 2024
1 parent 20a2945 commit c145441
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
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.TokenTxCount;

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

import org.cardanofoundation.job.repository.ledgersync.TokenTxCountRepository;
import org.cardanofoundation.job.service.TokenInfoService;

@Component
Expand All @@ -23,6 +24,8 @@ public class TokenInfoSchedule {

final TokenInfoService tokenInfoService;

private final TokenTxCountRepository tokenTxCountRepository;

@Scheduled(fixedDelayString = "${jobs.token-info.fixed-delay}")
public void updateTokenInfo() {
try {
Expand All @@ -38,4 +41,20 @@ public void updateTokenInfo() {
log.error("Error occurred during Token Info update: {}", e.getMessage(), e);
}
}

@Scheduled(fixedRate = 1000 * 60 * 15) // 15 minutes
public void updateNumberOfTokenTx() {
try {
log.info("Token Info Job: -------Start------");
long startTime = System.currentTimeMillis();

tokenTxCountRepository.refreshMaterializedView();

long executionTime = System.currentTimeMillis() - startTime;
log.info("Update number of token transactions successfully, takes: [{} ms]", executionTime);
log.info("Token Info Job: -------End------");
} catch (Exception e) {
log.error("Error occurred during Token Info update: {}", e.getMessage(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ 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;
GROUP BY add.address;

CREATE INDEX IF NOT EXISTS address_tx_count_address_idx ON address_tx_count (address);
CREATE INDEX IF NOT EXISTS address_tx_count_tx_count_idx ON address_tx_count (tx_count);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
DROP MATERIALIZED VIEW IF EXISTS token_tx_count;

CREATE MATERIALIZED VIEW token_tx_count as
SELECT ma.id as ident, count(distinct (ata.tx_hash)) as tx_count
FROM address_tx_amount ata
JOIN multi_asset ma ON ata.unit = ma.unit
GROUP BY ma.id;

CREATE INDEX token_tx_count_ident_idx ON token_tx_count(ident);
CREATE INDEX token_tx_count_tx_count_idx ON token_tx_count(tx_count);

0 comments on commit c145441

Please sign in to comment.