Skip to content

Commit

Permalink
Merge pull request #382 from cardano-foundation/feat/MET-2065-create-…
Browse files Browse the repository at this point in the history
…job-to-count-token-tx

feat: create job to count token tx and add index for address tx count…
  • Loading branch information
Sotatek-DucPhung committed May 7, 2024
2 parents 907ae57 + 43d12ee commit cd8ed89
Show file tree
Hide file tree
Showing 3 changed files with 59 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
@@ -1,8 +1,31 @@
-- address_tx_count
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;
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);

-- token_tx_count
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 IF NOT EXISTS token_tx_count_ident_idx ON token_tx_count(ident);
CREATE INDEX IF NOT EXISTS token_tx_count_tx_count_idx ON token_tx_count(tx_count);

-- add index for address entitys

CREATE INDEX IF NOT EXISTS address_address_hash_idx ON address using hash (address);
CREATE INDEX IF NOT EXISTS address_payment_credential_hash_idx ON address using hash (payment_credential);
CREATE INDEX IF NOT EXISTS address_stake_address_hash_idx ON address using hash (stake_address);
CREATE INDEX IF NOT EXISTS address_stake_credential_hash_idx ON address using hash (stake_credential);

0 comments on commit cd8ed89

Please sign in to comment.