Skip to content

Commit

Permalink
optimize api.
Browse files Browse the repository at this point in the history
  • Loading branch information
ssuchichan committed Jun 30, 2024
1 parent c8ab7ff commit dd95c47
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion explorer/src/service/v2/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub async fn get_assets(
let page = params.page.unwrap_or(1);
let page_size = params.page_size.unwrap_or(10);

let mut sql_total = "SELECT count(*) FROM assets ".to_string();
let mut sql_total = "SELECT count(height) FROM assets ".to_string();
let mut sql_query =
"SELECT asset,tx,block,issuer,height,timestamp,ty,content FROM assets ".to_string();
let mut query_params: Vec<String> = vec![];
Expand Down
2 changes: 1 addition & 1 deletion explorer/src/service/v2/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ pub async fn get_blocks(

let sql_total = "SELECT max(height) FROM block";
let row = sqlx::query(sql_total).fetch_one(&mut *pool).await?;
let total = row.try_get("max")?;
let total = row.try_get("max").unwrap_or(10);

let sql_query = r#"SELECT block_hash,height,size,tx_count,time,app_hash,proposer,block_data
FROM block ORDER BY height DESC LIMIT $1 OFFSET $2"#;
Expand Down
4 changes: 2 additions & 2 deletions explorer/src/service/v2/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ pub async fn get_claims(

let (sql_count, sql_query) = if let Some(from) = params.from {
(format!(
"SELECT count(*) FROM claims WHERE sender='{}'",
"SELECT count(height) FROM claims WHERE sender='{}'",
from.to_lowercase()
),format!(
"SELECT tx,block,sender,amount,height,timestamp,content FROM claims WHERE sender='{}' ORDER BY timestamp DESC LIMIT {} OFFSET {}",
from.to_lowercase(), page_size, (page-1)*page_size
))
} else {
("SELECT count(*) FROM claims".to_string(),
("SELECT count(height) FROM claims".to_string(),
format!(
"SELECT tx,block,sender,amount,height,timestamp,content FROM claims ORDER BY timestamp DESC LIMIT {} OFFSET {}",
page_size, (page-1)*page_size
Expand Down
4 changes: 2 additions & 2 deletions explorer/src/service/v2/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub async fn get_delegations(
let (sql_count, sql_query) = if let Some(addr) = params.from {
(
format!(
"SELECT count(*) FROM delegations WHERE sender='{}'",
"SELECT count(height) FROM delegations WHERE sender='{}'",
addr.to_lowercase()
),
format!(
Expand All @@ -96,7 +96,7 @@ pub async fn get_delegations(
)
} else {
(
"SELECT count(*) FROM delegations".to_string(),
"SELECT count(height) FROM delegations".to_string(),
format!(
"SELECT tx,block,sender,amount,validator,new_validator,timestamp,height,content \
FROM delegations ORDER BY timestamp DESC LIMIT {} OFFSET {}",
Expand Down
20 changes: 10 additions & 10 deletions explorer/src/service/v2/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub async fn get_statistics(
let start_time = Local::now().date_naive().and_hms_opt(0, 0, 0).unwrap();

if let Some(tx_type) = params.ty {
let sql_txs_count = format!("SELECT count(*) FROM transaction WHERE ty={}", tx_type);
let sql_txs_count = format!("SELECT count(height) FROM transaction WHERE ty={}", tx_type);
let row_txs_count = sqlx::query(sql_txs_count.as_str())
.fetch_one(&mut *conn)
.await?;
Expand All @@ -51,14 +51,14 @@ pub async fn get_statistics(
0 => {
sql_addrs_count = "SELECT count(distinct address) FROM native_addrs".to_string();
sql_daily_txs = format!(
"SELECT count(*) FROM transaction WHERE ty=0 AND timestamp>={}",
"SELECT count(height) FROM transaction WHERE ty=0 AND timestamp>={}",
start_time.and_utc().timestamp()
);
}
_ => {
sql_addrs_count = "SELECT count(distinct address) FROM evm_addrs".to_string();
sql_daily_txs = format!(
"SELECT count(*) FROM transaction WHERE ty=1 AND timestamp>={}",
"SELECT count(height) FROM transaction WHERE ty=1 AND timestamp>={}",
start_time.and_utc().timestamp()
);
}
Expand All @@ -78,7 +78,7 @@ pub async fn get_statistics(
stat.total_txs = txs_count;
stat.daily_txs = daily_txs
} else {
let sql_txs_count = "SELECT count(*) FROM transaction".to_string();
let sql_txs_count = "SELECT count(height) FROM transaction".to_string();
let row_txs_count = sqlx::query(sql_txs_count.as_str())
.fetch_one(&mut *conn)
.await?;
Expand All @@ -97,7 +97,7 @@ pub async fn get_statistics(
let native_addrs: i64 = row_native_addr.try_get("count")?;

let sql_daily_txs = format!(
"SELECT count(*) FROM transaction WHERE timestamp>={}",
"SELECT count(height) FROM transaction WHERE timestamp>={}",
start_time.and_utc().timestamp()
);
let row_daily = sqlx::query(sql_daily_txs.as_str())
Expand Down Expand Up @@ -127,23 +127,23 @@ pub async fn get_tx_distribute(
) -> Result<Json<TxsDistributeResponse>> {
let mut conn = state.pool.acquire().await?;

let sql_native = "SELECT count(*) FROM transaction WHERE ty=0";
let sql_native = "SELECT count(height) FROM transaction WHERE ty=0";
let row_native = sqlx::query(sql_native).fetch_one(&mut *conn).await?;
let native_count: i64 = row_native.try_get("count")?;

let sql_privacy = "SELECT count(*) FROM transaction WHERE ty_sub=2 or ty_sub=3 or ty_sub=4";
let sql_privacy = "SELECT count(height) FROM transaction WHERE ty_sub=2 or ty_sub=3 or ty_sub=4";
let row_privacy = sqlx::query(sql_privacy).fetch_one(&mut *conn).await?;
let privacy: i64 = row_privacy.try_get("count")?;

let sql_evm = "SELECT count(*) FROM transaction WHERE ty=1";
let sql_evm = "SELECT count(height) FROM transaction WHERE ty=1";
let row_evm = sqlx::query(sql_evm).fetch_one(&mut *conn).await?;
let evm_count: i64 = row_evm.try_get("count")?;

let sql_prism_n2e = "SELECT count(*) FROM n2e";
let sql_prism_n2e = "SELECT count(height) FROM n2e";
let row_n2e = sqlx::query(sql_prism_n2e).fetch_one(&mut *conn).await?;
let n2e_count: i64 = row_n2e.try_get("count")?;

let sql_prism_e2n = "SELECT count(*) FROM e2n";
let sql_prism_e2n = "SELECT count(height) FROM e2n";
let row_e2n = sqlx::query(sql_prism_e2n).fetch_one(&mut *conn).await?;
let e2n_count: i64 = row_e2n.try_get("count")?;

Expand Down
2 changes: 1 addition & 1 deletion explorer/src/service/v2/prism_evm_to_native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub async fn get_e2n_txs(
let page = params.page.unwrap_or(1);
let page_size = params.page_size.unwrap_or(10);

let mut sql_total = "SELECT count(*) FROM e2n ".to_string();
let mut sql_total = "SELECT count(height) FROM e2n ".to_string();
let mut sql_query = "SELECT tx_hash,block_hash,sender,receiver,asset,amount,decimal,height,timestamp,value FROM e2n ".to_string();

let mut query_params: Vec<String> = vec![];
Expand Down
2 changes: 1 addition & 1 deletion explorer/src/service/v2/prism_native_to_evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub async fn get_n2e_txs(
let mut conn = state.pool.acquire().await?;
let page = params.page.unwrap_or(1);
let page_size = params.page_size.unwrap_or(10);
let mut sql_total = "SELECT count(*) FROM n2e ".to_string();
let mut sql_total = "SELECT count(height) FROM n2e ".to_string();
let mut sql_query =
"SELECT tx,block,sender,receiver,asset,amount,height,timestamp,content FROM n2e "
.to_string();
Expand Down
2 changes: 1 addition & 1 deletion explorer/src/service/v2/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub async fn get_txs(
let page_size = params.page_size.unwrap_or(10);

let mut sql_query = String::from("SELECT tx_hash,block_hash,ty,timestamp,height,code,log,origin,result,value FROM transaction ");
let mut sql_total = String::from("SELECT count(*) FROM transaction ");
let mut sql_total = String::from("SELECT count(height) FROM transaction ");
let mut query_params: Vec<String> = vec![];
if let Some(from) = params.from {
query_params.push(format!("sender='{}' ", from))
Expand Down
4 changes: 2 additions & 2 deletions explorer/src/service/v2/undelegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub async fn get_undelegations(
let (sql_count, sql_query) = if let Some(from) = params.from {
(
format!(
"SELECT count(*) FROM undelegations WHERE sender='{}'",
"SELECT count(height) FROM undelegations WHERE sender='{}'",
from.to_lowercase()
),
format!(
Expand All @@ -92,7 +92,7 @@ pub async fn get_undelegations(
)
} else {
(
"SELECT count(*) FROM undelegations".to_string(),
"SELECT count(height) FROM undelegations".to_string(),
format!("SELECT tx,block,sender,amount,target_validator,new_delegator,height,timestamp,content \
FROM undelegations ORDER BY timestamp DESC LIMIT {} OFFSET {}", page_size, (page-1)*page_size)
)
Expand Down

0 comments on commit dd95c47

Please sign in to comment.